Repository navigation
Expand file tree
/
Copy pathDockerClientFactoryTest.php
More file actions
243 lines (194 loc) · 9.29 KB
/
Copy pathDockerClientFactoryTest.php
File metadata and controls
243 lines (194 loc) · 9.29 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
declare(strict_types=1);
namespace Docker\Tests;
use Docker\DockerClientFactory;
use Docker\Http\ApiVersionNegotiationPlugin;
use Http\Client\Common\Plugin\AddPathPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\Common\PluginClientFactory;
use Http\Client\Socket\Client;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Http\Client\ClientInterface;
class DockerClientFactoryTest extends TestCase
{
private array $environment = [];
protected function setUp(): void
{
parent::setUp();
foreach (['DOCKER_HOST', 'DOCKER_API_VERSION', 'DOCKER_TLS_VERIFY', 'DOCKER_CERT_PATH', 'DOCKER_PEER_NAME'] as $name) {
$this->environment[$name] = getenv($name);
putenv($name);
}
}
protected function tearDown(): void
{
parent::tearDown();
PluginClientFactory::setFactory(static function ($client, array $plugins, array $options): PluginClient {
unset($options['client_name']);
return new PluginClient($client, $plugins, $options);
});
foreach ($this->environment as $name => $value) {
putenv(false === $value ? $name : $name.'='.$value);
}
}
public function testStaticConstructor(): void
{
$this->assertInstanceOf(ClientInterface::class, DockerClientFactory::create());
}
public static function socketAddresses(): array
{
return [
'Unix socket' => ['unix:///var/run/docker.sock', 'unix:///var/run/docker.sock', 'http://localhost', null],
'TCP socket' => ['tcp://docker.test:2375', 'tcp://docker.test:2375', 'http://docker.test:2375', null],
'HTTP URL' => ['http://docker.test:2375', 'tcp://docker.test:2375', 'http://docker.test:2375', null],
'HTTPS URL' => ['https://docker.test:2376', 'tcp://docker.test:2376', 'https://docker.test:2376', true],
'Default HTTP port' => ['http://docker.test', 'tcp://docker.test:80', 'http://docker.test', null],
'Default HTTPS port' => ['https://docker.test', 'tcp://docker.test:443', 'https://docker.test', true],
'Explicit default HTTPS port' => ['https://docker.test:443', 'tcp://docker.test:443', 'https://docker.test', true],
'IPv6 HTTP' => ['http://[::1]:2375', 'tcp://[::1]:2375', 'http://[::1]:2375', null],
'IPv6 HTTPS' => ['https://[::1]', 'tcp://[::1]:443', 'https://[::1]', true],
'Uppercase HTTPS scheme' => ['HTTPS://docker.test:2376', 'tcp://docker.test:2376', 'https://docker.test:2376', true],
];
}
#[DataProvider('socketAddresses')]
public function testSocketAddressAndRequestUri(string $address, string $socket, string $host, ?bool $ssl): void
{
$config = [];
$uri = null;
PluginClientFactory::setFactory(static function (Client $client, array $plugins, array $options) use (&$config, &$uri): PluginClient {
$config = (new \ReflectionProperty($client, 'config'))->getValue($client);
foreach ($plugins as $plugin) {
if ($plugin instanceof \Http\Client\Common\Plugin\AddHostPlugin) {
$uri = (new \ReflectionProperty($plugin, 'host'))->getValue($plugin);
}
}
unset($options['client_name']);
return new PluginClient($client, $plugins, $options);
});
DockerClientFactory::create(['remote_socket' => $address]);
$this->assertSame($socket, $config['remote_socket']);
$this->assertSame($ssl, $config['ssl']);
$this->assertSame($host, (string) $uri);
}
public function testNegotiatesWithoutAnExplicitVersion(): void
{
$plugins = $this->createdPlugins();
$this->assertCount(1, array_filter($plugins, static fn (object $plugin): bool => $plugin instanceof ApiVersionNegotiationPlugin));
$this->assertCount(0, array_filter($plugins, static fn (object $plugin): bool => $plugin instanceof AddPathPlugin));
}
public function testDefaultApiVersionMatchesGeneratedClient(): void
{
$specification = (new \ReflectionClass(\Docker\API\Client::class))->getFileName();
$this->assertMatchesRegularExpression('#^1\.[0-9]+$#', DockerClientFactory::defaultApiVersion());
$this->assertStringContainsString("createUri('/v".DockerClientFactory::defaultApiVersion()."')", file_get_contents($specification));
}
public function testApiVersionFromEnvironment(): void
{
putenv('DOCKER_API_VERSION=v1.52');
$this->assertSame('/v1.52', $this->getApiPath());
}
public function testApiVersionWithLeadingSlash(): void
{
putenv('DOCKER_API_VERSION=/v1.52');
$this->assertSame('/v1.52', $this->getApiPath());
}
public function testApiVersionWithoutVersionPrefix(): void
{
putenv('DOCKER_API_VERSION=1.52');
$this->assertSame('/v1.52', $this->getApiPath());
}
public static function explicitApiVersions(): array
{
return [['1.45'], ['v1.45'], ['/v1.45'], ['/1.45']];
}
#[DataProvider('explicitApiVersions')]
public function testExplicitApiVersionOverridesEnvironment(string $version): void
{
putenv('DOCKER_API_VERSION=1.52');
$this->assertSame('/v1.45', $this->getApiPath(['api_version' => $version]));
$this->assertSame('1.52', getenv('DOCKER_API_VERSION'));
}
public static function invalidApiVersions(): array
{
return [[''], [null], [1.45], [145], [false], [[]], ['1'], ['1.45/containers'], ['1.45?x=1'], [' 1.45'], ["1.45\n"]];
}
#[DataProvider('invalidApiVersions')]
public function testInvalidExplicitApiVersionIsRejected($version): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('api_version must be a Docker API version');
DockerClientFactory::create(['api_version' => $version]);
}
public function testApiVersionIsNotPassedToSocketClient(): void
{
$config = [];
PluginClientFactory::setFactory(static function (Client $client, array $plugins, array $options) use (&$config): PluginClient {
$config = (new \ReflectionProperty($client, 'config'))->getValue($client);
unset($options['client_name']);
return new PluginClient($client, $plugins, $options);
});
DockerClientFactory::create(['api_version' => '1.45', 'timeout' => 30000]);
$this->assertArrayNotHasKey('api_version', $config);
$this->assertSame(30000, $config['timeout']);
}
public function testCreateFromEnvWithoutCertPath(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Connection to docker has been set to use TLS, but no PATH is defined for certificate in DOCKER_CERT_PATH docker environment variable');
putenv('DOCKER_TLS_VERIFY=1');
DockerClientFactory::createFromEnv();
}
public function testCreateCustomCa(): void
{
putenv('DOCKER_TLS_VERIFY=1');
putenv('DOCKER_CERT_PATH=/tmp');
$count = \count(get_resources('stream-context'));
$client = DockerClientFactory::createFromEnv();
$this->assertInstanceOf(ClientInterface::class, $client);
$contexts = get_resources('stream-context');
$this->assertCount($count + 1, $contexts);
// Get the last stream context.
$context = stream_context_get_options(end($contexts));
$this->assertSame('/tmp/ca.pem', $context['ssl']['cafile']);
$this->assertSame('/tmp/cert.pem', $context['ssl']['local_cert']);
$this->assertSame('/tmp/key.pem', $context['ssl']['local_pk']);
}
public function testCreateCustomPeerName(): void
{
putenv('DOCKER_TLS_VERIFY=1');
putenv('DOCKER_CERT_PATH=/abc');
putenv('DOCKER_PEER_NAME=test');
$count = \count(get_resources('stream-context'));
$client = DockerClientFactory::createFromEnv();
$this->assertInstanceOf(ClientInterface::class, $client);
$contexts = get_resources('stream-context');
$this->assertCount($count + 1, $contexts);
// Get the last stream context.
$context = stream_context_get_options(end($contexts));
$this->assertSame('/abc/ca.pem', $context['ssl']['cafile']);
$this->assertSame('/abc/cert.pem', $context['ssl']['local_cert']);
$this->assertSame('/abc/key.pem', $context['ssl']['local_pk']);
$this->assertSame('test', $context['ssl']['peer_name']);
}
private function createdPlugins(array $config = []): array
{
$plugins = [];
PluginClientFactory::setFactory(static function ($client, array $createdPlugins, array $options) use (&$plugins): PluginClient {
$plugins = $createdPlugins;
unset($options['client_name']);
return new PluginClient($client, $createdPlugins, $options);
});
DockerClientFactory::create($config);
return $plugins;
}
private function getApiPath(array $config = []): string
{
foreach ($this->createdPlugins($config) as $plugin) {
if ($plugin instanceof AddPathPlugin) {
$uri = (new \ReflectionProperty($plugin, 'uri'))->getValue($plugin);
return $uri->getPath();
}
}
$this->fail('The Docker API path plugin was not configured.');
}
}