require File.dirname(__FILE__) + '/test_helper' class TC_NERA_JOB_LAYER_CONTROLLER < Test::Unit::TestCase def setup @testdir = 'test_sim_controller' FileUtils.mkdir(@testdir) FileUtils.chdir(@testdir) @db_folder = "nera_db" NERA::DbFolders.create_db( @db_folder) @dbf = NERA::DbFolders.new( @db_folder) @slc = NERA::SimulatorLayerController.new( @db_folder) @plc = NERA::ParameterLayerController.new( @db_folder, Ising) pid = @plc.create_a_new_parameter_set({:L => 32, :K => 0.01, :tmax => 1}) @rlc = NERA::RunLayerController.new( @db_folder, Ising, pid) @rlc.create_jobs( 3, 1) @jlc = NERA::JobLayerController.new( @db_folder) end def teardown FileUtils.chdir('..') FileUtils.rm_r(@testdir) end def test_constructor assert_raise( ArgumentError) { NERA::JobLayerController.new( @dbf) } end def test_path_to_job_layer assert_equal( @dbf.path_to_job_layer, @jlc.path_to_job_layer) end def test_not_finished_list_in_csv h, l = @jlc.not_finished_list_in_csv assert_equal( "id, state, simulator, param_id, run_id, num_runs, created_at, updated_at, host_name", h) assert_equal( 3, l.size) dstr = DateTime.now.to_s.split('T')[0] assert_equal( "1, created, Ising, 1, 1, 1, #{dstr}, #{dstr}, ", l[0]) assert_equal( "2, created, Ising, 1, 2, 1, #{dstr}, #{dstr}, ", l[1]) assert_equal( "3, created, Ising, 1, 3, 1, #{dstr}, #{dstr}, ", l[2]) f = @jlc.cancel_jobs( [2]) h, l = @jlc.not_finished_list_in_csv assert_equal( "id, state, simulator, param_id, run_id, num_runs, created_at, updated_at, host_name", h) assert_equal( 2, l.size) assert_equal( "1, created, Ising, 1, 1, 1, #{dstr}, #{dstr}, ", l[0]) assert_equal( "3, created, Ising, 1, 3, 1, #{dstr}, #{dstr}, ", l[1]) end def test_cancel_jobs assert_raise( ArgumentError) { @jlc.cancel_jobs(1) } f = @jlc.cancel_jobs( [1,2] ) assert(f) h, l = @jlc.not_finished_list_in_csv assert_equal( 1, l.size) assert_equal( 3, l[0].split(',')[0].to_i) flag = File.exist?( @dbf.path_to_job_script(1)) assert_equal( false, flag) flag = File.exist?( @dbf.path_to_job_script(2)) assert_equal( false, flag) h, l = @rlc.not_finished_jobs_list_in_csv assert_equal( 1, l.size) f = @jlc.cancel_jobs( [1000] ) assert_nil( f) end def test_include_list l = @jlc.include_list assert_equal( 0, l.size) FileUtils.touch( @dbf.path_to_include_layer + "j000001.tar.bz2") FileUtils.touch( @dbf.path_to_include_layer + "j000003.tar.bz2") l = @jlc.include_list assert_equal( ["j000001.tar.bz2","j000003.tar.bz2"], l) FileUtils.touch( @dbf.path_to_include_layer + "g000001.tar.bz2") l2 = @jlc.include_list assert_equal( l, l2) end def test_include prepare_zipped_file fname = "j000001.tar.bz2" @jlc.include(fname) f = FileTest.exist?(fname) assert_equal( false, f) count = 0 File.open( @dbf.path_to_finished_jobs_file, 'r') do |io| YAML.load_documents( io) do |doc| p doc count += 1 end end assert_equal( 1, count) h, l = @jlc.not_finished_list_in_csv assert_equal( 2, l.size) h, l = @rlc.not_finished_jobs_list_in_csv assert_equal( 2, l.size) end def test_include_all prepare_zipped_file @jlc.include_all a = Dir.glob( @dbf.path_to_include_layer + "*" ) assert_equal( [], a) h, l = @jlc.not_finished_list_in_csv assert_equal( [], l) h, l = @rlc.not_finished_jobs_list_in_csv assert_equal( [], l) end def prepare_zipped_file Dir.chdir( @dbf.path_to_job_layer) { system "sh ./j000001.sh" system "mv j000001.tar.bz2 Include/" system "sh ./j000002.sh" system "mv j000002.tar.bz2 Include/" system "sh ./j000003.sh" system "mv j000003.tar.bz2 Include/" } end end