forked from docker/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Arash Deshmeh <[email protected]>
- Loading branch information
Showing
25 changed files
with
703 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.