forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerClientConfigTest.java
More file actions
187 lines (145 loc) · 7.15 KB
/
Copy pathDockerClientConfigTest.java
File metadata and controls
187 lines (145 loc) · 7.15 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
package com.github.dockerjava.core;
import com.github.dockerjava.api.model.AuthConfig;
import org.testng.annotations.Test;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import static org.testng.Assert.assertEquals;
public class DockerClientConfigTest {
public static final DockerClientConfig EXAMPLE_CONFIG = newExampleConfig();
private static DockerClientConfig newExampleConfig() {
return new DockerClientConfig(URI.create("http://foo"), "bar", "baz", "qux", "blam", "wham", "flam",
new LocalDirectorySSLConfig("flim"));
}
@Test
public void string() throws Exception {
assertEquals(
EXAMPLE_CONFIG.toString(),
"DockerClientConfig{uri=http://foo, version='{UNKNOWN_VERSION}', username='baz', password='qux', email='blam', serverAddress='wham', dockerCfgPath='flam', sslConfig='LocalDirectorySSLConfig{dockerCertPath=flim}'}");
}
@Test
public void equals() throws Exception {
assertEquals(EXAMPLE_CONFIG, newExampleConfig());
}
@Test
public void environmentDockerHost() throws Exception {
// given docker host in env
Map<String, String> env = new HashMap<String, String>();
env.put("DOCKER_HOST", "tcp://baz:8768");
// and it looks to be SSL disabled
env.remove("DOCKER_CERT_PATH");
// when you build a config
DockerClientConfig config = buildConfig(env, new Properties());
// then the URL is that value with "http" instead of "tcp"
assertEquals(config.getUri(), URI.create("http://baz:8768"));
}
@Test
public void environmentDockerHostHttpsAutoDetectByCertPath() throws Exception {
// given docker host in env
Map<String, String> env = new HashMap<String, String>(System.getenv());
env.put("DOCKER_HOST", "tcp://bar:8768");
// and it looks to be SSL enabled
env.put("DOCKER_CERT_PATH", "any value");
// when you build a config
DockerClientConfig config = buildConfig(env, new Properties());
// then the URL is that value with "tcp" changed to "https"
assertEquals(config.getUri(), URI.create("https://bar:8768"));
}
@Test
public void environmentDockerHostHttpsAutoDetectByTlsVerify() throws Exception {
// given docker host in env
Map<String, String> env = new HashMap<String, String>(System.getenv());
env.put("DOCKER_HOST", "tcp://bar:8768");
// and it looks to be SSL enabled
env.put("DOCKER_TLS_VERIFY", "1");
// when you build a config
DockerClientConfig config = buildConfig(env, new Properties());
// then the URL is that value with "tcp" changed to "https"
assertEquals(config.getUri(), URI.create("https://bar:8768"));
}
@Test
public void environmentDockerHostWithInvalidTlsVerify() throws Exception {
// given docker host in env
Map<String, String> env = new HashMap<String, String>(System.getenv());
env.put("DOCKER_HOST", "tcp://bar:8768");
// and it looks to be SSL disabled
env.remove("DOCKER_CERT_PATH");
// and it has an invalid TLS_VERIFY value
env.put("DOCKER_TLS_VERIFY", "any value different from '1'");
// when you build a config
DockerClientConfig config = buildConfig(env, new Properties());
// then the URL is that value with "tcp" changed to "https"
assertEquals(config.getUri(), URI.create("http://bar:8768"));
}
@Test
public void environmentDockerHostWithInvalidTlsVerifyButWithCertPath() throws Exception {
// given docker host in env
Map<String, String> env = new HashMap<String, String>(System.getenv());
env.put("DOCKER_HOST", "tcp://bar:8768");
// and it looks to be SSL enabled
env.put("DOCKER_CERT_PATH", "any value");
// and it has an invalid TLS_VERIFY value
env.put("DOCKER_TLS_VERIFY", "any value different from '1'");
// when you build a config
DockerClientConfig config = buildConfig(env, new Properties());
// then the URL is that value with "tcp" changed to "https"
assertEquals(config.getUri(), URI.create("https://bar:8768"));
}
@Test
public void environment() throws Exception {
// given a default config in env properties
Map<String, String> env = new HashMap<String, String>();
env.put("DOCKER_URL", "http://foo");
env.put("DOCKER_VERSION", "bar");
env.put("DOCKER_USERNAME", "baz");
env.put("DOCKER_PASSWORD", "qux");
env.put("DOCKER_EMAIL", "blam");
env.put("DOCKER_SERVER_ADDRESS", "wham");
env.put("DOCKER_CERT_PATH", "flim");
env.put("DOCKER_CFG_PATH", "flam");
env.put("DOCKER_READ_TIMEOUT", "877");
env.put("DOCKER_LOGGING_FILTER_ENABLED", "false");
// when you build a config
DockerClientConfig config = buildConfig(env, new Properties());
// then we get the example object
assertEquals(config, EXAMPLE_CONFIG);
}
private DockerClientConfig buildConfig(Map<String, String> env, Properties systemProperties) {
return DockerClientConfig.createDefaultConfigBuilder(env, systemProperties).build();
}
@Test
public void defaults() throws Exception {
// given default cert path
Properties systemProperties = new Properties();
systemProperties.setProperty("user.name", "someUserName");
systemProperties.setProperty("user.home", "someHomeDir");
// when you build config
DockerClientConfig config = buildConfig(Collections.<String, String> emptyMap(), systemProperties);
// then the cert path is as expected
assertEquals(config.getUri(), URI.create("https://localhost:2376"));
assertEquals(config.getUsername(), "someUserName");
assertEquals(config.getServerAddress(), AuthConfig.DEFAULT_SERVER_ADDRESS);
assertEquals(config.getVersion(), RemoteApiVersion.unknown());
assertEquals(config.getDockerCfgPath(), "someHomeDir/.dockercfg");
assertEquals(((LocalDirectorySSLConfig) config.getSslConfig()).getDockerCertPath(), "someHomeDir/.docker");
}
@Test
public void systemProperties() throws Exception {
// given system properties based on the example
Properties systemProperties = new Properties();
systemProperties.setProperty("docker.io.url", "http://foo");
systemProperties.setProperty("docker.io.version", "bar");
systemProperties.setProperty("docker.io.username", "baz");
systemProperties.setProperty("docker.io.password", "qux");
systemProperties.setProperty("docker.io.email", "blam");
systemProperties.setProperty("docker.io.serverAddress", "wham");
systemProperties.setProperty("docker.io.dockerCertPath", "flim");
systemProperties.setProperty("docker.io.dockerCfgPath", "flam");
// when you build new config
DockerClientConfig config = buildConfig(Collections.<String, String> emptyMap(), systemProperties);
// then it is the same as the example
assertEquals(config, EXAMPLE_CONFIG);
}
}