// ScriptedJob_t.cxx #include "dial_scripted/ScriptedJob.h" #include #include #include #include #include #include #include #include #include "dataset_util/getcwd.h" #include "dataset_util/copy_file.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_id/SimpleUniqueIdGenerator.h" #include "dataset_base/Dataset_t.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::ScriptedJob; string DSEP = "/"; void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } int ScriptedJob_t() { 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"); SimpleUniqueIdGenerator::set_as_default(); SimpleUniqueIdGenerator:: create_collection(Application::id_context(), 10, 101); system("touch build_task run"); 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); 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 ); 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 script"); string scr = "jobscript1"; Text scr1(scr); scr1.append("#! /bin/sh"); scr1.append(""); scr1.append("ACTION=$1"); scr1.append("if [ $ACTION = create ]; then"); scr1.append(" echo === Creating job"); scr1.append(" touch job_response.dat"); scr1.append("else if [ $ACTION = start ]; then"); scr1.append(" echo === Starting job"); scr1.append(" count=0"); scr1.append(" echo '=== count = '$count"); scr1.append(" echo $count > counter"); scr1.append(" RUNFILE=$2"); scr1.append(" echo '=== exe = '$RUNFILE"); scr1.append(" cp $RUNFILE myrunfile"); scr1.append(" rm -f job_response.dat"); scr1.append(" touch job_response.dat"); scr1.append(" echo set_running >> job_response.dat"); scr1.append(" echo set_local_id testjob-123 >> job_response.dat"); scr1.append("else if [ $ACTION = update ]; then"); scr1.append(" echo '=== Updating job'"); scr1.append(" count=`cat counter`"); scr1.append(" count=`expr \\( $count + 1 \\)`"); scr1.append(" echo '=== count = '$count"); scr1.append(" echo $count > counter"); scr1.append(" rm -f job_response.dat"); scr1.append(" touch job_response.dat"); scr1.append(" touch job_response.dat"); scr1.append(" if [ $count -ge 10 ]; then"); scr1.append(" ./myrunfile"); scr1.append(" echo set_done >> job_response.dat"); scr1.append(" echo set_run_host myhost.myorg >> job_response.dat"); scr1.append(" echo set_return_status 0 >> job_response.dat"); scr1.append(" fi"); scr1.append("else if [ $ACTION = kill ]; then"); scr1.append(" echo '=== Killing job'"); scr1.append(" rm -f job_response.dat"); scr1.append(" echo set_killed 123 >> job_response.dat"); scr1.append("fi; fi; fi; fi"); scr1.write(); string scrname = FileName(scr1.name()).fullpath_name(); assert( ssystem("chmod +x " + scr) == 0 ); msg("Create bad job."); ScriptedJob badjob(JobId(102,1), "nosuchfile", app, tsk, dst, prf, dir, runfile); cout << badjob << endl; assert( badjob.is_failed() ); msg("Copy run script to job directory"); string job_runfile = dir + "/dial_run_script"; copy_file(runfile, job_runfile); msg("Create job."); ScriptedJob job(JobId(102,1), scrname, app, tsk, dst, prf, dir, runfile); cout << job << endl; assert( job.is_initialized() ); msg("Check full type"); assert( job.full_type() == "ScriptedJob" ); 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() ); assert( job.local_id().size() ); msg("Update job"); jstat = job.update(); msg("Wait for job to finish"); for ( int count=0; count<20; ++count ) { cout << "..." << count << endl; jstat = job.update(); if ( job.is_inactive() ) break; } 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("Copy run script to 2nd job directory"); job_runfile = dir2 + "/dial_run_script"; copy_file(runfile, job_runfile); msg("Create 2nd job."); ScriptedJob job2(JobId(102,2), scrname, app, tsk, dst, prf, dir2, runfile); cout << job2 << endl; assert( job2.is_initialized() ); msg("Start job"); assert( job2.start() == 0 ); cout << job2 << endl; assert( job2.is_running() ); msg("Kill job"); int kstat = job2.kill(); cout << job2 << endl; if ( kstat != 0 ) { cout << "Kill returned status " << kstat << endl; assert(false); } assert( job2.is_killed() ); assert( job2.error() == 123 ); return 0; } #ifdef CTEST_MAIN int main() { return ScriptedJob_t(); } #endif