From fbaef1b0d3c5656a8aa5f0424b89c118ca277427 Mon Sep 17 00:00:00 2001 From: Arash Deshmeh Date: Wed, 18 Jul 2018 17:34:19 -0400 Subject: [PATCH] migrated service integration tests from integration-cli/docker_cli_service_update_test.go to integration/service Signed-off-by: Arash Deshmeh --- .../docker_cli_service_update_test.go | 137 ------------- integration/service/update_test.go | 186 ++++++++++++++++++ 2 files changed, 186 insertions(+), 137 deletions(-) delete mode 100644 integration-cli/docker_cli_service_update_test.go create mode 100644 integration/service/update_test.go diff --git a/integration-cli/docker_cli_service_update_test.go b/integration-cli/docker_cli_service_update_test.go deleted file mode 100644 index a281327afe..0000000000 --- a/integration-cli/docker_cli_service_update_test.go +++ /dev/null @@ -1,137 +0,0 @@ -// +build !windows - -package main - -import ( - "encoding/json" - "fmt" - - "github.com/docker/docker/api/types/swarm" - "github.com/docker/docker/integration-cli/checker" - "github.com/go-check/check" -) - -func (s *DockerSwarmSuite) TestServiceUpdateLabel(c *check.C) { - d := s.AddDaemon(c, true, true) - out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name=test", "busybox", "top") - c.Assert(err, checker.IsNil, check.Commentf(out)) - service := d.GetService(c, "test") - c.Assert(service.Spec.Labels, checker.HasLen, 0) - - // add label to empty set - out, err = d.Cmd("service", "update", "--detach", "test", "--label-add", "foo=bar") - c.Assert(err, checker.IsNil, check.Commentf(out)) - service = d.GetService(c, "test") - c.Assert(service.Spec.Labels, checker.HasLen, 1) - c.Assert(service.Spec.Labels["foo"], checker.Equals, "bar") - - // add label to non-empty set - out, err = d.Cmd("service", "update", "--detach", "test", "--label-add", "foo2=bar") - c.Assert(err, checker.IsNil, check.Commentf(out)) - service = d.GetService(c, "test") - c.Assert(service.Spec.Labels, checker.HasLen, 2) - c.Assert(service.Spec.Labels["foo2"], checker.Equals, "bar") - - out, err = d.Cmd("service", "update", "--detach", "test", "--label-rm", "foo2") - c.Assert(err, checker.IsNil, check.Commentf(out)) - service = d.GetService(c, "test") - c.Assert(service.Spec.Labels, checker.HasLen, 1) - c.Assert(service.Spec.Labels["foo2"], checker.Equals, "") - - out, err = d.Cmd("service", "update", "--detach", "test", "--label-rm", "foo") - c.Assert(err, checker.IsNil, check.Commentf(out)) - service = d.GetService(c, "test") - c.Assert(service.Spec.Labels, checker.HasLen, 0) - c.Assert(service.Spec.Labels["foo"], checker.Equals, "") - - // now make sure we can add again - out, err = d.Cmd("service", "update", "--detach", "test", "--label-add", "foo=bar") - c.Assert(err, checker.IsNil, check.Commentf(out)) - service = d.GetService(c, "test") - c.Assert(service.Spec.Labels, checker.HasLen, 1) - c.Assert(service.Spec.Labels["foo"], checker.Equals, "bar") -} - -func (s *DockerSwarmSuite) TestServiceUpdateSecrets(c *check.C) { - d := s.AddDaemon(c, true, true) - testName := "test_secret" - id := d.CreateSecret(c, swarm.SecretSpec{ - Annotations: swarm.Annotations{ - Name: testName, - }, - Data: []byte("TESTINGDATA"), - }) - c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id)) - testTarget := "testing" - serviceName := "test" - - out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "busybox", "top") - c.Assert(err, checker.IsNil, check.Commentf(out)) - - // add secret - out, err = d.Cmd("service", "update", "--detach", "test", "--secret-add", fmt.Sprintf("source=%s,target=%s", testName, testTarget)) - c.Assert(err, checker.IsNil, check.Commentf(out)) - - out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName) - c.Assert(err, checker.IsNil) - - var refs []swarm.SecretReference - c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil) - c.Assert(refs, checker.HasLen, 1) - - c.Assert(refs[0].SecretName, checker.Equals, testName) - c.Assert(refs[0].File, checker.Not(checker.IsNil)) - c.Assert(refs[0].File.Name, checker.Equals, testTarget) - - // remove - out, err = d.Cmd("service", "update", "--detach", "test", "--secret-rm", testName) - c.Assert(err, checker.IsNil, check.Commentf(out)) - - out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Secrets }}", serviceName) - c.Assert(err, checker.IsNil) - - c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil) - c.Assert(refs, checker.HasLen, 0) -} - -func (s *DockerSwarmSuite) TestServiceUpdateConfigs(c *check.C) { - d := s.AddDaemon(c, true, true) - testName := "test_config" - id := d.CreateConfig(c, swarm.ConfigSpec{ - Annotations: swarm.Annotations{ - Name: testName, - }, - Data: []byte("TESTINGDATA"), - }) - c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("configs: %s", id)) - testTarget := "/testing" - serviceName := "test" - - out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "busybox", "top") - c.Assert(err, checker.IsNil, check.Commentf(out)) - - // add config - out, err = d.Cmd("service", "update", "--detach", "test", "--config-add", fmt.Sprintf("source=%s,target=%s", testName, testTarget)) - c.Assert(err, checker.IsNil, check.Commentf(out)) - - out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Configs }}", serviceName) - c.Assert(err, checker.IsNil) - - var refs []swarm.ConfigReference - c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil) - c.Assert(refs, checker.HasLen, 1) - - c.Assert(refs[0].ConfigName, checker.Equals, testName) - c.Assert(refs[0].File, checker.Not(checker.IsNil)) - c.Assert(refs[0].File.Name, checker.Equals, testTarget) - - // remove - out, err = d.Cmd("service", "update", "--detach", "test", "--config-rm", testName) - c.Assert(err, checker.IsNil, check.Commentf(out)) - - out, err = d.Cmd("service", "inspect", "--format", "{{ json .Spec.TaskTemplate.ContainerSpec.Configs }}", serviceName) - c.Assert(err, checker.IsNil) - - c.Assert(json.Unmarshal([]byte(out), &refs), checker.IsNil) - c.Assert(refs, checker.HasLen, 0) -} diff --git a/integration/service/update_test.go b/integration/service/update_test.go new file mode 100644 index 0000000000..f1efafc07d --- /dev/null +++ b/integration/service/update_test.go @@ -0,0 +1,186 @@ +package service // import "github.com/docker/docker/integration/service" + +import ( + "context" + "testing" + + "github.com/docker/docker/api/types" + swarmtypes "github.com/docker/docker/api/types/swarm" + "github.com/docker/docker/client" + "github.com/docker/docker/integration/internal/swarm" + "gotest.tools/assert" + is "gotest.tools/assert/cmp" + "gotest.tools/skip" +) + +func TestServiceUpdateLabel(t *testing.T) { + skip.If(t, testEnv.DaemonInfo.OSType != "linux") + defer setupTest(t)() + d := swarm.NewSwarm(t, testEnv) + defer d.Stop(t) + cli := d.NewClientT(t) + defer cli.Close() + + ctx := context.Background() + serviceName := "TestService_" + t.Name() + serviceID := swarm.CreateService(t, d, swarm.ServiceWithName(serviceName)) + service := getService(t, cli, serviceID) + assert.Check(t, is.DeepEqual(service.Spec.Labels, map[string]string{})) + + // add label to empty set + service.Spec.Labels["foo"] = "bar" + _, err := cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + assert.NilError(t, err) + service = getService(t, cli, serviceID) + assert.Check(t, is.DeepEqual(service.Spec.Labels, map[string]string{"foo": "bar"})) + + // add label to non-empty set + service.Spec.Labels["foo2"] = "bar" + _, err = cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + assert.NilError(t, err) + service = getService(t, cli, serviceID) + assert.Check(t, is.DeepEqual(service.Spec.Labels, map[string]string{"foo": "bar", "foo2": "bar"})) + + delete(service.Spec.Labels, "foo2") + _, err = cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + assert.NilError(t, err) + service = getService(t, cli, serviceID) + assert.Check(t, is.DeepEqual(service.Spec.Labels, map[string]string{"foo": "bar"})) + + delete(service.Spec.Labels, "foo") + _, err = cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + assert.NilError(t, err) + service = getService(t, cli, serviceID) + assert.Check(t, is.DeepEqual(service.Spec.Labels, map[string]string{})) + + // now make sure we can add again + service.Spec.Labels["foo"] = "bar" + _, err = cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + assert.NilError(t, err) + service = getService(t, cli, serviceID) + assert.Check(t, is.DeepEqual(service.Spec.Labels, map[string]string{"foo": "bar"})) + + err = cli.ServiceRemove(context.Background(), serviceID) + assert.NilError(t, err) +} + +func TestServiceUpdateSecrets(t *testing.T) { + skip.If(t, testEnv.DaemonInfo.OSType != "linux") + defer setupTest(t)() + d := swarm.NewSwarm(t, testEnv) + defer d.Stop(t) + cli := d.NewClientT(t) + defer cli.Close() + + ctx := context.Background() + secretName := "TestSecret_" + t.Name() + secretTarget := "targetName" + resp, err := cli.SecretCreate(ctx, swarmtypes.SecretSpec{ + Annotations: swarmtypes.Annotations{ + Name: secretName, + }, + Data: []byte("TESTINGDATA"), + }) + assert.NilError(t, err) + assert.Check(t, resp.ID != "") + + serviceName := "TestService_" + t.Name() + serviceID := swarm.CreateService(t, d, swarm.ServiceWithName(serviceName)) + service := getService(t, cli, serviceID) + + // add secret + service.Spec.TaskTemplate.ContainerSpec.Secrets = append(service.Spec.TaskTemplate.ContainerSpec.Secrets, + &swarmtypes.SecretReference{ + File: &swarmtypes.SecretReferenceFileTarget{ + Name: secretTarget, + }, + SecretID: resp.ID, + SecretName: secretName, + }, + ) + _, err = cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + assert.NilError(t, err) + + service = getService(t, cli, serviceID) + secrets := service.Spec.TaskTemplate.ContainerSpec.Secrets + assert.Assert(t, is.Equal(1, len(secrets))) + + secret := *secrets[0] + assert.Check(t, is.Equal(secretName, secret.SecretName)) + assert.Check(t, nil != secret.File) + assert.Check(t, is.Equal(secretTarget, secret.File.Name)) + + // remove + service.Spec.TaskTemplate.ContainerSpec.Secrets = []*swarmtypes.SecretReference{} + _, err = cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + assert.NilError(t, err) + service = getService(t, cli, serviceID) + assert.Check(t, is.Equal(0, len(service.Spec.TaskTemplate.ContainerSpec.Secrets))) + + err = cli.ServiceRemove(context.Background(), serviceID) + assert.NilError(t, err) +} + +func TestServiceUpdateConfigs(t *testing.T) { + skip.If(t, testEnv.DaemonInfo.OSType != "linux") + defer setupTest(t)() + d := swarm.NewSwarm(t, testEnv) + defer d.Stop(t) + cli := d.NewClientT(t) + defer cli.Close() + + ctx := context.Background() + configName := "TestConfig_" + t.Name() + configTarget := "targetName" + resp, err := cli.ConfigCreate(ctx, swarmtypes.ConfigSpec{ + Annotations: swarmtypes.Annotations{ + Name: configName, + }, + Data: []byte("TESTINGDATA"), + }) + assert.NilError(t, err) + assert.Check(t, resp.ID != "") + + serviceName := "TestService_" + t.Name() + serviceID := swarm.CreateService(t, d, swarm.ServiceWithName(serviceName)) + service := getService(t, cli, serviceID) + + // add config + service.Spec.TaskTemplate.ContainerSpec.Configs = append(service.Spec.TaskTemplate.ContainerSpec.Configs, + &swarmtypes.ConfigReference{ + File: &swarmtypes.ConfigReferenceFileTarget{ + Name: configTarget, + }, + ConfigID: resp.ID, + ConfigName: configName, + }, + ) + _, err = cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + assert.NilError(t, err) + + service = getService(t, cli, serviceID) + configs := service.Spec.TaskTemplate.ContainerSpec.Configs + assert.Assert(t, is.Equal(1, len(configs))) + + config := *configs[0] + assert.Check(t, is.Equal(configName, config.ConfigName)) + assert.Check(t, nil != config.File) + assert.Check(t, is.Equal(configTarget, config.File.Name)) + + // remove + service.Spec.TaskTemplate.ContainerSpec.Configs = []*swarmtypes.ConfigReference{} + _, err = cli.ServiceUpdate(ctx, serviceID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + assert.NilError(t, err) + service = getService(t, cli, serviceID) + assert.Check(t, is.Equal(0, len(service.Spec.TaskTemplate.ContainerSpec.Configs))) + + err = cli.ServiceRemove(context.Background(), serviceID) + assert.NilError(t, err) +} + +func getService(t *testing.T, cli client.ServiceAPIClient, serviceID string) swarmtypes.Service { + t.Helper() + service, _, err := cli.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{}) + assert.NilError(t, err) + return service +}