Skip to content

Commit

Permalink
Add unit tests on config/secret indempotence in stack deploy
Browse files Browse the repository at this point in the history
Related PR : docker#509

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
  • Loading branch information
vdemeester committed Nov 30, 2017
1 parent 2dce2e0 commit 0dda75b
Showing 1 changed file with 85 additions and 13 deletions.
98 changes: 85 additions & 13 deletions cli/compose/convert/service_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package convert

import (
"context"
"os"
"sort"
"strings"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)

func TestConvertRestartPolicyFromNone(t *testing.T) {
Expand Down Expand Up @@ -438,7 +438,7 @@ func TestServiceConvertsIsolation(t *testing.T) {
}

func TestConvertServiceSecrets(t *testing.T) {
namespace := &Namespace{name: "foo"}
namespace := Namespace{name: "foo"}
secrets := []composetypes.ServiceSecretConfig{
{Source: "foo_secret"},
{Source: "bar_secret"},
Expand All @@ -451,32 +451,104 @@ func TestConvertServiceSecrets(t *testing.T) {
Name: "bar_secret",
},
}
client := &fakeSecretClient{
secretListFunc: func(opts types.SecretListOptions) ([]swarm.Secret, error) {
assert.Equal(t, opts.Filters.Get("name"), []string{"foo_secret","bar_secret"})
client := &fakeClient{
secretListFunc: func(opts types.SecretListOptions) ([]swarm.Secret, error) {
assert.Equal(t, opts.Filters.Get("name"), []string{"foo_secret", "bar_secret"})
return []swarm.Secret{
{Spec{Annotations{Name"foo_secret"}}},
{Spec{Annotations{Name"bar_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"},
{SecretName: "foo_secret"},
{
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)
}

type fakeSecretClient struct {
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": composetypes.ConfigObjConfig{
Name: "foo_config",
},
"bar_config": composetypes.ConfigObjConfig{
Name: "bar_config",
},
}
client := &fakeClient{
configListFunc: func(opts types.ConfigListOptions) ([]swarm.Config, error) {
assert.Equal(t, opts.Filters.Get("name"), []string{"foo_config", "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 *fakeSecretClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, 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
}

0 comments on commit 0dda75b

Please sign in to comment.