Skip to content

Commit

Permalink
add unit tests to stack package
Browse files Browse the repository at this point in the history
Signed-off-by: Arash Deshmeh <[email protected]>
  • Loading branch information
adshmh committed Jun 20, 2017
1 parent 5dd3073 commit 535af2d
Show file tree
Hide file tree
Showing 25 changed files with 703 additions and 18 deletions.
6 changes: 3 additions & 3 deletions cli/command/node/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func TestNodePs(t *testing.T) {
},
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{
*Task(TaskID("taskID1"), ServiceID("failure"),
*Task(TaskID("taskID1"), TaskServiceID("failure"),
WithStatus(Timestamp(time.Now().Add(-2*time.Hour)), StatusErr("a task error"))),
*Task(TaskID("taskID2"), ServiceID("failure"),
*Task(TaskID("taskID2"), TaskServiceID("failure"),
WithStatus(Timestamp(time.Now().Add(-3*time.Hour)), StatusErr("a task error"))),
*Task(TaskID("taskID3"), ServiceID("failure"),
*Task(TaskID("taskID3"), TaskServiceID("failure"),
WithStatus(Timestamp(time.Now().Add(-4*time.Hour)), StatusErr("a task error"))),
}, nil
},
Expand Down
40 changes: 32 additions & 8 deletions cli/command/stack/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ type fakeClient struct {
removedSecrets []string
removedConfigs []string

serviceListFunc func(options types.ServiceListOptions) ([]swarm.Service, error)
networkListFunc func(options types.NetworkListOptions) ([]types.NetworkResource, error)
secretListFunc func(options types.SecretListOptions) ([]swarm.Secret, error)
configListFunc func(options types.ConfigListOptions) ([]swarm.Config, error)
serviceRemoveFunc func(serviceID string) error
networkRemoveFunc func(networkID string) error
secretRemoveFunc func(secretID string) error
configRemoveFunc func(configID string) error
serviceListFunc func(options types.ServiceListOptions) ([]swarm.Service, error)
networkListFunc func(options types.NetworkListOptions) ([]types.NetworkResource, error)
secretListFunc func(options types.SecretListOptions) ([]swarm.Secret, error)
configListFunc func(options types.ConfigListOptions) ([]swarm.Config, error)
nodeListFunc func(options types.NodeListOptions) ([]swarm.Node, error)
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
nodeInspectWithRaw func(ref string) (swarm.Node, []byte, error)
serviceRemoveFunc func(serviceID string) error
networkRemoveFunc func(networkID string) error
secretRemoveFunc func(secretID string) error
configRemoveFunc func(configID string) error
}

func (cli *fakeClient) ServerVersion(ctx context.Context) (types.Version, error) {
Expand Down Expand Up @@ -102,6 +105,27 @@ func (cli *fakeClient) ConfigList(ctx context.Context, options types.ConfigListO
return configsList, nil
}

func (cli *fakeClient) TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) {
if cli.taskListFunc != nil {
return cli.taskListFunc(options)
}
return []swarm.Task{}, nil
}

func (cli *fakeClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) {
if cli.nodeListFunc != nil {
return cli.nodeListFunc(options)
}
return []swarm.Node{}, nil
}

func (cli *fakeClient) NodeInspectWithRaw(ctx context.Context, ref string) (swarm.Node, []byte, error) {
if cli.nodeInspectWithRaw != nil {
return cli.nodeInspectWithRaw(ref)
}
return swarm.Node{}, nil, nil
}

func (cli *fakeClient) ServiceRemove(ctx context.Context, serviceID string) error {
if cli.serviceRemoveFunc != nil {
return cli.serviceRemoveFunc(serviceID)
Expand Down
4 changes: 2 additions & 2 deletions cli/command/stack/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type listOptions struct {
format string
}

func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
func newListCommand(dockerCli command.Cli) *cobra.Command {
opts := listOptions{}

cmd := &cobra.Command{
Expand All @@ -36,7 +36,7 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
return cmd
}

func runList(dockerCli *command.DockerCli, opts listOptions) error {
func runList(dockerCli command.Cli, opts listOptions) error {
client := dockerCli.Client()
ctx := context.Background()

Expand Down
122 changes: 122 additions & 0 deletions cli/command/stack/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package stack

import (
"bytes"
"io/ioutil"
"testing"

"github.com/docker/cli/cli/internal/test"
// Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

func TestListErrors(t *testing.T) {
testCases := []struct {
args []string
flags map[string]string
serviceListFunc func(options types.ServiceListOptions) ([]swarm.Service, error)
expectedError string
}{
{
args: []string{"foo"},
expectedError: "accepts no argument",
},
{
flags: map[string]string{
"format": "{{invalid format}}",
},
expectedError: "Template parsing error",
},
{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{}, errors.Errorf("error getting services")
},
expectedError: "error getting services",
},
{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{*Service()}, nil
},
expectedError: "cannot get label",
},
}

for _, tc := range testCases {
cmd := newListCommand(test.NewFakeCli(&fakeClient{
serviceListFunc: tc.serviceListFunc,
}, &bytes.Buffer{}))
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}

func TestListWithFormat(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newListCommand(test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{
*Service(
ServiceLabels(map[string]string{
"com.docker.stack.namespace": "service-name-foo",
}),
)}, nil
},
}, buf))
cmd.Flags().Set("format", "{{ .Name }}")
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), "stack-list-with-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
}

func TestListWithoutFormat(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newListCommand(test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{
*Service(
ServiceLabels(map[string]string{
"com.docker.stack.namespace": "service-name-foo",
}),
)}, nil
},
}, buf))
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), "stack-list-without-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
}

func TestListOrder(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newListCommand(test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{
*Service(
ServiceLabels(map[string]string{
"com.docker.stack.namespace": "service-name-foo",
}),
),
*Service(
ServiceLabels(map[string]string{
"com.docker.stack.namespace": "service-name-bar",
}),
),
}, nil
},
}, buf))
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), "stack-list-sort.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
}
49 changes: 49 additions & 0 deletions cli/command/stack/opts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package stack

import (
"bytes"
"fmt"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLoadBundlefileErrors(t *testing.T) {
testCases := []struct {
namespace string
path string
expectedError error
}{
{
namespace: "namespace_foo",
expectedError: fmt.Errorf("Bundle %s.dab not found", "namespace_foo"),
},
{
namespace: "namespace_foo",
path: "invalid_path",
expectedError: fmt.Errorf("Bundle %s not found", "invalid_path"),
},
{
namespace: "namespace_foo",
path: filepath.Join("testdata", "bundlefile_with_invalid_syntax"),
expectedError: fmt.Errorf("Error reading"),
},
}

for _, tc := range testCases {
_, err := loadBundlefile(&bytes.Buffer{}, tc.namespace, tc.path)
assert.Error(t, err, tc.expectedError)
}
}

func TestLoadBundlefile(t *testing.T) {
buf := new(bytes.Buffer)

namespace := ""
path := filepath.Join("testdata", "bundlefile_with_two_services.dab")
bundleFile, err := loadBundlefile(buf, namespace, path)

assert.NoError(t, err)
assert.Equal(t, len(bundleFile.Services), 2)
}
Loading

0 comments on commit 535af2d

Please sign in to comment.