# this file MUST be included first in order to override the KVM_DIR # therefore dependencies are NOT required in this file # tests should require required dependencies AFTER this inclusion require 'test/unit' require 'fileutils' require 'fakeweb' module KVM TEST_KVM_DIR="/tmp/.kvm_test" KVM_DIR=TEST_KVM_DIR end # set up test data require 'kvm/archive' TEST_DATA_PATH=File.expand_path('data', File.dirname(__FILE__)) TEST_FILES_PATH=File.join(TEST_DATA_PATH, 'files') TEST_SVN_PATH=File.join(TEST_DATA_PATH, 'svn') TEST_RICE_VERSION="rice-0.0.0.0" TEST_ARCHIVE = KVM::Archive.new(TEST_RICE_VERSION) # register test archive FakeWeb.register_uri(:get, TEST_ARCHIVE.download_url, :body => File.read(File.join(TEST_FILES_PATH, TEST_ARCHIVE.filename))) require 'kvm/source' # override svn base REAL_SVN_ROOT = KVM::Subversion.svn_root p REAL_SVN_ROOT KVM::Subversion.svn_root="file://#{TEST_SVN_PATH}/" p KVM::Subversion.svn_root TEST_REPO="sample-project" module KVM class KVMTest < Test::Unit::TestCase ALPHAS = ('a'..'z').to_a + ('A'..'Z').to_a NUMS = ('0'..'9').to_a ALPHANUMS = ALPHAS + NUMS DEFAULT_DIST_SUFFIX = '-bin' def setup init_test_kvm_dir end def default_test # HACK. Stop TestUnit on 1.8.7 from complaining about this base class end def init_test_kvm_dir clear_test_kvm_dir FileUtils.mkdir_p(TEST_KVM_DIR) end def clear_test_kvm_dir FileUtils.rm_r(TEST_KVM_DIR) end def touch(file) FileUtils.mkdir_p(File.dirname(file)) FileUtils.touch(file) end def rand_alpha(size = 6) rand_str(ALPHAS, size) end def rand_alphanum(size = 6) rand_str(ALPHANUMS, size) end def rand_str(chars, size = 6) (0...size).collect { chars[Kernel.rand(chars.length)] }.join end def fake_project rand_alpha + '-' + rand_alphanum end def create_project with_project do |project| FileUtils.mkdir_p(File.join(KVM::RiceRelease::BASE_DIR, project_dir(project))) end end def create_archive with_project do |project| touch(File.join(KVM::Archive::BASE_DIR, project_dir(project) + '.tar.gz')) end end def rand_db [ 'oracle', 'mysql' ][rand(2)] end def fake_generated_dataset(pr = nil, ds = nil, db = nil) [ pr || fake_project, ds || rand_alpha, db || rand_db ] end def create_generated_dataset(pr = nil, ds = nil, db = nil) with_dataset(pr, ds, db) do |project, ds_name, db_type| path = File.join(KVM::RiceReleaseGeneratedDataset::BASE_DIR, project_dir(project), ds_name + '-dataset', db_type) FileUtils.mkdir_p(path) end end def create_src_dataset(pr = nil, ds = nil, db = nil) with_dataset(pr, ds, db) do |project, ds_name, db_type| path = File.join(KVM::RiceRelease::BASE_DIR, project_dir(project), 'database', ds_name + '-dataset', db_type) FileUtils.mkdir_p(path) end end def with_dataset(pr = nil, ds = nil, db = nil) project, ds_name, db_type = fake_generated_dataset(pr, ds, db) yield [ project, ds_name, db_type ] [ project_dir(project), ds_name, db_type ] end def with_project project = fake_project yield project project end def project_dir(project) project + DEFAULT_DIST_SUFFIX end def array_contains_values(array, legit_values) array.reduce(true) do |legit_elements, element| legit_elements and legit_values.reduce(false) do |legit_element, legit_value| legit_element or element =~ /#{legit_value}/ end end end def do_with_svn(root) prev_svn_root = KVM::Subversion.svn_root begin KVM::Subversion.svn_root=root yield ensure KVM::Subversion.svn_root=prev_svn_root end end end end