// CondorCodJob_t.cxx #include "dial_condor/CondorCodJob.h" #include "dial_condor/CondorCodTable.h" #include #include #include #include #include #include #include #include "dataset_util/getcwd.h" #include "dataset_util/FileStatus.h" #include "dataset_util/FileName.h" #include "dataset_util/mkdir.h" #include "dataset_util/DtdRegistry.h" #include "dataset_id/SimpleUniqueIdGenerator.h" #include "dataset_credential/CredentialSelectionCatalog.h" #include "dataset_credential/GssCredentialManager.h" #include "dataset_base/Dataset_t.h" #include "dataset_xml/XmlParser.h" #include "dial_task/Task.h" #include "dial_app/Application.h" #include "dial_condor/CondorJob.h" using std::string; using std::ostream; 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::CondorJob; using dial::CondorCodJob; typedef std::string Name; string DSEP = "/"; void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } int ssystem(string com) { return system(com.c_str()); } // Machines.dat the list of machine names on which the COD jobs need to be executed on need to be // specified in DIAL_CONDOR_COD_CONF int CondorCodJob_t() { msg("Check for the existence of condor"); string condir = CondorJob::find_condor(); if ( condir.size() == 0 ) { cout << "Unable to find condor--no testing done" << endl; assert( ! FileStatus("/usr/condor/bin/condor_q").exists() ); assert( ! FileStatus("/usr/bin/condor_q").exists() ); return 0; } msg("Check environment variable."); const Environment& sysenv = Environment::initial(); bool defined = sysenv.has("DIAL_CONDOR_COD_CONF"); if ( defined != true ) cout << "DIAL_CONDOR_COD_CONF not defined" << endl; assert( defined == true ); Name confdir = sysenv.value("DIAL_CONDOR_COD_CONF"); FileName fname(confdir); if( fname.is_relative() ) confdir = fname.fullpath().name(); FileStatus statconfd(confdir); assert( statconfd.is_directory() && statconfd.is_readable() ); 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 DTD"); 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("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); 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"; cout << " making directory " << dir << endl; 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("#!/bin/sh"); runtxt.append("#"); 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."); CondorCodJob job(JobId(103,1), app, tsk, dst, prf, dir, runfile, "Machines.dat"); cout << job << endl; assert( job.is_initialized() ); msg("Check full type"); assert( job.full_type() == "CondorCodJob" ); 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 << job << endl; cout << "Status is " << jstat << endl; assert( jstat == 0 ); assert( job.is_running() || job.is_done() ); msg("Update job"); jstat = job.update(); msg("Wait for job to finish"); int dtime = 1; for ( int time=0; job.is_running()&&time<=60; time+=dtime ) { cout << "..." << time << endl; jstat = job.update(); sleep(2); } 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 after finish"); assert( job.run_host().size() != 0 ); 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/sh"); runtxt2.append("#"); 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."); CondorCodJob job2(JobId(103,2), app, tsk, dst, prf, dir2, runfile2, "Machines.dat"); cout << job2 << endl; assert( job2.is_initialized() ); msg("Start job"); int jstat2 = job2.start(); cout << "Status is " << jstat2 << endl; assert( jstat2 == 0 ); cout << job2 << endl; 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("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)."); CondorCodJob job3(JobId(103,3), app, tsk, dst, prf, dir3, runfile3, "Machines.dat"); cout << job3 << endl; assert( job3.is_initialized() ); msg("Start job3"); int jstat3 = job3.start(); cout << "Status is " << jstat3 << endl; assert( jstat3 == 0 ); cout << job3 << endl; assert( job3.is_running() || job3.is_done() ); msg("Wait for job3 to finish"); for ( int time=0; job3.is_running()&&time<60; time+=dtime ) { cout << "..." << time << endl; jstat = job3.update(); sleep(2); } msg("Check exit code"); cout << job3 << endl; cout << "job3 exit code: " << job3.return_status() << endl; assert( job3.is_failed() ); assert( job3.return_status() == 111 ); return 0; } #ifdef CTEST_MAIN int main() { return CondorCodJob_t(); } #endif