|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Docker\Tests\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Docker\API\Model\ContainerConfig; |
|
6
|
|
|
use Docker\Manager\ContainerManager; |
|
7
|
|
|
use Docker\Tests\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class ContainerManagerTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Return the container manager |
|
13
|
|
|
* |
|
14
|
|
|
* @return ContainerManager |
|
15
|
|
|
*/ |
|
16
|
|
|
private function getManager() |
|
17
|
|
|
{ |
|
18
|
|
|
return self::getDocker()->getContainerManager(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Be sure to have image before doing test |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function setUpBeforeClass() |
|
25
|
|
|
{ |
|
26
|
|
|
self::getDocker()->getImageManager()->create([ |
|
27
|
|
|
'fromImage' => 'busybox:latest' |
|
28
|
|
|
]); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testAttach() |
|
32
|
|
|
{ |
|
33
|
|
|
$containerConfig = new ContainerConfig(); |
|
34
|
|
|
$containerConfig->setImage('busybox:latest'); |
|
35
|
|
|
$containerConfig->setCmd(['echo', '-n', 'output']); |
|
36
|
|
|
$containerConfig->setAttachStdout(true); |
|
37
|
|
|
$containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true'])); |
|
38
|
|
|
|
|
39
|
|
|
$containerCreateResult = $this->getManager()->create($containerConfig); |
|
40
|
|
|
$dockerRawStream = $this->getManager()->attach($containerCreateResult->getId(), [ |
|
|
|
|
|
|
41
|
|
|
'stream' => true, |
|
42
|
|
|
'stdout' => true, |
|
43
|
|
|
], ContainerManager::FETCH_STREAM); |
|
44
|
|
|
|
|
45
|
|
|
$stdoutFull = ""; |
|
46
|
|
|
$dockerRawStream->onStdout(function ($stdout) use (&$stdoutFull) { |
|
|
|
|
|
|
47
|
|
|
$stdoutFull .= $stdout; |
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
|
|
$this->getManager()->start($containerCreateResult->getId()); |
|
|
|
|
|
|
51
|
|
|
$this->getManager()->wait($containerCreateResult->getId()); |
|
52
|
|
|
|
|
53
|
|
|
$dockerRawStream->wait(); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals("output", $stdoutFull); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testAttachWebsocket() |
|
59
|
|
|
{ |
|
60
|
|
|
$containerConfig = new ContainerConfig(); |
|
61
|
|
|
$containerConfig->setImage('ubuntu:precise'); |
|
62
|
|
|
$containerConfig->setCmd(['sh']); |
|
63
|
|
|
$containerConfig->setAttachStdout(true); |
|
64
|
|
|
$containerConfig->setAttachStderr(true); |
|
65
|
|
|
$containerConfig->setAttachStdin(false); |
|
66
|
|
|
$containerConfig->setOpenStdin(true); |
|
67
|
|
|
$containerConfig->setTty(true); |
|
68
|
|
|
$containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true'])); |
|
69
|
|
|
|
|
70
|
|
|
$containerCreateResult = $this->getManager()->create($containerConfig); |
|
71
|
|
|
$webSocketStream = $this->getManager()->attachWebsocket($containerCreateResult->getId(), [ |
|
|
|
|
|
|
72
|
|
|
'stream' => true, |
|
73
|
|
|
'stdout' => true, |
|
74
|
|
|
'stderr' => true, |
|
75
|
|
|
'stdin' => true, |
|
76
|
|
|
], ContainerManager::FETCH_STREAM); |
|
77
|
|
|
|
|
78
|
|
|
$this->getManager()->start($containerCreateResult->getId()); |
|
79
|
|
|
|
|
80
|
|
|
// Read the bash first line |
|
81
|
|
|
$webSocketStream->read(); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
// No output after that so it should be false |
|
84
|
|
|
$this->assertFalse($webSocketStream->read()); |
|
85
|
|
|
|
|
86
|
|
|
// Write something to the container |
|
87
|
|
|
$webSocketStream->write("echo test\n"); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
// Test for echo present (stdin) |
|
90
|
|
|
$output = ""; |
|
91
|
|
|
|
|
92
|
|
|
while (($data = $webSocketStream->read()) != false) { |
|
93
|
|
|
$output .= $data; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->assertContains("echo", $output); |
|
97
|
|
|
|
|
98
|
|
|
// Exit the container |
|
99
|
|
|
$webSocketStream->write("exit\n"); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
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: