forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateServiceCmdExecIT.java
More file actions
184 lines (154 loc) · 7.26 KB
/
Copy pathCreateServiceCmdExecIT.java
File metadata and controls
184 lines (154 loc) · 7.26 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
package com.github.dockerjava.cmd.swarm;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.exception.ConflictException;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.ContainerSpec;
import com.github.dockerjava.api.model.EndpointResolutionMode;
import com.github.dockerjava.api.model.EndpointSpec;
import com.github.dockerjava.api.model.Mount;
import com.github.dockerjava.api.model.Network;
import com.github.dockerjava.api.model.NetworkAttachmentConfig;
import com.github.dockerjava.api.model.PortConfig;
import com.github.dockerjava.api.model.PortConfigProtocol;
import com.github.dockerjava.api.model.Service;
import com.github.dockerjava.api.model.ServiceModeConfig;
import com.github.dockerjava.api.model.ServiceReplicatedModeOptions;
import com.github.dockerjava.api.model.ServiceSpec;
import com.github.dockerjava.api.model.TaskSpec;
import com.github.dockerjava.api.model.TmpfsOptions;
import com.github.dockerjava.junit.PrivateRegistryRule;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List;
import static com.github.dockerjava.core.DockerRule.DEFAULT_IMAGE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
public class CreateServiceCmdExecIT extends SwarmCmdIT {
public static final Logger LOG = LoggerFactory.getLogger(CreateServiceCmdExecIT.class);
private static final String SERVICE_NAME = "theservice";
@ClassRule
public static PrivateRegistryRule REGISTRY = new PrivateRegistryRule();
@Rule
public ExpectedException exception = ExpectedException.none();
private AuthConfig authConfig;
private DockerClient dockerClient;
@Before
public final void setUpCreateServiceCmdExecIT() throws Exception {
authConfig = REGISTRY.getAuthConfig();
dockerClient = startSwarm();
}
@Test
public void testCreateService() throws DockerException {
dockerClient.createServiceCmd(new ServiceSpec()
.withName(SERVICE_NAME)
.withTaskTemplate(new TaskSpec()
.withContainerSpec(new ContainerSpec()
.withImage(DEFAULT_IMAGE))))
.exec();
List<Service> services = dockerClient.listServicesCmd()
.withNameFilter(Lists.newArrayList(SERVICE_NAME))
.exec();
assertThat(services, hasSize(1));
dockerClient.removeServiceCmd(SERVICE_NAME).exec();
}
@Test
public void testCreateServiceWithNetworks() {
String networkId = dockerClient.createNetworkCmd().withName("networkname")
.withDriver("overlay")
.withIpam(new Network.Ipam()
.withDriver("default"))
.exec().getId();
ServiceSpec spec = new ServiceSpec()
.withName(SERVICE_NAME)
.withTaskTemplate(new TaskSpec()
.withForceUpdate(0)
.withRuntime("container")
.withContainerSpec(new ContainerSpec()
.withImage("busybox"))
)
.withNetworks(Lists.newArrayList(
new NetworkAttachmentConfig()
.withTarget(networkId)
.withAliases(Lists.<String>newArrayList("alias1", "alias2"))
))
.withLabels(ImmutableMap.of("com.docker.java.usage", "SwarmServiceIT"))
.withMode(new ServiceModeConfig().withReplicated(
new ServiceReplicatedModeOptions()
.withReplicas(1)
)).withEndpointSpec(new EndpointSpec()
.withMode(EndpointResolutionMode.VIP)
.withPorts(Lists.<PortConfig>newArrayList(new PortConfig()
.withPublishMode(PortConfig.PublishMode.host)
.withPublishedPort(22)
.withProtocol(PortConfigProtocol.TCP)
)));
dockerClient.createServiceCmd(spec).exec();
List<Service> services = dockerClient.listServicesCmd()
.withNameFilter(Lists.newArrayList(SERVICE_NAME))
.exec();
assertThat(services, hasSize(1));
assertThat(services.get(0).getSpec(), is(spec));
dockerClient.removeServiceCmd(SERVICE_NAME).exec();
}
@Test
public void testCreateServiceWithTmpfs() {
Mount tmpMount = new Mount().withTmpfsOptions(new TmpfsOptions().withSizeBytes(600L)).withTarget("/tmp/foo");
dockerClient.createServiceCmd(new ServiceSpec()
.withName(SERVICE_NAME)
.withTaskTemplate(new TaskSpec()
.withContainerSpec(new ContainerSpec().withImage(DEFAULT_IMAGE).withMounts(Collections.singletonList(tmpMount)))))
.exec();
List<Service> services = dockerClient.listServicesCmd()
.withNameFilter(Lists.newArrayList(SERVICE_NAME))
.exec();
assertThat(services, hasSize(1));
List<Mount> mounts = dockerClient.inspectServiceCmd(SERVICE_NAME).exec().getSpec().getTaskTemplate()
.getContainerSpec().getMounts();
assertThat(mounts, hasSize(1));
assertThat(mounts.get(0), is(tmpMount));
dockerClient.removeServiceCmd(SERVICE_NAME).exec();
}
@Test
public void testCreateServiceWithValidAuth() throws DockerException {
dockerClient.createServiceCmd(new ServiceSpec()
.withName(SERVICE_NAME)
.withTaskTemplate(new TaskSpec()
.withContainerSpec(new ContainerSpec()
.withImage(DEFAULT_IMAGE))))
.withAuthConfig(authConfig)
.exec();
List<Service> services = dockerClient.listServicesCmd()
.withNameFilter(Lists.newArrayList(SERVICE_NAME))
.exec();
assertThat(services, hasSize(1));
dockerClient.removeServiceCmd(SERVICE_NAME).exec();
}
@Test
@Ignore // TODO rework test (does not throw as expected atm)
public void testCreateServiceWithInvalidAuth() throws DockerException {
AuthConfig invalidAuthConfig = new AuthConfig()
.withUsername("testuser")
.withPassword("testwrongpassword")
.withEmail("foo@bar.de")
.withRegistryAddress(authConfig.getRegistryAddress());
exception.expect(ConflictException.class);
dockerClient.createServiceCmd(new ServiceSpec()
.withName(SERVICE_NAME)
.withTaskTemplate(new TaskSpec()
.withContainerSpec(new ContainerSpec()
.withImage(DEFAULT_IMAGE))))
.withAuthConfig(invalidAuthConfig)
.exec();
}
}