2016-12-05 16:14:08 -05:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2017-11-10 16:24:32 -05:00
|
|
|
"os"
|
2016-12-05 16:14:08 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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/container"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-11-10 16:24:32 -05:00
|
|
|
"github.com/pkg/errors"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-11-10 16:24:32 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-05 16:14:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConvertRestartPolicyFromNone(t *testing.T) {
|
|
|
|
policy, err := convertRestartPolicy("no", nil)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, (*swarm.RestartPolicy)(nil), policy)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertRestartPolicyFromUnknown(t *testing.T) {
|
|
|
|
_, err := convertRestartPolicy("unknown", nil)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.EqualError(t, err, "unknown restart policy: unknown")
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertRestartPolicyFromAlways(t *testing.T) {
|
|
|
|
policy, err := convertRestartPolicy("always", nil)
|
|
|
|
expected := &swarm.RestartPolicy{
|
|
|
|
Condition: swarm.RestartPolicyConditionAny,
|
|
|
|
}
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, policy)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertRestartPolicyFromFailure(t *testing.T) {
|
|
|
|
policy, err := convertRestartPolicy("on-failure:4", nil)
|
|
|
|
attempts := uint64(4)
|
|
|
|
expected := &swarm.RestartPolicy{
|
|
|
|
Condition: swarm.RestartPolicyConditionOnFailure,
|
|
|
|
MaxAttempts: &attempts,
|
|
|
|
}
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, policy)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-03-14 12:39:26 -04:00
|
|
|
func strPtr(val string) *string {
|
|
|
|
return &val
|
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func TestConvertEnvironment(t *testing.T) {
|
2017-03-14 12:39:26 -04:00
|
|
|
source := map[string]*string{
|
|
|
|
"foo": strPtr("bar"),
|
|
|
|
"key": strPtr("value"),
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
env := convertEnvironment(source)
|
|
|
|
sort.Strings(env)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, []string{"foo=bar", "key=value"}, env)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
Preserve sort-order of extra hosts, and allow duplicate entries
Extra hosts (`extra_hosts` in compose-file, or `--hosts` in services) adds
custom host/ip mappings to the container's `/etc/hosts`.
The current implementation used a `map[string]string{}` as intermediate
storage, and sorted the results alphabetically when converting to a service-spec.
As a result, duplicate hosts were removed, and order of host/ip mappings was not
preserved (in case the compose-file used a list instead of a map).
According to the **host.conf(5)** man page (http://man7.org/linux/man-pages/man5/host.conf.5.html)
multi Valid values are on and off. If set to on, the resolver
library will return all valid addresses for a host that
appears in the /etc/hosts file, instead of only the first.
This is off by default, as it may cause a substantial
performance loss at sites with large hosts files.
Multiple entries for a host are allowed, and even required for some situations,
for example, to add mappings for IPv4 and IPv6 addreses for a host, as illustrated
by the example hosts file in the **hosts(5)** man page (http://man7.org/linux/man-pages/man5/hosts.5.html):
# The following lines are desirable for IPv4 capable hosts
127.0.0.1 localhost
# 127.0.1.1 is often used for the FQDN of the machine
127.0.1.1 thishost.mydomain.org thishost
192.168.1.10 foo.mydomain.org foo
192.168.1.13 bar.mydomain.org bar
146.82.138.7 master.debian.org master
209.237.226.90 www.opensource.org
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
This patch changes the intermediate storage format to use a `[]string`, and only
sorts entries if the input format in the compose file is a mapping. If the input
format is a list, the original sort-order is preserved.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-10-29 20:33:23 -04:00
|
|
|
func TestConvertExtraHosts(t *testing.T) {
|
|
|
|
source := composetypes.HostsList{
|
|
|
|
"zulu:127.0.0.2",
|
|
|
|
"alpha:127.0.0.1",
|
|
|
|
"zulu:ff02::1",
|
|
|
|
}
|
|
|
|
assert.Equal(t, []string{"127.0.0.2 zulu", "127.0.0.1 alpha", "ff02::1 zulu"}, convertExtraHosts(source))
|
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func TestConvertResourcesFull(t *testing.T) {
|
|
|
|
source := composetypes.Resources{
|
|
|
|
Limits: &composetypes.Resource{
|
|
|
|
NanoCPUs: "0.003",
|
|
|
|
MemoryBytes: composetypes.UnitBytes(300000000),
|
|
|
|
},
|
|
|
|
Reservations: &composetypes.Resource{
|
|
|
|
NanoCPUs: "0.002",
|
|
|
|
MemoryBytes: composetypes.UnitBytes(200000000),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resources, err := convertResources(source)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
2016-12-05 16:14:08 -05:00
|
|
|
|
|
|
|
expected := &swarm.ResourceRequirements{
|
|
|
|
Limits: &swarm.Resources{
|
|
|
|
NanoCPUs: 3000000,
|
|
|
|
MemoryBytes: 300000000,
|
|
|
|
},
|
|
|
|
Reservations: &swarm.Resources{
|
|
|
|
NanoCPUs: 2000000,
|
|
|
|
MemoryBytes: 200000000,
|
|
|
|
},
|
|
|
|
}
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, expected, resources)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-09 14:22:02 -05:00
|
|
|
func TestConvertResourcesOnlyMemory(t *testing.T) {
|
|
|
|
source := composetypes.Resources{
|
|
|
|
Limits: &composetypes.Resource{
|
|
|
|
MemoryBytes: composetypes.UnitBytes(300000000),
|
|
|
|
},
|
|
|
|
Reservations: &composetypes.Resource{
|
|
|
|
MemoryBytes: composetypes.UnitBytes(200000000),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resources, err := convertResources(source)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
2017-01-09 14:22:02 -05:00
|
|
|
|
|
|
|
expected := &swarm.ResourceRequirements{
|
|
|
|
Limits: &swarm.Resources{
|
|
|
|
MemoryBytes: 300000000,
|
|
|
|
},
|
|
|
|
Reservations: &swarm.Resources{
|
|
|
|
MemoryBytes: 200000000,
|
|
|
|
},
|
|
|
|
}
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, expected, resources)
|
2017-01-09 14:22:02 -05:00
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func TestConvertHealthcheck(t *testing.T) {
|
|
|
|
retries := uint64(10)
|
2017-08-29 21:09:06 -04:00
|
|
|
timeout := 30 * time.Second
|
|
|
|
interval := 2 * time.Millisecond
|
2016-12-05 16:14:08 -05:00
|
|
|
source := &composetypes.HealthCheckConfig{
|
|
|
|
Test: []string{"EXEC", "touch", "/foo"},
|
2017-08-29 21:09:06 -04:00
|
|
|
Timeout: &timeout,
|
|
|
|
Interval: &interval,
|
2016-12-05 16:14:08 -05:00
|
|
|
Retries: &retries,
|
|
|
|
}
|
|
|
|
expected := &container.HealthConfig{
|
|
|
|
Test: source.Test,
|
2017-08-29 21:09:06 -04:00
|
|
|
Timeout: timeout,
|
|
|
|
Interval: interval,
|
2016-12-05 16:14:08 -05:00
|
|
|
Retries: 10,
|
|
|
|
}
|
|
|
|
|
|
|
|
healthcheck, err := convertHealthcheck(source)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, healthcheck)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertHealthcheckDisable(t *testing.T) {
|
|
|
|
source := &composetypes.HealthCheckConfig{Disable: true}
|
|
|
|
expected := &container.HealthConfig{
|
|
|
|
Test: []string{"NONE"},
|
|
|
|
}
|
|
|
|
|
|
|
|
healthcheck, err := convertHealthcheck(source)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, healthcheck)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertHealthcheckDisableWithTest(t *testing.T) {
|
|
|
|
source := &composetypes.HealthCheckConfig{
|
|
|
|
Disable: true,
|
|
|
|
Test: []string{"EXEC", "touch"},
|
|
|
|
}
|
|
|
|
_, err := convertHealthcheck(source)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.EqualError(t, err, "test and disable can't be set at the same time")
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-31 15:45:45 -05:00
|
|
|
func TestConvertEndpointSpec(t *testing.T) {
|
|
|
|
source := []composetypes.ServicePortConfig{
|
|
|
|
{
|
|
|
|
Protocol: "udp",
|
|
|
|
Target: 53,
|
|
|
|
Published: 1053,
|
|
|
|
Mode: "host",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Target: 8080,
|
|
|
|
Published: 80,
|
|
|
|
},
|
|
|
|
}
|
2017-02-17 00:34:49 -05:00
|
|
|
endpoint, err := convertEndpointSpec("vip", source)
|
2017-01-31 15:45:45 -05:00
|
|
|
|
|
|
|
expected := swarm.EndpointSpec{
|
2017-02-17 00:34:49 -05:00
|
|
|
Mode: swarm.ResolutionMode(strings.ToLower("vip")),
|
2017-01-31 15:45:45 -05:00
|
|
|
Ports: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
TargetPort: 8080,
|
|
|
|
PublishedPort: 80,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Protocol: "udp",
|
|
|
|
TargetPort: 53,
|
|
|
|
PublishedPort: 1053,
|
|
|
|
PublishMode: "host",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, *endpoint)
|
2017-01-31 15:45:45 -05:00
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
func TestConvertServiceNetworksOnlyDefault(t *testing.T) {
|
|
|
|
networkConfigs := networkMap{}
|
|
|
|
|
|
|
|
configs, err := convertServiceNetworks(
|
2017-02-22 13:52:09 -05:00
|
|
|
nil, networkConfigs, NewNamespace("foo"), "service")
|
2016-12-05 16:14:08 -05:00
|
|
|
|
|
|
|
expected := []swarm.NetworkAttachmentConfig{
|
|
|
|
{
|
|
|
|
Target: "foo_default",
|
|
|
|
Aliases: []string{"service"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, configs)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertServiceNetworks(t *testing.T) {
|
|
|
|
networkConfigs := networkMap{
|
|
|
|
"front": composetypes.NetworkConfig{
|
|
|
|
External: composetypes.External{
|
|
|
|
External: true,
|
|
|
|
Name: "fronttier",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"back": composetypes.NetworkConfig{},
|
|
|
|
}
|
|
|
|
networks := map[string]*composetypes.ServiceNetworkConfig{
|
|
|
|
"front": {
|
|
|
|
Aliases: []string{"something"},
|
|
|
|
},
|
|
|
|
"back": {
|
|
|
|
Aliases: []string{"other"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configs, err := convertServiceNetworks(
|
|
|
|
networks, networkConfigs, NewNamespace("foo"), "service")
|
|
|
|
|
|
|
|
expected := []swarm.NetworkAttachmentConfig{
|
|
|
|
{
|
|
|
|
Target: "foo_back",
|
|
|
|
Aliases: []string{"other", "service"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Target: "fronttier",
|
|
|
|
Aliases: []string{"something", "service"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sortedConfigs := byTargetSort(configs)
|
|
|
|
sort.Sort(&sortedConfigs)
|
|
|
|
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, []swarm.NetworkAttachmentConfig(sortedConfigs))
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-02-22 13:52:09 -05:00
|
|
|
func TestConvertServiceNetworksCustomDefault(t *testing.T) {
|
|
|
|
networkConfigs := networkMap{
|
|
|
|
"default": composetypes.NetworkConfig{
|
|
|
|
External: composetypes.External{
|
|
|
|
External: true,
|
|
|
|
Name: "custom",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
networks := map[string]*composetypes.ServiceNetworkConfig{}
|
|
|
|
|
|
|
|
configs, err := convertServiceNetworks(
|
|
|
|
networks, networkConfigs, NewNamespace("foo"), "service")
|
|
|
|
|
|
|
|
expected := []swarm.NetworkAttachmentConfig{
|
|
|
|
{
|
|
|
|
Target: "custom",
|
|
|
|
Aliases: []string{"service"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, []swarm.NetworkAttachmentConfig(configs))
|
2017-02-22 13:52:09 -05:00
|
|
|
}
|
|
|
|
|
2016-12-05 16:14:08 -05:00
|
|
|
type byTargetSort []swarm.NetworkAttachmentConfig
|
|
|
|
|
|
|
|
func (s byTargetSort) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s byTargetSort) Less(i, j int) bool {
|
|
|
|
return strings.Compare(s[i].Target, s[j].Target) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s byTargetSort) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
2017-03-23 19:38:17 -04:00
|
|
|
|
|
|
|
func TestConvertDNSConfigEmpty(t *testing.T) {
|
|
|
|
dnsConfig, err := convertDNSConfig(nil, nil)
|
|
|
|
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, (*swarm.DNSConfig)(nil), dnsConfig)
|
2017-03-23 19:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
nameservers = []string{"8.8.8.8", "9.9.9.9"}
|
|
|
|
search = []string{"dc1.example.com", "dc2.example.com"}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConvertDNSConfigAll(t *testing.T) {
|
|
|
|
dnsConfig, err := convertDNSConfig(nameservers, search)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, &swarm.DNSConfig{
|
2017-03-23 19:38:17 -04:00
|
|
|
Nameservers: nameservers,
|
|
|
|
Search: search,
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
}, dnsConfig)
|
2017-03-23 19:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertDNSConfigNameservers(t *testing.T) {
|
|
|
|
dnsConfig, err := convertDNSConfig(nameservers, nil)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, &swarm.DNSConfig{
|
2017-03-23 19:38:17 -04:00
|
|
|
Nameservers: nameservers,
|
|
|
|
Search: nil,
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
}, dnsConfig)
|
2017-03-23 19:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertDNSConfigSearch(t *testing.T) {
|
|
|
|
dnsConfig, err := convertDNSConfig(nil, search)
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, &swarm.DNSConfig{
|
2017-03-23 19:38:17 -04:00
|
|
|
Nameservers: nil,
|
|
|
|
Search: search,
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
}, dnsConfig)
|
2017-03-23 19:38:17 -04:00
|
|
|
}
|
2017-05-11 08:30:04 -04:00
|
|
|
|
|
|
|
func TestConvertCredentialSpec(t *testing.T) {
|
|
|
|
swarmSpec, err := convertCredentialSpec(composetypes.CredentialSpecConfig{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, swarmSpec)
|
|
|
|
|
|
|
|
swarmSpec, err = convertCredentialSpec(composetypes.CredentialSpecConfig{
|
|
|
|
File: "/foo",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, swarmSpec.File, "/foo")
|
|
|
|
assert.Equal(t, swarmSpec.Registry, "")
|
|
|
|
|
|
|
|
swarmSpec, err = convertCredentialSpec(composetypes.CredentialSpecConfig{
|
|
|
|
Registry: "foo",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, swarmSpec.File, "")
|
|
|
|
assert.Equal(t, swarmSpec.Registry, "foo")
|
|
|
|
|
|
|
|
swarmSpec, err = convertCredentialSpec(composetypes.CredentialSpecConfig{
|
|
|
|
File: "/asdf",
|
|
|
|
Registry: "foo",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, swarmSpec)
|
|
|
|
}
|
2017-07-19 04:55:34 -04:00
|
|
|
|
|
|
|
func TestConvertUpdateConfigOrder(t *testing.T) {
|
|
|
|
// test default behavior
|
|
|
|
updateConfig := convertUpdateConfig(&composetypes.UpdateConfig{})
|
|
|
|
assert.Equal(t, "", updateConfig.Order)
|
|
|
|
|
|
|
|
// test start-first
|
|
|
|
updateConfig = convertUpdateConfig(&composetypes.UpdateConfig{
|
|
|
|
Order: "start-first",
|
|
|
|
})
|
|
|
|
assert.Equal(t, updateConfig.Order, "start-first")
|
|
|
|
|
|
|
|
// test stop-first
|
|
|
|
updateConfig = convertUpdateConfig(&composetypes.UpdateConfig{
|
|
|
|
Order: "stop-first",
|
|
|
|
})
|
|
|
|
assert.Equal(t, updateConfig.Order, "stop-first")
|
|
|
|
}
|
2017-11-10 16:24:32 -05:00
|
|
|
|
|
|
|
func TestConvertFileObject(t *testing.T) {
|
|
|
|
namespace := NewNamespace("testing")
|
|
|
|
config := composetypes.FileReferenceConfig{
|
|
|
|
Source: "source",
|
|
|
|
Target: "target",
|
|
|
|
UID: "user",
|
|
|
|
GID: "group",
|
|
|
|
Mode: uint32Ptr(0644),
|
|
|
|
}
|
|
|
|
swarmRef, err := convertFileObject(namespace, config, lookupConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := swarmReferenceObject{
|
|
|
|
Name: "testing_source",
|
|
|
|
File: swarmReferenceTarget{
|
|
|
|
Name: config.Target,
|
|
|
|
UID: config.UID,
|
|
|
|
GID: config.GID,
|
|
|
|
Mode: os.FileMode(0644),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, swarmRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupConfig(key string) (composetypes.FileObjectConfig, error) {
|
|
|
|
if key != "source" {
|
|
|
|
return composetypes.FileObjectConfig{}, errors.New("bad key")
|
|
|
|
}
|
|
|
|
return composetypes.FileObjectConfig{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertFileObjectDefaults(t *testing.T) {
|
|
|
|
namespace := NewNamespace("testing")
|
|
|
|
config := composetypes.FileReferenceConfig{Source: "source"}
|
|
|
|
swarmRef, err := convertFileObject(namespace, config, lookupConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := swarmReferenceObject{
|
|
|
|
Name: "testing_source",
|
|
|
|
File: swarmReferenceTarget{
|
|
|
|
Name: config.Source,
|
|
|
|
UID: "0",
|
|
|
|
GID: "0",
|
|
|
|
Mode: os.FileMode(0444),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, swarmRef)
|
|
|
|
}
|