From 5ed399e58873944bf5e992284c751a200417318c Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 29 Nov 2017 19:04:40 +0100 Subject: [PATCH] Add unit tests on config/secret indempotence in stack deploy Related PR : https://github.com/docker/cli/pull/509 Signed-off-by: Vincent Demeester --- cli/compose/convert/service_test.go | 121 ++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/cli/compose/convert/service_test.go b/cli/compose/convert/service_test.go index ebcce495c6..265937339f 100644 --- a/cli/compose/convert/service_test.go +++ b/cli/compose/convert/service_test.go @@ -8,11 +8,14 @@ import ( "time" composetypes "github.com/docker/cli/cli/compose/types" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/swarm" + "github.com/docker/docker/client" "github.com/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "golang.org/x/net/context" ) func TestConvertRestartPolicyFromNone(t *testing.T) { @@ -433,3 +436,121 @@ func TestServiceConvertsIsolation(t *testing.T) { require.NoError(t, err) assert.Equal(t, container.IsolationHyperV, result.TaskTemplate.ContainerSpec.Isolation) } + +func TestConvertServiceSecrets(t *testing.T) { + namespace := Namespace{name: "foo"} + secrets := []composetypes.ServiceSecretConfig{ + {Source: "foo_secret"}, + {Source: "bar_secret"}, + } + secretSpecs := map[string]composetypes.SecretConfig{ + "foo_secret": { + Name: "foo_secret", + }, + "bar_secret": { + Name: "bar_secret", + }, + } + client := &fakeClient{ + secretListFunc: func(opts types.SecretListOptions) ([]swarm.Secret, error) { + assert.Contains(t, opts.Filters.Get("name"), "foo_secret") + assert.Contains(t, opts.Filters.Get("name"), "bar_secret") + return []swarm.Secret{ + {Spec: swarm.SecretSpec{Annotations: swarm.Annotations{Name: "foo_secret"}}}, + {Spec: swarm.SecretSpec{Annotations: swarm.Annotations{Name: "bar_secret"}}}, + }, nil + }, + } + refs, err := convertServiceSecrets(client, namespace, secrets, secretSpecs) + require.NoError(t, err) + expected := []*swarm.SecretReference{ + { + SecretName: "bar_secret", + File: &swarm.SecretReferenceFileTarget{ + Name: "bar_secret", + UID: "0", + GID: "0", + Mode: 0444, + }, + }, + { + SecretName: "foo_secret", + File: &swarm.SecretReferenceFileTarget{ + Name: "foo_secret", + UID: "0", + GID: "0", + Mode: 0444, + }, + }, + } + require.Equal(t, expected, refs) +} + +func TestConvertServiceConfigs(t *testing.T) { + namespace := Namespace{name: "foo"} + configs := []composetypes.ServiceConfigObjConfig{ + {Source: "foo_config"}, + {Source: "bar_config"}, + } + configSpecs := map[string]composetypes.ConfigObjConfig{ + "foo_config": { + Name: "foo_config", + }, + "bar_config": { + Name: "bar_config", + }, + } + client := &fakeClient{ + configListFunc: func(opts types.ConfigListOptions) ([]swarm.Config, error) { + assert.Contains(t, opts.Filters.Get("name"), "foo_config") + assert.Contains(t, opts.Filters.Get("name"), "bar_config") + return []swarm.Config{ + {Spec: swarm.ConfigSpec{Annotations: swarm.Annotations{Name: "foo_config"}}}, + {Spec: swarm.ConfigSpec{Annotations: swarm.Annotations{Name: "bar_config"}}}, + }, nil + }, + } + refs, err := convertServiceConfigObjs(client, namespace, configs, configSpecs) + require.NoError(t, err) + expected := []*swarm.ConfigReference{ + { + ConfigName: "bar_config", + File: &swarm.ConfigReferenceFileTarget{ + Name: "bar_config", + UID: "0", + GID: "0", + Mode: 0444, + }, + }, + { + ConfigName: "foo_config", + File: &swarm.ConfigReferenceFileTarget{ + Name: "foo_config", + UID: "0", + GID: "0", + Mode: 0444, + }, + }, + } + require.Equal(t, expected, refs) +} + +type fakeClient struct { + client.Client + secretListFunc func(types.SecretListOptions) ([]swarm.Secret, error) + configListFunc func(types.ConfigListOptions) ([]swarm.Config, error) +} + +func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) { + if c.secretListFunc != nil { + return c.secretListFunc(options) + } + return []swarm.Secret{}, nil +} + +func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) { + if c.configListFunc != nil { + return c.configListFunc(options) + } + return []swarm.Config{}, nil +}