test_dev_container.py 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from copy import deepcopy
  2. from pathlib import Path
  3. import subprocess
  4. import pytest
  5. from tools.dev_container import Controller,create_args,validate_spec
  6. def fixture_spec(tmp_path):
  7. return {'user':'0:0','privileged':True,'network':'host','pid':'host','ipc':'host','uts':'host',
  8. 'entrypoint':['/bin/sleep'],'command':['infinity'],'workdir':'/workspace','env':{'TEST':'literal $() `value`'},
  9. 'mounts':[{'source':str(tmp_path),'target':'/workspace','rw':True}]}
  10. def test_creation_is_persistent_rootful_and_preserves_literal_arguments(tmp_path):
  11. args=create_args('prod2u-test','sha256:abc',fixture_spec(tmp_path))
  12. assert '--rm' not in args and '--network=none' not in args
  13. assert '--privileged' in args and '--network=host' in args and '--userns=host' in args
  14. assert 'TEST=literal $() `value`' in args
  15. assert str(tmp_path)+':/workspace:rw' in args
  16. @pytest.mark.parametrize('target',['/','/usr','/usr/local','/opt','/root','/etc','/var/lib/dpkg'])
  17. def test_system_tree_mounts_are_rejected(tmp_path,target):
  18. spec=fixture_spec(tmp_path);spec['mounts'][0]['target']=target
  19. with pytest.raises(ValueError,match='unsafe'):validate_spec(spec)
  20. def test_checkpoint_failure_restarts_original_and_never_removes_it(tmp_path,monkeypatch):
  21. manager=Controller('prod2u-test',tmp_path)
  22. old={'Id':'original','State':{'Running':True}}
  23. monkeypatch.setattr(manager,'inspect',lambda *a:old)
  24. monkeypatch.setattr(manager,'spec',lambda *a:fixture_spec(tmp_path))
  25. monkeypatch.setattr(manager,'idle',lambda *a:None)
  26. calls=[]
  27. def call(args,check=True):
  28. calls.append(args)
  29. if args[1]=='commit':raise subprocess.CalledProcessError(1,args)
  30. return subprocess.CompletedProcess(args,0,'','')
  31. monkeypatch.setattr(manager,'call',call)
  32. with pytest.raises(subprocess.CalledProcessError):manager.checkpoint_unlocked(False)
  33. assert calls[0]==['podman','stop','--time','30','original']
  34. assert calls[-1]==['podman','start','original']
  35. assert not any(x[1]=='rm' for x in calls)
  36. def test_failed_replacement_keeps_original_container(tmp_path,monkeypatch):
  37. manager=Controller('prod2u-test',tmp_path)
  38. old={'Id':'original','State':{'Running':True}}
  39. monkeypatch.setattr(manager,'inspect',lambda *a:old)
  40. monkeypatch.setattr(manager,'checkpoint_unlocked',lambda **kw:(old,{'image_id':'image','spec':fixture_spec(tmp_path)}))
  41. monkeypatch.setattr(manager,'probe',lambda *a:(_ for _ in ()).throw(RuntimeError('probe failed')))
  42. calls=[]
  43. def call(args,check=True):
  44. calls.append(args);return subprocess.CompletedProcess(args,0,'candidate-id\n' if args[1]=='create' else '','')
  45. monkeypatch.setattr(manager,'call',call)
  46. with pytest.raises(RuntimeError,match='probe failed'):manager.recreate_unlocked()
  47. assert ['podman','rm','--force','candidate-id'] in calls
  48. assert ['podman','start','original'] in calls
  49. assert not any(x[1]=='rm' and 'original' in x for x in calls)
  50. def test_down_checkpoints_before_removing(tmp_path,monkeypatch):
  51. manager=Controller('prod2u-test',tmp_path);calls=[]
  52. monkeypatch.setattr(manager,'inspect',lambda *a:{'Id':'original'})
  53. def checkpoint(**kw):
  54. calls.append('checkpoint');return {'Id':'original'},{}
  55. monkeypatch.setattr(manager,'checkpoint_unlocked',checkpoint)
  56. monkeypatch.setattr(manager,'call',lambda args,**kw:calls.append(args))
  57. manager.action('down')
  58. assert calls==['checkpoint',['podman','rm','original']]
  59. def test_busy_exec_blocks_lifecycle(tmp_path):
  60. manager=Controller('prod2u-test',tmp_path)
  61. with pytest.raises(RuntimeError,match='active exec'):manager.idle({'ExecIDs':['running']})
  62. def test_desired_mount_changes_do_not_hide_actual_config(tmp_path):
  63. workspace=tmp_path/'source with spaces';workspace.mkdir()
  64. manager=Controller('prod2u-test',tmp_path,workspace=workspace)
  65. original=fixture_spec(tmp_path);before=deepcopy(original)
  66. desired=manager.desired(original)
  67. assert original==before and desired['mounts'][0]['source']==str(workspace)
  68. assert desired['env']['XDG_CACHE_HOME']=='/data/prod2u/cache'
  69. def test_status_distinguishes_running_image_from_checkpoint(tmp_path,monkeypatch):
  70. manager=Controller('prod2u-test',tmp_path)
  71. spec=fixture_spec(tmp_path)
  72. saved=manager.save('checkpoint-image',spec)
  73. monkeypatch.setattr(manager,'spec',lambda info:spec)
  74. result=manager.summary({'Id':'container','Image':'running-image','State':{'Status':'running'}})
  75. assert result['checkpoint_image_id']=='checkpoint-image'
  76. assert result['checkpoint_environment_id']==saved['environment_id']
  77. assert result['environment_id']!=saved['environment_id']
  78. def test_status_can_report_while_run_holds_shared_lifecycle_lock(tmp_path,monkeypatch):
  79. manager=Controller('prod2u-test',tmp_path)
  80. monkeypatch.setattr(manager,'lock',lambda **kw:(_ for _ in ()).throw(AssertionError('status must not wait for jobs')))
  81. monkeypatch.setattr(manager,'inspect',lambda:None)
  82. assert manager.action('status')['state']=='absent'