2017-06-21 05:25:25 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2022-02-25 07:12:19 -05:00
|
|
|
"io"
|
2017-06-21 05:25:25 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-06-21 05:25:25 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-06-21 05:25:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkCreateErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
2024-06-06 20:31:09 -04:00
|
|
|
networkCreateFunc func(ctx context.Context, name string, options network.CreateOptions) (network.CreateResponse, error)
|
2017-06-21 05:25:25 -04:00
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
2024-07-04 20:49:31 -04:00
|
|
|
expectedError: "1 argument",
|
2017-06-21 05:25:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
2024-06-06 20:31:09 -04:00
|
|
|
networkCreateFunc: func(ctx context.Context, name string, createBody network.CreateOptions) (network.CreateResponse, error) {
|
2024-05-31 11:28:10 -04:00
|
|
|
return network.CreateResponse{}, errors.Errorf("error creating network")
|
2017-06-21 05:25:25 -04:00
|
|
|
},
|
|
|
|
expectedError: "error creating network",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"ip-range": "255.255.0.0/24",
|
|
|
|
"gateway": "255.0.255.0/24",
|
|
|
|
"subnet": "10.1.2.0.30.50",
|
|
|
|
},
|
|
|
|
expectedError: "invalid CIDR address: 10.1.2.0.30.50",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"ip-range": "255.255.0.0.30/24",
|
|
|
|
"gateway": "255.0.255.0/24",
|
|
|
|
"subnet": "255.0.0.0/24",
|
|
|
|
},
|
|
|
|
expectedError: "invalid CIDR address: 255.255.0.0.30/24",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"gateway": "255.0.0.0/24",
|
|
|
|
},
|
|
|
|
expectedError: "every ip-range or gateway must have a corresponding subnet",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"ip-range": "255.0.0.0/24",
|
|
|
|
},
|
|
|
|
expectedError: "every ip-range or gateway must have a corresponding subnet",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"ip-range": "255.0.0.0/24",
|
|
|
|
"gateway": "255.0.0.0/24",
|
|
|
|
},
|
|
|
|
expectedError: "every ip-range or gateway must have a corresponding subnet",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"ip-range": "255.255.0.0/24",
|
|
|
|
"gateway": "255.0.255.0/24",
|
|
|
|
"subnet": "10.1.2.0/23,10.1.3.248/30",
|
|
|
|
},
|
|
|
|
expectedError: "multiple overlapping subnet configuration is not supported",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"ip-range": "192.168.1.0/24,192.168.1.200/24",
|
|
|
|
"gateway": "192.168.1.1,192.168.1.4",
|
|
|
|
"subnet": "192.168.2.0/24,192.168.1.250/24",
|
|
|
|
},
|
|
|
|
expectedError: "cannot configure multiple ranges (192.168.1.200/24, 192.168.1.0/24) on the same subnet (192.168.1.250/24)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"ip-range": "255.255.200.0/24,255.255.120.0/24",
|
|
|
|
"gateway": "255.0.255.0/24",
|
|
|
|
"subnet": "255.255.255.0/24,255.255.0.255/24",
|
|
|
|
},
|
|
|
|
expectedError: "no matching subnet for range 255.255.200.0/24",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"ip-range": "192.168.1.0/24",
|
|
|
|
"gateway": "192.168.1.1,192.168.1.4",
|
|
|
|
"subnet": "192.168.2.0/24,192.168.1.250/24",
|
|
|
|
},
|
|
|
|
expectedError: "cannot configure multiple gateways (192.168.1.4, 192.168.1.1) for the same subnet (192.168.1.250/24)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"ip-range": "192.168.1.0/24",
|
|
|
|
"gateway": "192.168.4.1,192.168.5.4",
|
|
|
|
"subnet": "192.168.2.0/24,192.168.1.250/24",
|
|
|
|
},
|
|
|
|
expectedError: "no matching subnet for gateway 192.168.4.1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"toto"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"gateway": "255.255.0.0/24",
|
|
|
|
"subnet": "255.255.0.0/24",
|
|
|
|
"aux-address": "255.255.0.30/24",
|
|
|
|
},
|
|
|
|
expectedError: "no matching subnet for aux-address",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newCreateCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{
|
|
|
|
networkCreateFunc: tc.networkCreateFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
}),
|
2017-06-21 05:25:25 -04:00
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, cmd.Flags().Set(key, value))
|
2017-06-21 05:25:25 -04:00
|
|
|
}
|
2022-02-25 07:12:19 -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-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-06-21 05:25:25 -04:00
|
|
|
}
|
|
|
|
}
|
2022-09-29 11:21:51 -04:00
|
|
|
|
2017-06-21 05:25:25 -04:00
|
|
|
func TestNetworkCreateWithFlags(t *testing.T) {
|
|
|
|
expectedDriver := "foo"
|
|
|
|
expectedOpts := []network.IPAMConfig{
|
|
|
|
{
|
2018-04-10 09:55:00 -04:00
|
|
|
Subnet: "192.168.4.0/24",
|
|
|
|
IPRange: "192.168.4.0/24",
|
|
|
|
Gateway: "192.168.4.1/24",
|
|
|
|
AuxAddress: map[string]string{},
|
2017-06-21 05:25:25 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2024-06-06 20:31:09 -04:00
|
|
|
networkCreateFunc: func(ctx context.Context, name string, options network.CreateOptions) (network.CreateResponse, error) {
|
|
|
|
assert.Check(t, is.Equal(expectedDriver, options.Driver), "not expected driver error")
|
|
|
|
assert.Check(t, is.DeepEqual(expectedOpts, options.IPAM.Config), "not expected driver error")
|
2024-05-31 11:28:10 -04:00
|
|
|
return network.CreateResponse{
|
2017-06-21 05:25:25 -04:00
|
|
|
ID: name,
|
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-06-21 05:25:25 -04:00
|
|
|
args := []string{"banana"}
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
|
|
|
|
cmd.SetArgs(args)
|
|
|
|
cmd.Flags().Set("driver", "foo")
|
|
|
|
cmd.Flags().Set("ip-range", "192.168.4.0/24")
|
|
|
|
cmd.Flags().Set("gateway", "192.168.4.1/24")
|
|
|
|
cmd.Flags().Set("subnet", "192.168.4.0/24")
|
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("banana", strings.TrimSpace(cli.OutBuffer().String())))
|
2017-06-21 05:25:25 -04:00
|
|
|
}
|
2024-06-07 04:10:15 -04:00
|
|
|
|
|
|
|
// TestNetworkCreateIPv6 verifies behavior of the "--ipv6" option. This option
|
|
|
|
// is an optional bool, and must default to "nil", not "true" or "false".
|
|
|
|
func TestNetworkCreateIPv6(t *testing.T) {
|
|
|
|
strPtr := func(val bool) *bool { return &val }
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
doc, name string
|
|
|
|
flags []string
|
|
|
|
expected *bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "IPV6 default",
|
|
|
|
name: "ipv6-default",
|
|
|
|
expected: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "IPV6 enabled",
|
|
|
|
name: "ipv6-enabled",
|
|
|
|
flags: []string{"--ipv6=true"},
|
|
|
|
expected: strPtr(true),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "IPV6 enabled (shorthand)",
|
|
|
|
name: "ipv6-enabled-shorthand",
|
|
|
|
flags: []string{"--ipv6"},
|
|
|
|
expected: strPtr(true),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "IPV6 disabled",
|
|
|
|
name: "ipv6-disabled",
|
|
|
|
flags: []string{"--ipv6=false"},
|
|
|
|
expected: strPtr(false),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2024-06-06 20:31:09 -04:00
|
|
|
networkCreateFunc: func(ctx context.Context, name string, createBody network.CreateOptions) (network.CreateResponse, error) {
|
2024-06-07 04:10:15 -04:00
|
|
|
assert.Check(t, is.DeepEqual(tc.expected, createBody.EnableIPv6))
|
|
|
|
return network.CreateResponse{ID: name}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{tc.name})
|
|
|
|
if tc.expected != nil {
|
|
|
|
assert.Check(t, cmd.ParseFlags(tc.flags))
|
|
|
|
}
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
assert.Check(t, is.Equal(tc.name, strings.TrimSpace(cli.OutBuffer().String())))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|