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