2017-06-20 14:00:01 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
2022-02-25 08:31:31 -05:00
|
|
|
"io"
|
2017-06-20 14:00:01 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2023-10-23 08:51:01 -04:00
|
|
|
"github.com/docker/cli/internal/test/builders"
|
2017-06-20 14:00:01 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-06-20 14:00:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStackServicesErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
serviceListFunc func(options types.ServiceListOptions) ([]swarm.Service, error)
|
|
|
|
nodeListFunc func(options types.NodeListOptions) ([]swarm.Node, error)
|
|
|
|
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return nil, errors.Errorf("error getting services")
|
|
|
|
},
|
|
|
|
expectedError: "error getting services",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return []swarm.Service{*builders.Service(builders.GlobalService())}, nil
|
2017-06-20 14:00:01 -04:00
|
|
|
},
|
|
|
|
nodeListFunc: func(options types.NodeListOptions) ([]swarm.Node, error) {
|
|
|
|
return nil, errors.Errorf("error getting nodes")
|
|
|
|
},
|
2019-10-28 07:12:28 -04:00
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return []swarm.Task{*builders.Task()}, nil
|
2019-10-28 07:12:28 -04:00
|
|
|
},
|
2017-06-20 14:00:01 -04:00
|
|
|
expectedError: "error getting nodes",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return []swarm.Service{*builders.Service(builders.GlobalService())}, nil
|
2017-06-20 14:00:01 -04:00
|
|
|
},
|
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return nil, errors.Errorf("error getting tasks")
|
|
|
|
},
|
|
|
|
expectedError: "error getting tasks",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"format": "{{invalid format}}",
|
|
|
|
},
|
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return []swarm.Service{*builders.Service()}, nil
|
2017-06-20 14:00:01 -04:00
|
|
|
},
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
expectedError: "template parsing error",
|
2017-06-20 14:00:01 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2019-10-28 07:12:28 -04:00
|
|
|
t.Run(tc.expectedError, func(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
serviceListFunc: tc.serviceListFunc,
|
|
|
|
nodeListFunc: tc.nodeListFunc,
|
|
|
|
taskListFunc: tc.taskListFunc,
|
|
|
|
})
|
2022-02-22 07:46:35 -05:00
|
|
|
cmd := newServicesCommand(cli)
|
2019-10-28 07:12:28 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set(key, value))
|
2019-10-28 07:12:28 -04:00
|
|
|
}
|
2022-02-25 08:31:31 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
test spring-cleaning
This makes a quick pass through our tests;
Discard output/err
----------------------------------------------
Many tests were testing for error-conditions, but didn't discard output.
This produced a lot of noise when running the tests, and made it hard
to discover if there were actual failures, or if the output was expected.
For example:
=== RUN TestConfigCreateErrors
Error: "create" requires exactly 2 arguments.
See 'create --help'.
Usage: create [OPTIONS] CONFIG file|- [flags]
Create a config from a file or STDIN
Error: "create" requires exactly 2 arguments.
See 'create --help'.
Usage: create [OPTIONS] CONFIG file|- [flags]
Create a config from a file or STDIN
Error: error creating config
--- PASS: TestConfigCreateErrors (0.00s)
And after discarding output:
=== RUN TestConfigCreateErrors
--- PASS: TestConfigCreateErrors (0.00s)
Use sub-tests where possible
----------------------------------------------
Some tests were already set-up to use test-tables, and even had a usable
name (or in some cases "error" to check for). Change them to actual sub-
tests. Same test as above, but now with sub-tests and output discarded:
=== RUN TestConfigCreateErrors
=== RUN TestConfigCreateErrors/requires_exactly_2_arguments
=== RUN TestConfigCreateErrors/requires_exactly_2_arguments#01
=== RUN TestConfigCreateErrors/error_creating_config
--- PASS: TestConfigCreateErrors (0.00s)
--- PASS: TestConfigCreateErrors/requires_exactly_2_arguments (0.00s)
--- PASS: TestConfigCreateErrors/requires_exactly_2_arguments#01 (0.00s)
--- PASS: TestConfigCreateErrors/error_creating_config (0.00s)
PASS
It's not perfect in all cases (in the above, there's duplicate "expected"
errors, but Go conveniently adds "#01" for the duplicate). There's probably
also various tests I missed that could still use the same changes applied;
we can improve these in follow-ups.
Set cmd.Args to prevent test-failures
----------------------------------------------
When running tests from my IDE, it compiles the tests before running,
then executes the compiled binary to run the tests. Cobra doesn't like
that, because in that situation `os.Args` is taken as argument for the
command that's executed. The command that's tested now sees the test-
flags as arguments (`-test.v -test.run ..`), which causes various tests
to fail ("Command XYZ does not accept arguments").
# compile the tests:
go test -c -o foo.test
# execute the test:
./foo.test -test.v -test.run TestFoo
=== RUN TestFoo
Error: "foo" accepts no arguments.
The Cobra maintainers ran into the same situation, and for their own
use have added a special case to ignore `os.Args` in these cases;
https://github.com/spf13/cobra/blob/v1.8.1/command.go#L1078-L1083
args := c.args
// Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155
if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {
args = os.Args[1:]
}
Unfortunately, that exception is too specific (only checks for `cobra.test`),
so doesn't automatically fix the issue for other test-binaries. They did
provide a `cmd.SetArgs()` utility for this purpose
https://github.com/spf13/cobra/blob/v1.8.1/command.go#L276-L280
// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
// particularly useful when testing.
func (c *Command) SetArgs(a []string) {
c.args = a
}
And the fix is to explicitly set the command's args to an empty slice to
prevent Cobra from falling back to using `os.Args[1:]` as arguments.
cmd := newSomeThingCommand()
cmd.SetArgs([]string{})
Some tests already take this issue into account, and I updated some tests
for this, but there's likely many other ones that can use the same treatment.
Perhaps the Cobra maintainers would accept a contribution to make their
condition less specific and to look for binaries ending with a `.test`
suffix (which is what compiled binaries usually are named as).
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-07-03 19:29:04 -04:00
|
|
|
cmd.SetErr(io.Discard)
|
2019-10-28 07:12:28 -04:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-07-05 14:34:16 -04:00
|
|
|
})
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 08:07:26 -04:00
|
|
|
func TestRunServicesWithEmptyName(t *testing.T) {
|
2022-02-22 07:46:35 -05:00
|
|
|
cmd := newServicesCommand(test.NewFakeCli(&fakeClient{}))
|
2018-06-26 08:07:26 -04:00
|
|
|
cmd.SetArgs([]string{"' '"})
|
2022-02-25 08:31:31 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
test spring-cleaning
This makes a quick pass through our tests;
Discard output/err
----------------------------------------------
Many tests were testing for error-conditions, but didn't discard output.
This produced a lot of noise when running the tests, and made it hard
to discover if there were actual failures, or if the output was expected.
For example:
=== RUN TestConfigCreateErrors
Error: "create" requires exactly 2 arguments.
See 'create --help'.
Usage: create [OPTIONS] CONFIG file|- [flags]
Create a config from a file or STDIN
Error: "create" requires exactly 2 arguments.
See 'create --help'.
Usage: create [OPTIONS] CONFIG file|- [flags]
Create a config from a file or STDIN
Error: error creating config
--- PASS: TestConfigCreateErrors (0.00s)
And after discarding output:
=== RUN TestConfigCreateErrors
--- PASS: TestConfigCreateErrors (0.00s)
Use sub-tests where possible
----------------------------------------------
Some tests were already set-up to use test-tables, and even had a usable
name (or in some cases "error" to check for). Change them to actual sub-
tests. Same test as above, but now with sub-tests and output discarded:
=== RUN TestConfigCreateErrors
=== RUN TestConfigCreateErrors/requires_exactly_2_arguments
=== RUN TestConfigCreateErrors/requires_exactly_2_arguments#01
=== RUN TestConfigCreateErrors/error_creating_config
--- PASS: TestConfigCreateErrors (0.00s)
--- PASS: TestConfigCreateErrors/requires_exactly_2_arguments (0.00s)
--- PASS: TestConfigCreateErrors/requires_exactly_2_arguments#01 (0.00s)
--- PASS: TestConfigCreateErrors/error_creating_config (0.00s)
PASS
It's not perfect in all cases (in the above, there's duplicate "expected"
errors, but Go conveniently adds "#01" for the duplicate). There's probably
also various tests I missed that could still use the same changes applied;
we can improve these in follow-ups.
Set cmd.Args to prevent test-failures
----------------------------------------------
When running tests from my IDE, it compiles the tests before running,
then executes the compiled binary to run the tests. Cobra doesn't like
that, because in that situation `os.Args` is taken as argument for the
command that's executed. The command that's tested now sees the test-
flags as arguments (`-test.v -test.run ..`), which causes various tests
to fail ("Command XYZ does not accept arguments").
# compile the tests:
go test -c -o foo.test
# execute the test:
./foo.test -test.v -test.run TestFoo
=== RUN TestFoo
Error: "foo" accepts no arguments.
The Cobra maintainers ran into the same situation, and for their own
use have added a special case to ignore `os.Args` in these cases;
https://github.com/spf13/cobra/blob/v1.8.1/command.go#L1078-L1083
args := c.args
// Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155
if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {
args = os.Args[1:]
}
Unfortunately, that exception is too specific (only checks for `cobra.test`),
so doesn't automatically fix the issue for other test-binaries. They did
provide a `cmd.SetArgs()` utility for this purpose
https://github.com/spf13/cobra/blob/v1.8.1/command.go#L276-L280
// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
// particularly useful when testing.
func (c *Command) SetArgs(a []string) {
c.args = a
}
And the fix is to explicitly set the command's args to an empty slice to
prevent Cobra from falling back to using `os.Args[1:]` as arguments.
cmd := newSomeThingCommand()
cmd.SetArgs([]string{})
Some tests already take this issue into account, and I updated some tests
for this, but there's likely many other ones that can use the same treatment.
Perhaps the Cobra maintainers would accept a contribution to make their
condition less specific and to look for binaries ending with a `.test`
suffix (which is what compiled binaries usually are named as).
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-07-03 19:29:04 -04:00
|
|
|
cmd.SetErr(io.Discard)
|
2018-06-26 08:07:26 -04:00
|
|
|
|
|
|
|
assert.ErrorContains(t, cmd.Execute(), `invalid stack name: "' '"`)
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:00:01 -04:00
|
|
|
func TestStackServicesEmptyServiceList(t *testing.T) {
|
2017-07-05 13:32:54 -04:00
|
|
|
fakeCli := test.NewFakeCli(&fakeClient{
|
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return []swarm.Service{}, nil
|
|
|
|
},
|
2017-07-05 14:19:52 -04:00
|
|
|
})
|
2022-02-22 07:46:35 -05:00
|
|
|
cmd := newServicesCommand(fakeCli)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("", fakeCli.OutBuffer().String()))
|
|
|
|
assert.Check(t, is.Equal("Nothing found in stack: foo\n", fakeCli.ErrBuffer().String()))
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackServicesWithQuietOption(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return []swarm.Service{*builders.Service(builders.ServiceID("id-foo"))}, nil
|
2017-06-20 14:00:01 -04:00
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2022-02-22 07:46:35 -05:00
|
|
|
cmd := newServicesCommand(cli)
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set("quiet", "true"))
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-services-with-quiet-option.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackServicesWithFormat(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return []swarm.Service{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Service(builders.ServiceName("service-name-foo")),
|
2017-06-20 14:00:01 -04:00
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2022-02-22 07:46:35 -05:00
|
|
|
cmd := newServicesCommand(cli)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set("format", "{{ .Name }}"))
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-services-with-format.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackServicesWithConfigFormat(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
return []swarm.Service{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Service(builders.ServiceName("service-name-foo")),
|
2017-06-20 14:00:01 -04:00
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-07-05 14:34:16 -04:00
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
2017-06-20 14:00:01 -04:00
|
|
|
ServicesFormat: "{{ .Name }}",
|
|
|
|
})
|
2022-02-22 07:46:35 -05:00
|
|
|
cmd := newServicesCommand(cli)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-services-with-config-format.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackServicesWithoutFormat(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return []swarm.Service{*builders.Service(
|
|
|
|
builders.ServiceName("name-foo"),
|
|
|
|
builders.ServiceID("id-foo"),
|
|
|
|
builders.ReplicatedService(2),
|
|
|
|
builders.ServiceImage("busybox:latest"),
|
|
|
|
builders.ServicePort(swarm.PortConfig{
|
2017-06-20 14:00:01 -04:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
|
|
|
PublishedPort: 0,
|
|
|
|
TargetPort: 3232,
|
|
|
|
Protocol: swarm.PortConfigProtocolTCP,
|
|
|
|
}),
|
|
|
|
)}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2022-02-22 07:46:35 -05:00
|
|
|
cmd := newServicesCommand(cli)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-services-without-format.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|