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