2017-11-20 09:30:52 -05:00
|
|
|
package swarm
|
2017-04-12 12:42:35 -04:00
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-04-12 12:42:35 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test/network"
|
2024-05-31 11:28:10 -04:00
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
2017-05-26 12:15:23 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2017-04-12 12:42:35 -04:00
|
|
|
)
|
|
|
|
|
2017-05-26 12:15:23 -04:00
|
|
|
type notFound struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
2023-05-10 16:37:32 -04:00
|
|
|
func (n notFound) NotFound() {}
|
2017-05-26 12:15:23 -04:00
|
|
|
|
|
|
|
func TestValidateExternalNetworks(t *testing.T) {
|
2022-09-29 11:21:51 -04:00
|
|
|
testcases := []struct {
|
2024-06-05 10:28:24 -04:00
|
|
|
inspectResponse networktypes.Inspect
|
2017-05-26 12:15:23 -04:00
|
|
|
inspectError error
|
|
|
|
expectedMsg string
|
|
|
|
network string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
inspectError: notFound{},
|
|
|
|
expectedMsg: "could not be found. You need to create a swarm-scoped network",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
inspectError: errors.New("Unexpected"),
|
|
|
|
expectedMsg: "Unexpected",
|
|
|
|
},
|
2018-02-27 10:54:36 -05:00
|
|
|
// FIXME(vdemeester) that doesn't work under windows, the check needs to be smarter
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
inspectError: errors.New("host net does not exist on swarm classic"),
|
|
|
|
network: "host",
|
|
|
|
},
|
|
|
|
*/
|
2017-05-26 12:15:23 -04:00
|
|
|
{
|
|
|
|
network: "user",
|
|
|
|
expectedMsg: "is not in the right scope",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
network: "user",
|
2024-06-05 10:28:24 -04:00
|
|
|
inspectResponse: networktypes.Inspect{Scope: "swarm"},
|
2017-05-26 12:15:23 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
fakeClient := &network.FakeClient{
|
2024-06-05 10:28:24 -04:00
|
|
|
NetworkInspectFunc: func(_ context.Context, _ string, _ networktypes.InspectOptions) (networktypes.Inspect, error) {
|
2017-05-26 12:15:23 -04:00
|
|
|
return testcase.inspectResponse, testcase.inspectError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
networks := []string{testcase.network}
|
|
|
|
err := validateExternalNetworks(context.Background(), fakeClient, networks)
|
|
|
|
if testcase.expectedMsg == "" {
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-05-26 12:15:23 -04:00
|
|
|
} else {
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, testcase.expectedMsg)
|
2017-05-26 12:15:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|