1515from models_library .projects_nodes_io import NodeID
1616from models_library .services_types import ServiceRunID
1717from pydantic import NonNegativeInt
18+ from pytest_mock import MockerFixture
19+ from servicelib .container_utils import run_command_in_container
1820from simcore_service_agent .core .settings import ApplicationSettings
1921from simcore_service_agent .services .backup import backup_volume
2022from simcore_service_agent .services .docker_utils import get_volume_details
@@ -42,21 +44,26 @@ def volume_content(tmpdir: Path) -> Path:
4244@pytest .fixture
4345async def mock_container_with_data (
4446 volume_content : Path , monkeypatch : pytest .MonkeyPatch
45- ) -> AsyncIterable [None ]:
47+ ) -> AsyncIterable [str ]:
4648 async with aiodocker .Docker () as client :
4749 container = await client .containers .run (
4850 config = {
4951 "Image" : "alpine:latest" ,
5052 "Cmd" : ["/bin/ash" , "-c" , "sleep 10000" ],
51- "HostConfig" : {"Binds" : [f"{ volume_content } :{ volume_content } :rw" ]},
53+ "HostConfig" : {
54+ "Binds" : [
55+ f"{ volume_content } :{ volume_content } :rw" ,
56+ "/var/run/docker.sock:/var/run/docker.sock:rw" ,
57+ ]
58+ },
5259 }
5360 )
5461 container_inspect = await container .show ()
5562
5663 container_name = container_inspect ["Name" ][1 :]
5764 monkeypatch .setenv ("HOSTNAME" , container_name )
5865
59- yield None
66+ yield container_inspect [ "Id" ]
6067
6168 await container .delete (force = True )
6269
@@ -68,8 +75,24 @@ def downlaoded_from_s3(tmpdir: Path) -> Path:
6875 return path
6976
7077
78+ @pytest .fixture
79+ async def mock__get_self_container_ip (
80+ mock_container_with_data : str ,
81+ mocker : MockerFixture ,
82+ ) -> None :
83+ container_ip = await run_command_in_container (
84+ mock_container_with_data , command = "hostname -i"
85+ )
86+
87+ mocker .patch (
88+ "simcore_service_agent.services.backup._get_self_container_ip" ,
89+ return_value = container_ip .strip (),
90+ )
91+
92+
7193async def test_backup_volume (
72- mock_container_with_data : None ,
94+ mock_container_with_data : str ,
95+ mock__get_self_container_ip : None ,
7396 volume_content : Path ,
7497 project_id : ProjectID ,
7598 swarm_stack_name : str ,
0 commit comments