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"
|
2017-05-26 12:15:23 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n notFound) NotFound() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateExternalNetworks(t *testing.T) {
|
2022-09-29 11:21:51 -04:00
|
|
|
testcases := []struct {
|
2017-05-26 12:15:23 -04:00
|
|
|
inspectResponse types.NetworkResource
|
|
|
|
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",
|
|
|
|
inspectResponse: types.NetworkResource{Scope: "swarm"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
fakeClient := &network.FakeClient{
|
2017-06-12 22:53:53 -04:00
|
|
|
NetworkInspectFunc: func(_ context.Context, _ string, _ types.NetworkInspectOptions) (types.NetworkResource, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|