refactored network integration tests to make use of swarm.CreateService
| ... | ... |
@@ -136,6 +136,21 @@ func ServiceWithName(name string) ServiceSpecOpt {
|
| 136 | 136 |
} |
| 137 | 137 |
} |
| 138 | 138 |
|
| 139 |
+// ServiceWithNetwork sets the network of the service |
|
| 140 |
+func ServiceWithNetwork(network string) ServiceSpecOpt {
|
|
| 141 |
+ return func(spec *swarmtypes.ServiceSpec) {
|
|
| 142 |
+ spec.TaskTemplate.Networks = append(spec.TaskTemplate.Networks, |
|
| 143 |
+ swarmtypes.NetworkAttachmentConfig{Target: network})
|
|
| 144 |
+ } |
|
| 145 |
+} |
|
| 146 |
+ |
|
| 147 |
+// ServiceWithEndpoint sets the Endpoint of the service |
|
| 148 |
+func ServiceWithEndpoint(endpoint *swarmtypes.EndpointSpec) ServiceSpecOpt {
|
|
| 149 |
+ return func(spec *swarmtypes.ServiceSpec) {
|
|
| 150 |
+ spec.EndpointSpec = endpoint |
|
| 151 |
+ } |
|
| 152 |
+} |
|
| 153 |
+ |
|
| 139 | 154 |
// GetRunningTasks gets the list of running tasks for a service |
| 140 | 155 |
func GetRunningTasks(t *testing.T, d *daemon.Daemon, serviceID string) []swarmtypes.Task {
|
| 141 | 156 |
t.Helper() |
| ... | ... |
@@ -35,16 +35,13 @@ func TestInspectNetwork(t *testing.T) {
|
| 35 | 35 |
|
| 36 | 36 |
var instances uint64 = 4 |
| 37 | 37 |
serviceName := "TestService" |
| 38 |
- // FIXME(vdemeester) consolidate with swarm.CreateService |
|
| 39 |
- serviceSpec := swarmServiceSpec(serviceName, instances) |
|
| 40 |
- serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarmtypes.NetworkAttachmentConfig{Target: overlayName})
|
|
| 41 | 38 |
|
| 42 |
- serviceResp, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{
|
|
| 43 |
- QueryRegistry: false, |
|
| 44 |
- }) |
|
| 45 |
- assert.NilError(t, err) |
|
| 39 |
+ serviceID := swarm.CreateService(t, d, |
|
| 40 |
+ swarm.ServiceWithReplicas(instances), |
|
| 41 |
+ swarm.ServiceWithName(serviceName), |
|
| 42 |
+ swarm.ServiceWithNetwork(overlayName), |
|
| 43 |
+ ) |
|
| 46 | 44 |
|
| 47 |
- serviceID := serviceResp.ID |
|
| 48 | 45 |
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll) |
| 49 | 46 |
|
| 50 | 47 |
_, _, err = client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
| ... | ... |
@@ -78,12 +75,12 @@ func TestInspectNetwork(t *testing.T) {
|
| 78 | 78 |
poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll) |
| 79 | 79 |
poll.WaitOn(t, noTasks(client), swarm.ServicePoll) |
| 80 | 80 |
|
| 81 |
- serviceResp, err = client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{
|
|
| 82 |
- QueryRegistry: false, |
|
| 83 |
- }) |
|
| 84 |
- assert.NilError(t, err) |
|
| 81 |
+ serviceID2 := swarm.CreateService(t, d, |
|
| 82 |
+ swarm.ServiceWithReplicas(instances), |
|
| 83 |
+ swarm.ServiceWithName(serviceName), |
|
| 84 |
+ swarm.ServiceWithNetwork(overlayName), |
|
| 85 |
+ ) |
|
| 85 | 86 |
|
| 86 |
- serviceID2 := serviceResp.ID |
|
| 87 | 87 |
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll) |
| 88 | 88 |
|
| 89 | 89 |
err = client.ServiceRemove(context.Background(), serviceID2) |
| ... | ... |
@@ -98,25 +95,6 @@ func TestInspectNetwork(t *testing.T) {
|
| 98 | 98 |
poll.WaitOn(t, networkIsRemoved(client, overlayID), poll.WithTimeout(1*time.Minute), poll.WithDelay(10*time.Second)) |
| 99 | 99 |
} |
| 100 | 100 |
|
| 101 |
-func swarmServiceSpec(name string, replicas uint64) swarmtypes.ServiceSpec {
|
|
| 102 |
- return swarmtypes.ServiceSpec{
|
|
| 103 |
- Annotations: swarmtypes.Annotations{
|
|
| 104 |
- Name: name, |
|
| 105 |
- }, |
|
| 106 |
- TaskTemplate: swarmtypes.TaskSpec{
|
|
| 107 |
- ContainerSpec: &swarmtypes.ContainerSpec{
|
|
| 108 |
- Image: "busybox:latest", |
|
| 109 |
- Command: []string{"/bin/top"},
|
|
| 110 |
- }, |
|
| 111 |
- }, |
|
| 112 |
- Mode: swarmtypes.ServiceMode{
|
|
| 113 |
- Replicated: &swarmtypes.ReplicatedService{
|
|
| 114 |
- Replicas: &replicas, |
|
| 115 |
- }, |
|
| 116 |
- }, |
|
| 117 |
- } |
|
| 118 |
-} |
|
| 119 |
- |
|
| 120 | 101 |
func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result {
|
| 121 | 102 |
return func(log poll.LogT) poll.Result {
|
| 122 | 103 |
filter := filters.NewArgs() |
| ... | ... |
@@ -202,18 +202,16 @@ func TestServiceWithPredefinedNetwork(t *testing.T) {
|
| 202 | 202 |
hostName := "host" |
| 203 | 203 |
var instances uint64 = 1 |
| 204 | 204 |
serviceName := "TestService" |
| 205 |
- serviceSpec := swarmServiceSpec(serviceName, instances) |
|
| 206 |
- serviceSpec.TaskTemplate.Networks = append(serviceSpec.TaskTemplate.Networks, swarmtypes.NetworkAttachmentConfig{Target: hostName})
|
|
| 207 | 205 |
|
| 208 |
- serviceResp, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{
|
|
| 209 |
- QueryRegistry: false, |
|
| 210 |
- }) |
|
| 211 |
- assert.NilError(t, err) |
|
| 206 |
+ serviceID := swarm.CreateService(t, d, |
|
| 207 |
+ swarm.ServiceWithReplicas(instances), |
|
| 208 |
+ swarm.ServiceWithName(serviceName), |
|
| 209 |
+ swarm.ServiceWithNetwork(hostName), |
|
| 210 |
+ ) |
|
| 212 | 211 |
|
| 213 |
- serviceID := serviceResp.ID |
|
| 214 | 212 |
poll.WaitOn(t, serviceRunningCount(client, serviceID, instances), swarm.ServicePoll) |
| 215 | 213 |
|
| 216 |
- _, _, err = client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
|
| 214 |
+ _, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
|
| 217 | 215 |
assert.NilError(t, err) |
| 218 | 216 |
|
| 219 | 217 |
err = client.ServiceRemove(context.Background(), serviceID) |
| ... | ... |
@@ -232,26 +230,24 @@ func TestServiceRemoveKeepsIngressNetwork(t *testing.T) {
|
| 232 | 232 |
poll.WaitOn(t, swarmIngressReady(client), swarm.NetworkPoll) |
| 233 | 233 |
|
| 234 | 234 |
var instances uint64 = 1 |
| 235 |
- serviceSpec := swarmServiceSpec(t.Name()+"-service", instances) |
|
| 236 |
- serviceSpec.EndpointSpec = &swarmtypes.EndpointSpec{
|
|
| 237 |
- Ports: []swarmtypes.PortConfig{
|
|
| 238 |
- {
|
|
| 239 |
- Protocol: swarmtypes.PortConfigProtocolTCP, |
|
| 240 |
- TargetPort: 80, |
|
| 241 |
- PublishMode: swarmtypes.PortConfigPublishModeIngress, |
|
| 242 |
- }, |
|
| 243 |
- }, |
|
| 244 |
- } |
|
| 245 | 235 |
|
| 246 |
- serviceResp, err := client.ServiceCreate(context.Background(), serviceSpec, types.ServiceCreateOptions{
|
|
| 247 |
- QueryRegistry: false, |
|
| 248 |
- }) |
|
| 249 |
- assert.NilError(t, err) |
|
| 236 |
+ serviceID := swarm.CreateService(t, d, |
|
| 237 |
+ swarm.ServiceWithReplicas(instances), |
|
| 238 |
+ swarm.ServiceWithName(t.Name()+"-service"), |
|
| 239 |
+ swarm.ServiceWithEndpoint(&swarmtypes.EndpointSpec{
|
|
| 240 |
+ Ports: []swarmtypes.PortConfig{
|
|
| 241 |
+ {
|
|
| 242 |
+ Protocol: swarmtypes.PortConfigProtocolTCP, |
|
| 243 |
+ TargetPort: 80, |
|
| 244 |
+ PublishMode: swarmtypes.PortConfigPublishModeIngress, |
|
| 245 |
+ }, |
|
| 246 |
+ }, |
|
| 247 |
+ }), |
|
| 248 |
+ ) |
|
| 250 | 249 |
|
| 251 |
- serviceID := serviceResp.ID |
|
| 252 | 250 |
poll.WaitOn(t, serviceRunningCount(client, serviceID, instances), swarm.ServicePoll) |
| 253 | 251 |
|
| 254 |
- _, _, err = client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
|
| 252 |
+ _, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
|
| 255 | 253 |
assert.NilError(t, err) |
| 256 | 254 |
|
| 257 | 255 |
err = client.ServiceRemove(context.Background(), serviceID) |