2017-04-12 12:42:35 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-08-22 11:41:12 -04:00
|
|
|
"strings"
|
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-08-21 16:39:35 -04:00
|
|
|
"github.com/docker/cli/internal/test/testutil"
|
2017-05-26 12:15:23 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-08-16 14:07:49 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/fs"
|
2017-05-26 12:15:23 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-04-12 12:42:35 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-05-26 12:15:23 -04:00
|
|
|
"golang.org/x/net/context"
|
2017-04-12 12:42:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetConfigDetails(t *testing.T) {
|
|
|
|
content := `
|
|
|
|
version: "3.0"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: alpine:3.5
|
|
|
|
`
|
2017-08-16 14:07:49 -04:00
|
|
|
file := fs.NewFile(t, "test-get-config-details", fs.WithContent(content))
|
2017-04-12 12:42:35 -04:00
|
|
|
defer file.Remove()
|
|
|
|
|
2017-08-22 11:41:12 -04:00
|
|
|
details, err := getConfigDetails(file.Path(), nil)
|
2017-04-12 12:42:35 -04:00
|
|
|
require.NoError(t, err)
|
2017-08-16 14:07:49 -04:00
|
|
|
assert.Equal(t, filepath.Dir(file.Path()), details.WorkingDir)
|
2017-08-22 11:41:12 -04:00
|
|
|
require.Len(t, details.ConfigFiles, 1)
|
|
|
|
assert.Equal(t, "3.0", details.ConfigFiles[0].Config["version"])
|
|
|
|
assert.Len(t, details.Environment, len(os.Environ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetConfigDetailsStdin(t *testing.T) {
|
|
|
|
content := `
|
|
|
|
version: "3.0"
|
|
|
|
services:
|
|
|
|
foo:
|
|
|
|
image: alpine:3.5
|
|
|
|
`
|
|
|
|
details, err := getConfigDetails("-", strings.NewReader(content))
|
|
|
|
require.NoError(t, err)
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, cwd, details.WorkingDir)
|
|
|
|
require.Len(t, details.ConfigFiles, 1)
|
|
|
|
assert.Equal(t, "3.0", details.ConfigFiles[0].Config["version"])
|
2017-04-12 12:42:35 -04:00
|
|
|
assert.Len(t, details.Environment, len(os.Environ()))
|
|
|
|
}
|
2017-05-26 12:15:23 -04:00
|
|
|
|
|
|
|
type notFound struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n notFound) NotFound() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateExternalNetworks(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
{
|
2017-07-20 12:05:20 -04:00
|
|
|
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 == "" {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
testutil.ErrorContains(t, err, testcase.expectedMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|