11package com .github .dockerjava .core .command ;
22
33import com .github .dockerjava .api .exception .DockerException ;
4- import com .github .dockerjava .api .exception .NotAcceptableException ;
54import com .github .dockerjava .api .model .ContainerSpec ;
5+ import com .github .dockerjava .api .model .Network ;
6+ import com .github .dockerjava .api .model .NetworkAttachmentConfig ;
67import com .github .dockerjava .api .model .Service ;
8+ import com .github .dockerjava .api .model .ServiceModeConfig ;
9+ import com .github .dockerjava .api .model .ServiceReplicatedModeOptions ;
710import com .github .dockerjava .api .model .ServiceSpec ;
8- import com .github .dockerjava .api .model .Swarm ;
9- import com .github .dockerjava .api .model .SwarmCAConfig ;
10- import com .github .dockerjava .api .model .SwarmDispatcherConfig ;
11- import com .github .dockerjava .api .model .SwarmOrchestration ;
12- import com .github .dockerjava .api .model .SwarmRaftConfig ;
1311import com .github .dockerjava .api .model .SwarmSpec ;
14- import com .github .dockerjava .api .model .TaskDefaults ;
1512import com .github .dockerjava .api .model .TaskSpec ;
1613import org .slf4j .Logger ;
1714import org .slf4j .LoggerFactory ;
2724import java .util .List ;
2825
2926import static org .hamcrest .MatcherAssert .assertThat ;
30- import static org .hamcrest .Matchers .equalTo ;
3127import static org .hamcrest .Matchers .hasSize ;
3228import static org .hamcrest .Matchers .is ;
3329
@@ -58,8 +54,11 @@ public void afterMethod(ITestResult result) {
5854 }
5955
6056 @ Test
61- public void initializeSwarm () throws DockerException {
62- dockerClient .initializeSwarmCmd (new SwarmSpec ()).withListenAddr ("127.0.0.1" ).exec ();
57+ public void testCreateService () throws DockerException {
58+ dockerClient .initializeSwarmCmd (new SwarmSpec ())
59+ .withListenAddr ("127.0.0.1" )
60+ .withAdvertiseAddr ("127.0.0.1" )
61+ .exec ();
6362
6463 dockerClient .createServiceCmd (new ServiceSpec ()
6564 .withName (SERVICE_NAME )
@@ -77,4 +76,46 @@ public void initializeSwarm() throws DockerException {
7776 dockerClient .removeServiceCmd (SERVICE_NAME ).exec ();
7877 }
7978
79+ @ Test
80+ public void testCreateServiceWithNetworks () {
81+ dockerClient .initializeSwarmCmd (new SwarmSpec ())
82+ .withListenAddr ("127.0.0.1" )
83+ .withAdvertiseAddr ("127.0.0.1" )
84+ .exec ();
85+
86+ String networkId = dockerClient .createNetworkCmd ().withName ("networkname" )
87+ .withDriver ("overlay" )
88+ .withIpam (new Network .Ipam ()
89+ .withDriver ("default" ))
90+ .exec ().getId ();
91+
92+ ServiceSpec spec = new ServiceSpec ()
93+ .withName (SERVICE_NAME )
94+ .withTaskTemplate (new TaskSpec ()
95+ .withContainerSpec (new ContainerSpec ()
96+ .withImage ("busybox" ))
97+ )
98+ .withNetworks (Lists .newArrayList (
99+ new NetworkAttachmentConfig ()
100+ .withTarget (networkId )
101+ .withAliases (Lists .<String >newArrayList ("alias1" , "alias2" ))
102+ ))
103+ .withMode (new ServiceModeConfig ().withReplicated (
104+ new ServiceReplicatedModeOptions ()
105+ .withReplicas (1 )
106+ ));
107+
108+ dockerClient .createServiceCmd (spec ).exec ();
109+
110+ List <Service > services = dockerClient .listServicesCmd ()
111+ .withNameFilter (Lists .newArrayList (SERVICE_NAME ))
112+ .exec ();
113+
114+ assertThat (services , hasSize (1 ));
115+
116+ assertThat (services .get (0 ).getSpec (), is (spec ));
117+
118+ dockerClient .removeServiceCmd (SERVICE_NAME ).exec ();
119+ }
120+
80121}
0 commit comments