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