// LsfJob_t.cxx #include "dial_lsf/LsfJob.h" #include #include #include #include #include #include #include #include #include "dataset_util/getcwd.h" #include "dataset_util/ssystem.h" #include "dataset_util/FileStatus.h" #include "dataset_util/FileName.h" #include "dataset_util/mkdir.h" #include "dataset_util/DtdRegistry.h" #include "dataset_xml/XmlParser.h" #include "dataset_base/Dataset_t.h" #include "dataset_id/SimpleUniqueIdGenerator.h" #include "dataset_credential/CredentialSelectionCatalog.h" #include "dataset_credential/GssCredentialManager.h" #include "dial_task/Task.h" #include "dial_app/Application.h" using std::string; using std::ostream; using std::istringstream; using std::cout; using std::endl; using std::vector; using dset::CredentialSelectionCatalog; using dset::GssCredentialManager; using dial::TaskId; using dial::Task; using dial::Application; using dial::JobPreferences; using dial::JobId; using dial::LsfJob; string DSEP = "/"; void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } int LsfJob_t() { msg("Fetch environment setup"); string queue = Environment::current().value("DIAL_LSF_TEST_QUEUE"); string swait = Environment::current().value("DIAL_LSF_TEST_WAIT"); istringstream sswait(swait); int waittime = 0; sswait >> waittime; cout << "LSF test queue: " << queue << endl; cout << "LSF test wait: " << waittime << endl; msg("Check for the existence of LSF"); string lsfdir = LsfJob::find_lsf(); if ( lsfdir.size() == 0 ) { cout << "Unable to find LSF--no testing done" << endl; msg("Make sure LSF is not available"); assert( ! FileStatus("/usr/lsf/bin/bjobs").exists() ); assert( ! FileStatus("/usr/bin/bjobs").exists() ); assert( queue.size() == 0 ); return 0; } assert( queue.size() ); assert( waittime > 0 ); system("rm -rf UniqueId"); Text dset_dtd = DtdRegistry::instance("dataset").text(); string dset_dtdname = DtdRegistry::instance("dataset").filename(); Text dial_dtd = DtdRegistry::instance("dial").text(); string dial_dtdname = DtdRegistry::instance("dial").filename(); msg("Fetch default instance of CSC."); assert( CredentialSelectionCatalog::create_default_instance() == 0 ); CredentialSelectionCatalog& sc = CredentialSelectionCatalog::default_instance(); cout << sc << endl; assert( sc.is_valid() ); string owner = "me"; assert( GssCredentialManager::set_default() == 0 ); assert( GssCredentialManager::set_owner("me") == 0 ); msg("Create application"); system("touch build_task run"); SimpleUniqueIdGenerator::set_as_default(); SimpleUniqueIdGenerator::create_collection(Application::id_context(), 11, 101); Application app = Application::test_instance(); assert( app.is_valid() ); cout << app << endl; msg("Create result in advance"); string resfile = getcwd() + "/result.xml"; TestDataset res; res.lock(); cout << res << endl; const XmlElement* pxres = res.xml(); assert( pxres != 0 ); XmlParser parser; assert( parser.write(resfile, *pxres) == 0 ); msg("Create task"); SimpleUniqueIdGenerator::create_collection(Task::id_context(), 11, 101); SimpleUniqueIdGenerator::set_as_default(); Task::NameList files; Task tsk(files); assert( tsk.is_valid() ); cout << tsk << endl; msg("Create dataset"); TestDataset dst; dst.lock(); cout << dst << endl; msg("Create preferences"); JobPreferences prf; prf.lock(); msg("Create run directory"); system("rm -rf job1"); string dir = getcwd() + "/job1"; assert( mkdir(dir) == 0 ); assert( dset_dtd.write(dir+DSEP+dset_dtdname) == 0 ); assert( dial_dtd.write(dir+DSEP+dial_dtdname) == 0 ); ssystem("cp " + resfile + " " + dir); msg("Create run command"); Text runtxt; runtxt.append("cp " + resfile + " ."); runtxt.append("touch std.log "); runtxt.append("echo dial me! >> std.log"); runtxt.append("exit 0"); string runfile = dir + "/run"; runtxt.write(runfile); string chcom = "chmod +x " + runfile; system(chcom.c_str()); cout << runtxt << endl; msg("Create job."); LsfJob job(JobId(102,1), queue, app, tsk, dst, prf, dir, runfile); cout << job << endl; assert( job.is_initialized() ); msg("Check full type"); assert( job.full_type() == "LsfJob" ); msg("Check ID"); assert( job.id().is_valid() ); msg("Check run host before starting"); assert( job.run_host().size() == 0 ); msg("Check job directory"); assert( job.job_directory().size() ); msg("Start job"); int jstat = job.start(); cout << "Status is " << jstat << endl; cout << job << endl; assert( jstat == 0 ); assert( job.is_running() || job.is_done() ); assert( job.local_id().size() ); msg("Update job"); jstat = job.update(); msg("Wait for job to finish"); int dtime = 2; for ( int time=0; job.is_running()&&time<=waittime; time+=dtime ) { cout << "..." << time << endl; jstat = job.update(); sleep(dtime); } msg("Check job is done"); cout << job << endl; assert( job.is_done() ); assert( job.return_status() == 0 ); assert( job.has_result() ); msg("Check run host"); assert( job.run_host().size() ); msg("Create 2nd run directory"); system("rm -rf job2"); string dir2 = getcwd() + "/job2"; assert( mkdir(dir2) == 0 ); msg("Create run command"); Text runtxt2; runtxt2.append("/bin/sleep 100"); string runfile2 = dir2 + "/run2"; runtxt2.write(runfile2); string chcom2 = "chmod +x " + runfile2; system(chcom2.c_str()); cout << runtxt2 << endl; msg("Create job."); LsfJob job2(JobId(102,2), queue, app, tsk, dst, prf, dir2, runfile2); cout << job2 << endl; assert( job2.is_initialized() ); msg("Start job"); int jstat2 = job2.start(); cout << "Status is " << jstat2 << endl; cout << job2 << endl; assert( jstat2 == 0 ); assert( job2.is_running() || job2.is_done() ); msg("Kill job"); assert( job2.is_running() ); assert( job2.kill() == 0 ); cout << job2 << endl; assert( job2.is_killed() ); msg("Create 3nd run directory"); system("rm -rf job3"); string dir3 = getcwd() + "/job3"; assert( mkdir(dir3) == 0 ); msg("Create run command"); Text runtxt3; runtxt3.append("#!/bin/sh"); runtxt3.append(" "); runtxt3.append("sleep 3"); runtxt3.append("exit 111"); string runfile3 = dir3 + "/run3"; runtxt3.write(runfile3); string chcom3 = "chmod +x " + runfile3; system(chcom3.c_str()); cout << runtxt3 << endl; msg("Create job3 (should fail)."); LsfJob job3(JobId(102,3), queue, app, tsk, dst, prf, dir3, runfile3); cout << job3 << endl; assert( job3.is_initialized() ); msg("Start job3"); int jstat3 = job3.start(); cout << "Status is " << jstat3 << endl; cout << job3 << endl; assert( jstat3 == 0 ); assert( job3.is_running() || job3.is_done() ); msg("Wait for job3 to finish"); for ( int time=0; job3.is_running()&&time