@@ -441,7 +441,7 @@ def launched(monkeypatch):
441441 ran = []
442442 monkeypatch .setattr (local_sandbox , "kvm_platforms" , lambda : frozenset ({"linux/arm64" }))
443443 monkeypatch .setattr (local_sandbox , "host_platform" , lambda : "linux/arm64" )
444- monkeypatch .setattr (local_sandbox , "machine_image " , AsyncMock (return_value = "agentenv-vm:0123" ))
444+ monkeypatch .setattr (local_sandbox , "machine_for " , AsyncMock (return_value = local_disk . Machine ( "agentenv-vm:0123" ) ))
445445 monkeypatch .setattr (local_sandbox , "_free_host_port" , lambda : 20001 )
446446 monkeypatch .setattr (local_sandbox , "_host_ips" , lambda : ("127.0.0.1" ,))
447447 monkeypatch .setattr (local_sandbox , "docker" , lambda * args , input = None , timeout = None : ran .append (
@@ -526,6 +526,82 @@ async def test_a_disk_isnt_booted_where_docker_cant_pass_kvm(launched, monkeypat
526526 assert launched == [] and LocalSandboxProvider .disk_image_platforms () == frozenset ()
527527
528528
529+ @pytest .mark .asyncio
530+ async def test_a_disk_boots_with_its_image_mounted_into_the_launcher_where_docker_mounts_images (launched , monkeypatch ):
531+ mounted = local_disk .Machine ("launcher:1" , ("--mount" , "type=image,source=disk:1,target=/disk,image-subpath=disk" ))
532+ monkeypatch .setattr (local_sandbox , "machine_for" , AsyncMock (return_value = mounted ))
533+
534+ await LocalSandboxProvider ().create_vm (image = "disk:1" , exposed_ports = [18765 ], env = {}, cpu = 2 , memory = 4096 )
535+
536+ (run ,) = launched
537+ assert run [- 1 ] == "launcher:1" and run [run .index ("--mount" ):run .index ("--mount" ) + 2 ] == mounted .args
538+
539+
540+ @pytest .mark .asyncio
541+ @pytest .mark .parametrize ("mounts" , [True , False ], ids = ["mounted" , "copied" ])
542+ async def test_a_disk_is_mounted_into_the_launcher_where_docker_mounts_images_else_copied_into_a_machine_image (
543+ monkeypatch , mounts ):
544+ """No copy of a disk, nor of a Windows disk's base, is made for each launcher where Docker can mount it."""
545+ monkeypatch .setattr (local_disk , "mounts_images" , lambda : mounts )
546+ monkeypatch .setattr (local_disk , "launcher" , lambda : "launcher:1" )
547+ monkeypatch .setattr (local_disk , "machine_image" , AsyncMock (return_value = "agentenv-vm:1" ))
548+
549+ machine = await local_disk .machine_for ("disk:1" )
550+
551+ assert machine == (local_disk .Machine ("launcher:1" , (
552+ "--mount" , "type=image,source=disk:1,target=/disk,image-subpath=disk" )) if mounts else local_disk .Machine (
553+ "agentenv-vm:1" ))
554+ assert local_disk .machine_image .await_count == (0 if mounts else 1 )
555+
556+
557+ @pytest .mark .parametrize ("info, mounts" , [
558+ ('29.9.3 29.9.3 [["driver-type","io.containerd.snapshotter.v1"]]' , True ),
559+ ('28.0.0 28.0.0 [["driver-type","io.containerd.snapshotter.v1"]]' , True ),
560+ ('27.5.1 27.5.1 [["driver-type","io.containerd.snapshotter.v1"]]' , False ),
561+ ('27.5.1 29.9.3 [["driver-type","io.containerd.snapshotter.v1"]]' , False ),
562+ ('29.9.3 29.9.3 [["Backing Filesystem","extfs"],["Supports d_type","true"]]' , False ),
563+ ("4.9.3 4.9.3 null" , False ),
564+ (DockerFailed ("docker info failed: template: :1: can't evaluate field ClientInfo" , 1 ), False ),
565+ (subprocess .TimeoutExpired (["docker" , "info" ], 30 ), False )],
566+ ids = ["29-containerd" , "28-containerd" , "27" , "27-client-on-a-29-server" , "29-overlay2" , "podman" ,
567+ "a-client-too-old-to-say" , "no-answer" ])
568+ def test_docker_mounts_images_from_28_on_both_ends_on_the_containerd_image_store (monkeypatch , info , mounts ):
569+ """A Docker that fails to say, or doesn't in time, gets the copy."""
570+ local_disk ._docker_mounts_images .cache_clear ()
571+ asked = []
572+
573+ def docker (* args , timeout = None ):
574+ asked .append (timeout )
575+ if isinstance (info , Exception ):
576+ raise info
577+ return info
578+
579+ monkeypatch .setattr (local_disk , "docker" , docker )
580+
581+ assert local_disk .mounts_images () is mounts and asked == [local_disk ._DOCKER_INFO_SECONDS ]
582+ local_disk ._docker_mounts_images .cache_clear ()
583+
584+
585+ def test_a_docker_that_failed_to_say_is_asked_again_and_an_answer_is_kept (monkeypatch ):
586+ """A daemon busy for a moment doesn't leave a long-lived process copying disks for good."""
587+ local_disk ._docker_mounts_images .cache_clear ()
588+ answers = [subprocess .TimeoutExpired (["docker" , "info" ], 30 ),
589+ '29.9.3 29.9.3 [["driver-type","io.containerd.snapshotter.v1"]]' ]
590+ asked = []
591+
592+ def docker (* args , timeout = None ):
593+ asked .append (args )
594+ answer = answers .pop (0 )
595+ if isinstance (answer , Exception ):
596+ raise answer
597+ return answer
598+
599+ monkeypatch .setattr (local_disk , "docker" , docker )
600+
601+ assert [local_disk .mounts_images () for _ in range (3 )] == [False , True , True ] and len (asked ) == 2
602+ local_disk ._docker_mounts_images .cache_clear ()
603+
604+
529605@pytest .mark .asyncio
530606@pytest .mark .parametrize ("layers, dockerfile" , [
531607 ("1" , "FROM launcher:1\n COPY --from=disk:1 /disk/ /disk/\n " ),
0 commit comments