2016-12-08 16:32:10 -05:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
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/docker/docker/pkg/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-12-08 16:32:10 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPortOptValidSimpleSyntax(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expected []swarm.PortConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "80",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
2016-12-09 15:17:57 -05:00
|
|
|
Protocol: "tcp",
|
|
|
|
TargetPort: 80,
|
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "80:8080",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
Protocol: "tcp",
|
|
|
|
TargetPort: 8080,
|
|
|
|
PublishedPort: 80,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "8080:80/tcp",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
Protocol: "tcp",
|
|
|
|
TargetPort: 80,
|
|
|
|
PublishedPort: 8080,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "80:8080/udp",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
Protocol: "udp",
|
|
|
|
TargetPort: 8080,
|
|
|
|
PublishedPort: 80,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "80-81:8080-8081/tcp",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
Protocol: "tcp",
|
|
|
|
TargetPort: 8080,
|
|
|
|
PublishedPort: 80,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Protocol: "tcp",
|
|
|
|
TargetPort: 8081,
|
|
|
|
PublishedPort: 81,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "80-82:8080-8082/udp",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
Protocol: "udp",
|
|
|
|
TargetPort: 8080,
|
|
|
|
PublishedPort: 80,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Protocol: "udp",
|
|
|
|
TargetPort: 8081,
|
|
|
|
PublishedPort: 81,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Protocol: "udp",
|
|
|
|
TargetPort: 8082,
|
|
|
|
PublishedPort: 82,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
var port PortOpt
|
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, port.Set(tc.value))
|
|
|
|
assert.Len(t, port.Value(), len(tc.expected))
|
2016-12-08 16:32:10 -05:00
|
|
|
for _, expectedPortConfig := range tc.expected {
|
|
|
|
assertContains(t, port.Value(), expectedPortConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPortOptValidComplexSyntax(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expected []swarm.PortConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "target=80",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
2016-12-09 15:17:57 -05:00
|
|
|
TargetPort: 80,
|
|
|
|
Protocol: "tcp",
|
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "target=80,protocol=tcp",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
2016-12-09 15:17:57 -05:00
|
|
|
Protocol: "tcp",
|
|
|
|
TargetPort: 80,
|
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "target=80,published=8080,protocol=tcp",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
Protocol: "tcp",
|
|
|
|
TargetPort: 80,
|
|
|
|
PublishedPort: 8080,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "published=80,target=8080,protocol=tcp",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
Protocol: "tcp",
|
|
|
|
TargetPort: 8080,
|
|
|
|
PublishedPort: 80,
|
2016-12-09 15:17:57 -05:00
|
|
|
PublishMode: swarm.PortConfigPublishModeIngress,
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "target=80,published=8080,protocol=tcp,mode=host",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
Protocol: "tcp",
|
|
|
|
TargetPort: 80,
|
|
|
|
PublishedPort: 8080,
|
|
|
|
PublishMode: "host",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "target=80,published=8080,mode=host",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
TargetPort: 80,
|
|
|
|
PublishedPort: 8080,
|
|
|
|
PublishMode: "host",
|
2016-12-09 15:17:57 -05:00
|
|
|
Protocol: "tcp",
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "target=80,published=8080,mode=ingress",
|
|
|
|
expected: []swarm.PortConfig{
|
|
|
|
{
|
|
|
|
TargetPort: 80,
|
|
|
|
PublishedPort: 8080,
|
|
|
|
PublishMode: "ingress",
|
2016-12-09 15:17:57 -05:00
|
|
|
Protocol: "tcp",
|
2016-12-08 16:32:10 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
var port PortOpt
|
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, port.Set(tc.value))
|
|
|
|
assert.Len(t, port.Value(), len(tc.expected))
|
2016-12-08 16:32:10 -05:00
|
|
|
for _, expectedPortConfig := range tc.expected {
|
|
|
|
assertContains(t, port.Value(), expectedPortConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPortOptInvalidComplexSyntax(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "invalid,target=80",
|
|
|
|
expectedError: "invalid field",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "invalid=field",
|
|
|
|
expectedError: "invalid field",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "protocol=invalid",
|
|
|
|
expectedError: "invalid protocol value",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "target=invalid",
|
|
|
|
expectedError: "invalid syntax",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "published=invalid",
|
|
|
|
expectedError: "invalid syntax",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "mode=invalid",
|
|
|
|
expectedError: "invalid publish mode value",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "published=8080,protocol=tcp,mode=ingress",
|
|
|
|
expectedError: "missing mandatory field",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: `target=80,protocol="tcp,mode=ingress"`,
|
|
|
|
expectedError: "non-quoted-field",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: `target=80,"protocol=tcp,mode=ingress"`,
|
|
|
|
expectedError: "invalid protocol value",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
var port PortOpt
|
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
|
|
|
testutil.ErrorContains(t, port.Set(tc.value), tc.expectedError)
|
2016-12-08 16:32:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 08:16:03 -05:00
|
|
|
func TestPortOptInvalidSimpleSyntax(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "9999999",
|
|
|
|
expectedError: "Invalid containerPort: 9999999",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "80/xyz",
|
|
|
|
expectedError: "Invalid proto: xyz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "tcp",
|
|
|
|
expectedError: "Invalid containerPort: tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "udp",
|
|
|
|
expectedError: "Invalid containerPort: udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "",
|
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
|
|
|
expectedError: "No port specified: <empty>",
|
2017-02-06 08:16:03 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "1.1.1.1:80:80",
|
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
|
|
|
expectedError: "HostIP is not supported.",
|
2017-02-06 08:16:03 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
var port PortOpt
|
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, port.Set(tc.value), tc.expectedError)
|
2017-02-06 08:16:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 16:32:10 -05:00
|
|
|
func assertContains(t *testing.T, portConfigs []swarm.PortConfig, expected swarm.PortConfig) {
|
|
|
|
var contains = false
|
|
|
|
for _, portConfig := range portConfigs {
|
|
|
|
if portConfig == expected {
|
|
|
|
contains = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !contains {
|
|
|
|
t.Errorf("expected %v to contain %v, did not", portConfigs, expected)
|
|
|
|
}
|
|
|
|
}
|