Skip to content

Commit

Permalink
Add test for status code on conflicting service names
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Nov 24, 2018
1 parent 0b7cb16 commit b0de11c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
13 changes: 8 additions & 5 deletions integration/internal/swarm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,27 @@ type ServiceSpecOpt func(*swarmtypes.ServiceSpec)
// CreateService creates a service on the passed in swarm daemon.
func CreateService(t *testing.T, d *daemon.Daemon, opts ...ServiceSpecOpt) string {
t.Helper()
spec := defaultServiceSpec()
for _, o := range opts {
o(&spec)
}

client := d.NewClientT(t)
defer client.Close()

spec := CreateServiceSpec(t, opts...)
resp, err := client.ServiceCreate(context.Background(), spec, types.ServiceCreateOptions{})
assert.NilError(t, err, "error creating service")
return resp.ID
}

func defaultServiceSpec() swarmtypes.ServiceSpec {
// CreateServiceSpec creates a default service-spec, and applies the provided options
func CreateServiceSpec(t *testing.T, opts ...ServiceSpecOpt) swarmtypes.ServiceSpec {
t.Helper()
var spec swarmtypes.ServiceSpec
ServiceWithImage("busybox:latest")(&spec)
ServiceWithCommand([]string{"/bin/top"})(&spec)
ServiceWithReplicas(1)(&spec)

for _, o := range opts {
o(&spec)
}
return spec
}

Expand Down
30 changes: 30 additions & 0 deletions integration/service/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"

Expand All @@ -15,6 +16,7 @@ import (
"github.com/docker/docker/integration/internal/network"
"github.com/docker/docker/integration/internal/swarm"
"github.com/docker/docker/internal/test/daemon"
"github.com/docker/docker/internal/test/request"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
"gotest.tools/poll"
Expand Down Expand Up @@ -123,6 +125,34 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
poll.WaitOn(t, networkIsRemoved(client, overlayID), poll.WithTimeout(1*time.Minute), poll.WithDelay(10*time.Second))
}

func TestCreateServiceConflict(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
defer setupTest(t)()
d := swarm.NewSwarm(t, testEnv)
defer d.Stop(t)

serviceName := "TestService_" + t.Name()
serviceSpec := []swarm.ServiceSpecOpt{
swarm.ServiceWithName(serviceName),
}

swarm.CreateService(t, d, serviceSpec...)

spec := swarm.CreateServiceSpec(t, serviceSpec...)
res, body, err := request.Post(
"/services/create",
request.Host(d.Sock()),
request.JSONBody(spec),
request.JSON,
)
assert.NilError(t, err)
assert.Equal(t, res.StatusCode, http.StatusConflict)

buf, err := request.ReadBody(body)
assert.NilError(t, err)
assert.Check(t, is.Contains(string(buf), "service "+serviceName+" already exists"))
}

func TestCreateWithDuplicateNetworkNames(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
defer setupTest(t)()
Expand Down

0 comments on commit b0de11c

Please sign in to comment.