Repository navigation
Expand file tree
/
Copy pathUnexpectedStatusTest.php
More file actions
154 lines (122 loc) · 6.03 KB
/
Copy pathUnexpectedStatusTest.php
File metadata and controls
154 lines (122 loc) · 6.03 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
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
namespace Docker\Tests;
use Docker\API\Exception\ClientException;
use Docker\API\Exception\ContainerInspectNotFoundException;
use Docker\API\Model\ContainersIdUpdatePostBody;
use Docker\Docker;
use Docker\Exception\UnexpectedClientErrorException;
use Docker\Exception\UnexpectedServerErrorException;
use Docker\Stream\DockerRawStream;
use Nyholm\Psr7\Response;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class UnexpectedStatusTest extends TestCase
{
private const JSON = ['Content-Type' => 'application/json'];
private array $deprecations = [];
protected function setUp(): void
{
set_error_handler(function (int $level, string $message): bool {
$this->deprecations[] = $message;
return true;
}, \E_USER_DEPRECATED);
}
protected function tearDown(): void
{
restore_error_handler();
}
public function testUnhandledErrorThrowsByDefault(): void
{
$docker = $this->docker(new Response(400, self::JSON, '{"message":"Minimum memory limit allowed is 6MB"}'));
$this->expectException(UnexpectedClientErrorException::class);
$this->expectExceptionMessage('Docker returned HTTP 400 for POST /containers/web/update: Minimum memory limit allowed is 6MB');
$docker->containerUpdate('web', new ContainersIdUpdatePostBody());
}
public function testOptOutReturnsNull(): void
{
$docker = $this->docker(new Response(400, self::JSON, '{"message":"Minimum memory limit allowed is 6MB"}'))->throwOnUnexpectedStatus(false);
$this->assertNull($docker->containerUpdate('web', new ContainersIdUpdatePostBody()));
$this->assertSame([], $this->deprecations);
}
public function testClientErrorDetails(): void
{
$docker = $this->docker(new Response(409, self::JSON, '{"message":"network with name web already exists"}'))->throwOnUnexpectedStatus();
try {
$docker->containerUpdate('web', new ContainersIdUpdatePostBody());
$this->fail('An unhandled 409 should throw.');
} catch (UnexpectedClientErrorException $error) {
$this->assertInstanceOf(ClientException::class, $error);
$this->assertSame(409, $error->getStatusCode());
$this->assertSame(409, $error->getCode());
$this->assertSame('POST', $error->getMethod());
$this->assertSame('/containers/web/update', $error->getPath());
$this->assertSame('network with name web already exists', $error->getErrorMessage());
$this->assertSame('{"message":"network with name web already exists"}', (string) $error->getResponse()->getBody());
}
$this->assertSame([], $this->deprecations);
}
public function testServerErrorWithPlainTextBody(): void
{
$docker = $this->docker(new Response(500, ['Content-Type' => 'text/plain'], "invalid reference format\n"))->throwOnUnexpectedStatus();
$this->expectException(UnexpectedServerErrorException::class);
$this->expectExceptionMessage('Docker returned HTTP 500 for POST /images/create: invalid reference format');
$docker->imageCreate(null, ['fromImage' => 'Bad Name']);
}
public function testDocumentedErrorsKeepGeneratedExceptions(): void
{
$docker = $this->docker(new Response(404, self::JSON, '{"message":"No such container: web"}'));
try {
$docker->containerInspect('web');
$this->fail('A documented 404 should throw the generated exception.');
} catch (ContainerInspectNotFoundException) {
}
$this->assertSame([], $this->deprecations);
}
public function testSuccessWithoutBodyStillReturnsNull(): void
{
$docker = $this->docker(new Response(304))->throwOnUnexpectedStatus();
$this->assertNull($docker->containerStart('web'));
$this->assertSame([], $this->deprecations);
}
public function testRawResponsesAreNotChecked(): void
{
$docker = $this->docker(new Response(400, self::JSON, '{"message":"bad"}'))->throwOnUnexpectedStatus();
$response = $docker->containerUpdate('web', new ContainersIdUpdatePostBody(), Docker::FETCH_RESPONSE);
$this->assertSame(400, $response->getStatusCode());
}
public function testSuccessfulStreamsAreNotBuffered(): void
{
$response = new Response(200, ['Content-Type' => DockerRawStream::MULTIPLEXED_HEADER], pack('CxxxN', 1, 5).'hello');
$docker = $this->docker($response)->throwOnUnexpectedStatus();
$stream = $docker->containerLogs('web', ['stdout' => true]);
$this->assertInstanceOf(DockerRawStream::class, $stream);
$this->assertSame(0, $response->getBody()->tell(), 'Successful responses must stay unread');
}
public function testUnlimitedPidsLimitDecodes(): void
{
// Docker reports a container without a PID limit as the largest uint64.
$docker = $this->docker(new Response(200, self::JSON, '{"read":"t1","pids_stats":{"current":3,"limit":18446744073709551615}}'));
$stats = $docker->containerStats('web', ['stream' => false]);
if ($stats instanceof \stdClass) {
// Before API 1.48 the sample is decoded without generated setters.
$this->assertSame(18446744073709551615.0, $stats->pids_stats->limit);
} else {
$this->assertSame(\PHP_INT_MAX, $stats->getPidsStats()->getLimit());
}
}
private function docker(ResponseInterface ...$responses): Docker
{
$httpClient = new class([new Response(200, self::JSON, '{}'), ...$responses]) implements ClientInterface {
public function __construct(private array $responses)
{
}
public function sendRequest(RequestInterface $request): ResponseInterface
{
return array_shift($this->responses) ?? throw new \LogicException('Unexpected request '.$request->getUri());
}
};
return Docker::create($httpClient, [], [], false);
}
}