Browse code

Add test for status code on conflicting service names

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2018/11/05 23:59:26
Showing 2 changed files
... ...
@@ -66,24 +66,27 @@ type ServiceSpecOpt func(*swarmtypes.ServiceSpec)
66 66
 // CreateService creates a service on the passed in swarm daemon.
67 67
 func CreateService(t *testing.T, d *daemon.Daemon, opts ...ServiceSpecOpt) string {
68 68
 	t.Helper()
69
-	spec := defaultServiceSpec()
70
-	for _, o := range opts {
71
-		o(&spec)
72
-	}
73 69
 
74 70
 	client := d.NewClientT(t)
75 71
 	defer client.Close()
76 72
 
73
+	spec := CreateServiceSpec(t, opts...)
77 74
 	resp, err := client.ServiceCreate(context.Background(), spec, types.ServiceCreateOptions{})
78 75
 	assert.NilError(t, err, "error creating service")
79 76
 	return resp.ID
80 77
 }
81 78
 
82
-func defaultServiceSpec() swarmtypes.ServiceSpec {
79
+// CreateServiceSpec creates a default service-spec, and applies the provided options
80
+func CreateServiceSpec(t *testing.T, opts ...ServiceSpecOpt) swarmtypes.ServiceSpec {
81
+	t.Helper()
83 82
 	var spec swarmtypes.ServiceSpec
84 83
 	ServiceWithImage("busybox:latest")(&spec)
85 84
 	ServiceWithCommand([]string{"/bin/top"})(&spec)
86 85
 	ServiceWithReplicas(1)(&spec)
86
+
87
+	for _, o := range opts {
88
+		o(&spec)
89
+	}
87 90
 	return spec
88 91
 }
89 92
 
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"context"
5 5
 	"fmt"
6 6
 	"io/ioutil"
7
+	"net/http"
7 8
 	"testing"
8 9
 	"time"
9 10
 
... ...
@@ -15,6 +16,7 @@ import (
15 15
 	"github.com/docker/docker/integration/internal/network"
16 16
 	"github.com/docker/docker/integration/internal/swarm"
17 17
 	"github.com/docker/docker/internal/test/daemon"
18
+	"github.com/docker/docker/internal/test/request"
18 19
 	"gotest.tools/assert"
19 20
 	is "gotest.tools/assert/cmp"
20 21
 	"gotest.tools/poll"
... ...
@@ -123,6 +125,34 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
123 123
 	poll.WaitOn(t, networkIsRemoved(client, overlayID), poll.WithTimeout(1*time.Minute), poll.WithDelay(10*time.Second))
124 124
 }
125 125
 
126
+func TestCreateServiceConflict(t *testing.T) {
127
+	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
128
+	defer setupTest(t)()
129
+	d := swarm.NewSwarm(t, testEnv)
130
+	defer d.Stop(t)
131
+
132
+	serviceName := "TestService_" + t.Name()
133
+	serviceSpec := []swarm.ServiceSpecOpt{
134
+		swarm.ServiceWithName(serviceName),
135
+	}
136
+
137
+	swarm.CreateService(t, d, serviceSpec...)
138
+
139
+	spec := swarm.CreateServiceSpec(t, serviceSpec...)
140
+	res, body, err := request.Post(
141
+		"/services/create",
142
+		request.Host(d.Sock()),
143
+		request.JSONBody(spec),
144
+		request.JSON,
145
+	)
146
+	assert.NilError(t, err)
147
+	assert.Equal(t, res.StatusCode, http.StatusConflict)
148
+
149
+	buf, err := request.ReadBody(body)
150
+	assert.NilError(t, err)
151
+	assert.Check(t, is.Contains(string(buf), "service "+serviceName+" already exists"))
152
+}
153
+
126 154
 func TestCreateWithDuplicateNetworkNames(t *testing.T) {
127 155
 	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
128 156
 	defer setupTest(t)()