2018-02-05 16:05:59 -05:00
|
|
|
package service // import "github.com/docker/docker/integration/service"
|
2017-08-28 23:49:26 -07:00
|
|
|
|
|
|
|
import (
|
2018-04-19 15:30:59 -07:00
|
|
|
"context"
|
2018-01-28 07:15:44 +00:00
|
|
|
"io/ioutil"
|
2017-08-28 23:49:26 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2018-02-02 22:36:59 +00:00
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
2017-08-28 23:49:26 -07:00
|
|
|
"github.com/docker/docker/client"
|
2018-02-09 19:13:26 +01:00
|
|
|
"github.com/docker/docker/integration/internal/swarm"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-08-28 23:49:26 -07:00
|
|
|
"github.com/gotestyourself/gotestyourself/poll"
|
|
|
|
)
|
|
|
|
|
2017-11-07 06:12:03 +00:00
|
|
|
func TestCreateServiceMultipleTimes(t *testing.T) {
|
2017-08-28 23:49:26 -07:00
|
|
|
defer setupTest(t)()
|
2018-02-02 22:36:59 +00:00
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
2017-08-28 23:49:26 -07:00
|
|
|
defer d.Stop(t)
|
2018-04-13 17:02:56 +02:00
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
2017-08-28 23:49:26 -07:00
|
|
|
|
|
|
|
overlayName := "overlay1"
|
|
|
|
networkCreate := types.NetworkCreate{
|
|
|
|
CheckDuplicate: true,
|
|
|
|
Driver: "overlay",
|
|
|
|
}
|
|
|
|
|
|
|
|
netResp, err := client.NetworkCreate(context.Background(), overlayName, networkCreate)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-28 23:49:26 -07:00
|
|
|
overlayID := netResp.ID
|
|
|
|
|
2017-11-07 06:12:03 +00:00
|
|
|
var instances uint64 = 4
|
2017-08-28 23:49:26 -07:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
serviceSpec := []swarm.ServiceSpecOpt{
|
|
|
|
swarm.ServiceWithReplicas(instances),
|
|
|
|
swarm.ServiceWithName("TestService"),
|
|
|
|
swarm.ServiceWithNetwork(overlayName),
|
|
|
|
}
|
2017-08-28 23:49:26 -07:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
serviceID := swarm.CreateService(t, d, serviceSpec...)
|
2018-03-27 04:06:46 +00:00
|
|
|
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
|
2017-08-28 23:49:26 -07:00
|
|
|
|
|
|
|
_, _, err = client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-28 23:49:26 -07:00
|
|
|
|
|
|
|
err = client.ServiceRemove(context.Background(), serviceID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-28 23:49:26 -07:00
|
|
|
|
2018-03-27 04:06:46 +00:00
|
|
|
poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
|
2017-11-07 06:12:03 +00:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
serviceID2 := swarm.CreateService(t, d, serviceSpec...)
|
2018-03-27 04:06:46 +00:00
|
|
|
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID2, instances), swarm.ServicePoll)
|
2017-11-07 06:12:03 +00:00
|
|
|
|
|
|
|
err = client.ServiceRemove(context.Background(), serviceID2)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-11-07 06:12:03 +00:00
|
|
|
|
2018-03-27 04:06:46 +00:00
|
|
|
poll.WaitOn(t, serviceIsRemoved(client, serviceID2), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
|
2017-11-07 06:12:03 +00:00
|
|
|
|
2017-08-28 23:49:26 -07:00
|
|
|
err = client.NetworkRemove(context.Background(), overlayID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-08-28 23:49:26 -07:00
|
|
|
|
|
|
|
poll.WaitOn(t, networkIsRemoved(client, overlayID), poll.WithTimeout(1*time.Minute), poll.WithDelay(10*time.Second))
|
|
|
|
}
|
|
|
|
|
2017-10-31 20:05:24 +00:00
|
|
|
func TestCreateWithDuplicateNetworkNames(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
2018-02-02 22:36:59 +00:00
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
2017-10-31 20:05:24 +00:00
|
|
|
defer d.Stop(t)
|
2018-04-13 17:02:56 +02:00
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
name := "foo"
|
|
|
|
networkCreate := types.NetworkCreate{
|
|
|
|
CheckDuplicate: false,
|
|
|
|
Driver: "bridge",
|
|
|
|
}
|
|
|
|
|
|
|
|
n1, err := client.NetworkCreate(context.Background(), name, networkCreate)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
n2, err := client.NetworkCreate(context.Background(), name, networkCreate)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
// Dupliates with name but with different driver
|
|
|
|
networkCreate.Driver = "overlay"
|
|
|
|
n3, err := client.NetworkCreate(context.Background(), name, networkCreate)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
// Create Service with the same name
|
|
|
|
var instances uint64 = 1
|
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
serviceID := swarm.CreateService(t, d,
|
|
|
|
swarm.ServiceWithReplicas(instances),
|
|
|
|
swarm.ServiceWithName("top"),
|
|
|
|
swarm.ServiceWithNetwork(name),
|
|
|
|
)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
resp, _, err := client.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(n3.ID, resp.Spec.TaskTemplate.Networks[0].Target))
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
// Remove Service
|
2018-05-10 05:45:11 -04:00
|
|
|
err = client.ServiceRemove(context.Background(), serviceID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
// Make sure task has been destroyed.
|
2018-05-10 05:45:11 -04:00
|
|
|
poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
// Remove networks
|
|
|
|
err = client.NetworkRemove(context.Background(), n3.ID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
err = client.NetworkRemove(context.Background(), n2.ID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
err = client.NetworkRemove(context.Background(), n1.ID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-10-31 20:05:24 +00:00
|
|
|
|
|
|
|
// Make sure networks have been destroyed.
|
|
|
|
poll.WaitOn(t, networkIsRemoved(client, n3.ID), poll.WithTimeout(1*time.Minute), poll.WithDelay(10*time.Second))
|
|
|
|
poll.WaitOn(t, networkIsRemoved(client, n2.ID), poll.WithTimeout(1*time.Minute), poll.WithDelay(10*time.Second))
|
|
|
|
poll.WaitOn(t, networkIsRemoved(client, n1.ID), poll.WithTimeout(1*time.Minute), poll.WithDelay(10*time.Second))
|
|
|
|
}
|
|
|
|
|
2018-01-28 07:15:44 +00:00
|
|
|
func TestCreateServiceSecretFileMode(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
2018-02-02 22:36:59 +00:00
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
2018-01-28 07:15:44 +00:00
|
|
|
defer d.Stop(t)
|
2018-04-13 17:02:56 +02:00
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
2018-01-28 07:15:44 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2018-02-02 22:36:59 +00:00
|
|
|
secretResp, err := client.SecretCreate(ctx, swarmtypes.SecretSpec{
|
|
|
|
Annotations: swarmtypes.Annotations{
|
2018-01-28 07:15:44 +00:00
|
|
|
Name: "TestSecret",
|
|
|
|
},
|
|
|
|
Data: []byte("TESTSECRET"),
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-28 07:15:44 +00:00
|
|
|
|
|
|
|
var instances uint64 = 1
|
2018-05-10 05:45:11 -04:00
|
|
|
serviceID := swarm.CreateService(t, d,
|
|
|
|
swarm.ServiceWithReplicas(instances),
|
|
|
|
swarm.ServiceWithName("TestService"),
|
|
|
|
swarm.ServiceWithCommand([]string{"/bin/sh", "-c", "ls -l /etc/secret || /bin/top"}),
|
|
|
|
swarm.ServiceWithSecret(&swarmtypes.SecretReference{
|
|
|
|
File: &swarmtypes.SecretReferenceFileTarget{
|
|
|
|
Name: "/etc/secret",
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
|
|
|
Mode: 0777,
|
2018-01-28 07:15:44 +00:00
|
|
|
},
|
2018-05-10 05:45:11 -04:00
|
|
|
SecretID: secretResp.ID,
|
|
|
|
SecretName: "TestSecret",
|
|
|
|
}),
|
|
|
|
)
|
2018-01-28 07:15:44 +00:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances), swarm.ServicePoll)
|
2018-01-28 07:15:44 +00:00
|
|
|
|
|
|
|
filter := filters.NewArgs()
|
2018-05-10 05:45:11 -04:00
|
|
|
filter.Add("service", serviceID)
|
2018-01-28 07:15:44 +00:00
|
|
|
tasks, err := client.TaskList(ctx, types.TaskListOptions{
|
|
|
|
Filters: filter,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(len(tasks), 1))
|
2018-01-28 07:15:44 +00:00
|
|
|
|
|
|
|
body, err := client.ContainerLogs(ctx, tasks[0].Status.ContainerStatus.ContainerID, types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-28 07:15:44 +00:00
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(content), "-rwxrwxrwx"))
|
2018-01-28 07:15:44 +00:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
err = client.ServiceRemove(ctx, serviceID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-28 07:15:44 +00:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
poll.WaitOn(t, serviceIsRemoved(client, serviceID), swarm.ServicePoll)
|
2018-03-27 04:06:46 +00:00
|
|
|
poll.WaitOn(t, noTasks(client), swarm.ServicePoll)
|
2018-01-28 07:15:44 +00:00
|
|
|
|
|
|
|
err = client.SecretRemove(ctx, "TestSecret")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-28 07:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateServiceConfigFileMode(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
2018-02-02 22:36:59 +00:00
|
|
|
d := swarm.NewSwarm(t, testEnv)
|
2018-01-28 07:15:44 +00:00
|
|
|
defer d.Stop(t)
|
2018-04-13 17:02:56 +02:00
|
|
|
client := d.NewClientT(t)
|
|
|
|
defer client.Close()
|
2018-01-28 07:15:44 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2018-02-02 22:36:59 +00:00
|
|
|
configResp, err := client.ConfigCreate(ctx, swarmtypes.ConfigSpec{
|
|
|
|
Annotations: swarmtypes.Annotations{
|
2018-01-28 07:15:44 +00:00
|
|
|
Name: "TestConfig",
|
|
|
|
},
|
|
|
|
Data: []byte("TESTCONFIG"),
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-28 07:15:44 +00:00
|
|
|
|
|
|
|
var instances uint64 = 1
|
2018-05-10 05:45:11 -04:00
|
|
|
serviceID := swarm.CreateService(t, d,
|
|
|
|
swarm.ServiceWithName("TestService"),
|
|
|
|
swarm.ServiceWithCommand([]string{"/bin/sh", "-c", "ls -l /etc/config || /bin/top"}),
|
|
|
|
swarm.ServiceWithReplicas(instances),
|
|
|
|
swarm.ServiceWithConfig(&swarmtypes.ConfigReference{
|
|
|
|
File: &swarmtypes.ConfigReferenceFileTarget{
|
|
|
|
Name: "/etc/config",
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
|
|
|
Mode: 0777,
|
2018-01-28 07:15:44 +00:00
|
|
|
},
|
2018-05-10 05:45:11 -04:00
|
|
|
ConfigID: configResp.ID,
|
|
|
|
ConfigName: "TestConfig",
|
|
|
|
}),
|
|
|
|
)
|
2018-01-28 07:15:44 +00:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, instances))
|
2018-01-28 07:15:44 +00:00
|
|
|
|
|
|
|
filter := filters.NewArgs()
|
2018-05-10 05:45:11 -04:00
|
|
|
filter.Add("service", serviceID)
|
2018-01-28 07:15:44 +00:00
|
|
|
tasks, err := client.TaskList(ctx, types.TaskListOptions{
|
|
|
|
Filters: filter,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(len(tasks), 1))
|
2018-01-28 07:15:44 +00:00
|
|
|
|
|
|
|
body, err := client.ContainerLogs(ctx, tasks[0].Status.ContainerStatus.ContainerID, types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-28 07:15:44 +00:00
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(content), "-rwxrwxrwx"))
|
2018-01-28 07:15:44 +00:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
err = client.ServiceRemove(ctx, serviceID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-28 07:15:44 +00:00
|
|
|
|
2018-05-10 05:45:11 -04:00
|
|
|
poll.WaitOn(t, serviceIsRemoved(client, serviceID))
|
2018-01-28 07:15:44 +00:00
|
|
|
poll.WaitOn(t, noTasks(client))
|
|
|
|
|
|
|
|
err = client.ConfigRemove(ctx, "TestConfig")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-01-28 07:15:44 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 23:49:26 -07:00
|
|
|
func serviceRunningTasksCount(client client.ServiceAPIClient, serviceID string, instances uint64) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
filter := filters.NewArgs()
|
|
|
|
filter.Add("service", serviceID)
|
|
|
|
tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
|
|
|
|
Filters: filter,
|
|
|
|
})
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return poll.Error(err)
|
|
|
|
case len(tasks) == int(instances):
|
|
|
|
for _, task := range tasks {
|
2018-02-02 22:36:59 +00:00
|
|
|
if task.Status.State != swarmtypes.TaskStateRunning {
|
2017-08-28 23:49:26 -07:00
|
|
|
return poll.Continue("waiting for tasks to enter run state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
default:
|
|
|
|
return poll.Continue("task count at %d waiting for %d", len(tasks), instances)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 06:12:03 +00:00
|
|
|
func noTasks(client client.ServiceAPIClient) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
filter := filters.NewArgs()
|
|
|
|
tasks, err := client.TaskList(context.Background(), types.TaskListOptions{
|
|
|
|
Filters: filter,
|
|
|
|
})
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return poll.Error(err)
|
|
|
|
case len(tasks) == 0:
|
|
|
|
return poll.Success()
|
|
|
|
default:
|
|
|
|
return poll.Continue("task count at %d waiting for 0", len(tasks))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 23:49:26 -07:00
|
|
|
func serviceIsRemoved(client client.ServiceAPIClient, serviceID string) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
filter := filters.NewArgs()
|
|
|
|
filter.Add("service", serviceID)
|
|
|
|
_, err := client.TaskList(context.Background(), types.TaskListOptions{
|
|
|
|
Filters: filter,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return poll.Continue("waiting for service %s to be deleted", serviceID)
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkIsRemoved(client client.NetworkAPIClient, networkID string) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
_, err := client.NetworkInspect(context.Background(), networkID, types.NetworkInspectOptions{})
|
|
|
|
if err == nil {
|
|
|
|
return poll.Continue("waiting for network %s to be removed", networkID)
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
}
|