4
4
"""
5
5
6
6
import pytest
7
+ import docker
8
+ import io
7
9
8
10
from labgrid import Environment
9
11
from labgrid .driver import DockerDriver
@@ -44,6 +46,34 @@ def docker_env(tmp_path_factory):
44
46
drivers:
45
47
- DockerDriver:
46
48
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"
47
77
container_name: "ubuntu-lg-example"
48
78
host_config: {"network_mode": "bridge"}
49
79
network_services: [
@@ -91,6 +121,25 @@ def command(docker_target):
91
121
strategy .transition ("gone" )
92
122
93
123
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
+
94
143
@pytest .mark .skipif (not check_external_progs_present (),
95
144
reason = "No access to a docker daemon" )
96
145
def test_docker_with_daemon (command ):
@@ -110,6 +159,32 @@ def test_docker_with_daemon(command):
110
159
assert len (stderr ) == 0
111
160
112
161
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
+
113
188
def test_create_driver_fail_missing_docker_daemon (target ):
114
189
"""The test target does not contain any DockerDaemon instance -
115
190
and so creation must fail.
@@ -159,6 +234,8 @@ def test_docker_without_daemon(docker_env, mocker):
159
234
'Id' : '1'
160
235
}]
161
236
]
237
+ docker_client .images .get .side_effect = docker .errors .ImageNotFound (
238
+ "Image not found" , response = None , explanation = "" )
162
239
163
240
# Mock actions on the imported "socket" python module
164
241
socket_create_connection = mocker .patch ('socket.create_connection' )
@@ -199,7 +276,10 @@ def test_docker_without_daemon(docker_env, mocker):
199
276
# Assert what mock calls transitioning to "shell" must have caused
200
277
#
201
278
# 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
+
203
283
assert api_client .create_host_config .call_count == 1
204
284
assert api_client .create_container .call_count == 1
205
285
#
0 commit comments