Repository navigation
Expand file tree
/
Copy pathExecResourceTest.php
More file actions
143 lines (116 loc) · 5.16 KB
/
Copy pathExecResourceTest.php
File metadata and controls
143 lines (116 loc) · 5.16 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace Docker\Tests\Resource;
use Docker\API\Model\ContainersCreatePostBody;
use Docker\API\Model\ContainersIdExecPostBody;
use Docker\API\Model\ExecIdJsonGetResponse200;
use Docker\API\Model\ExecIdStartPostBody;
use Docker\Docker;
use Docker\Stream\DockerRawStream;
use Docker\Tests\TestCase;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class ExecResourceTest extends TestCase
{
/**
* Return the container manager.
*/
private function getManager()
{
return self::getDocker();
}
public static function streamProvider(): iterable
{
foreach ([false, true] as $upgrade) {
foreach ([false, true] as $tty) {
foreach ([false, true] as $empty) {
yield sprintf('upgrade=%d tty=%d empty=%d', $upgrade, $tty, $empty) => [$upgrade, $tty, $empty];
}
}
}
}
/** @dataProvider streamProvider */
public function testStartStream(bool $upgrade, bool $tty, bool $empty): void
{
$createContainerResult = $this->createContainer();
try {
$execConfig = new ContainersIdExecPostBody();
$execConfig->setAttachStdout(true);
$execConfig->setAttachStderr(true);
$execConfig->setTty($tty);
$execConfig->setCmd($empty ? ['true'] : ['sh', '-c', 'printf stdout; printf stderr >&2']);
$execCreateResult = $this->getManager()->containerExec($createContainerResult->getId(), $execConfig);
$execStartConfig = new ExecIdStartPostBody();
$execStartConfig->setDetach(false);
$execStartConfig->setTty($tty);
$capture = new class($upgrade) implements Plugin {
public ?int $status = null;
public function __construct(private bool $upgrade)
{
}
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
if (!preg_match('#/exec/[^/]+/start$#', $request->getUri()->getPath())) {
return $next($request);
}
if ($this->upgrade) {
$request = $request->withHeader('Connection', 'Upgrade')->withHeader('Upgrade', 'tcp');
}
return $next($request)->then(function (ResponseInterface $response): ResponseInterface {
$this->status = $response->getStatusCode();
return $response;
});
}
};
$docker = Docker::create(null, [$capture]);
$stream = $docker->execStart($execCreateResult->getId(), $execStartConfig);
$this->assertSame($upgrade ? 101 : 200, $capture->status);
$this->assertInstanceOf(DockerRawStream::class, $stream);
$stdoutFull = '';
$stderrFull = '';
$stream->onStdout(function ($stdout) use (&$stdoutFull): void {
$stdoutFull .= $stdout;
});
$stream->onStderr(function ($stderr) use (&$stderrFull): void {
$stderrFull .= $stderr;
});
$stream->wait();
$this->assertSame($empty ? '' : ($tty ? 'stdoutstderr' : 'stdout'), $stdoutFull);
$this->assertSame($empty || $tty ? '' : 'stderr', $stderrFull);
$execInfo = $this->getManager()->execInspect($execCreateResult->getId());
$this->assertFalse($execInfo->getRunning());
$this->assertSame(0, $execInfo->getExitCode());
} finally {
self::getDocker()->containerDelete($createContainerResult->getId(), ['force' => true]);
}
}
public function testExecFind(): void
{
$createContainerResult = $this->createContainer();
$execConfig = new ContainersIdExecPostBody();
$execConfig->setCmd(['/bin/true']);
$execCreateResult = $this->getManager()->containerExec($createContainerResult->getId(), $execConfig);
$execStartConfig = new ExecIdStartPostBody();
$execStartConfig->setDetach(false);
$execStartConfig->setTty(false);
$this->getManager()->execStart($execCreateResult->getId(), $execStartConfig);
$execFindResult = $this->getManager()->execInspect($execCreateResult->getId());
$this->assertInstanceOf(ExecIdJsonGetResponse200::class, $execFindResult);
self::getDocker()->containerKill($createContainerResult->getId(), [
'signal' => 'SIGKILL',
]);
}
private function createContainer()
{
$containerConfig = new ContainersCreatePostBody();
$containerConfig->setImage('busybox:latest');
$containerConfig->setCmd(['sh']);
$containerConfig->setOpenStdin(true);
$containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
$containerCreateResult = self::getDocker()->containerCreate($containerConfig);
self::getDocker()->containerStart($containerCreateResult->getId());
return $containerCreateResult;
}
}