forked from docker-php/docker-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerResourceTest.php
More file actions
123 lines (97 loc) · 3.9 KB
/
Copy pathContainerResourceTest.php
File metadata and controls
123 lines (97 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace Docker\Tests\Resource;
use Docker\API\Model\ContainersCreatePostBody;
use Docker\Docker;
use Docker\Stream\DockerRawStream;
use Docker\Tests\TestCase;
class ContainerResourceTest extends TestCase
{
/**
* Return the container manager.
*/
private function getManager()
{
return self::getDocker();
}
/**
* Be sure to have image before doing test.
*/
public static function setUpBeforeClass(): void
{
self::getDocker()->imageCreate('', [
'fromImage' => 'busybox:latest',
]);
}
public function testAttach(): void
{
$containerConfig = new ContainersCreatePostBody();
$containerConfig->setImage('busybox:latest');
$containerConfig->setCmd(['echo', '-n', 'output']);
$containerConfig->setAttachStdout(true);
$containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
$containerCreateResult = $this->getManager()->containerCreate($containerConfig);
$dockerRawStream = $this->getManager()->containerAttach($containerCreateResult->getId(), [
'stream' => true,
'stdout' => true,
]);
$stdoutFull = '';
$dockerRawStream->onStdout(function ($stdout) use (&$stdoutFull): void {
$stdoutFull .= $stdout;
});
$this->getManager()->containerStart($containerCreateResult->getId());
$this->getManager()->containerWait($containerCreateResult->getId());
$dockerRawStream->wait();
$this->assertSame('output', $stdoutFull);
}
public function testAttachWebsocket(): void
{
$containerConfig = new ContainersCreatePostBody();
$containerConfig->setImage('busybox:latest');
$containerConfig->setCmd(['sh']);
$containerConfig->setAttachStdout(true);
$containerConfig->setAttachStderr(true);
$containerConfig->setAttachStdin(false);
$containerConfig->setOpenStdin(true);
$containerConfig->setTty(true);
$containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
$containerCreateResult = $this->getManager()->containerCreate($containerConfig);
$webSocketStream = $this->getManager()->containerAttachWebsocket($containerCreateResult->getId(), [
'stream' => true,
'stdout' => true,
'stderr' => true,
'stdin' => true,
]);
$this->getManager()->containerStart($containerCreateResult->getId());
// Read the bash first line
$webSocketStream->read();
// No output after that so it should be false
$this->assertFalse($webSocketStream->read());
// Write something to the container
$webSocketStream->write("echo test\n");
// Test for echo present (stdin)
$output = '';
while (false !== ($data = $webSocketStream->read())) {
$output .= $data;
}
$this->assertContains('echo', $output);
// Exit the container
$webSocketStream->write("exit\n");
}
public function testLogs(): void
{
$containerConfig = new ContainersCreatePostBody();
$containerConfig->setImage('busybox:latest');
$containerConfig->setCmd(['echo', '-n', 'output']);
$containerConfig->setAttachStdout(true);
$containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
$containerCreateResult = $this->getManager()->containerCreate($containerConfig);
$this->getManager()->containerStart($containerCreateResult->getId());
$this->getManager()->containerWait($containerCreateResult->getId());
$logsStream = $this->getManager()->containerLogs($containerCreateResult->getId(), [
'stdout' => true,
'stderr' => true,
], Docker::FETCH_OBJECT);
self::assertInstanceOf(DockerRawStream::class, $logsStream);
}
}