Repository navigation
Expand file tree
/
Copy pathDockerLogsTest.php
More file actions
73 lines (63 loc) · 3.04 KB
/
Copy pathDockerLogsTest.php
File metadata and controls
73 lines (63 loc) · 3.04 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
<?php
declare(strict_types=1);
namespace Docker\Tests;
use Docker\API\Endpoint\ContainerLogs;
use Docker\Docker;
use Docker\Stream\DockerRawStream;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class DockerLogsTest extends TestCase
{
public static function logsProvider(): iterable
{
$framed = pack('CxxxN', 1, 6)."hello\n";
yield 'modern non-TTY' => [DockerRawStream::MULTIPLEXED_HEADER, $framed, false, ['/containers/test/logs']];
yield 'modern TTY' => [DockerRawStream::HEADER, "hello\n", true, ['/containers/test/logs', '/containers/test/json']];
yield 'legacy non-TTY' => [DockerRawStream::HEADER, $framed, false, ['/containers/test/logs', '/containers/test/json']];
}
/** @dataProvider logsProvider */
public function testContainerLogs(string $contentType, string $body, bool $tty, array $expectedPaths): void
{
$paths = [];
$httpClient = $this->createMock(ClientInterface::class);
$httpClient->method('sendRequest')->willReturnCallback(static function (RequestInterface $request) use (&$paths, $contentType, $body, $tty): ResponseInterface {
$path = $request->getUri()->getPath();
if ('/info' === $path) {
return new Response(200, ['Content-Type' => 'application/json'], '{}');
}
$paths[] = $path;
if ('/containers/test/logs' === $path) {
return new Response(200, ['Content-Type' => $contentType], $body);
}
if ('/containers/test/json' === $path) {
return new Response(200, ['Content-Type' => 'application/json'], json_encode(['Config' => ['Tty' => $tty]], \JSON_THROW_ON_ERROR));
}
throw new \LogicException('Unexpected request: '.$path);
});
$docker = Docker::create($httpClient, [], [], false);
$stream = $docker->containerLogs('test', ['stdout' => true]);
self::assertInstanceOf(DockerRawStream::class, $stream);
$output = '';
$stream->onStdout(static function (string $chunk) use (&$output): void {
$output .= $chunk;
});
$stream->wait();
self::assertSame("hello\n", $output);
self::assertSame($expectedPaths, $paths);
}
public function testRawEndpointStillReturnsUnmodifiedResponse(): void
{
$body = pack('CxxxN', 1, 6)."hello\n";
$httpClient = $this->createMock(ClientInterface::class);
$httpClient->expects(self::exactly(2))->method('sendRequest')->willReturnOnConsecutiveCalls(
new Response(200, ['Content-Type' => 'application/json'], '{}'),
new Response(200, ['Content-Type' => DockerRawStream::MULTIPLEXED_HEADER], $body)
);
$docker = Docker::create($httpClient, [], [], false);
$response = $docker->executeRawEndpoint(new ContainerLogs('test', ['stdout' => true]));
self::assertSame($body, $response->getBody()->getContents());
}
}