2016-09-08 13:11:39 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeAddrOptionSetHostAndPort(t *testing.T) {
|
|
|
|
opt := NewNodeAddrOption("old:123")
|
|
|
|
addr := "newhost:5555"
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, opt.Set(addr))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(addr, opt.Value()))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeAddrOptionSetHostOnly(t *testing.T) {
|
|
|
|
opt := NewListenAddrOption()
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, opt.Set("newhost"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("newhost:2377", opt.Value()))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeAddrOptionSetHostOnlyIPv6(t *testing.T) {
|
|
|
|
opt := NewListenAddrOption()
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, opt.Set("::1"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("[::1]:2377", opt.Value()))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeAddrOptionSetPortOnly(t *testing.T) {
|
|
|
|
opt := NewListenAddrOption()
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, opt.Set(":4545"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("0.0.0.0:4545", opt.Value()))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeAddrOptionSetInvalidFormat(t *testing.T) {
|
|
|
|
opt := NewListenAddrOption()
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, opt.Set("http://localhost:4545"), "Invalid proto, expected tcp: http://localhost:4545")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-12-25 16:23:35 -05:00
|
|
|
|
|
|
|
func TestExternalCAOptionErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
externalCA string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
externalCA: "",
|
|
|
|
expectedError: "EOF",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
externalCA: "anything",
|
|
|
|
expectedError: "invalid field 'anything' must be a key=value pair",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
externalCA: "foo=bar",
|
|
|
|
expectedError: "the external-ca option needs a protocol= parameter",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
externalCA: "protocol=baz",
|
|
|
|
expectedError: "unrecognized external CA protocol baz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
externalCA: "protocol=cfssl",
|
|
|
|
expectedError: "the external-ca option needs a url= parameter",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
opt := &ExternalCAOption{}
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, opt.Set(tc.externalCA), tc.expectedError)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExternalCAOption(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
externalCA string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
externalCA: "protocol=cfssl,url=anything",
|
|
|
|
expected: "cfssl: anything",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
externalCA: "protocol=CFSSL,url=anything",
|
|
|
|
expected: "cfssl: anything",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
externalCA: "protocol=Cfssl,url=https://example.com",
|
|
|
|
expected: "cfssl: https://example.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
externalCA: "protocol=Cfssl,url=https://example.com,foo=bar",
|
|
|
|
expected: "cfssl: https://example.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
externalCA: "protocol=Cfssl,url=https://example.com,foo=bar,foo=baz",
|
|
|
|
expected: "cfssl: https://example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
opt := &ExternalCAOption{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, opt.Set(tc.externalCA))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(tc.expected, opt.String()))
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExternalCAOptionMultiple(t *testing.T) {
|
|
|
|
opt := &ExternalCAOption{}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, opt.Set("protocol=cfssl,url=https://example.com"))
|
|
|
|
assert.NilError(t, opt.Set("protocol=CFSSL,url=anything"))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Len(opt.Value(), 2))
|
|
|
|
assert.Check(t, is.Equal("cfssl: https://example.com, cfssl: anything", opt.String()))
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|