13
13
14
14
from mininet_test_util import DEVNULL
15
15
16
- def MakeDockerHost (image , prefix = 'mininet' , startup_timeout_ms = None ):
17
-
18
- class ImageHost (DockerHost ):
19
-
20
- def __init__ (self , * args , ** kwargs ):
21
- host_name = args [0 ]
22
- kwargs ['image' ] = image
23
- assert kwargs ['tmpdir' ], 'tmpdir required for docker host'
24
- kwargs ['tmpdir' ] = os .path .join (kwargs ['tmpdir' ], host_name )
25
- kwargs ['prefix' ] = prefix
26
- if startup_timeout_ms :
27
- kwargs ['startup_timeout_ms' ] = startup_timeout_ms
28
- elif 'DOCKER_STARTUP_TIMEOUT_MS' in os .environ :
29
- env_val = os .environ ['DOCKER_STARTUP_TIMEOUT_MS' ]
30
- if env_val :
31
- kwargs ['startup_timeout_ms' ] = int (env_val )
32
- DockerHost .__init__ (self , * args , ** kwargs )
33
- return ImageHost
34
-
35
16
36
17
class DockerHost (Host ):
37
18
@@ -63,12 +44,16 @@ class DockerHost(Host):
63
44
pollIn = None
64
45
active_log = None
65
46
66
- def __init__ (self , name , image = None , tmpdir = None , prefix = None , env_vars = [] ,
67
- vol_maps = [] , startup_timeout_ms = STARTUP_TIMEOUT_MS , ** kwargs ):
47
+ def __init__ (self , name , image = None , tmpdir = None , prefix = None , env_vars = None ,
48
+ vol_maps = None , startup_timeout_ms = STARTUP_TIMEOUT_MS , ** kwargs ):
68
49
self .image = image
69
50
self .tmpdir = tmpdir
70
51
self .prefix = prefix
52
+ if env_vars is None :
53
+ env_vars = []
71
54
self .env_vars = env_vars
55
+ if vol_maps is None :
56
+ vol_maps = []
72
57
self .vol_maps = vol_maps
73
58
self .startup_timeout_ms = startup_timeout_ms
74
59
Host .__init__ (self , name , ** kwargs )
@@ -131,6 +116,7 @@ def startShell(self):
131
116
self .cmd ('unset HISTFILE; stty -echo; set +m' ) # pylint: disable=no-member
132
117
133
118
def kill (self , purge = False ):
119
+ """Kill a container."""
134
120
debug ('killing container %s.' % self .container )
135
121
if purge :
136
122
kill_cmd = ["docker" , "rm" , "-f" , self .container ]
@@ -146,6 +132,7 @@ def kill(self, purge=False):
146
132
raise
147
133
148
134
def inspect_pid (self ):
135
+ """Return container PID."""
149
136
try :
150
137
pid_cmd = ["docker" , "inspect" , "--format={{ .State.Pid }}" , self .container ]
151
138
pid_pipe = self ._popen (pid_cmd , stdin = DEVNULL , stdout = PIPE , stderr = STDOUT )
@@ -158,9 +145,11 @@ def inspect_pid(self):
158
145
raise
159
146
160
147
def open_log (self , log_name = 'activate.log' ):
148
+ """Open a log file for writing and return it."""
161
149
return open (os .path .join (self .tmpdir , log_name ), 'w' )
162
150
163
151
def activate (self , log_name = 'activate.log' ):
152
+ """Active a container and return STDOUT to it."""
164
153
assert not self .active_pipe , 'container %s already activated' % self .container
165
154
debug ('activating container %s.' % self .container )
166
155
inspect_cmd = ["docker" , "inspect" , "--format={{json .Config}}" , self .image ]
@@ -194,6 +183,7 @@ def activate(self, log_name='activate.log'):
194
183
return self .active_pipe
195
184
196
185
def wait (self ):
186
+ """Wait for an activated container to terminate."""
197
187
try :
198
188
if self .active_pipe_returncode != None :
199
189
return self .active_pipe_returncode
@@ -209,6 +199,7 @@ def wait(self):
209
199
raise
210
200
211
201
def read (self , maxbytes = 1024 ):
202
+ """Read from an activated container."""
212
203
poll_results = self .pollIn .poll (self .startup_timeout_ms )
213
204
data_ready = poll_results and (poll_results [0 ][1 ] & select .POLLIN )
214
205
assert data_ready , (
@@ -272,3 +263,23 @@ def _popen(self, cmd, **params):
272
263
out_fd = pipe .stdout .fileno () if stdout else None
273
264
debug ('docker pid %d: %s, fd %s' % (pipe .pid , cmd , out_fd ))
274
265
return pipe
266
+
267
+
268
+ def MakeDockerHost (image , prefix = 'mininet' , startup_timeout_ms = None ):
269
+
270
+ class ImageHost (DockerHost ):
271
+
272
+ def __init__ (self , * args , ** kwargs ):
273
+ host_name = args [0 ]
274
+ kwargs ['image' ] = image
275
+ assert kwargs ['tmpdir' ], 'tmpdir required for docker host'
276
+ kwargs ['tmpdir' ] = os .path .join (kwargs ['tmpdir' ], host_name )
277
+ kwargs ['prefix' ] = prefix
278
+ if startup_timeout_ms :
279
+ kwargs ['startup_timeout_ms' ] = startup_timeout_ms
280
+ elif 'DOCKER_STARTUP_TIMEOUT_MS' in os .environ :
281
+ env_val = os .environ ['DOCKER_STARTUP_TIMEOUT_MS' ]
282
+ if env_val :
283
+ kwargs ['startup_timeout_ms' ] = int (env_val )
284
+ DockerHost .__init__ (self , * args , ** kwargs )
285
+ return ImageHost
0 commit comments