|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Docker\Tests\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Docker\API\Model\Container; |
|
6
|
|
|
use Docker\Context\ContextBuilder; |
|
7
|
|
|
use Docker\Tests\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class ContainerManagerTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Return the container manager |
|
13
|
|
|
* |
|
14
|
|
|
* @return \Docker\API\Resource\ContainerResource |
|
15
|
|
|
*/ |
|
16
|
|
|
private function getManager() |
|
17
|
|
|
{ |
|
18
|
|
|
return $this->getDocker()->getContainerManager(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testFindAll() |
|
22
|
|
|
{ |
|
23
|
|
|
$manager = $this->getManager(); |
|
24
|
|
|
$containers = $manager->findAll(); |
|
25
|
|
|
|
|
26
|
|
|
$this->assertInternalType('array', $containers); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testCreate() |
|
30
|
|
|
{ |
|
31
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$manager = $this->getManager(); |
|
34
|
|
|
$manager->create($container); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertNotEmpty($container->getId()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testInteract() |
|
40
|
|
|
{ |
|
41
|
|
|
$container = new Container([ |
|
|
|
|
|
|
42
|
|
|
'Image' => 'ubuntu:precise', |
|
43
|
|
|
'Cmd' => ['/bin/bash'], |
|
44
|
|
|
'AttachStdin' => false, |
|
45
|
|
|
'AttachStdout' => true, |
|
46
|
|
|
'AttachStderr' => true, |
|
47
|
|
|
'OpenStdin' => true, |
|
48
|
|
|
'Tty' => true, |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
|
|
$manager = $this->getManager(); |
|
52
|
|
|
$manager->create($container); |
|
53
|
|
|
$stream = $manager->interact($container); |
|
|
|
|
|
|
54
|
|
|
$manager->start($container); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertNotEmpty($container->getId()); |
|
57
|
|
|
$this->assertInstanceOf('\Docker\Http\Stream\InteractiveStream', $stream); |
|
58
|
|
|
|
|
59
|
|
|
stream_set_blocking($stream->getSocket(), 0); |
|
60
|
|
|
|
|
61
|
|
|
$read = [$stream->getSocket()]; |
|
62
|
|
|
$write = null; |
|
63
|
|
|
$expect = null; |
|
64
|
|
|
|
|
65
|
|
|
$stream->write("echo test\n"); |
|
66
|
|
|
$data = ""; |
|
67
|
|
|
do { |
|
68
|
|
|
$frame = $stream->receive(true); |
|
69
|
|
|
$data .= $frame['data']; |
|
70
|
|
|
} while (stream_select($read, $write, $expect, 1) > 0); |
|
71
|
|
|
|
|
72
|
|
|
$manager->stop($container, 1); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$this->assertRegExp('#root@'.substr($container->getId(), 0, 12).':/\# echo test#', $data, $data); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testCreateThrowsRightFormedException() |
|
78
|
|
|
{ |
|
79
|
|
|
$container = new Container(['Image' => 'non-existent']); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$manager = $this->getManager(); |
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
|
|
$manager->create($container); |
|
85
|
|
|
} catch (\GuzzleHttp\Exception\RequestException $e) { |
|
|
|
|
|
|
86
|
|
|
$this->assertTrue($e->hasResponse()); |
|
87
|
|
|
$this->assertEquals("404", $e->getResponse()->getStatusCode()); |
|
88
|
|
|
$this->assertContains('No such image', $e->getMessage()); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
public function testStart() |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
$manager = $this->getManager(); |
|
97
|
|
|
$manager->create($container); |
|
98
|
|
|
$manager->start($container); |
|
99
|
|
|
|
|
100
|
|
|
$runtimeInformations = $container->getRuntimeInformations(); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$this->assertEquals(0, $runtimeInformations['State']['ExitCode']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testRunDefault() |
|
106
|
|
|
{ |
|
107
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]); |
|
|
|
|
|
|
108
|
|
|
$manager = $this |
|
109
|
|
|
->getMockBuilder('\Docker\Manager\ContainerManager') |
|
110
|
|
|
->setMethods(['create', 'start', 'wait']) |
|
111
|
|
|
->disableOriginalConstructor() |
|
112
|
|
|
->getMock(); |
|
113
|
|
|
|
|
114
|
|
|
$container->setExitCode(0); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$manager->expects($this->once()) |
|
117
|
|
|
->method('create') |
|
118
|
|
|
->with($this->isInstanceOf('\Docker\Container')) |
|
119
|
|
|
->will($this->returnSelf()); |
|
120
|
|
|
|
|
121
|
|
|
$manager->expects($this->once()) |
|
122
|
|
|
->method('start') |
|
123
|
|
|
->with($this->isInstanceOf('\Docker\Container')) |
|
124
|
|
|
->will($this->returnSelf()); |
|
125
|
|
|
|
|
126
|
|
|
$manager->expects($this->once()) |
|
127
|
|
|
->method('wait') |
|
128
|
|
|
->with($this->isInstanceOf('\Docker\Container')) |
|
129
|
|
|
->will($this->returnSelf()); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertTrue($manager->run($container)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function testRunAttach() |
|
135
|
|
|
{ |
|
136
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]); |
|
|
|
|
|
|
137
|
|
|
$manager = $this |
|
138
|
|
|
->getMockBuilder('\Docker\Manager\ContainerManager') |
|
139
|
|
|
->setMethods(['create', 'start', 'wait', 'attach']) |
|
140
|
|
|
->disableOriginalConstructor() |
|
141
|
|
|
->getMock(); |
|
142
|
|
|
|
|
143
|
|
|
$response = $this->getMockBuilder('\GuzzleHttp\Message\Response')->disableOriginalConstructor()->getMock(); |
|
144
|
|
|
$stream = $this->getMockBuilder('\GuzzleHttp\Stream\Stream')->disableOriginalConstructor()->getMock(); |
|
145
|
|
|
|
|
146
|
|
|
$container->setExitCode(0); |
|
|
|
|
|
|
147
|
|
|
$callback = function () {}; |
|
148
|
|
|
|
|
149
|
|
|
$manager->expects($this->once()) |
|
150
|
|
|
->method('create') |
|
151
|
|
|
->with($this->isInstanceOf('\Docker\Container')) |
|
152
|
|
|
->will($this->returnSelf()); |
|
153
|
|
|
|
|
154
|
|
|
$manager->expects($this->once()) |
|
155
|
|
|
->method('attach') |
|
156
|
|
|
->with($this->isInstanceOf('\Docker\Container'), $this->equalTo($callback), $this->equalTo(true), $this->equalTo(true), $this->equalTo(true), $this->equalTo(true), $this->equalTo(true), $this->equalTo(null)) |
|
157
|
|
|
->will($this->returnValue($response)); |
|
158
|
|
|
|
|
159
|
|
|
$manager->expects($this->once()) |
|
160
|
|
|
->method('start') |
|
161
|
|
|
->with($this->isInstanceOf('\Docker\Container')) |
|
162
|
|
|
->will($this->returnSelf()); |
|
163
|
|
|
|
|
164
|
|
|
$response->expects($this->once()) |
|
165
|
|
|
->method('getBody') |
|
166
|
|
|
->will($this->returnValue($stream)); |
|
167
|
|
|
|
|
168
|
|
|
$manager->expects($this->once()) |
|
169
|
|
|
->method('wait') |
|
170
|
|
|
->with($this->isInstanceOf('\Docker\Container')) |
|
171
|
|
|
->will($this->returnSelf()); |
|
172
|
|
|
|
|
173
|
|
|
$this->assertTrue($manager->run($container, $callback)); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function testRunDaemon() |
|
177
|
|
|
{ |
|
178
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]); |
|
|
|
|
|
|
179
|
|
|
$manager = $this |
|
180
|
|
|
->getMockBuilder('\Docker\Manager\ContainerManager') |
|
181
|
|
|
->setMethods(['create', 'start', 'wait']) |
|
182
|
|
|
->disableOriginalConstructor() |
|
183
|
|
|
->getMock(); |
|
184
|
|
|
|
|
185
|
|
|
$container->setExitCode(0); |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
$manager->expects($this->once()) |
|
188
|
|
|
->method('create') |
|
189
|
|
|
->with($this->isInstanceOf('\Docker\Container')) |
|
190
|
|
|
->will($this->returnSelf()); |
|
191
|
|
|
|
|
192
|
|
|
$manager->expects($this->once()) |
|
193
|
|
|
->method('start') |
|
194
|
|
|
->with($this->isInstanceOf('\Docker\Container')) |
|
195
|
|
|
->will($this->returnSelf()); |
|
196
|
|
|
|
|
197
|
|
|
$manager->expects($this->never()) |
|
198
|
|
|
->method('wait'); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertNull($manager->run($container, null, [], true)); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
View Code Duplication |
public function testAttach() |
|
|
|
|
|
|
204
|
|
|
{ |
|
205
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/bash', '-c', 'echo -n "output"']]); |
|
|
|
|
|
|
206
|
|
|
$manager = $this->getManager(); |
|
207
|
|
|
|
|
208
|
|
|
$type = 0; |
|
209
|
|
|
$output = ""; |
|
210
|
|
|
|
|
211
|
|
|
$manager->create($container); |
|
212
|
|
|
$response = $manager->attach($container, function ($log, $stdtype) use (&$type, &$output) { |
|
|
|
|
|
|
213
|
|
|
$type = $stdtype; |
|
214
|
|
|
$output = $log; |
|
215
|
|
|
}); |
|
216
|
|
|
$manager->start($container); |
|
217
|
|
|
|
|
218
|
|
|
$response->getBody()->getContents(); |
|
219
|
|
|
|
|
220
|
|
|
$this->assertEquals(1, $type); |
|
221
|
|
|
$this->assertEquals('output', $output); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
View Code Duplication |
public function testAttachStderr() |
|
|
|
|
|
|
225
|
|
|
{ |
|
226
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/bash', '-c', 'echo -n "error" 1>&2']]); |
|
|
|
|
|
|
227
|
|
|
$manager = $this->getManager(); |
|
228
|
|
|
|
|
229
|
|
|
$type = 0; |
|
230
|
|
|
$output = ""; |
|
231
|
|
|
|
|
232
|
|
|
$manager->create($container); |
|
233
|
|
|
$response = $manager->attach($container, function ($log, $stdtype) use (&$type, &$output) { |
|
|
|
|
|
|
234
|
|
|
$type = $stdtype; |
|
235
|
|
|
$output = $log; |
|
236
|
|
|
}); |
|
237
|
|
|
$manager->start($container); |
|
238
|
|
|
|
|
239
|
|
|
$response->getBody()->getContents(); |
|
240
|
|
|
|
|
241
|
|
|
$this->assertEquals(2, $type); |
|
242
|
|
|
$this->assertEquals('error', $output); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Not sure how to reliably test that we actually waited for the container |
|
247
|
|
|
* but this should at least ensure no exception is thrown |
|
248
|
|
|
*/ |
|
249
|
|
View Code Duplication |
public function testWait() |
|
|
|
|
|
|
250
|
|
|
{ |
|
251
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '1']]); |
|
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
$manager = $this->getManager(); |
|
254
|
|
|
$manager->run($container); |
|
|
|
|
|
|
255
|
|
|
$manager->wait($container); |
|
256
|
|
|
|
|
257
|
|
|
$runtimeInformations = $container->getRuntimeInformations(); |
|
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
$this->assertEquals(0, $runtimeInformations['State']['ExitCode']); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @expectedException GuzzleHttp\Exception\RequestException |
|
264
|
|
|
*/ |
|
265
|
|
View Code Duplication |
public function testWaitWithTimeout() |
|
|
|
|
|
|
266
|
|
|
{ |
|
267
|
|
|
if (getenv('DOCKER_TLS_VERIFY')) { |
|
268
|
|
|
$this->markTestSkipped('This test failed when using ssl due to this bug : https://bugs.php.net/bug.php?id=41631'); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '2']]); |
|
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
$manager = $this->getManager(); |
|
274
|
|
|
$manager->create($container); |
|
275
|
|
|
$manager->start($container); |
|
276
|
|
|
$manager->wait($container, 1); |
|
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
View Code Duplication |
public function testTimeoutExceptionHasRequest() |
|
|
|
|
|
|
280
|
|
|
{ |
|
281
|
|
|
if (getenv('DOCKER_TLS_VERIFY')) { |
|
282
|
|
|
$this->markTestSkipped('This test failed when using ssl due to this bug : https://bugs.php.net/bug.php?id=41631'); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '2']]); |
|
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
$manager = $this->getManager(); |
|
288
|
|
|
$manager->run($container); |
|
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
try { |
|
291
|
|
|
$manager->wait($container, 1); |
|
|
|
|
|
|
292
|
|
|
} catch (RequestException $e) { |
|
|
|
|
|
|
293
|
|
|
$this->assertInstanceOf('Docker\\Http\\Request', $e->getRequest()); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
View Code Duplication |
public function testExposeFixedPort() |
|
|
|
|
|
|
298
|
|
|
{ |
|
299
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '1']]); |
|
|
|
|
|
|
300
|
|
|
|
|
301
|
|
|
$port = new Port('8888:80/tcp'); |
|
302
|
|
|
|
|
303
|
|
|
$container->setExposedPorts($port); |
|
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
$manager = $this->getManager(); |
|
306
|
|
|
$manager->create($container); |
|
307
|
|
|
$manager->start($container, ['PortBindings' => $port->toSpec()]); |
|
308
|
|
|
|
|
309
|
|
|
$this->assertEquals(8888, $container->getMappedPort(80)->getHostPort()); |
|
|
|
|
|
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
View Code Duplication |
public function testExposeRandomPort() |
|
|
|
|
|
|
313
|
|
|
{ |
|
314
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '1']]); |
|
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
$port = new Port('80/tcp'); |
|
317
|
|
|
$container->setExposedPorts($port); |
|
|
|
|
|
|
318
|
|
|
|
|
319
|
|
|
$manager = $this->getManager(); |
|
320
|
|
|
$manager->create($container); |
|
321
|
|
|
$manager->start($container, ['PortBindings' => $port->toSpec()]); |
|
322
|
|
|
|
|
323
|
|
|
$this->assertInternalType('integer', $container->getMappedPort(80)->getHostPort()); |
|
|
|
|
|
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
public function testInspect() |
|
327
|
|
|
{ |
|
328
|
|
|
$manager = $this->getManager(); |
|
329
|
|
|
|
|
330
|
|
|
$this->assertEquals(null, $manager->find('foo')); |
|
331
|
|
|
|
|
332
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]); |
|
|
|
|
|
|
333
|
|
|
$manager->create($container); |
|
334
|
|
|
|
|
335
|
|
|
$this->assertInstanceOf('Docker\\Container', $manager->find($container->getId())); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
public function testRemove() |
|
339
|
|
|
{ |
|
340
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['date']]); |
|
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
$manager = $this->getManager(); |
|
343
|
|
|
$manager->create($container); |
|
344
|
|
|
$manager->start($container); |
|
345
|
|
|
$manager->wait($container); |
|
346
|
|
|
$manager->remove($container); |
|
347
|
|
|
|
|
348
|
|
|
$this->setExpectedException('\\Docker\\Exception\\ContainerNotFoundException', 'Container not found'); |
|
349
|
|
|
$manager->inspect($container); |
|
|
|
|
|
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
public function testForceRemove() |
|
353
|
|
|
{ |
|
354
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['tail', '-f', '/var/log/lastlog']]); |
|
|
|
|
|
|
355
|
|
|
|
|
356
|
|
|
$manager = $this->getManager(); |
|
357
|
|
|
$manager->create($container); |
|
358
|
|
|
$manager->start($container); |
|
359
|
|
|
|
|
360
|
|
|
try { |
|
361
|
|
|
$manager->remove($container); |
|
362
|
|
|
$this->assertTrue(false); |
|
363
|
|
|
} catch (APIException $e) { |
|
|
|
|
|
|
364
|
|
|
$this->assertTrue(true); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
$manager->remove($container, false, true); |
|
|
|
|
|
|
368
|
|
|
|
|
369
|
|
|
try { |
|
370
|
|
|
$manager->inspect($container); |
|
|
|
|
|
|
371
|
|
|
$this->assertTrue(false); |
|
372
|
|
|
} catch (ContainerNotFoundException $e) { |
|
|
|
|
|
|
373
|
|
|
$this->assertTrue(true); |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
public function testRemoveContainers() |
|
378
|
|
|
{ |
|
379
|
|
|
$containers = ['3360ea744df2', 'a412d121d015']; |
|
380
|
|
|
$manager = $this |
|
381
|
|
|
->getMockBuilder('\Docker\Manager\ContainerManager') |
|
382
|
|
|
->setMethods(['remove']) |
|
383
|
|
|
->disableOriginalConstructor() |
|
384
|
|
|
->getMock(); |
|
385
|
|
|
|
|
386
|
|
|
$manager->expects($this->exactly(2)) |
|
387
|
|
|
->method('remove') |
|
388
|
|
|
->with($this->isInstanceOf('\Docker\Container'), false) |
|
389
|
|
|
->will($this->returnSelf()); |
|
390
|
|
|
|
|
391
|
|
|
$manager->removeContainers($containers); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
View Code Duplication |
public function testForceRemoveContainers() |
|
|
|
|
|
|
395
|
|
|
{ |
|
396
|
|
|
$containers = ['3360ea744df2', 'a412d121d015']; |
|
397
|
|
|
$manager = $this |
|
398
|
|
|
->getMockBuilder('\Docker\Manager\ContainerManager') |
|
399
|
|
|
->setMethods(['remove']) |
|
400
|
|
|
->disableOriginalConstructor() |
|
401
|
|
|
->getMock(); |
|
402
|
|
|
|
|
403
|
|
|
$manager->expects($this->exactly(2)) |
|
404
|
|
|
->method('remove') |
|
405
|
|
|
->with($this->isInstanceOf('\Docker\Container'), false, true) |
|
406
|
|
|
->will($this->returnSelf()); |
|
407
|
|
|
|
|
408
|
|
|
$manager->removeContainers($containers, false, true); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
public function testTop() |
|
412
|
|
|
{ |
|
413
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['sleep', '5']]); |
|
|
|
|
|
|
414
|
|
|
$manager = $this->getManager(); |
|
415
|
|
|
$manager->run($container, null, [], true); |
|
|
|
|
|
|
416
|
|
|
|
|
417
|
|
|
sleep(1); |
|
418
|
|
|
|
|
419
|
|
|
$processes = $manager->top($container); |
|
|
|
|
|
|
420
|
|
|
|
|
421
|
|
|
$this->assertCount(1, $processes); |
|
422
|
|
|
$this->assertArrayHasKey('COMMAND', $processes[0]); |
|
423
|
|
|
$this->assertEquals('sleep 5', $processes[0]['COMMAND']); |
|
424
|
|
|
|
|
425
|
|
|
$manager->kill($container); |
|
426
|
|
|
$manager->remove($container); |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
public function testChanges() |
|
430
|
|
|
{ |
|
431
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['touch', '/docker-php-test']]); |
|
|
|
|
|
|
432
|
|
|
$manager = $this->getManager(); |
|
433
|
|
|
$manager->run($container); |
|
|
|
|
|
|
434
|
|
|
$manager->wait($container); |
|
435
|
|
|
|
|
436
|
|
|
$changes = $manager->changes($container); |
|
437
|
|
|
|
|
438
|
|
|
$manager->remove($container); |
|
439
|
|
|
|
|
440
|
|
|
$this->assertCount(1, $changes); |
|
441
|
|
|
$this->assertEquals('/docker-php-test', $changes[0]['Path']); |
|
442
|
|
|
$this->assertEquals(1, $changes[0]['Kind']); |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
public function testExport() |
|
446
|
|
|
{ |
|
447
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['touch', '/docker-php-test']]); |
|
|
|
|
|
|
448
|
|
|
$manager = $this->getManager(); |
|
449
|
|
|
$manager->run($container); |
|
|
|
|
|
|
450
|
|
|
$manager->wait($container); |
|
451
|
|
|
|
|
452
|
|
|
$exportStream = $manager->export($container); |
|
453
|
|
|
|
|
454
|
|
|
$this->assertInstanceOf('\GuzzleHttp\Stream\Stream', $exportStream); |
|
455
|
|
|
|
|
456
|
|
|
$tarFileName = tempnam(sys_get_temp_dir(), 'docker-php-export-test-'); |
|
457
|
|
|
$tarFile = fopen($tarFileName, 'w+'); |
|
458
|
|
|
|
|
459
|
|
|
stream_copy_to_stream($exportStream->detach(), $tarFile); |
|
|
|
|
|
|
460
|
|
|
fclose($tarFile); |
|
461
|
|
|
|
|
462
|
|
|
exec('/usr/bin/env tar -tf '.$tarFileName, $output); |
|
463
|
|
|
|
|
464
|
|
|
$this->assertContains('docker-php-test', $output); |
|
465
|
|
|
$this->assertContains('.dockerinit', $output); |
|
466
|
|
|
|
|
467
|
|
|
unlink($tarFileName); |
|
468
|
|
|
$manager->remove($container); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
public function testCopyToDisk() |
|
472
|
|
|
{ |
|
473
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['touch', '/etc/default/docker-php-test']]); |
|
|
|
|
|
|
474
|
|
|
$manager = $this->getManager(); |
|
475
|
|
|
$manager->run($container); |
|
|
|
|
|
|
476
|
|
|
$manager->wait($container); |
|
477
|
|
|
|
|
478
|
|
|
$tarFileName = tempnam(sys_get_temp_dir(), 'testcopyToDisk.tar'); |
|
479
|
|
|
$manager->copyToDisk($container, '/etc/default', $tarFileName); |
|
|
|
|
|
|
480
|
|
|
|
|
481
|
|
|
exec('/usr/bin/env tar -tf '.$tarFileName, $output); |
|
482
|
|
|
$this->assertContains('default/docker-php-test', $output); |
|
483
|
|
|
|
|
484
|
|
|
unlink($tarFileName); |
|
485
|
|
|
$manager->remove($container); |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
public function testLogs() |
|
489
|
|
|
{ |
|
490
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['echo', 'test']]); |
|
|
|
|
|
|
491
|
|
|
$manager = $this->getManager(); |
|
492
|
|
|
$manager->run($container); |
|
|
|
|
|
|
493
|
|
|
$manager->stop($container); |
|
494
|
|
|
$logs = $manager->logs($container, false, true); |
|
|
|
|
|
|
495
|
|
|
$manager->remove($container); |
|
496
|
|
|
|
|
497
|
|
|
$this->assertGreaterThanOrEqual(1, count($logs)); |
|
498
|
|
|
|
|
499
|
|
|
$logs = array_map(function ($value) { |
|
500
|
|
|
return $value['output']; |
|
501
|
|
|
}, $logs); |
|
502
|
|
|
|
|
503
|
|
|
$this->assertContains("test", implode("", $logs)); |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
public function testRestart() |
|
507
|
|
|
{ |
|
508
|
|
|
$manager = $this->getManager(); |
|
509
|
|
|
|
|
510
|
|
|
$container = new Container(['Image' => 'busybox', 'Cmd' => ['sleep', '10']]); |
|
|
|
|
|
|
511
|
|
|
|
|
512
|
|
|
$manager->create($container); |
|
513
|
|
|
$manager->start($container); |
|
514
|
|
|
$this->assertEquals('0001-01-01T00:00:00Z', $container->getRuntimeInformations()['State']['FinishedAt']); |
|
|
|
|
|
|
515
|
|
|
|
|
516
|
|
|
$manager->restart($container); |
|
517
|
|
|
$this->assertNotEquals('0001-01-01T00:00:00Z', $container->getRuntimeInformations()['State']['FinishedAt']); |
|
|
|
|
|
|
518
|
|
|
|
|
519
|
|
|
$manager->stop($container); |
|
520
|
|
|
$manager->remove($container); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
public function testKill() |
|
524
|
|
|
{ |
|
525
|
|
|
$manager = $this->getManager(); |
|
526
|
|
|
$dockerFileBuilder = new ContextBuilder(); |
|
527
|
|
|
$dockerFileBuilder->from('ubuntu:precise'); |
|
528
|
|
|
$dockerFileBuilder->add('/kill.sh', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'script' . DIRECTORY_SEPARATOR . 'kill.sh')); |
|
529
|
|
|
$dockerFileBuilder->run('chmod +x /kill.sh'); |
|
530
|
|
|
|
|
531
|
|
|
$this->getDocker()->build($dockerFileBuilder->getContext(), 'docker-php-kill-test', null, true, false, true); |
|
532
|
|
|
|
|
533
|
|
|
$container = new Container(['Image' => 'docker-php-kill-test:latest', 'Cmd' => ['/kill.sh']]); |
|
|
|
|
|
|
534
|
|
|
$manager->create($container); |
|
535
|
|
|
$manager->start($container); |
|
536
|
|
|
$manager->kill($container, "SIGHUP"); |
|
|
|
|
|
|
537
|
|
|
$manager->wait($container); |
|
538
|
|
|
|
|
539
|
|
|
$logs = $manager->logs($container, false, true); |
|
|
|
|
|
|
540
|
|
|
$logs = array_map(function ($value) { |
|
541
|
|
|
return $value['output']; |
|
542
|
|
|
}, $logs); |
|
543
|
|
|
|
|
544
|
|
|
$manager->remove($container); |
|
545
|
|
|
$this->getDocker()->getImageManager()->remove($container->getImage()); |
|
546
|
|
|
|
|
547
|
|
|
$this->assertContains('HUP', implode("", $logs)); |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
public function testExec() |
|
551
|
|
|
{ |
|
552
|
|
|
$manager = $this->getManager(); |
|
553
|
|
|
$dockerFileBuilder = new ContextBuilder(); |
|
554
|
|
|
$dockerFileBuilder->from('ubuntu:precise'); |
|
555
|
|
|
$dockerFileBuilder->add('/daemon.sh', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'script' . DIRECTORY_SEPARATOR . 'daemon.sh')); |
|
556
|
|
|
$dockerFileBuilder->run('chmod +x /daemon.sh'); |
|
557
|
|
|
|
|
558
|
|
|
$this->getDocker()->build($dockerFileBuilder->getContext(), 'docker-php-restart-test', null, true, false, true); |
|
559
|
|
|
|
|
560
|
|
|
$container = new Container(['Image' => 'docker-php-restart-test', 'Cmd' => ['/daemon.sh']]); |
|
|
|
|
|
|
561
|
|
|
$manager->create($container); |
|
562
|
|
|
$manager->start($container); |
|
563
|
|
|
|
|
564
|
|
|
$type = 0; |
|
565
|
|
|
$output = ""; |
|
566
|
|
|
$execId = $manager->exec($container, ['/bin/bash', '-c', 'echo -n "output"']); |
|
|
|
|
|
|
567
|
|
|
|
|
568
|
|
|
$this->assertNotNull($execId); |
|
569
|
|
|
|
|
570
|
|
|
$response = $manager->execstart($execId, function ($log, $stdtype) use (&$type, &$output) { |
|
|
|
|
|
|
571
|
|
|
$type = $stdtype; |
|
572
|
|
|
$output = $log; |
|
573
|
|
|
}); |
|
574
|
|
|
|
|
575
|
|
|
$response->getBody()->getContents(); |
|
576
|
|
|
|
|
577
|
|
|
$manager->kill($container); |
|
578
|
|
|
$manager->remove($container); |
|
579
|
|
|
|
|
580
|
|
|
$this->assertEquals(1, $type); |
|
581
|
|
|
$this->assertEquals('output', $output); |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
public function testExecInspect() |
|
585
|
|
|
{ |
|
586
|
|
|
$manager = $this->getManager(); |
|
587
|
|
|
$dockerFileBuilder = new ContextBuilder(); |
|
588
|
|
|
$dockerFileBuilder->from('ubuntu:precise'); |
|
589
|
|
|
$dockerFileBuilder->add('/daemon.sh', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'script' . DIRECTORY_SEPARATOR . 'daemon.sh')); |
|
590
|
|
|
$dockerFileBuilder->run('chmod +x /daemon.sh'); |
|
591
|
|
|
|
|
592
|
|
|
$this->getDocker()->build($dockerFileBuilder->getContext(), 'docker-php-restart-test', null, true, false, true); |
|
593
|
|
|
|
|
594
|
|
|
$container = new Container(['Image' => 'docker-php-restart-test', 'Cmd' => ['/daemon.sh']]); |
|
|
|
|
|
|
595
|
|
|
$manager->create($container); |
|
596
|
|
|
$manager->start($container); |
|
597
|
|
|
|
|
598
|
|
|
$execId = $manager->exec($container, ['/bin/bash', '-c', 'echo -n "output"']); |
|
|
|
|
|
|
599
|
|
|
$inspection = $manager->execinspect($execId); |
|
|
|
|
|
|
600
|
|
|
|
|
601
|
|
|
$this->assertEquals(0, $inspection->ExitCode); |
|
602
|
|
|
$this->assertEquals(false, $inspection->Running); |
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
|
|
public function testRename() |
|
606
|
|
|
{ |
|
607
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]); |
|
|
|
|
|
|
608
|
|
|
|
|
609
|
|
|
$manager = $this->getManager(); |
|
610
|
|
|
$manager->create($container); |
|
611
|
|
|
$manager->start($container); |
|
612
|
|
|
$manager->rename($container, 'FoobarRenamed'); |
|
|
|
|
|
|
613
|
|
|
|
|
614
|
|
|
$runtimeInformations = $container->getRuntimeInformations(); |
|
|
|
|
|
|
615
|
|
|
|
|
616
|
|
|
$this->assertInstanceOf('Docker\\Container', $manager->find('FoobarRenamed')); |
|
617
|
|
|
$manager->stop($container); // cleanup |
|
618
|
|
|
$manager->remove($container); |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
public function testPause() |
|
622
|
|
|
{ |
|
623
|
|
|
$container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '1']]); |
|
|
|
|
|
|
624
|
|
|
|
|
625
|
|
|
$manager = $this->getManager(); |
|
626
|
|
|
$manager->create($container); |
|
627
|
|
|
$manager->start($container); |
|
628
|
|
|
|
|
629
|
|
|
$runtimeInformations = $container->getRuntimeInformations(); |
|
|
|
|
|
|
630
|
|
|
$this->assertEquals(false, $runtimeInformations['State']['Paused']); |
|
631
|
|
|
$this->assertEquals(true, $runtimeInformations['State']['Running']); |
|
632
|
|
|
|
|
633
|
|
|
$manager->pause($container); |
|
634
|
|
|
|
|
635
|
|
|
$runtimeInformations = $container->getRuntimeInformations(); |
|
|
|
|
|
|
636
|
|
|
$this->assertEquals(true, $runtimeInformations['State']['Paused']); |
|
637
|
|
|
$this->assertEquals(true, $runtimeInformations['State']['Running']); |
|
638
|
|
|
|
|
639
|
|
|
$manager->unpause($container); |
|
640
|
|
|
|
|
641
|
|
|
$runtimeInformations = $container->getRuntimeInformations(); |
|
|
|
|
|
|
642
|
|
|
$this->assertEquals(false, $runtimeInformations['State']['Paused']); |
|
643
|
|
|
$this->assertEquals(true, $runtimeInformations['State']['Running']); |
|
644
|
|
|
|
|
645
|
|
|
// cleanup |
|
646
|
|
|
$manager->stop($container); |
|
647
|
|
|
$manager->remove($container); |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
} |
|
651
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.