| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- from copy import deepcopy
- from pathlib import Path
- import subprocess
- import pytest
- from tools.dev_container import Controller,create_args,validate_spec
- def fixture_spec(tmp_path):
- return {'user':'0:0','privileged':True,'network':'host','pid':'host','ipc':'host','uts':'host',
- 'entrypoint':['/bin/sleep'],'command':['infinity'],'workdir':'/workspace','env':{'TEST':'literal $() `value`'},
- 'mounts':[{'source':str(tmp_path),'target':'/workspace','rw':True}]}
- def test_creation_is_persistent_rootful_and_preserves_literal_arguments(tmp_path):
- args=create_args('prod2u-test','sha256:abc',fixture_spec(tmp_path))
- assert '--rm' not in args and '--network=none' not in args
- assert '--privileged' in args and '--network=host' in args and '--userns=host' in args
- assert 'TEST=literal $() `value`' in args
- assert str(tmp_path)+':/workspace:rw' in args
- @pytest.mark.parametrize('target',['/','/usr','/usr/local','/opt','/root','/etc','/var/lib/dpkg'])
- def test_system_tree_mounts_are_rejected(tmp_path,target):
- spec=fixture_spec(tmp_path);spec['mounts'][0]['target']=target
- with pytest.raises(ValueError,match='unsafe'):validate_spec(spec)
- def test_checkpoint_failure_restarts_original_and_never_removes_it(tmp_path,monkeypatch):
- manager=Controller('prod2u-test',tmp_path)
- old={'Id':'original','State':{'Running':True}}
- monkeypatch.setattr(manager,'inspect',lambda *a:old)
- monkeypatch.setattr(manager,'spec',lambda *a:fixture_spec(tmp_path))
- monkeypatch.setattr(manager,'idle',lambda *a:None)
- calls=[]
- def call(args,check=True):
- calls.append(args)
- if args[1]=='commit':raise subprocess.CalledProcessError(1,args)
- return subprocess.CompletedProcess(args,0,'','')
- monkeypatch.setattr(manager,'call',call)
- with pytest.raises(subprocess.CalledProcessError):manager.checkpoint_unlocked(False)
- assert calls[0]==['podman','stop','--time','30','original']
- assert calls[-1]==['podman','start','original']
- assert not any(x[1]=='rm' for x in calls)
- def test_failed_replacement_keeps_original_container(tmp_path,monkeypatch):
- manager=Controller('prod2u-test',tmp_path)
- old={'Id':'original','State':{'Running':True}}
- monkeypatch.setattr(manager,'inspect',lambda *a:old)
- monkeypatch.setattr(manager,'checkpoint_unlocked',lambda **kw:(old,{'image_id':'image','spec':fixture_spec(tmp_path)}))
- monkeypatch.setattr(manager,'probe',lambda *a:(_ for _ in ()).throw(RuntimeError('probe failed')))
- calls=[]
- def call(args,check=True):
- calls.append(args);return subprocess.CompletedProcess(args,0,'candidate-id\n' if args[1]=='create' else '','')
- monkeypatch.setattr(manager,'call',call)
- with pytest.raises(RuntimeError,match='probe failed'):manager.recreate_unlocked()
- assert ['podman','rm','--force','candidate-id'] in calls
- assert ['podman','start','original'] in calls
- assert not any(x[1]=='rm' and 'original' in x for x in calls)
- def test_down_checkpoints_before_removing(tmp_path,monkeypatch):
- manager=Controller('prod2u-test',tmp_path);calls=[]
- monkeypatch.setattr(manager,'inspect',lambda *a:{'Id':'original'})
- def checkpoint(**kw):
- calls.append('checkpoint');return {'Id':'original'},{}
- monkeypatch.setattr(manager,'checkpoint_unlocked',checkpoint)
- monkeypatch.setattr(manager,'call',lambda args,**kw:calls.append(args))
- manager.action('down')
- assert calls==['checkpoint',['podman','rm','original']]
- def test_busy_exec_blocks_lifecycle(tmp_path):
- manager=Controller('prod2u-test',tmp_path)
- with pytest.raises(RuntimeError,match='active exec'):manager.idle({'ExecIDs':['running']})
- def test_desired_mount_changes_do_not_hide_actual_config(tmp_path):
- workspace=tmp_path/'source with spaces';workspace.mkdir()
- manager=Controller('prod2u-test',tmp_path,workspace=workspace)
- original=fixture_spec(tmp_path);before=deepcopy(original)
- desired=manager.desired(original)
- assert original==before and desired['mounts'][0]['source']==str(workspace)
- assert desired['env']['XDG_CACHE_HOME']=='/data/prod2u/cache'
- def test_status_distinguishes_running_image_from_checkpoint(tmp_path,monkeypatch):
- manager=Controller('prod2u-test',tmp_path)
- spec=fixture_spec(tmp_path)
- saved=manager.save('checkpoint-image',spec)
- monkeypatch.setattr(manager,'spec',lambda info:spec)
- result=manager.summary({'Id':'container','Image':'running-image','State':{'Status':'running'}})
- assert result['checkpoint_image_id']=='checkpoint-image'
- assert result['checkpoint_environment_id']==saved['environment_id']
- assert result['environment_id']!=saved['environment_id']
- def test_status_can_report_while_run_holds_shared_lifecycle_lock(tmp_path,monkeypatch):
- manager=Controller('prod2u-test',tmp_path)
- monkeypatch.setattr(manager,'lock',lambda **kw:(_ for _ in ()).throw(AssertionError('status must not wait for jobs')))
- monkeypatch.setattr(manager,'inspect',lambda:None)
- assert manager.action('status')['state']=='absent'
|