2016-12-05 16:14:08 -05:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
2016-12-05 16:14:08 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/fs"
|
2016-12-05 16:14:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNamespaceScope(t *testing.T) {
|
|
|
|
scoped := Namespace{name: "foo"}.Scope("bar")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("foo_bar", scoped))
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2024-02-20 04:46:05 -05:00
|
|
|
func TestNamespaceDescope(t *testing.T) {
|
|
|
|
descoped := Namespace{name: "foo"}.Descope("foo_bar")
|
|
|
|
assert.Check(t, is.Equal("bar", descoped))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNamespaceName(t *testing.T) {
|
|
|
|
namespaceName := Namespace{name: "foo"}.Name()
|
|
|
|
assert.Check(t, is.Equal("foo", namespaceName))
|
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func TestAddStackLabel(t *testing.T) {
|
|
|
|
labels := map[string]string{
|
|
|
|
"something": "labeled",
|
|
|
|
}
|
|
|
|
actual := AddStackLabel(Namespace{name: "foo"}, labels)
|
|
|
|
expected := map[string]string{
|
|
|
|
"something": "labeled",
|
|
|
|
LabelNamespace: "foo",
|
|
|
|
}
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expected, actual))
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworks(t *testing.T) {
|
|
|
|
namespace := Namespace{name: "foo"}
|
2017-01-10 03:57:36 -05:00
|
|
|
serviceNetworks := map[string]struct{}{
|
2017-02-04 16:55:28 -05:00
|
|
|
"normal": {},
|
|
|
|
"outside": {},
|
|
|
|
"default": {},
|
|
|
|
"attachablenet": {},
|
2018-03-12 13:09:59 -04:00
|
|
|
"named": {},
|
2017-01-10 03:57:36 -05:00
|
|
|
}
|
2016-12-05 16:14:08 -05:00
|
|
|
source := networkMap{
|
|
|
|
"normal": composetypes.NetworkConfig{
|
|
|
|
Driver: "overlay",
|
|
|
|
DriverOpts: map[string]string{
|
|
|
|
"opt": "value",
|
|
|
|
},
|
|
|
|
Ipam: composetypes.IPAMConfig{
|
|
|
|
Driver: "driver",
|
|
|
|
Config: []*composetypes.IPAMPool{
|
|
|
|
{
|
|
|
|
Subnet: "10.0.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
"something": "labeled",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"outside": composetypes.NetworkConfig{
|
2017-12-11 11:29:45 -05:00
|
|
|
External: composetypes.External{External: true},
|
|
|
|
Name: "special",
|
2016-12-05 16:14:08 -05:00
|
|
|
},
|
2017-02-04 16:55:28 -05:00
|
|
|
"attachablenet": composetypes.NetworkConfig{
|
|
|
|
Driver: "overlay",
|
|
|
|
Attachable: true,
|
|
|
|
},
|
2018-03-12 13:09:59 -04:00
|
|
|
"named": composetypes.NetworkConfig{
|
|
|
|
Name: "othername",
|
|
|
|
},
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
expected := map[string]types.NetworkCreate{
|
2018-03-12 13:09:59 -04:00
|
|
|
"foo_default": {
|
2016-12-05 16:14:08 -05:00
|
|
|
Labels: map[string]string{
|
|
|
|
LabelNamespace: "foo",
|
|
|
|
},
|
|
|
|
},
|
2018-03-12 13:09:59 -04:00
|
|
|
"foo_normal": {
|
2016-12-05 16:14:08 -05:00
|
|
|
Driver: "overlay",
|
|
|
|
IPAM: &network.IPAM{
|
|
|
|
Driver: "driver",
|
|
|
|
Config: []network.IPAMConfig{
|
|
|
|
{
|
|
|
|
Subnet: "10.0.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Options: map[string]string{
|
|
|
|
"opt": "value",
|
|
|
|
},
|
|
|
|
Labels: map[string]string{
|
|
|
|
LabelNamespace: "foo",
|
|
|
|
"something": "labeled",
|
|
|
|
},
|
|
|
|
},
|
2018-03-12 13:09:59 -04:00
|
|
|
"foo_attachablenet": {
|
2017-02-04 16:55:28 -05:00
|
|
|
Driver: "overlay",
|
|
|
|
Attachable: true,
|
|
|
|
Labels: map[string]string{
|
|
|
|
LabelNamespace: "foo",
|
|
|
|
},
|
|
|
|
},
|
2018-03-12 13:09:59 -04:00
|
|
|
"othername": {
|
|
|
|
Labels: map[string]string{LabelNamespace: "foo"},
|
|
|
|
},
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-10 03:57:36 -05:00
|
|
|
networks, externals := Networks(namespace, source, serviceNetworks)
|
2018-03-12 13:09:59 -04:00
|
|
|
assert.DeepEqual(t, expected, networks)
|
|
|
|
assert.DeepEqual(t, []string{"special"}, externals)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
2017-01-13 11:26:29 -05:00
|
|
|
|
|
|
|
func TestSecrets(t *testing.T) {
|
|
|
|
namespace := Namespace{name: "foo"}
|
|
|
|
|
|
|
|
secretText := "this is the first secret"
|
2017-08-16 14:07:49 -04:00
|
|
|
secretFile := fs.NewFile(t, "convert-secrets", fs.WithContent(secretText))
|
2017-01-13 11:26:29 -05:00
|
|
|
defer secretFile.Remove()
|
|
|
|
|
|
|
|
source := map[string]composetypes.SecretConfig{
|
|
|
|
"one": {
|
2017-08-16 14:07:49 -04:00
|
|
|
File: secretFile.Path(),
|
2017-01-13 11:26:29 -05:00
|
|
|
Labels: map[string]string{"monster": "mash"},
|
|
|
|
},
|
|
|
|
"ext": {
|
|
|
|
External: composetypes.External{
|
|
|
|
External: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
specs, err := Secrets(namespace, source)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Assert(t, is.Len(specs, 1))
|
2017-01-13 11:26:29 -05:00
|
|
|
secret := specs[0]
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("foo_one", secret.Name))
|
|
|
|
assert.Check(t, is.DeepEqual(map[string]string{
|
2017-01-13 11:26:29 -05:00
|
|
|
"monster": "mash",
|
|
|
|
LabelNamespace: "foo",
|
2018-03-05 18:53:52 -05:00
|
|
|
}, secret.Labels))
|
|
|
|
assert.Check(t, is.DeepEqual([]byte(secretText), secret.Data))
|
2017-01-13 11:26:29 -05:00
|
|
|
}
|
2017-05-15 11:19:32 -04:00
|
|
|
|
|
|
|
func TestConfigs(t *testing.T) {
|
|
|
|
namespace := Namespace{name: "foo"}
|
|
|
|
|
|
|
|
configText := "this is the first config"
|
2017-08-16 14:07:49 -04:00
|
|
|
configFile := fs.NewFile(t, "convert-configs", fs.WithContent(configText))
|
2017-05-15 11:19:32 -04:00
|
|
|
defer configFile.Remove()
|
|
|
|
|
|
|
|
source := map[string]composetypes.ConfigObjConfig{
|
|
|
|
"one": {
|
2017-08-16 14:07:49 -04:00
|
|
|
File: configFile.Path(),
|
2017-05-15 11:19:32 -04:00
|
|
|
Labels: map[string]string{"monster": "mash"},
|
|
|
|
},
|
|
|
|
"ext": {
|
|
|
|
External: composetypes.External{
|
|
|
|
External: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
specs, err := Configs(namespace, source)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Assert(t, is.Len(specs, 1))
|
2017-05-15 11:19:32 -04:00
|
|
|
config := specs[0]
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("foo_one", config.Name))
|
|
|
|
assert.Check(t, is.DeepEqual(map[string]string{
|
2017-05-15 11:19:32 -04:00
|
|
|
"monster": "mash",
|
|
|
|
LabelNamespace: "foo",
|
2018-03-05 18:53:52 -05:00
|
|
|
}, config.Labels))
|
|
|
|
assert.Check(t, is.DeepEqual([]byte(configText), config.Data))
|
2017-05-15 11:19:32 -04:00
|
|
|
}
|