11package com .github .dockerjava .cmd .swarm ;
22
3+ import com .github .dockerjava .api .exception .ConflictException ;
34import com .github .dockerjava .api .exception .DockerException ;
5+ import com .github .dockerjava .api .model .AuthConfig ;
46import com .github .dockerjava .api .model .ContainerSpec ;
57import com .github .dockerjava .api .model .EndpointResolutionMode ;
68import com .github .dockerjava .api .model .EndpointSpec ;
1618import com .github .dockerjava .api .model .SwarmSpec ;
1719import com .github .dockerjava .api .model .TaskSpec ;
1820import com .github .dockerjava .api .model .TmpfsOptions ;
21+ import com .github .dockerjava .junit .PrivateRegistryRule ;
1922import com .google .common .collect .ImmutableMap ;
2023import com .google .common .collect .Lists ;
24+ import org .junit .Before ;
25+ import org .junit .ClassRule ;
26+ import org .junit .Rule ;
2127import org .junit .Test ;
28+ import org .junit .rules .ExpectedException ;
2229import org .slf4j .Logger ;
2330import org .slf4j .LoggerFactory ;
2431
@@ -35,6 +42,18 @@ public class CreateServiceCmdExecIT extends SwarmCmdIT {
3542 public static final Logger LOG = LoggerFactory .getLogger (CreateServiceCmdExecIT .class );
3643 private static final String SERVICE_NAME = "theservice" ;
3744
45+ @ ClassRule
46+ public static PrivateRegistryRule REGISTRY = new PrivateRegistryRule ();
47+
48+ @ Rule
49+ public ExpectedException exception = ExpectedException .none ();
50+ private AuthConfig authConfig ;
51+
52+ @ Before
53+ public void beforeTest () throws Exception {
54+ authConfig = REGISTRY .getAuthConfig ();
55+ }
56+
3857 @ Test
3958 public void testCreateService () throws DockerException {
4059 dockerRule .getClient ().initializeSwarmCmd (new SwarmSpec ())
@@ -132,4 +151,52 @@ public void testCreateServiceWithTmpfs() {
132151 assertThat (mounts .get (0 ), is (tmpMount ));
133152 dockerRule .getClient ().removeServiceCmd (SERVICE_NAME ).exec ();
134153 }
154+
155+ @ Test
156+ public void testCreateServiceWithValidAuth () throws DockerException {
157+ dockerRule .getClient ().initializeSwarmCmd (new SwarmSpec ())
158+ .withListenAddr ("127.0.0.1" )
159+ .withAdvertiseAddr ("127.0.0.1" )
160+ .exec ();
161+
162+ dockerRule .getClient ().createServiceCmd (new ServiceSpec ()
163+ .withName (SERVICE_NAME )
164+ .withTaskTemplate (new TaskSpec ()
165+ .withContainerSpec (new ContainerSpec ()
166+ .withImage (DEFAULT_IMAGE ))))
167+ .withAuthConfig (authConfig )
168+ .exec ();
169+
170+ List <Service > services = dockerRule .getClient ().listServicesCmd ()
171+ .withNameFilter (Lists .newArrayList (SERVICE_NAME ))
172+ .exec ();
173+
174+ assertThat (services , hasSize (1 ));
175+
176+ dockerRule .getClient ().removeServiceCmd (SERVICE_NAME ).exec ();
177+ }
178+
179+ @ Test
180+ public void testCreateServiceWithInvalidAuth () throws DockerException {
181+ dockerRule .getClient ().initializeSwarmCmd (new SwarmSpec ())
182+ .withListenAddr ("127.0.0.1" )
183+ .withAdvertiseAddr ("127.0.0.1" )
184+ .exec ();
185+
186+ AuthConfig invalidAuthConfig = new AuthConfig ()
187+ .withUsername ("testuser" )
188+ .withPassword ("testwrongpassword" )
189+ .withEmail ("foo@bar.de" )
190+ .withRegistryAddress (authConfig .getRegistryAddress ());
191+
192+ exception .expect (ConflictException .class );
193+
194+ dockerRule .getClient ().createServiceCmd (new ServiceSpec ()
195+ .withName (SERVICE_NAME )
196+ .withTaskTemplate (new TaskSpec ()
197+ .withContainerSpec (new ContainerSpec ()
198+ .withImage (DEFAULT_IMAGE ))))
199+ .withAuthConfig (invalidAuthConfig )
200+ .exec ();
201+ }
135202}
0 commit comments