test_remote_job.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from argparse import Namespace
  2. import subprocess
  3. import pytest
  4. from tools import remote_job as job
  5. def args(tmp_path):
  6. return Namespace(run_root=tmp_path,run_id='fixture',workspace=str(tmp_path),
  7. snapshot_id='snapshot',target='mrdimm-chamber',task='smoke',
  8. toolchain_id='test-image',container='prod2u-dev',heavy=False,
  9. command=['python3','-c','print("literal $() quoted")'])
  10. def test_duplicate_submit_starts_one_unit_and_conflict_does_not_overwrite(tmp_path,monkeypatch):
  11. a=args(tmp_path)
  12. monkeypatch.setattr(job,'source_binding',lambda p:{'workspace':str(p),'commit':'a'*40,'tree':'b'*40})
  13. monkeypatch.setattr(job,'boot_id',lambda:'boot')
  14. calls=[]
  15. def run(command,**kw):
  16. calls.append(command)
  17. return subprocess.CompletedProcess(command,0,'active\n' if command[0]=='systemctl' else '','')
  18. monkeypatch.setattr(job.subprocess,'run',run)
  19. first=job.submit(a);second=job.submit(a)
  20. assert first['identity']==second['identity']
  21. assert sum(x[0]=='systemd-run' for x in calls)==1
  22. assert first['spec']['command']==a.command
  23. original=(tmp_path/'fixture/SPEC.json').read_bytes()
  24. a.command=['false']
  25. with pytest.raises(ValueError,match='RUN_ID_CONFLICT'):job.submit(a)
  26. assert (tmp_path/'fixture/SPEC.json').read_bytes()==original
  27. assert sum(x[0]=='systemd-run' for x in calls)==1
  28. def test_empty_command_never_creates_run(tmp_path):
  29. a=args(tmp_path);a.command=[]
  30. with pytest.raises(ValueError,match='nonempty'):job.submit(a)
  31. assert not (tmp_path/'fixture').exists()
  32. @pytest.mark.parametrize('name',['../escape','/absolute','x/y','','.','..','bad name'])
  33. def test_invalid_run_paths_are_rejected(tmp_path,name):
  34. with pytest.raises(ValueError):job.job_dir(tmp_path,name)
  35. def test_boot_change_marks_interrupted_without_pid_or_unit_lookup(tmp_path,monkeypatch):
  36. folder=tmp_path/'run';folder.mkdir()
  37. job.save_status(folder,{'status':'RUNNING','boot_id':'previous-boot','unit':'old','process':{'pid':1}})
  38. monkeypatch.setattr(job,'boot_id',lambda:'new-boot')
  39. monkeypatch.setattr(job.subprocess,'run',lambda *a,**kw:pytest.fail('old unit must not be consulted after reboot'))
  40. result=job.refresh(folder)
  41. assert result['status']=='INTERRUPTED' and result['exit_code'] is None
  42. assert job.read(folder/'RESULT.json')['status']=='INTERRUPTED'
  43. def test_live_cgroup_is_not_declared_stopped_when_supervisor_unit_is_missing(tmp_path,monkeypatch):
  44. folder=tmp_path/'run';folder.mkdir()
  45. job.save_status(folder,{'status':'RUNNING','boot_id':'boot','unit':'old','job_cgroup':'/owned'})
  46. monkeypatch.setattr(job,'boot_id',lambda:'boot')
  47. monkeypatch.setattr(job,'populated',lambda path:True)
  48. monkeypatch.setattr(job.subprocess,'run',lambda *a,**kw:pytest.fail('live group is authoritative'))
  49. assert job.refresh(folder)['status']=='RUNNING'
  50. def test_transient_unit_query_failure_is_not_terminal(tmp_path,monkeypatch):
  51. folder=tmp_path/'run';folder.mkdir()
  52. job.save_status(folder,{'status':'QUEUED','boot_id':'boot','unit':'unit'})
  53. monkeypatch.setattr(job,'boot_id',lambda:'boot')
  54. monkeypatch.setattr(job.subprocess,'run',lambda *a,**kw:subprocess.CompletedProcess(a,1,'','bus unavailable'))
  55. with pytest.raises(RuntimeError,match='cannot inspect'):job.refresh(folder)
  56. assert job.read(folder/'RUN-STATUS.json')['status']=='QUEUED'
  57. assert not (folder/'RESULT.json').exists()
  58. def test_cancel_kills_only_bound_cgroup_and_rejects_stale_boot(tmp_path,monkeypatch):
  59. monkeypatch.setattr(job,'CGROOT',tmp_path);monkeypatch.setattr(job,'boot_id',lambda:'boot')
  60. state={'boot_id':'boot','identity':'a'*64,'container_cgroup':'/container',
  61. 'job_cgroup':'/container/prod2u-job-'+('a'*24)}
  62. path=tmp_path/state['job_cgroup'].lstrip('/');path.mkdir(parents=True)
  63. kill=path/'cgroup.kill';kill.write_text('0')
  64. job.kill_job_group(state);assert kill.read_text()=='1'
  65. kill.write_text('0');state['boot_id']='old'
  66. with pytest.raises(RuntimeError,match='stale'):job.kill_job_group(state)
  67. assert kill.read_text()=='0'
  68. def test_failed_command_cannot_be_reported_as_pass(tmp_path):
  69. folder=tmp_path/'run';folder.mkdir()
  70. job.save_status(folder,{'status':'RUNNING'})
  71. (folder/'stdout.log').write_text('native failure')
  72. assert job.finish(folder,7,None)==1
  73. result=job.read(folder/'RESULT.json')
  74. assert result['status']=='FAIL' and result['exit_code']==7
  75. assert result['artifacts'][0]['path']=='stdout.log'
  76. def test_cancel_before_heavy_lock_stops_only_its_unit(tmp_path,monkeypatch):
  77. folder=tmp_path/'queued';folder.mkdir()
  78. job.save_status(folder,{'status':'QUEUED','phase':'submit','boot_id':'boot','unit':job.unit_name(folder)})
  79. monkeypatch.setattr(job,'boot_id',lambda:'boot')
  80. calls=[]
  81. monkeypatch.setattr(job.subprocess,'run',lambda args,**kw:calls.append(args))
  82. result=job.cancel(folder)
  83. assert result['status']=='CANCELLED' and result['exit_code']==130
  84. assert calls==[['systemctl','stop',job.unit_name(folder)]]
  85. assert not (folder/'GO').exists()
  86. @pytest.mark.parametrize('status,phase',[('PASS','finished'),('RUNNING','command'),('QUEUED','container_start')])
  87. def test_repeated_worker_does_not_execute_again(tmp_path,monkeypatch,status,phase):
  88. folder=tmp_path/'worker';folder.mkdir()
  89. job.atomic(folder/'SPEC.json',{})
  90. job.save_status(folder,{'status':status,'phase':phase})
  91. monkeypatch.setattr(job.signal,'signal',lambda *args:None)
  92. assert job.launch_locked(folder,None)==0
  93. assert not (folder/'stdout.log').exists() and not (folder/'GO').exists()