44"""
55
66import pytest
7+ import docker
8+ import io
79
810from labgrid import Environment
911from labgrid .driver import DockerDriver
@@ -44,6 +46,34 @@ def docker_env(tmp_path_factory):
4446 drivers:
4547 - DockerDriver:
4648 image_uri: "rastasheep/ubuntu-sshd:16.04"
49+ pull: 'missing'
50+ container_name: "ubuntu-lg-example"
51+ host_config: {"network_mode": "bridge"}
52+ network_services: [
53+ {"port": 22, "username": "root", "password": "root"}]
54+ - DockerStrategy: {}
55+ - SSHDriver:
56+ keyfile: ""
57+ """
58+ )
59+ return Environment (str (p ))
60+
61+
62+ @pytest .fixture
63+ def docker_env_for_local_container (tmp_path_factory ):
64+ """Create Environment instance from the given inline YAML file."""
65+ p = tmp_path_factory .mktemp ("docker" ) / "config.yaml"
66+ p .write_text (
67+ """
68+ targets:
69+ main:
70+ resources:
71+ - DockerDaemon:
72+ docker_daemon_url: "unix:///var/run/docker.sock"
73+ drivers:
74+ - DockerDriver:
75+ image_uri: "local_rastasheep"
76+ pull: "never"
4777 container_name: "ubuntu-lg-example"
4878 host_config: {"network_mode": "bridge"}
4979 network_services: [
@@ -91,6 +121,25 @@ def command(docker_target):
91121 strategy .transition ("gone" )
92122
93123
124+ @pytest .fixture
125+ def docker_target_for_local_image (docker_env_for_local_container ):
126+ """Same as `docker_target` but uses a different image uri"""
127+ t = docker_env_for_local_container .get_target ()
128+ yield t
129+
130+ from labgrid .resource import ResourceManager
131+ ResourceManager .instances = {}
132+
133+
134+ @pytest .fixture
135+ def local_command (docker_target_for_local_image ):
136+ """Same as `command` but uses a different image uri"""
137+ strategy = docker_target_for_local_image .get_driver ('DockerStrategy' )
138+ strategy .transition ("accessible" )
139+ shell = docker_target_for_local_image .get_driver ('CommandProtocol' )
140+ yield shell
141+ strategy .transition ("gone" )
142+
94143@pytest .mark .skipif (not check_external_progs_present (),
95144 reason = "No access to a docker daemon" )
96145def test_docker_with_daemon (command ):
@@ -110,6 +159,32 @@ def test_docker_with_daemon(command):
110159 assert len (stderr ) == 0
111160
112161
162+ @pytest .fixture
163+ def build_image ():
164+ client = docker .from_env ()
165+ dockerfile_content = """
166+ FROM rastasheep/ubuntu-sshd:16.04
167+ """
168+ dockerfile_stream = io .BytesIO (dockerfile_content .encode ("utf-8" ))
169+ image , logs = client .images .build (fileobj = dockerfile_stream , tag = "local_rastasheep" , rm = True )
170+
171+
172+ @pytest .mark .skipif (not check_external_progs_present (),
173+ reason = "No access to a docker daemon" )
174+ def test_docker_with_daemon_and_local_image (build_image , local_command ):
175+ """Build a container locally and connect to it"""
176+ stdout , stderr , return_code = local_command .run ('cat /proc/version' )
177+ assert return_code == 0
178+ assert len (stdout ) > 0
179+ assert len (stderr ) == 0
180+ assert 'Linux' in stdout [0 ]
181+
182+ stdout , stderr , return_code = local_command .run ('false' )
183+ assert return_code != 0
184+ assert len (stdout ) == 0
185+ assert len (stderr ) == 0
186+
187+
113188def test_create_driver_fail_missing_docker_daemon (target ):
114189 """The test target does not contain any DockerDaemon instance -
115190 and so creation must fail.
@@ -159,6 +234,8 @@ def test_docker_without_daemon(docker_env, mocker):
159234 'Id' : '1'
160235 }]
161236 ]
237+ docker_client .images .get .side_effect = docker .errors .ImageNotFound (
238+ "Image not found" , response = None , explanation = "" )
162239
163240 # Mock actions on the imported "socket" python module
164241 socket_create_connection = mocker .patch ('socket.create_connection' )
@@ -199,7 +276,10 @@ def test_docker_without_daemon(docker_env, mocker):
199276 # Assert what mock calls transitioning to "shell" must have caused
200277 #
201278 # DockerDriver::on_activate():
202- assert docker_client .images .pull .call_count == 1
279+ image_uri = t .get_driver ('DockerDriver' ).image_uri
280+ docker_client .images .get .assert_called_once_with (image_uri )
281+ docker_client .images .pull .assert_called_once_with (image_uri )
282+
203283 assert api_client .create_host_config .call_count == 1
204284 assert api_client .create_container .call_count == 1
205285 #
0 commit comments