require File.dirname(__FILE__) + '/test_helper' class TC_NERA_DB_FOLDERS < Test::Unit::TestCase def setup @testdir = "testdir" FileUtils.mkdir(@testdir) FileUtils.chdir(@testdir) @db_folder = "nera_db" NERA::DbFolders.create_db( @db_folder) @dbf = NERA::DbFolders.new( @db_folder) end def teardown FileUtils.chdir('..') FileUtils.rm_r(@testdir) end def test_constructor FileUtils.mkdir("dummy") assert_raise( RuntimeError) { NERA::DbFolders.new("dummy") } assert_raise( RuntimeError) { NERA::DbFolders.new("unknown") } end def test_createdb assert( FileTest.directory?(@db_folder) ) assert( FileTest.directory?(@db_folder+"/Jobs/") ) assert( FileTest.directory?(@db_folder+"/Jobs/Include") ) assert( FileTest.directory?(@db_folder+"/Tables") ) assert( FileTest.directory?(@db_folder+"/Simulator_classes") ) assert( FileTest.exist?(@db_folder+"/Tables/jobs.pstore") ) assert( FileTest.exist?(@db_folder+"/simulators.yml") ) assert( FileTest.exist?(@db_folder+"/Jobs/jobs.yml") ) assert_raise( RuntimeError) { NERA::DbFolders.create_db( @db_folder) } end def test_path_to_sim_layer p = @dbf.path_to_simulator_layer assert_equal( @db_folder+'/', p) end def test_path_to_sim_tables p = @dbf.path_to_simulators_table assert_equal( @db_folder+"/Tables/simulators.pstore", p) p = @dbf.path_to_simulators_yaml assert_equal( @db_folder+"/simulators.yml", p) end def test_create_new_simulator_folder r = @dbf.create_new_simulator_folder( Ising ) assert_equal( true, r) assert( FileTest.directory?( @db_folder+"/Ising/") ) assert( FileTest.directory?( @db_folder+"/Tables/Ising/") ) assert( FileTest.exist?( @db_folder+"/Ising/parameters.yml") ) r = @dbf.create_new_simulator_folder( XY ) assert_equal( true, r) assert( FileTest.directory?( @db_folder+"/XY/") ) assert( FileTest.directory?( @db_folder+"/Tables/XY/") ) assert( FileTest.exist?( @db_folder+"/XY/parameters.yml") ) assert_raise( ArgumentError) { @dbf.create_new_simulator_folder( Temp) } r = @dbf.create_new_simulator_folder( Ising ) assert_nil( r) end def test_path_to_param_layer r = @dbf.path_to_parameter_layer( Ising) assert_nil(r) r = @dbf.create_new_simulator_folder( Ising ) r = @dbf.path_to_parameter_layer( Ising) assert_equal( @db_folder+"/Ising/", r) assert_raise( ArgumentError) { @dbf.path_to_parameter_layer( Temp) } end def test_path_to_param_table r = @dbf.path_to_parameters_table( Ising) assert_nil(r) r = @dbf.create_new_simulator_folder( Ising ) assert_equal( true, r) r = @dbf.path_to_parameters_table( Ising) assert_equal( @db_folder+"/Tables/Ising/parameters.pstore", r) r = @dbf.path_to_parameters_yaml( Ising) assert_equal( @db_folder+ "/Ising/parameters.yml", r) assert_raise( ArgumentError) { @dbf.path_to_parameters_table( Temp) } end def test_create_new_param_folder r = @dbf.create_new_parameter_folder( Ising, 5) assert_nil( r) @dbf.create_new_simulator_folder( Ising ) r = @dbf.create_new_parameter_folder( Ising, 1) assert_equal( true, r) assert( FileTest.directory?( @db_folder+"/Ising/00001/") ) assert( FileTest.directory?( @db_folder+"/Tables/Ising/00001/") ) assert( FileTest.exist?( @db_folder+"/Ising/00001/runs.yml") ) r = @dbf.create_new_parameter_folder( Ising, 2) assert_equal( true, r) assert( FileTest.directory?( @db_folder+"/Ising/00002/") ) assert( FileTest.directory?( @db_folder+"/Tables/Ising/00002/") ) assert( FileTest.exist?( @db_folder+"/Ising/00002/runs.yml") ) r = @dbf.create_new_parameter_folder( Ising, 2) assert_equal( nil, r) assert_raise( RuntimeError) { r = @dbf.create_new_parameter_folder( Ising, 100000) } assert_raise( RuntimeError) { r = @dbf.create_new_parameter_folder( Ising, 0) } assert_raise( ArgumentError) { @dbf.create_new_parameter_folder( Temp) } end def test_path_to_run_layer assert_raise( ArgumentError) { s = @dbf.path_to_run_layer( Temp, 1) } s = @dbf.path_to_run_layer( Ising, 1) assert_nil( s) @dbf.create_new_simulator_folder( Ising) s = @dbf.path_to_run_layer( Ising, 1) assert_equal( false, FileTest.directory?(s)) @dbf.create_new_parameter_folder( Ising, 1) s = @dbf.path_to_run_layer( Ising, 1) assert_equal( @db_folder+"/Ising/00001/", s) s = @dbf.path_to_run_layer( Ising, 1000) assert_equal( @db_folder+"/Ising/01000/", s) end def test_path_to_runs_table assert_raise( ArgumentError) { s = @dbf.path_to_runs_table( Temp, 1) } s = @dbf.path_to_runs_table( Ising, 1) assert_nil( s) @dbf.create_new_simulator_folder( Ising) s = @dbf.path_to_runs_table( Ising, 1) assert_equal( false, FileTest.exist?(s)) @dbf.create_new_parameter_folder( Ising, 1) s = @dbf.path_to_runs_table( Ising, 1) assert_equal( @db_folder+"/Tables/Ising/00001/runs.pstore", s) s = @dbf.path_to_runs_yaml( Ising, 1) assert_equal( @db_folder+"/Ising/00001/runs.yml", s) s = @dbf.path_to_runs_table( Ising, 999) assert_equal( @db_folder+"/Tables/Ising/00999/runs.pstore", s) s = @dbf.path_to_runs_yaml( Ising, 999) assert_equal( @db_folder+"/Ising/00999/runs.yml", s) end def test_path_to_job_layer p = @dbf.path_to_job_layer assert_equal( @db_folder+"/Jobs/", p) end def test_path_to_jobs_table p = @dbf.path_to_jobs_table assert_equal( @db_folder+"/Tables/jobs.pstore", p) p = @dbf.path_to_jobs_yaml assert_equal( @db_folder+"/Jobs/jobs.yml", p) end def test_to_job_script( job_id) p = @dbf.path_to_job_script(125) assert_equal( @db_folder+"/Jobs/j000125.sh", p) end def test_path_to_include_layer p = @dbf.path_to_include_layer assert_equal( @db_folder+"/Jobs/Include/", p) end def test_search_include_files FileUtils.touch( @dbf.path_to_include_layer + "j000001.tar.bz2") FileUtils.touch( @dbf.path_to_include_layer + "j000003.tar.bz2") FileUtils.touch( @dbf.path_to_include_layer + "j00001.tar.bz2") should_be = [ @dbf.path_to_include_layer + "j000001.tar.bz2", @dbf.path_to_include_layer + "j000003.tar.bz2"] l = @dbf.search_include_files assert_equal( should_be, l) end def test_path_to_finished_jobs_file p = @dbf.path_to_finished_jobs_file assert_equal( @db_folder+"/Tables/finished_jobs.yml", p) end def test_path_to_hosts_yaml p = @dbf.path_to_hosts_yaml assert_equal( @db_folder+"/Tables/hosts.yml",p) end def test_path_to_log_file path = @dbf.path_to_log_file assert_equal( @db_folder + "/Tables/logfile.txt", path) end def test_logger @dbf.logger.info("controller") do "test_message" end assert( File.exist?( @db_folder+"/Tables/logfile.txt") ) last_line = File.open( @db_folder+"/Tables/logfile.txt", 'r').readlines[-1] assert( last_line =~ /^I,/) assert( last_line =~ /controller: test_message$/) end end