def test_job_description_attributes(): # attributes serialization attrs = {'j1_name': 'j1', 'j1_var1': 'var1'} j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), attributes=attrs) assert all((len(j.attributes) == len(attrs), j.attributes == attrs)) j_json = j.to_json() j_clone = Job(**json.loads(j_json)) assert all( (len(j_clone.attributes) == len(attrs), j_clone.attributes == attrs)) assert j.to_dict() == j_clone.to_dict() # attributes wrong format with pytest.raises(IllegalJobDescription): Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), attributes="some_illegal_attributes") with pytest.raises(IllegalJobDescription): Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), attributes=["some_illegal_attributes", "more_illegal_attributes"])
def test_local_error_duplicate_name_job_separate_reqs(tmpdir): file_path = tmpdir.join('jobs.json') print('tmpdir: {}'.format(str(tmpdir))) jobName = 'mdate' jobs1 = [ job.to_dict() for job in [ Job( jobName, JobExecution('date', wd=abspath(tmpdir.join('date.sandbox')), stdout='date.out', stderr='date.err'), JobResources(numCores=ResourceSize(1))) ] ] jobs2 = [ job.to_dict() for job in [ Job( jobName, JobExecution('sleep', wd=abspath(tmpdir.join('sleep.sandbox')), stdout='sleep.out', stderr='sleep.err'), JobResources(numCores=ResourceSize(1))) ] ] reqs = [{ 'request': 'submit', 'jobs': jobs1 }, { 'request': 'submit', 'jobs': jobs2 }, { 'request': 'control', 'command': 'finishAfterAllTasksDone' }] save_reqs_to_file(reqs, file_path) print('jobs saved to file_path: {}'.format(str(file_path))) sys.argv = [ 'QCG-PilotJob', '--file', '--file-path', str(file_path), '--nodes', '2', '--wd', str(tmpdir), '--report-format', 'json' ] QCGPMService().start() # the first job (date) should execute check_job_status_in_json([jobName], workdir=str(tmpdir), dest_state='SUCCEED') assert all((isdir(abspath(tmpdir.join('date.sandbox'))), exists(join(abspath(tmpdir.join('date.sandbox')), 'date.out')), exists(join(abspath(tmpdir.join('date.sandbox')), 'date.err')))) # the second job (sleep) due to the name clash should not execute assert not isdir(abspath(tmpdir.join('sleep.sandbox')))
def test_joblist(): jlist = JobList() # adding and removing jobs from list jlist.add( Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1))) assert jlist.exist('j1') assert jlist.get('j1').get_name() == 'j1' jlist.add( Job(name='j2', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1))) assert all((jlist.exist('j1'), jlist.exist('j2'))) assert jlist.get('j2').get_name() == 'j2' with pytest.raises(JobAlreadyExist): jlist.add( Job(name='j2', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1))) with pytest.raises(JobAlreadyExist): jlist.add( Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1))) jnames = jlist.jobs() assert all((len(jnames) == 2, 'j1' in jnames, 'j2' in jnames)) jlist.remove('j2') assert not jlist.exist('j2') assert jlist.get('j2') is None jnames = jlist.jobs() assert all((len(jnames) == 1, 'j1' in jnames)) # try to add something which is not a job with pytest.raises(Exception): jlist.add('another job') # parsing job iteration names assert JobList.parse_jobname('j1') == ('j1', None) assert JobList.parse_jobname('j1:1') == ('j1', 1) with pytest.raises(ValueError): assert JobList.parse_jobname('j1:2:1') == ('j1', '2:1') with pytest.raises(ValueError): assert JobList.parse_jobname('j1:') == ('j1', '')
def test_jobdescription_jobname(): # job name ok j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1)) assert j # missing job name with pytest.raises(IllegalJobDescription): Job(name=None, execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1)) # illegal character in job name with pytest.raises(IllegalJobDescription): Job(name='j1:1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1))
def test_local_error_duplicate_name_job(tmpdir): file_path = tmpdir.join('jobs.json') print('tmpdir: {}'.format(str(tmpdir))) jobName = 'mdate' jobs = [ job.to_dict() for job in [ Job( jobName, JobExecution('date', wd=abspath(tmpdir.join('date.sandbox')), stdout='date.out', stderr='date.err'), JobResources(numCores=ResourceSize(1))), Job( jobName, JobExecution('sleep', wd=abspath(tmpdir.join('sleep.sandbox')), stdout='sleep.out', stderr='sleep.err'), JobResources(numCores=ResourceSize(1))) ] ] reqs = [{ 'request': 'submit', 'jobs': jobs }, { 'request': 'control', 'command': 'finishAfterAllTasksDone' }] save_reqs_to_file(reqs, file_path) print('jobs saved to file_path: {}'.format(str(file_path))) sys.argv = [ 'QCG-PilotJob', '--file', '--file-path', str(file_path), '--nodes', '2', '--wd', str(tmpdir), '--report-format', 'json' ] QCGPMService().start() # no job should be executed due to the failed submit request with non-unique jobs inside assert not isdir(abspath(tmpdir.join('date.sandbox'))) assert not isdir(abspath(tmpdir.join('sleep.sandbox')))
def test_local_error_job_desc(): # missing job execution with pytest.raises(IllegalJobDescription): Job('error_job', JobExecution(None, stdout='date.out', stderr='date.err'), JobResources(numCores=ResourceSize(1))) # wrong format of arguments with pytest.raises(IllegalJobDescription): Job( 'error_job', JobExecution('date', args='this should be a list', stdout='date.out', stderr='date.err'), JobResources(numCores=ResourceSize(1))) # wrong format of environment with pytest.raises(IllegalJobDescription): Job( 'error_job', JobExecution('date', args=['arg1'], env=['this shuld be a dict'], stdout='date.out', stderr='date.err'), JobResources(numCores=ResourceSize(1))) # missing execution definition with pytest.raises(IllegalJobDescription): Job('error_job', None, JobResources(numCores=ResourceSize(1))) # missing resources definition with pytest.raises(IllegalJobDescription): Job( 'error_job', JobExecution('date', args=['arg1'], env=['this shuld be a dict'], stdout='date.out', stderr='date.err'), None)
def test_local_simple_script_job(tmpdir): file_path = tmpdir.join('jobs.json') print('tmpdir: {}'.format(str(tmpdir))) jobName = 'mdate_script' jobs = [ job.to_dict() for job in [ Job( jobName, JobExecution(script='/bin/date\n/bin/hostname\n', wd=abspath(tmpdir.join('date.sandbox')), stdout='date.out', stderr='date.err'), JobResources(numCores=ResourceSize(1))) ] ] reqs = [{ 'request': 'submit', 'jobs': jobs }, { 'request': 'control', 'command': 'finishAfterAllTasksDone' }] save_reqs_to_file(reqs, file_path) print('jobs saved to file_path: {}'.format(str(file_path))) sys.argv = [ 'QCG-PilotJob', '--log', 'debug', '--file', '--file-path', str(file_path), '--nodes', '2', '--wd', str(tmpdir), '--report-format', 'json' ] QCGPMService().start() check_job_status_in_json([jobName], workdir=str(tmpdir), dest_state='SUCCEED') assert all( (isdir(abspath(tmpdir.join('date.sandbox'))), exists(join(abspath(tmpdir.join('date.sandbox')), 'date.out')), exists(join(abspath(tmpdir.join('date.sandbox')), 'date.err')), stat(join(abspath(tmpdir.join('date.sandbox')), 'date.out')).st_size > 0, stat(join(abspath(tmpdir.join('date.sandbox')), 'date.err')).st_size == 0)) with pytest.raises(ValueError): check_job_status_in_json([jobName + 'xxx'], workdir=str(tmpdir), dest_state='SUCCEED')
def test_local_workflows_error(tmpdir): file_path = tmpdir.join('jobs.json') print('tmpdir: {}'.format(str(tmpdir))) jobs = [ job.to_dict() for job in [ Job('first', JobExecution('sleep', args=['2s'], wd=abspath(tmpdir.join('first.sandbox')), stdout='out', stderr='err'), JobResources(numCores=ResourceSize(1)), dependencies=JobDependencies(after=['not-existing'])) ] ] reqs = [{ 'request': 'submit', 'jobs': jobs }, { 'request': 'control', 'command': 'finishAfterAllTasksDone' }] save_reqs_to_file(reqs, file_path) print('jobs saved to file_path: {}'.format(str(file_path))) sys.argv = [ 'QCG-PilotJob', '--file', '--file-path', str(file_path), '--nodes', '2', '--wd', str(tmpdir), '--report-format', 'json' ] QCGPMService().start() assert not exists(abspath(tmpdir.join('first.sandbox'))) rmtree(str(tmpdir))
def test_local_workflows(tmpdir): file_path = tmpdir.join('jobs.json') print('tmpdir: {}'.format(str(tmpdir))) jobs = [ job.to_dict() for job in [ Job( 'first', JobExecution('sleep', args=['2s'], wd=abspath(tmpdir.join('first.sandbox')), stdout='out', stderr='err'), JobResources(numCores=ResourceSize(1))), Job('second', JobExecution('sleep', args=['1s'], wd=abspath(tmpdir.join('second.sandbox')), stdout='out', stderr='err'), JobResources(numCores=ResourceSize(1)), dependencies=JobDependencies(after=['first'])), Job('third', JobExecution('date', wd=abspath(tmpdir.join('third.sandbox')), stdout='out', stderr='err'), JobResources(numCores=ResourceSize(1)), dependencies=JobDependencies(after=['first', 'second'])) ] ] reqs = [{ 'request': 'submit', 'jobs': jobs }, { 'request': 'control', 'command': 'finishAfterAllTasksDone' }] save_reqs_to_file(reqs, file_path) print('jobs saved to file_path: {}'.format(str(file_path))) # the ammount of resources should be enough to theoretically start all three job's at once sys.argv = [ 'QCG-PilotJob', '--file', '--file-path', str(file_path), '--nodes', '4', '--wd', str(tmpdir), '--report-format', 'json' ] QCGPMService().start() jnames = ['first', 'second', 'third'] check_job_status_in_json(jnames, workdir=str(tmpdir), dest_state='SUCCEED') for jname in jnames: assert all((isdir(abspath(tmpdir.join('{}.sandbox'.format(jname)))), exists( join(abspath(tmpdir.join('{}.sandbox'.format(jname))), 'out')), exists( join(abspath(tmpdir.join('{}.sandbox'.format(jname))), 'err')))) with open(join(find_single_aux_dir(str(tmpdir)), 'jobs.report'), 'r') as f: job_stats = [json.loads(line) for line in f.readlines()] assert len(job_stats) == len(jnames) jstats = {} for i in range(0, len(jnames)): job = job_stats[i] print('readed job stats: {}'.format(str(job))) t = datetime.strptime(job['runtime']['rtime'], "%H:%M:%S.%f") rtime = timedelta(hours=t.hour, minutes=t.minute, seconds=t.second, microseconds=t.microsecond) # find start executing time exec_state = list( filter(lambda st_en: st_en['state'] == 'EXECUTING', job['history'])) assert len(exec_state) == 1 # find finish executing time finish_state = list( filter(lambda st_en: st_en['state'] == 'SUCCEED', job['history'])) assert len(finish_state) == 1 start_time = datetime.strptime(exec_state[0]['date'], '%Y-%m-%dT%H:%M:%S.%f') finish_time = datetime.strptime(finish_state[0]['date'], '%Y-%m-%dT%H:%M:%S.%f') jstats[job['name']] = { 'r_time': rtime, 's_time': start_time, 'f_time': finish_time } # assert second job started after the first one assert jstats['second']['s_time'] > jstats['first']['f_time'] # assert third job started after the first and second ones assert all((jstats['third']['s_time'] > jstats['first']['f_time'], jstats['third']['s_time'] > jstats['second']['f_time'])) rmtree(str(tmpdir))
def test_slurmenv_simple_job(): if not in_slurm_allocation() or get_num_slurm_nodes() < 2: pytest.skip( 'test not run in slurm allocation or allocation is smaller than 2 nodes' ) resources, allocation = get_slurm_resources_binded() resources_node_names = set(n.name for n in resources.nodes) set_pythonpath_to_qcg_module() tmpdir = str(tempfile.mkdtemp(dir=SHARED_PATH)) file_path = join(tmpdir, 'jobs.json') print('tmpdir: {}'.format(tmpdir)) jobName = 'mdate' jobs = [ job.to_dict() for job in [ Job( jobName, JobExecution('date', wd=abspath(join(tmpdir, 'date.sandbox')), stdout='date.out', stderr='date.err'), JobResources(numCores=ResourceSize(1))) ] ] reqs = [{ 'request': 'submit', 'jobs': jobs }, { 'request': 'control', 'command': 'finishAfterAllTasksDone' }] save_reqs_to_file(reqs, file_path) print('jobs saved to file_path: {}'.format(str(file_path))) sys.argv = [ 'QCG-PilotJob', '--log', 'debug', '--file', '--file-path', str(file_path), '--wd', tmpdir, '--report-format', 'json' ] QCGPMService().start() jobEntries = check_job_status_in_json([jobName], workdir=tmpdir, dest_state='SUCCEED') assert all( (isdir(abspath(join(tmpdir, 'date.sandbox'))), exists(join(abspath(join(tmpdir, 'date.sandbox')), 'date.out')), exists(join(abspath(join(tmpdir, 'date.sandbox')), 'date.err')), stat(join(abspath(join(tmpdir, 'date.sandbox')), 'date.out')).st_size > 0)) # there can be some debugging messages in the stderr # stat(join(abspath(join(tmpdir, 'date.sandbox')), 'date.err')).st_size == 0)) for jname, jentry in jobEntries.items(): assert all(('runtime' in jentry, 'allocation' in jentry.get('runtime', {}))) jalloc = jentry['runtime']['allocation'] for jalloc_node in jalloc.split(','): node_name = jalloc_node[:jalloc_node.index('[')] print('{} in available nodes ({})'.format( node_name, ','.join(resources_node_names))) assert node_name in resources_node_names, '{} not in nodes ({}'.format( node_name, ','.join(resources_node_names)) with pytest.raises(ValueError): check_job_status_in_json([jobName + 'xxx'], workdir=tmpdir, dest_state='SUCCEED') rmtree(tmpdir)
def test_slurmenv_many_nodes_many_cores(): if not in_slurm_allocation() or get_num_slurm_nodes() < 2: pytest.skip( 'test not run in slurm allocation or allocation is smaller than 2 nodes' ) resources, allocation = get_slurm_resources_binded() resources_node_names = set(n.name for n in resources.nodes) set_pythonpath_to_qcg_module() tmpdir = str(tempfile.mkdtemp(dir=SHARED_PATH)) file_path = join(tmpdir, 'jobs.json') print('tmpdir: {}'.format(tmpdir)) jobName = 'hostname' jobwdir_base = 'hostname.sandbox' cores_num = resources.nodes[0].free nodes_num = resources.total_nodes jobs = [ job.to_dict() for job in [ Job( jobName, JobExecution(exec='mpirun', args=['--allow-run-as-root', 'hostname'], wd=abspath(join(tmpdir, jobwdir_base)), stdout='hostname.out', stderr='hostname.err', modules=['mpi/openmpi-x86_64']), JobResources(numCores=ResourceSize(cores_num), numNodes=ResourceSize(nodes_num))) ] ] reqs = [{ 'request': 'submit', 'jobs': jobs }, { 'request': 'control', 'command': 'finishAfterAllTasksDone' }] save_reqs_to_file(reqs, file_path) print('jobs saved to file_path: {}'.format(str(file_path))) sys.argv = [ 'QCG-PilotJob', '--log', 'debug', '--file', '--file-path', str(file_path), '--wd', tmpdir, '--report-format', 'json' ] QCGPMService().start() jobEntries = check_job_status_in_json([jobName], workdir=tmpdir, dest_state='SUCCEED') assert all( (isdir(abspath(join(tmpdir, jobwdir_base))), exists(join(abspath(join(tmpdir, jobwdir_base)), 'hostname.out')), exists(join(abspath(join(tmpdir, jobwdir_base)), 'hostname.err')), stat(join(abspath(join(tmpdir, jobwdir_base)), 'hostname.out')).st_size > 0)) job_nodes = [] allocated_cores = 0 for jname, jentry in jobEntries.items(): assert all(('runtime' in jentry, 'allocation' in jentry.get('runtime', {}))) jalloc = jentry['runtime']['allocation'] for jalloc_node in jalloc.split(','): node_name = jalloc_node[:jalloc_node.index('[')] job_nodes.append(node_name) print('{} in available nodes ({})'.format( node_name, ','.join(resources_node_names))) assert node_name in resources_node_names, '{} not in nodes ({}'.format( node_name, ','.join(resources_node_names)) ncores = len(jalloc_node[jalloc_node.index('[') + 1:-1].split(':')) print('#{} cores on node {}'.format(ncores, node_name)) allocated_cores += ncores assert len(job_nodes) == nodes_num, str(job_nodes) assert allocated_cores == nodes_num * cores_num, allocated_cores # check if hostname is in stdout in two lines with open(abspath(join(tmpdir, join(jobwdir_base, 'hostname.out'))), 'rt') as stdout_file: stdout_content = [line.rstrip() for line in stdout_file.readlines()] assert len(stdout_content) == nodes_num * cores_num, str(stdout_content) assert all(hostname in job_nodes for hostname in stdout_content), str(stdout_content) with pytest.raises(ValueError): check_job_status_in_json([jobName + 'xxx'], workdir=tmpdir, dest_state='SUCCEED') rmtree(tmpdir)
def test_job_description_subjobs(): # iteration job j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), iteration=JobIteration(stop=10)) assert j.has_iterations # iteration names assert j.get_name() == 'j1' assert all( j.get_name(it) == '{}:{}'.format(j.get_name(), it) for it in range(10)) # iteration states (initial) assert j.state() == JobState.QUEUED assert all(j.state(it) == JobState.QUEUED for it in range(10)) assert all(j.str_state(it) == JobState.QUEUED.name for it in range(10)) # iteration runtimes for it in range(10): j.append_runtime({'host': 'local.{}'.format(it)}, it) assert all( j.runtime(it).get('host') == 'local.{}'.format(it) for it in range(10)) # whole job success for it in range(10): j.set_state(JobState.SUCCEED, it, 'job {} succeed'.format(it)) assert j.state() == JobState.SUCCEED assert all( j.messages(it) == 'job {} succeed'.format(it) for it in range(10)) # whole job fail (one of the iteration failed) j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), iteration=JobIteration(stop=10)) assert j.has_iterations for it in range(9): j.set_state(JobState.SUCCEED, it, 'job {} succeed'.format(it)) j.set_state(JobState.FAILED, 9, 'job 9 failed') assert j.state() == JobState.FAILED assert all( j.messages(it) == 'job {} succeed'.format(it) for it in range(9)) assert j.messages(9) == 'job 9 failed' # whole job fail (one of the iteration canceled) j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), iteration=JobIteration(stop=10)) assert j.has_iterations for it in range(9): j.set_state(JobState.SUCCEED, it, 'job {} succeed'.format(it)) j.set_state(JobState.CANCELED, 9, 'job 9 canceled') assert j.state() == JobState.FAILED assert all( j.messages(it) == 'job {} succeed'.format(it) for it in range(9)) assert j.messages(9) == 'job 9 canceled' # whole job fail (one of the iteration omitted) j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), iteration=JobIteration(stop=10)) assert j.has_iterations for it in range(9): j.set_state(JobState.SUCCEED, it, 'job {} succeed'.format(it)) j.set_state(JobState.OMITTED, 9, 'job 9 omitted') assert j.state() == JobState.FAILED assert all( j.messages(it) == 'job {} succeed'.format(it) for it in range(9)) assert j.messages(9) == 'job 9 omitted' # not all iterations finished j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), iteration=JobIteration(stop=10)) assert j.has_iterations for it in range(9): j.set_state(JobState.SUCCEED, it, 'job {} succeed'.format(it)) assert j.state() == JobState.QUEUED assert all( j.messages(it) == 'job {} succeed'.format(it) for it in range(9)) # whole job fail (just one succeed j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), iteration=JobIteration(stop=10)) assert j.has_iterations for it in range(9): j.set_state(JobState.FAILED, it, 'job {} failed'.format(it)) j.set_state(JobState.CANCELED, 9, 'job 9 succeed') assert j.state() == JobState.FAILED assert all(j.messages(it) == 'job {} failed'.format(it) for it in range(9)) assert j.messages(9) == 'job 9 succeed' # many messages per iteration j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), iteration=JobIteration(stop=10)) assert j.has_iterations for it in range(10): j.set_state(JobState.EXECUTING, it, 'job {} executing'.format(it)) assert j.state() == JobState.QUEUED assert all(j.state(it) == JobState.EXECUTING for it in range(10)) assert all(j.str_state(it) == JobState.EXECUTING.name for it in range(10)) for it in range(10): j.set_state(JobState.SUCCEED, it, 'job {} finished'.format(it)) assert j.state() == JobState.SUCCEED assert all(j.state(it) == JobState.SUCCEED for it in range(10)) assert all(j.str_state(it) == JobState.SUCCEED.name for it in range(10)) assert all( j.messages(it) == 'job {it} executing\njob {it} finished'.format(it=it) for it in range(10)) # messages for job j = Job(name='j1', execution=JobExecution(exec='/bin/date'), resources=JobResources(numCores=1), iteration=JobIteration(stop=10)) assert j.has_iterations j.set_state(JobState.EXECUTING, iteration=None, err_msg='job executing') assert all((j.state() == JobState.EXECUTING, j.str_state() == JobState.EXECUTING.name)) assert j.messages() == 'job executing' for it in range(10): j.set_state(JobState.EXECUTING, it, 'job {} executing'.format(it)) j.set_state(JobState.FAILED, iteration=None, err_msg='job failed') assert all( (j.state() == JobState.FAILED, j.str_state() == JobState.FAILED.name)) assert j.messages() == 'job executing\njob failed' # failed job will not change state if once set for it in range(10): j.set_state(JobState.SUCCEED, it, 'job {} finished'.format(it)) assert all( (j.state() == JobState.FAILED, j.str_state() == JobState.FAILED.name)) assert all(j.str_state(it) == JobState.SUCCEED.name for it in range(10)) assert all( j.messages(it) == 'job {it} executing\njob {it} finished'.format(it=it) for it in range(10))