2014-02-08 00:40:52 +00:00
|
|
|
package structs
|
2014-10-18 01:23:13 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-17 14:51:50 +00:00
|
|
|
"encoding/json"
|
2015-11-07 02:13:20 +00:00
|
|
|
"fmt"
|
2014-10-18 01:23:13 +00:00
|
|
|
"reflect"
|
2015-11-07 02:13:20 +00:00
|
|
|
"strings"
|
2014-10-18 01:23:13 +00:00
|
|
|
"testing"
|
2016-06-07 20:24:51 +00:00
|
|
|
|
2019-01-07 21:30:47 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-06-07 20:24:51 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2018-03-09 06:13:35 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-12 16:07:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-10-18 01:23:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeDecode(t *testing.T) {
|
|
|
|
arg := &RegisterRequest{
|
|
|
|
Datacenter: "foo",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "baz",
|
|
|
|
Service: &NodeService{
|
|
|
|
Service: "test",
|
2015-01-02 21:10:05 +00:00
|
|
|
Address: "127.0.0.2",
|
2014-10-18 01:23:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := Encode(RegisterRequestType, arg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out RegisterRequest
|
|
|
|
err = Decode(buf[1:], &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(arg.Service, out.Service) {
|
|
|
|
t.Fatalf("bad: %#v %#v", arg.Service, out.Service)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(arg, &out) {
|
|
|
|
t.Fatalf("bad: %#v %#v", arg, out)
|
|
|
|
}
|
|
|
|
}
|
2014-10-07 18:05:31 +00:00
|
|
|
|
|
|
|
func TestStructs_Implements(t *testing.T) {
|
|
|
|
var (
|
|
|
|
_ RPCInfo = &RegisterRequest{}
|
|
|
|
_ RPCInfo = &DeregisterRequest{}
|
|
|
|
_ RPCInfo = &DCSpecificRequest{}
|
|
|
|
_ RPCInfo = &ServiceSpecificRequest{}
|
|
|
|
_ RPCInfo = &NodeSpecificRequest{}
|
|
|
|
_ RPCInfo = &ChecksInStateRequest{}
|
|
|
|
_ RPCInfo = &KVSRequest{}
|
|
|
|
_ RPCInfo = &KeyRequest{}
|
|
|
|
_ RPCInfo = &KeyListRequest{}
|
|
|
|
_ RPCInfo = &SessionRequest{}
|
|
|
|
_ RPCInfo = &SessionSpecificRequest{}
|
|
|
|
_ RPCInfo = &EventFireRequest{}
|
2018-10-19 16:04:07 +00:00
|
|
|
_ RPCInfo = &ACLPolicyResolveLegacyRequest{}
|
2018-10-31 20:00:46 +00:00
|
|
|
_ RPCInfo = &ACLPolicyBatchGetRequest{}
|
|
|
|
_ RPCInfo = &ACLPolicyGetRequest{}
|
|
|
|
_ RPCInfo = &ACLTokenGetRequest{}
|
2014-10-07 18:05:31 +00:00
|
|
|
_ RPCInfo = &KeyringRequest{}
|
|
|
|
_ CompoundResponse = &KeyringResponses{}
|
|
|
|
)
|
|
|
|
}
|
2015-10-14 20:41:04 +00:00
|
|
|
|
2016-12-09 00:01:01 +00:00
|
|
|
func TestStructs_RegisterRequest_ChangesNode(t *testing.T) {
|
|
|
|
req := &RegisterRequest{
|
2017-01-18 22:26:42 +00:00
|
|
|
ID: types.NodeID("40e4a748-2192-161a-0510-9bf59fe950b5"),
|
2016-12-09 00:01:01 +00:00
|
|
|
Node: "test",
|
|
|
|
Address: "127.0.0.1",
|
2017-04-18 12:02:24 +00:00
|
|
|
Datacenter: "dc1",
|
2016-12-09 00:01:01 +00:00
|
|
|
TaggedAddresses: make(map[string]string),
|
2017-01-09 19:21:49 +00:00
|
|
|
NodeMeta: map[string]string{
|
|
|
|
"role": "server",
|
|
|
|
},
|
2016-12-09 00:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node := &Node{
|
2017-01-18 22:26:42 +00:00
|
|
|
ID: types.NodeID("40e4a748-2192-161a-0510-9bf59fe950b5"),
|
2016-12-09 00:01:01 +00:00
|
|
|
Node: "test",
|
|
|
|
Address: "127.0.0.1",
|
2017-04-18 12:02:24 +00:00
|
|
|
Datacenter: "dc1",
|
2016-12-09 00:01:01 +00:00
|
|
|
TaggedAddresses: make(map[string]string),
|
2017-01-09 19:21:49 +00:00
|
|
|
Meta: map[string]string{
|
|
|
|
"role": "server",
|
|
|
|
},
|
2016-12-09 00:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
check := func(twiddle, restore func()) {
|
|
|
|
if req.ChangesNode(node) {
|
|
|
|
t.Fatalf("should not change")
|
|
|
|
}
|
|
|
|
|
|
|
|
twiddle()
|
|
|
|
if !req.ChangesNode(node) {
|
|
|
|
t.Fatalf("should change")
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:01:46 +00:00
|
|
|
req.SkipNodeUpdate = true
|
|
|
|
if req.ChangesNode(node) {
|
|
|
|
t.Fatalf("should skip")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SkipNodeUpdate = false
|
|
|
|
if !req.ChangesNode(node) {
|
|
|
|
t.Fatalf("should change")
|
|
|
|
}
|
|
|
|
|
2016-12-09 00:01:01 +00:00
|
|
|
restore()
|
|
|
|
if req.ChangesNode(node) {
|
|
|
|
t.Fatalf("should not change")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:26:42 +00:00
|
|
|
check(func() { req.ID = "nope" }, func() { req.ID = types.NodeID("40e4a748-2192-161a-0510-9bf59fe950b5") })
|
2016-12-09 00:01:01 +00:00
|
|
|
check(func() { req.Node = "nope" }, func() { req.Node = "test" })
|
|
|
|
check(func() { req.Address = "127.0.0.2" }, func() { req.Address = "127.0.0.1" })
|
2017-04-18 12:02:24 +00:00
|
|
|
check(func() { req.Datacenter = "dc2" }, func() { req.Datacenter = "dc1" })
|
2016-12-09 00:01:01 +00:00
|
|
|
check(func() { req.TaggedAddresses["wan"] = "nope" }, func() { delete(req.TaggedAddresses, "wan") })
|
2017-01-09 21:40:50 +00:00
|
|
|
check(func() { req.NodeMeta["invalid"] = "nope" }, func() { delete(req.NodeMeta, "invalid") })
|
2016-12-09 00:01:01 +00:00
|
|
|
|
|
|
|
if !req.ChangesNode(nil) {
|
|
|
|
t.Fatalf("should change")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 20:55:35 +00:00
|
|
|
// testServiceNode gives a fully filled out ServiceNode instance.
|
2018-09-12 16:07:47 +00:00
|
|
|
func testServiceNode(t *testing.T) *ServiceNode {
|
2015-10-19 20:55:35 +00:00
|
|
|
return &ServiceNode{
|
2017-04-18 12:02:24 +00:00
|
|
|
ID: types.NodeID("40e4a748-2192-161a-0510-9bf59fe950b5"),
|
|
|
|
Node: "node1",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Datacenter: "dc1",
|
2016-08-12 23:28:56 +00:00
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"hello": "world",
|
|
|
|
},
|
2017-01-05 22:10:26 +00:00
|
|
|
NodeMeta: map[string]string{
|
|
|
|
"tag": "value",
|
|
|
|
},
|
2018-03-10 01:21:26 +00:00
|
|
|
ServiceKind: ServiceKindTypical,
|
2018-02-09 00:37:45 +00:00
|
|
|
ServiceID: "service1",
|
|
|
|
ServiceName: "dogs",
|
|
|
|
ServiceTags: []string{"prod", "v1"},
|
|
|
|
ServiceAddress: "127.0.0.2",
|
2019-06-17 14:51:50 +00:00
|
|
|
ServiceTaggedAddresses: map[string]ServiceAddress{
|
|
|
|
"lan": ServiceAddress{
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
"wan": ServiceAddress{
|
|
|
|
Address: "198.18.0.1",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServicePort: 8080,
|
2018-02-09 00:37:45 +00:00
|
|
|
ServiceMeta: map[string]string{
|
|
|
|
"service": "metadata",
|
|
|
|
},
|
2015-10-19 20:55:35 +00:00
|
|
|
ServiceEnableTagOverride: true,
|
2015-10-14 20:46:01 +00:00
|
|
|
RaftIndex: RaftIndex{
|
2015-10-14 20:41:04 +00:00
|
|
|
CreateIndex: 1,
|
|
|
|
ModifyIndex: 2,
|
|
|
|
},
|
2018-09-12 16:07:47 +00:00
|
|
|
ServiceProxy: TestConnectProxyConfig(t),
|
|
|
|
// DEPRECATED (ProxyDestination) - remove this when removing ProxyDestination
|
|
|
|
// ServiceProxyDestination is deprecated bit must be set consistently with
|
|
|
|
// the value of ServiceProxy.DestinationServiceName otherwise a round-trip
|
|
|
|
// through ServiceNode -> NodeService and back will not match and fail
|
|
|
|
// tests.
|
|
|
|
ServiceProxyDestination: "web",
|
|
|
|
ServiceConnect: ServiceConnect{
|
|
|
|
Native: true,
|
|
|
|
},
|
2015-10-14 20:41:04 +00:00
|
|
|
}
|
2015-10-19 20:55:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 11:42:39 +00:00
|
|
|
func TestNode_IsSame(t *testing.T) {
|
|
|
|
id := types.NodeID("e62f3b31-9284-4e26-ab14-2a59dea85b55")
|
|
|
|
node := "mynode1"
|
|
|
|
address := ""
|
|
|
|
datacenter := "dc1"
|
|
|
|
n := &Node{
|
|
|
|
ID: id,
|
|
|
|
Node: node,
|
|
|
|
Datacenter: datacenter,
|
|
|
|
Address: address,
|
|
|
|
TaggedAddresses: make(map[string]string),
|
|
|
|
Meta: make(map[string]string),
|
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: 1,
|
|
|
|
ModifyIndex: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
other := &Node{
|
|
|
|
ID: id,
|
|
|
|
Node: node,
|
|
|
|
Datacenter: datacenter,
|
|
|
|
Address: address,
|
|
|
|
TaggedAddresses: make(map[string]string),
|
|
|
|
Meta: make(map[string]string),
|
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: 1,
|
|
|
|
ModifyIndex: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
check := func(twiddle, restore func()) {
|
|
|
|
t.Helper()
|
|
|
|
if !n.IsSame(other) || !other.IsSame(n) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
twiddle()
|
|
|
|
if n.IsSame(other) || other.IsSame(n) {
|
|
|
|
t.Fatalf("should be different, was %#v VS %#v", n, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
restore()
|
|
|
|
if !n.IsSame(other) || !other.IsSame(n) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check(func() { other.ID = types.NodeID("") }, func() { other.ID = id })
|
|
|
|
check(func() { other.Node = "other" }, func() { other.Node = node })
|
|
|
|
check(func() { other.Datacenter = "dcX" }, func() { other.Datacenter = datacenter })
|
|
|
|
check(func() { other.Address = "127.0.0.1" }, func() { other.Address = address })
|
|
|
|
check(func() { other.TaggedAddresses = map[string]string{"my": "address"} }, func() { other.TaggedAddresses = map[string]string{} })
|
|
|
|
check(func() { other.Meta = map[string]string{"my": "meta"} }, func() { other.Meta = map[string]string{} })
|
|
|
|
|
|
|
|
if !n.IsSame(other) {
|
|
|
|
t.Fatalf("should be equal, was %#v VS %#v", n, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructs_ServiceNode_IsSameService(t *testing.T) {
|
|
|
|
sn := testServiceNode(t)
|
|
|
|
node := "node1"
|
|
|
|
serviceID := sn.ServiceID
|
|
|
|
serviceAddress := sn.ServiceAddress
|
|
|
|
serviceEnableTagOverride := sn.ServiceEnableTagOverride
|
|
|
|
serviceMeta := make(map[string]string)
|
|
|
|
for k, v := range sn.ServiceMeta {
|
|
|
|
serviceMeta[k] = v
|
|
|
|
}
|
|
|
|
serviceName := sn.ServiceName
|
|
|
|
servicePort := sn.ServicePort
|
|
|
|
serviceTags := sn.ServiceTags
|
|
|
|
serviceWeights := Weights{Passing: 2, Warning: 1}
|
|
|
|
sn.ServiceWeights = serviceWeights
|
|
|
|
serviceProxyDestination := sn.ServiceProxyDestination
|
|
|
|
serviceProxy := sn.ServiceProxy
|
|
|
|
serviceConnect := sn.ServiceConnect
|
2019-06-17 14:51:50 +00:00
|
|
|
serviceTaggedAddresses := sn.ServiceTaggedAddresses
|
2018-10-11 11:42:39 +00:00
|
|
|
|
|
|
|
n := sn.ToNodeService().ToServiceNode(node)
|
|
|
|
other := sn.ToNodeService().ToServiceNode(node)
|
|
|
|
|
|
|
|
check := func(twiddle, restore func()) {
|
|
|
|
t.Helper()
|
|
|
|
if !n.IsSameService(other) || !other.IsSameService(n) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
twiddle()
|
|
|
|
if n.IsSameService(other) || other.IsSameService(n) {
|
|
|
|
t.Fatalf("should be different, was %#v VS %#v", n, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
restore()
|
|
|
|
if !n.IsSameService(other) || !other.IsSameService(n) {
|
|
|
|
t.Fatalf("should be the same after restore, was:\n %#v VS\n %#v", n, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check(func() { other.ServiceID = "66fb695a-c782-472f-8d36-4f3edd754b37" }, func() { other.ServiceID = serviceID })
|
|
|
|
check(func() { other.Node = "other" }, func() { other.Node = node })
|
|
|
|
check(func() { other.ServiceAddress = "1.2.3.4" }, func() { other.ServiceAddress = serviceAddress })
|
|
|
|
check(func() { other.ServiceEnableTagOverride = !serviceEnableTagOverride }, func() { other.ServiceEnableTagOverride = serviceEnableTagOverride })
|
|
|
|
check(func() { other.ServiceKind = "newKind" }, func() { other.ServiceKind = "" })
|
|
|
|
check(func() { other.ServiceMeta = map[string]string{"my": "meta"} }, func() { other.ServiceMeta = serviceMeta })
|
|
|
|
check(func() { other.ServiceName = "duck" }, func() { other.ServiceName = serviceName })
|
|
|
|
check(func() { other.ServicePort = 65534 }, func() { other.ServicePort = servicePort })
|
|
|
|
check(func() { other.ServiceProxyDestination = "duck" }, func() { other.ServiceProxyDestination = serviceProxyDestination })
|
|
|
|
check(func() { other.ServiceTags = []string{"new", "tags"} }, func() { other.ServiceTags = serviceTags })
|
|
|
|
check(func() { other.ServiceWeights = Weights{Passing: 42, Warning: 41} }, func() { other.ServiceWeights = serviceWeights })
|
|
|
|
check(func() { other.ServiceProxy = ConnectProxyConfig{} }, func() { other.ServiceProxy = serviceProxy })
|
|
|
|
check(func() { other.ServiceConnect = ServiceConnect{} }, func() { other.ServiceConnect = serviceConnect })
|
2019-06-17 14:51:50 +00:00
|
|
|
check(func() { other.ServiceTaggedAddresses = nil }, func() { other.ServiceTaggedAddresses = serviceTaggedAddresses })
|
2018-10-11 11:42:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 23:28:56 +00:00
|
|
|
func TestStructs_ServiceNode_PartialClone(t *testing.T) {
|
2018-09-12 16:07:47 +00:00
|
|
|
sn := testServiceNode(t)
|
2015-10-14 20:41:04 +00:00
|
|
|
|
2016-08-12 23:28:56 +00:00
|
|
|
clone := sn.PartialClone()
|
|
|
|
|
|
|
|
// Make sure the parts that weren't supposed to be cloned didn't get
|
|
|
|
// copied over, then zero-value them out so we can do a DeepEqual() on
|
|
|
|
// the rest of the contents.
|
2017-01-18 22:26:42 +00:00
|
|
|
if clone.ID != "" ||
|
|
|
|
clone.Address != "" ||
|
2017-04-18 12:02:24 +00:00
|
|
|
clone.Datacenter != "" ||
|
2017-01-18 22:26:42 +00:00
|
|
|
len(clone.TaggedAddresses) != 0 ||
|
|
|
|
len(clone.NodeMeta) != 0 {
|
2016-08-12 23:28:56 +00:00
|
|
|
t.Fatalf("bad: %v", clone)
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:26:42 +00:00
|
|
|
sn.ID = ""
|
2016-08-12 23:28:56 +00:00
|
|
|
sn.Address = ""
|
2017-04-18 12:02:24 +00:00
|
|
|
sn.Datacenter = ""
|
2016-08-12 23:28:56 +00:00
|
|
|
sn.TaggedAddresses = nil
|
2017-01-05 22:10:26 +00:00
|
|
|
sn.NodeMeta = nil
|
2018-09-12 16:07:47 +00:00
|
|
|
require.Equal(t, sn, clone)
|
2015-10-14 20:41:04 +00:00
|
|
|
|
|
|
|
sn.ServiceTags = append(sn.ServiceTags, "hello")
|
|
|
|
if reflect.DeepEqual(sn, clone) {
|
|
|
|
t.Fatalf("clone wasn't independent of the original")
|
|
|
|
}
|
2018-02-09 00:37:45 +00:00
|
|
|
|
|
|
|
revert := make([]string, len(sn.ServiceTags)-1)
|
|
|
|
copy(revert, sn.ServiceTags[0:len(sn.ServiceTags)-1])
|
|
|
|
sn.ServiceTags = revert
|
|
|
|
if !reflect.DeepEqual(sn, clone) {
|
|
|
|
t.Fatalf("bad: %v VS %v", clone, sn)
|
|
|
|
}
|
2018-09-07 14:30:47 +00:00
|
|
|
oldPassingWeight := clone.ServiceWeights.Passing
|
|
|
|
sn.ServiceWeights.Passing = 1000
|
|
|
|
if reflect.DeepEqual(sn, clone) {
|
|
|
|
t.Fatalf("clone wasn't independent of the original for Meta")
|
|
|
|
}
|
|
|
|
sn.ServiceWeights.Passing = oldPassingWeight
|
2018-02-09 00:37:45 +00:00
|
|
|
sn.ServiceMeta["new_meta"] = "new_value"
|
|
|
|
if reflect.DeepEqual(sn, clone) {
|
2018-03-28 14:04:50 +00:00
|
|
|
t.Fatalf("clone wasn't independent of the original for Meta")
|
2018-02-09 00:37:45 +00:00
|
|
|
}
|
2019-06-17 14:51:50 +00:00
|
|
|
|
|
|
|
// ensure that the tagged addresses were copied and not just a pointer to the map
|
|
|
|
sn.ServiceTaggedAddresses["foo"] = ServiceAddress{Address: "consul.is.awesome", Port: 443}
|
|
|
|
require.NotEqual(t, sn, clone)
|
2015-10-14 20:41:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 20:55:35 +00:00
|
|
|
func TestStructs_ServiceNode_Conversions(t *testing.T) {
|
2018-09-12 16:07:47 +00:00
|
|
|
sn := testServiceNode(t)
|
2015-10-19 20:55:35 +00:00
|
|
|
|
2016-08-12 23:28:56 +00:00
|
|
|
sn2 := sn.ToNodeService().ToServiceNode("node1")
|
|
|
|
|
|
|
|
// These two fields get lost in the conversion, so we have to zero-value
|
|
|
|
// them out before we do the compare.
|
2017-01-18 22:26:42 +00:00
|
|
|
sn.ID = ""
|
2016-08-12 23:28:56 +00:00
|
|
|
sn.Address = ""
|
2017-04-18 12:02:24 +00:00
|
|
|
sn.Datacenter = ""
|
2016-08-12 23:28:56 +00:00
|
|
|
sn.TaggedAddresses = nil
|
2017-01-05 22:10:26 +00:00
|
|
|
sn.NodeMeta = nil
|
2018-09-07 14:30:47 +00:00
|
|
|
sn.ServiceWeights = Weights{Passing: 1, Warning: 1}
|
2018-09-12 16:07:47 +00:00
|
|
|
require.Equal(t, sn, sn2)
|
2018-10-11 11:42:39 +00:00
|
|
|
if !sn.IsSameService(sn2) || !sn2.IsSameService(sn) {
|
|
|
|
t.Fatalf("bad: %#v, should be the same %#v", sn2, sn)
|
|
|
|
}
|
|
|
|
// Those fields are lost in conversion, so IsSameService() should not take them into account
|
|
|
|
sn.Address = "y"
|
|
|
|
sn.Datacenter = "z"
|
|
|
|
sn.TaggedAddresses = map[string]string{"one": "1", "two": "2"}
|
|
|
|
sn.NodeMeta = map[string]string{"meta": "data"}
|
|
|
|
if !sn.IsSameService(sn2) || !sn2.IsSameService(sn) {
|
|
|
|
t.Fatalf("bad: %#v, should be the same %#v", sn2, sn)
|
|
|
|
}
|
2015-10-19 20:55:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
func TestStructs_NodeService_ValidateMeshGateway(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
Modify func(*NodeService)
|
|
|
|
Err string
|
|
|
|
}
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"valid": testCase{
|
|
|
|
func(x *NodeService) {},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
"zero-port": testCase{
|
|
|
|
func(x *NodeService) { x.Port = 0 },
|
|
|
|
"Port must be non-zero",
|
|
|
|
},
|
|
|
|
"sidecar-service": testCase{
|
|
|
|
func(x *NodeService) { x.Connect.SidecarService = &ServiceDefinition{} },
|
|
|
|
"cannot have a sidecar service",
|
|
|
|
},
|
|
|
|
"connect-managed-proxy": testCase{
|
|
|
|
func(x *NodeService) { x.Connect.Proxy = &ServiceDefinitionConnectProxy{} },
|
|
|
|
"Connect.Proxy configuration is invalid",
|
|
|
|
},
|
|
|
|
"proxy-destination-name": testCase{
|
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceName = "foo" },
|
|
|
|
"Proxy.DestinationServiceName configuration is invalid",
|
|
|
|
},
|
|
|
|
"proxy-destination-id": testCase{
|
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceID = "foo" },
|
|
|
|
"Proxy.DestinationServiceID configuration is invalid",
|
|
|
|
},
|
|
|
|
"proxy-local-address": testCase{
|
|
|
|
func(x *NodeService) { x.Proxy.LocalServiceAddress = "127.0.0.1" },
|
|
|
|
"Proxy.LocalServiceAddress configuration is invalid",
|
|
|
|
},
|
|
|
|
"proxy-local-port": testCase{
|
|
|
|
func(x *NodeService) { x.Proxy.LocalServicePort = 36 },
|
|
|
|
"Proxy.LocalServicePort configuration is invalid",
|
|
|
|
},
|
|
|
|
"proxy-upstreams": testCase{
|
|
|
|
func(x *NodeService) { x.Proxy.Upstreams = []Upstream{Upstream{}} },
|
|
|
|
"Proxy.Upstreams configuration is invalid",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
ns := TestNodeServiceMeshGateway(t)
|
|
|
|
tc.Modify(ns)
|
|
|
|
|
|
|
|
err := ns.Validate()
|
|
|
|
if tc.Err == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Contains(t, strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:13:35 +00:00
|
|
|
func TestStructs_NodeService_ValidateConnectProxy(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Modify func(*NodeService)
|
|
|
|
Err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid",
|
|
|
|
func(x *NodeService) {},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"connect-proxy: no ProxyDestination",
|
2018-09-12 16:07:47 +00:00
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceName = "" },
|
|
|
|
"Proxy.DestinationServiceName must be",
|
2018-03-09 06:13:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"connect-proxy: whitespace ProxyDestination",
|
2018-09-12 16:07:47 +00:00
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceName = " " },
|
|
|
|
"Proxy.DestinationServiceName must be",
|
2018-03-09 06:13:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"connect-proxy: valid ProxyDestination",
|
2018-09-12 16:07:47 +00:00
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceName = "hello" },
|
2018-03-09 06:13:35 +00:00
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"connect-proxy: no port set",
|
|
|
|
func(x *NodeService) { x.Port = 0 },
|
|
|
|
"Port must",
|
|
|
|
},
|
2018-06-05 03:04:45 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"connect-proxy: ConnectNative set",
|
2018-06-05 17:51:05 +00:00
|
|
|
func(x *NodeService) { x.Connect.Native = true },
|
2018-06-05 03:04:45 +00:00
|
|
|
"cannot also be",
|
|
|
|
},
|
2018-03-09 06:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
ns := TestNodeServiceProxy(t)
|
|
|
|
tc.Modify(ns)
|
|
|
|
|
|
|
|
err := ns.Validate()
|
2018-09-27 13:33:12 +00:00
|
|
|
assert.Equal(err != nil, tc.Err != "", err)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Contains(strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructs_NodeService_ValidateSidecarService(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Modify func(*NodeService)
|
|
|
|
Err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid",
|
|
|
|
func(x *NodeService) {},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"ID can't be set",
|
|
|
|
func(x *NodeService) { x.Connect.SidecarService.ID = "foo" },
|
|
|
|
"SidecarService cannot specify an ID",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"Nested sidecar can't be set",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Connect.SidecarService.Connect = &ServiceConnect{
|
|
|
|
SidecarService: &ServiceDefinition{},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"SidecarService cannot have a nested SidecarService",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"Sidecar can't have managed proxy",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Connect.SidecarService.Connect = &ServiceConnect{
|
|
|
|
Proxy: &ServiceDefinitionConnectProxy{},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"SidecarService cannot have a managed proxy",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
ns := TestNodeServiceSidecar(t)
|
|
|
|
tc.Modify(ns)
|
|
|
|
|
|
|
|
err := ns.Validate()
|
2018-03-09 06:13:35 +00:00
|
|
|
assert.Equal(err != nil, tc.Err != "", err)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Contains(strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 21:32:00 +00:00
|
|
|
func TestStructs_NodeService_IsSame(t *testing.T) {
|
|
|
|
ns := &NodeService{
|
2018-02-09 00:37:45 +00:00
|
|
|
ID: "node1",
|
|
|
|
Service: "theservice",
|
|
|
|
Tags: []string{"foo", "bar"},
|
|
|
|
Address: "127.0.0.1",
|
2019-06-17 14:51:50 +00:00
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
|
|
|
"lan": ServiceAddress{
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 3456,
|
|
|
|
},
|
|
|
|
"wan": ServiceAddress{
|
|
|
|
Address: "198.18.0.1",
|
|
|
|
Port: 1234,
|
|
|
|
},
|
|
|
|
},
|
2018-03-28 14:04:50 +00:00
|
|
|
Meta: map[string]string{
|
2018-02-09 00:37:45 +00:00
|
|
|
"meta1": "value1",
|
|
|
|
"meta2": "value2",
|
|
|
|
},
|
2015-10-28 21:32:00 +00:00
|
|
|
Port: 1234,
|
|
|
|
EnableTagOverride: true,
|
2018-09-12 16:07:47 +00:00
|
|
|
Proxy: ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "db",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
2018-10-11 11:42:39 +00:00
|
|
|
Weights: &Weights{Passing: 1, Warning: 1},
|
2015-10-28 21:32:00 +00:00
|
|
|
}
|
|
|
|
if !ns.IsSame(ns) {
|
|
|
|
t.Fatalf("should be equal to itself")
|
|
|
|
}
|
|
|
|
|
|
|
|
other := &NodeService{
|
|
|
|
ID: "node1",
|
|
|
|
Service: "theservice",
|
|
|
|
Tags: []string{"foo", "bar"},
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 1234,
|
|
|
|
EnableTagOverride: true,
|
2019-06-17 14:51:50 +00:00
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
|
|
|
"wan": ServiceAddress{
|
|
|
|
Address: "198.18.0.1",
|
|
|
|
Port: 1234,
|
|
|
|
},
|
|
|
|
"lan": ServiceAddress{
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 3456,
|
|
|
|
},
|
|
|
|
},
|
2018-03-28 14:04:50 +00:00
|
|
|
Meta: map[string]string{
|
2018-02-09 00:37:45 +00:00
|
|
|
// We don't care about order
|
|
|
|
"meta2": "value2",
|
|
|
|
"meta1": "value1",
|
|
|
|
},
|
2018-09-12 16:07:47 +00:00
|
|
|
Proxy: ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "db",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
2018-10-11 11:42:39 +00:00
|
|
|
Weights: &Weights{Passing: 1, Warning: 1},
|
2015-10-28 21:32:00 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: 1,
|
|
|
|
ModifyIndex: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !ns.IsSame(other) || !other.IsSame(ns) {
|
|
|
|
t.Fatalf("should not care about Raft fields")
|
|
|
|
}
|
|
|
|
|
|
|
|
check := func(twiddle, restore func()) {
|
2018-09-12 16:07:47 +00:00
|
|
|
t.Helper()
|
2015-10-28 21:32:00 +00:00
|
|
|
if !ns.IsSame(other) || !other.IsSame(ns) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
twiddle()
|
|
|
|
if ns.IsSame(other) || other.IsSame(ns) {
|
|
|
|
t.Fatalf("should not be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
restore()
|
|
|
|
if !ns.IsSame(other) || !other.IsSame(ns) {
|
2018-09-12 16:07:47 +00:00
|
|
|
t.Fatalf("should be the same again")
|
2015-10-28 21:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check(func() { other.ID = "XXX" }, func() { other.ID = "node1" })
|
|
|
|
check(func() { other.Service = "XXX" }, func() { other.Service = "theservice" })
|
|
|
|
check(func() { other.Tags = nil }, func() { other.Tags = []string{"foo", "bar"} })
|
|
|
|
check(func() { other.Tags = []string{"foo"} }, func() { other.Tags = []string{"foo", "bar"} })
|
|
|
|
check(func() { other.Address = "XXX" }, func() { other.Address = "127.0.0.1" })
|
|
|
|
check(func() { other.Port = 9999 }, func() { other.Port = 1234 })
|
2018-03-28 14:04:50 +00:00
|
|
|
check(func() { other.Meta["meta2"] = "wrongValue" }, func() { other.Meta["meta2"] = "value2" })
|
2015-10-28 21:32:00 +00:00
|
|
|
check(func() { other.EnableTagOverride = false }, func() { other.EnableTagOverride = true })
|
2018-03-10 01:21:26 +00:00
|
|
|
check(func() { other.Kind = ServiceKindConnectProxy }, func() { other.Kind = "" })
|
2018-09-12 16:07:47 +00:00
|
|
|
check(func() { other.Proxy.DestinationServiceName = "" }, func() { other.Proxy.DestinationServiceName = "db" })
|
|
|
|
check(func() { other.Proxy.DestinationServiceID = "XXX" }, func() { other.Proxy.DestinationServiceID = "" })
|
|
|
|
check(func() { other.Proxy.LocalServiceAddress = "XXX" }, func() { other.Proxy.LocalServiceAddress = "" })
|
|
|
|
check(func() { other.Proxy.LocalServicePort = 9999 }, func() { other.Proxy.LocalServicePort = 0 })
|
|
|
|
check(func() { other.Proxy.Config["baz"] = "XXX" }, func() { delete(other.Proxy.Config, "baz") })
|
2018-06-05 17:51:05 +00:00
|
|
|
check(func() { other.Connect.Native = true }, func() { other.Connect.Native = false })
|
2018-10-11 11:42:39 +00:00
|
|
|
otherServiceNode := other.ToServiceNode("node1")
|
|
|
|
copyNodeService := otherServiceNode.ToNodeService()
|
|
|
|
if !copyNodeService.IsSame(other) {
|
|
|
|
t.Fatalf("copy should be the same, but was\n %#v\nVS\n %#v", copyNodeService, other)
|
|
|
|
}
|
|
|
|
otherServiceNodeCopy2 := copyNodeService.ToServiceNode("node1")
|
|
|
|
if !otherServiceNode.IsSameService(otherServiceNodeCopy2) {
|
|
|
|
t.Fatalf("copy should be the same, but was\n %#v\nVS\n %#v", otherServiceNode, otherServiceNodeCopy2)
|
|
|
|
}
|
2019-06-17 14:51:50 +00:00
|
|
|
check(func() { other.TaggedAddresses["lan"] = ServiceAddress{Address: "127.0.0.1", Port: 9999} }, func() { other.TaggedAddresses["lan"] = ServiceAddress{Address: "127.0.0.1", Port: 3456} })
|
2015-10-28 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructs_HealthCheck_IsSame(t *testing.T) {
|
|
|
|
hc := &HealthCheck{
|
|
|
|
Node: "node1",
|
|
|
|
CheckID: "check1",
|
|
|
|
Name: "thecheck",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-10-28 21:32:00 +00:00
|
|
|
Notes: "it's all good",
|
|
|
|
Output: "lgtm",
|
|
|
|
ServiceID: "service1",
|
|
|
|
ServiceName: "theservice",
|
2017-04-27 23:03:05 +00:00
|
|
|
ServiceTags: []string{"foo"},
|
2015-10-28 21:32:00 +00:00
|
|
|
}
|
|
|
|
if !hc.IsSame(hc) {
|
|
|
|
t.Fatalf("should be equal to itself")
|
|
|
|
}
|
|
|
|
|
|
|
|
other := &HealthCheck{
|
|
|
|
Node: "node1",
|
|
|
|
CheckID: "check1",
|
|
|
|
Name: "thecheck",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-10-28 21:32:00 +00:00
|
|
|
Notes: "it's all good",
|
|
|
|
Output: "lgtm",
|
|
|
|
ServiceID: "service1",
|
|
|
|
ServiceName: "theservice",
|
2017-04-27 23:03:05 +00:00
|
|
|
ServiceTags: []string{"foo"},
|
2015-10-28 21:32:00 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: 1,
|
|
|
|
ModifyIndex: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !hc.IsSame(other) || !other.IsSame(hc) {
|
|
|
|
t.Fatalf("should not care about Raft fields")
|
|
|
|
}
|
|
|
|
|
2016-06-07 20:24:51 +00:00
|
|
|
checkCheckIDField := func(field *types.CheckID) {
|
|
|
|
if !hc.IsSame(other) || !other.IsSame(hc) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
old := *field
|
|
|
|
*field = "XXX"
|
|
|
|
if hc.IsSame(other) || other.IsSame(hc) {
|
|
|
|
t.Fatalf("should not be the same")
|
|
|
|
}
|
|
|
|
*field = old
|
|
|
|
|
|
|
|
if !hc.IsSame(other) || !other.IsSame(hc) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkStringField := func(field *string) {
|
2015-10-28 21:32:00 +00:00
|
|
|
if !hc.IsSame(other) || !other.IsSame(hc) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
old := *field
|
|
|
|
*field = "XXX"
|
|
|
|
if hc.IsSame(other) || other.IsSame(hc) {
|
|
|
|
t.Fatalf("should not be the same")
|
|
|
|
}
|
|
|
|
*field = old
|
|
|
|
|
|
|
|
if !hc.IsSame(other) || !other.IsSame(hc) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 20:24:51 +00:00
|
|
|
checkStringField(&other.Node)
|
|
|
|
checkCheckIDField(&other.CheckID)
|
|
|
|
checkStringField(&other.Name)
|
|
|
|
checkStringField(&other.Status)
|
|
|
|
checkStringField(&other.Notes)
|
|
|
|
checkStringField(&other.Output)
|
|
|
|
checkStringField(&other.ServiceID)
|
|
|
|
checkStringField(&other.ServiceName)
|
2015-10-28 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 10:00:56 +00:00
|
|
|
func TestStructs_HealthCheck_Marshalling(t *testing.T) {
|
2019-01-24 15:45:54 +00:00
|
|
|
d := &HealthCheckDefinition{}
|
|
|
|
buf, err := d.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotContains(t, string(buf), `"Interval":""`)
|
|
|
|
require.NotContains(t, string(buf), `"Timeout":""`)
|
|
|
|
require.NotContains(t, string(buf), `"DeregisterCriticalServiceAfter":""`)
|
|
|
|
}
|
|
|
|
|
2016-04-11 07:05:39 +00:00
|
|
|
func TestStructs_HealthCheck_Clone(t *testing.T) {
|
|
|
|
hc := &HealthCheck{
|
|
|
|
Node: "node1",
|
|
|
|
CheckID: "check1",
|
|
|
|
Name: "thecheck",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2016-04-11 07:05:39 +00:00
|
|
|
Notes: "it's all good",
|
|
|
|
Output: "lgtm",
|
|
|
|
ServiceID: "service1",
|
|
|
|
ServiceName: "theservice",
|
|
|
|
}
|
|
|
|
clone := hc.Clone()
|
|
|
|
if !hc.IsSame(clone) {
|
|
|
|
t.Fatalf("should be equal to its clone")
|
|
|
|
}
|
|
|
|
|
|
|
|
clone.Output = "different"
|
|
|
|
if hc.IsSame(clone) {
|
|
|
|
t.Fatalf("should not longer be equal to its clone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-07 00:59:32 +00:00
|
|
|
func TestStructs_CheckServiceNodes_Shuffle(t *testing.T) {
|
2015-11-07 02:13:20 +00:00
|
|
|
// Make a huge list of nodes.
|
|
|
|
var nodes CheckServiceNodes
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
nodes = append(nodes, CheckServiceNode{
|
2015-11-07 00:59:32 +00:00
|
|
|
Node: &Node{
|
2015-11-07 02:13:20 +00:00
|
|
|
Node: fmt.Sprintf("node%d", i),
|
|
|
|
Address: fmt.Sprintf("127.0.0.%d", i+1),
|
2015-11-07 00:59:32 +00:00
|
|
|
},
|
2015-11-07 02:13:20 +00:00
|
|
|
})
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-07 02:13:20 +00:00
|
|
|
// Keep track of how many unique shuffles we get.
|
|
|
|
uniques := make(map[string]struct{})
|
2015-11-07 00:59:32 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2015-11-07 02:13:20 +00:00
|
|
|
nodes.Shuffle()
|
|
|
|
|
|
|
|
var names []string
|
|
|
|
for _, node := range nodes {
|
|
|
|
names = append(names, node.Node.Node)
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
2015-11-07 02:13:20 +00:00
|
|
|
key := strings.Join(names, "|")
|
|
|
|
uniques[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to allow for the fact that there won't always be a unique
|
|
|
|
// shuffle each pass, so we just look for smell here without the test
|
|
|
|
// being flaky.
|
|
|
|
if len(uniques) < 50 {
|
|
|
|
t.Fatalf("unique shuffle ratio too low: %d/100", len(uniques))
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructs_CheckServiceNodes_Filter(t *testing.T) {
|
|
|
|
nodes := CheckServiceNodes{
|
|
|
|
CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Node: "node1",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
},
|
|
|
|
Checks: HealthChecks{
|
|
|
|
&HealthCheck{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthWarning,
|
2015-11-07 00:59:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Node: "node2",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
},
|
|
|
|
Checks: HealthChecks{
|
|
|
|
&HealthCheck{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2015-11-07 00:59:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Node: "node3",
|
|
|
|
Address: "127.0.0.3",
|
|
|
|
},
|
|
|
|
Checks: HealthChecks{
|
|
|
|
&HealthCheck{
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2015-11-07 00:59:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-04-10 12:28:27 +00:00
|
|
|
CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Node: "node4",
|
|
|
|
Address: "127.0.0.4",
|
|
|
|
},
|
|
|
|
Checks: HealthChecks{
|
|
|
|
// This check has a different ID to the others to ensure it is not
|
|
|
|
// ignored by accident
|
|
|
|
&HealthCheck{
|
|
|
|
CheckID: "failing2",
|
|
|
|
Status: api.HealthCritical,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test the case where warnings are allowed.
|
|
|
|
{
|
|
|
|
twiddle := make(CheckServiceNodes, len(nodes))
|
|
|
|
if n := copy(twiddle, nodes); n != len(nodes) {
|
|
|
|
t.Fatalf("bad: %d", n)
|
|
|
|
}
|
|
|
|
filtered := twiddle.Filter(false)
|
|
|
|
expected := CheckServiceNodes{
|
|
|
|
nodes[0],
|
|
|
|
nodes[1],
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(filtered, expected) {
|
|
|
|
t.Fatalf("bad: %v", filtered)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit to only passing checks.
|
|
|
|
{
|
|
|
|
twiddle := make(CheckServiceNodes, len(nodes))
|
|
|
|
if n := copy(twiddle, nodes); n != len(nodes) {
|
|
|
|
t.Fatalf("bad: %d", n)
|
|
|
|
}
|
|
|
|
filtered := twiddle.Filter(true)
|
|
|
|
expected := CheckServiceNodes{
|
|
|
|
nodes[1],
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(filtered, expected) {
|
|
|
|
t.Fatalf("bad: %v", filtered)
|
|
|
|
}
|
|
|
|
}
|
2018-04-10 12:28:27 +00:00
|
|
|
|
|
|
|
// Allow failing checks to be ignored (note that the test checks have empty
|
|
|
|
// CheckID which is valid).
|
|
|
|
{
|
|
|
|
twiddle := make(CheckServiceNodes, len(nodes))
|
|
|
|
if n := copy(twiddle, nodes); n != len(nodes) {
|
|
|
|
t.Fatalf("bad: %d", n)
|
|
|
|
}
|
|
|
|
filtered := twiddle.FilterIgnore(true, []types.CheckID{""})
|
|
|
|
expected := CheckServiceNodes{
|
|
|
|
nodes[0],
|
|
|
|
nodes[1],
|
|
|
|
nodes[2], // Node 3's critical check should be ignored.
|
|
|
|
// Node 4 should still be failing since it's got a critical check with a
|
|
|
|
// non-ignored ID.
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(filtered, expected) {
|
|
|
|
t.Fatalf("bad: %v", filtered)
|
|
|
|
}
|
|
|
|
}
|
2015-11-07 00:59:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 20:41:04 +00:00
|
|
|
func TestStructs_DirEntry_Clone(t *testing.T) {
|
|
|
|
e := &DirEntry{
|
|
|
|
LockIndex: 5,
|
2015-10-14 20:46:01 +00:00
|
|
|
Key: "hello",
|
|
|
|
Flags: 23,
|
|
|
|
Value: []byte("this is a test"),
|
|
|
|
Session: "session1",
|
|
|
|
RaftIndex: RaftIndex{
|
2015-10-14 20:41:04 +00:00
|
|
|
CreateIndex: 1,
|
|
|
|
ModifyIndex: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
clone := e.Clone()
|
|
|
|
if !reflect.DeepEqual(e, clone) {
|
|
|
|
t.Fatalf("bad: %v", clone)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Value = []byte("a new value")
|
|
|
|
if reflect.DeepEqual(e, clone) {
|
|
|
|
t.Fatalf("clone wasn't independent of the original")
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 23:53:45 +00:00
|
|
|
|
|
|
|
func TestStructs_ValidateMetadata(t *testing.T) {
|
|
|
|
// Load a valid set of key/value pairs
|
|
|
|
meta := map[string]string{
|
|
|
|
"key1": "value1",
|
|
|
|
"key2": "value2",
|
|
|
|
}
|
|
|
|
// Should succeed
|
2017-08-30 00:02:50 +00:00
|
|
|
if err := ValidateMetadata(meta, false); err != nil {
|
2017-01-23 23:53:45 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should get error
|
|
|
|
meta = map[string]string{
|
|
|
|
"": "value1",
|
|
|
|
}
|
2017-08-30 00:02:50 +00:00
|
|
|
if err := ValidateMetadata(meta, false); !strings.Contains(err.Error(), "Couldn't load metadata pair") {
|
2017-01-23 23:53:45 +00:00
|
|
|
t.Fatalf("should have failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should get error
|
|
|
|
meta = make(map[string]string)
|
|
|
|
for i := 0; i < metaMaxKeyPairs+1; i++ {
|
|
|
|
meta[string(i)] = "value"
|
|
|
|
}
|
2017-08-30 00:02:50 +00:00
|
|
|
if err := ValidateMetadata(meta, false); !strings.Contains(err.Error(), "cannot contain more than") {
|
2017-01-23 23:53:45 +00:00
|
|
|
t.Fatalf("should have failed")
|
|
|
|
}
|
2017-09-01 00:39:46 +00:00
|
|
|
|
|
|
|
// Should not error
|
|
|
|
meta = map[string]string{
|
|
|
|
metaKeyReservedPrefix + "key": "value1",
|
|
|
|
}
|
|
|
|
// Should fail
|
|
|
|
if err := ValidateMetadata(meta, false); err == nil || !strings.Contains(err.Error(), "reserved for internal use") {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
// Should succeed
|
|
|
|
if err := ValidateMetadata(meta, true); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-01-23 23:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructs_validateMetaPair(t *testing.T) {
|
|
|
|
longKey := strings.Repeat("a", metaKeyMaxLength+1)
|
|
|
|
longValue := strings.Repeat("b", metaValueMaxLength+1)
|
|
|
|
pairs := []struct {
|
2017-09-01 00:39:46 +00:00
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
Error string
|
|
|
|
AllowConsulPrefix bool
|
2017-01-23 23:53:45 +00:00
|
|
|
}{
|
|
|
|
// valid pair
|
2017-09-01 00:39:46 +00:00
|
|
|
{"key", "value", "", false},
|
2017-01-23 23:53:45 +00:00
|
|
|
// invalid, blank key
|
2017-09-01 00:39:46 +00:00
|
|
|
{"", "value", "cannot be blank", false},
|
2017-01-23 23:53:45 +00:00
|
|
|
// allowed special chars in key name
|
2017-09-01 00:39:46 +00:00
|
|
|
{"k_e-y", "value", "", false},
|
2017-01-23 23:53:45 +00:00
|
|
|
// disallowed special chars in key name
|
2017-09-01 00:39:46 +00:00
|
|
|
{"(%key&)", "value", "invalid characters", false},
|
2017-01-23 23:53:45 +00:00
|
|
|
// key too long
|
2017-09-01 00:39:46 +00:00
|
|
|
{longKey, "value", "Key is too long", false},
|
2017-01-23 23:53:45 +00:00
|
|
|
// reserved prefix
|
2017-09-01 00:39:46 +00:00
|
|
|
{metaKeyReservedPrefix + "key", "value", "reserved for internal use", false},
|
|
|
|
// reserved prefix, allowed
|
|
|
|
{metaKeyReservedPrefix + "key", "value", "", true},
|
2017-01-23 23:53:45 +00:00
|
|
|
// value too long
|
2017-09-01 00:39:46 +00:00
|
|
|
{"key", longValue, "Value is too long", false},
|
2017-01-23 23:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pair := range pairs {
|
2017-09-01 00:39:46 +00:00
|
|
|
err := validateMetaPair(pair.Key, pair.Value, pair.AllowConsulPrefix)
|
2017-01-23 23:53:45 +00:00
|
|
|
if pair.Error == "" && err != nil {
|
|
|
|
t.Fatalf("should have succeeded: %v, %v", pair, err)
|
|
|
|
} else if pair.Error != "" && !strings.Contains(err.Error(), pair.Error) {
|
|
|
|
t.Fatalf("should have failed: %v, %v", pair, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 21:30:47 +00:00
|
|
|
|
|
|
|
func TestSpecificServiceRequest_CacheInfo(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
req ServiceSpecificRequest
|
|
|
|
mutate func(req *ServiceSpecificRequest)
|
|
|
|
want *cache.RequestInfo
|
|
|
|
wantSame bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic params",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
QueryOptions: QueryOptions{Token: "foo"},
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
|
|
|
want: &cache.RequestInfo{
|
|
|
|
Token: "foo",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
|
|
|
wantSame: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "name should be considered",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
ServiceName: "web",
|
|
|
|
},
|
|
|
|
mutate: func(req *ServiceSpecificRequest) {
|
|
|
|
req.ServiceName = "db"
|
|
|
|
},
|
|
|
|
wantSame: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "node meta should be considered",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
NodeMetaFilters: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mutate: func(req *ServiceSpecificRequest) {
|
|
|
|
req.NodeMetaFilters = map[string]string{
|
|
|
|
"foo": "qux",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
wantSame: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "address should be considered",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
ServiceAddress: "1.2.3.4",
|
|
|
|
},
|
|
|
|
mutate: func(req *ServiceSpecificRequest) {
|
|
|
|
req.ServiceAddress = "4.3.2.1"
|
|
|
|
},
|
|
|
|
wantSame: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tag filter should be considered",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
TagFilter: true,
|
|
|
|
},
|
|
|
|
mutate: func(req *ServiceSpecificRequest) {
|
|
|
|
req.TagFilter = false
|
|
|
|
},
|
|
|
|
wantSame: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "connect should be considered",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
Connect: true,
|
|
|
|
},
|
|
|
|
mutate: func(req *ServiceSpecificRequest) {
|
|
|
|
req.Connect = false
|
|
|
|
},
|
|
|
|
wantSame: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tags should be different",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
ServiceName: "web",
|
|
|
|
ServiceTags: []string{"foo"},
|
|
|
|
},
|
|
|
|
mutate: func(req *ServiceSpecificRequest) {
|
|
|
|
req.ServiceTags = []string{"foo", "bar"}
|
|
|
|
},
|
|
|
|
wantSame: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tags should not depend on order",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
ServiceName: "web",
|
|
|
|
ServiceTags: []string{"bar", "foo"},
|
|
|
|
},
|
|
|
|
mutate: func(req *ServiceSpecificRequest) {
|
|
|
|
req.ServiceTags = []string{"foo", "bar"}
|
|
|
|
},
|
|
|
|
wantSame: true,
|
|
|
|
},
|
|
|
|
// DEPRECATED (singular-service-tag) - remove this when upgrade RPC compat
|
|
|
|
// with 1.2.x is not required.
|
|
|
|
{
|
|
|
|
name: "legacy requests with singular tag should be different",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
ServiceName: "web",
|
|
|
|
ServiceTag: "foo",
|
|
|
|
},
|
|
|
|
mutate: func(req *ServiceSpecificRequest) {
|
|
|
|
req.ServiceTag = "bar"
|
|
|
|
},
|
|
|
|
wantSame: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
info := tc.req.CacheInfo()
|
|
|
|
if tc.mutate != nil {
|
|
|
|
tc.mutate(&tc.req)
|
|
|
|
}
|
|
|
|
afterInfo := tc.req.CacheInfo()
|
|
|
|
|
|
|
|
// Check key matches or not
|
|
|
|
if tc.wantSame {
|
|
|
|
require.Equal(t, info, afterInfo)
|
|
|
|
} else {
|
|
|
|
require.NotEqual(t, info, afterInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.want != nil {
|
|
|
|
// Reset key since we don't care about the actual hash value as long as
|
|
|
|
// it does/doesn't change appropriately (asserted with wantSame above).
|
|
|
|
info.Key = ""
|
|
|
|
require.Equal(t, *tc.want, info)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 14:51:50 +00:00
|
|
|
|
|
|
|
func TestNodeService_JSON_OmitTaggedAdddresses(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
ns NodeService
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
NodeService{
|
|
|
|
TaggedAddresses: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty",
|
|
|
|
NodeService{
|
|
|
|
TaggedAddresses: make(map[string]ServiceAddress),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
name := tc.name
|
|
|
|
ns := tc.ns
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
data, err := json.Marshal(ns)
|
|
|
|
require.NoError(t, err)
|
|
|
|
var raw map[string]interface{}
|
|
|
|
err = json.Unmarshal(data, &raw)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotContains(t, raw, "TaggedAddresses")
|
|
|
|
require.NotContains(t, raw, "tagged_addresses")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceNode_JSON_OmitServiceTaggedAdddresses(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
sn ServiceNode
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"nil",
|
|
|
|
ServiceNode{
|
|
|
|
ServiceTaggedAddresses: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty",
|
|
|
|
ServiceNode{
|
|
|
|
ServiceTaggedAddresses: make(map[string]ServiceAddress),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
name := tc.name
|
|
|
|
sn := tc.sn
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
data, err := json.Marshal(sn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
var raw map[string]interface{}
|
|
|
|
err = json.Unmarshal(data, &raw)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotContains(t, raw, "ServiceTaggedAddresses")
|
|
|
|
require.NotContains(t, raw, "service_tagged_addresses")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
func TestNode_BestAddress(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
input Node
|
|
|
|
lanAddr string
|
|
|
|
wanAddr string
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeAddr := "10.1.2.3"
|
|
|
|
nodeWANAddr := "198.18.19.20"
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"address": testCase{
|
|
|
|
input: Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: nodeAddr,
|
|
|
|
wanAddr: nodeAddr,
|
|
|
|
},
|
|
|
|
"wan-address": testCase{
|
|
|
|
input: Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: nodeAddr,
|
|
|
|
wanAddr: nodeWANAddr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
name := name
|
|
|
|
tc := tc
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require.Equal(t, tc.lanAddr, tc.input.BestAddress(false))
|
|
|
|
require.Equal(t, tc.wanAddr, tc.input.BestAddress(true))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeService_BestAddress(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
input NodeService
|
|
|
|
lanAddr string
|
|
|
|
lanPort int
|
|
|
|
wanAddr string
|
|
|
|
wanPort int
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceAddr := "10.2.3.4"
|
|
|
|
servicePort := 1234
|
|
|
|
serviceWANAddr := "198.19.20.21"
|
|
|
|
serviceWANPort := 987
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"no-address": testCase{
|
|
|
|
input: NodeService{
|
|
|
|
Port: servicePort,
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: "",
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: "",
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
|
|
|
"service-address": testCase{
|
|
|
|
input: NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceAddr,
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
|
|
|
"service-wan-address": testCase{
|
|
|
|
input: NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
|
|
|
"wan": ServiceAddress{
|
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: serviceWANPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
|
|
|
wanPort: serviceWANPort,
|
|
|
|
},
|
|
|
|
"service-wan-address-default-port": testCase{
|
|
|
|
input: NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
|
|
|
"wan": ServiceAddress{
|
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
|
|
|
"service-wan-address-node-lan": testCase{
|
|
|
|
input: NodeService{
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
|
|
|
"wan": ServiceAddress{
|
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: serviceWANPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: "",
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
|
|
|
wanPort: serviceWANPort,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
name := name
|
|
|
|
tc := tc
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
addr, port := tc.input.BestAddress(false)
|
|
|
|
require.Equal(t, tc.lanAddr, addr)
|
|
|
|
require.Equal(t, tc.lanPort, port)
|
|
|
|
|
|
|
|
addr, port = tc.input.BestAddress(true)
|
|
|
|
require.Equal(t, tc.wanAddr, addr)
|
|
|
|
require.Equal(t, tc.wanPort, port)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckServiceNode_BestAddress(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
input CheckServiceNode
|
|
|
|
lanAddr string
|
|
|
|
lanPort int
|
|
|
|
wanAddr string
|
|
|
|
wanPort int
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeAddr := "10.1.2.3"
|
|
|
|
nodeWANAddr := "198.18.19.20"
|
|
|
|
serviceAddr := "10.2.3.4"
|
|
|
|
servicePort := 1234
|
|
|
|
serviceWANAddr := "198.19.20.21"
|
|
|
|
serviceWANPort := 987
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"node-address": testCase{
|
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Port: servicePort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: nodeAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: nodeAddr,
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
|
|
|
"node-wan-address": testCase{
|
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Port: servicePort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: nodeAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: nodeWANAddr,
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
|
|
|
"service-address": testCase{
|
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
// this will be ignored
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceAddr,
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
|
|
|
"service-wan-address": testCase{
|
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
// this will be ignored
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
|
|
|
"wan": ServiceAddress{
|
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: serviceWANPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
|
|
|
wanPort: serviceWANPort,
|
|
|
|
},
|
|
|
|
"service-wan-address-default-port": testCase{
|
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
// this will be ignored
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
|
|
|
"wan": ServiceAddress{
|
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
|
|
|
"service-wan-address-node-lan": testCase{
|
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
// this will be ignored
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
|
|
|
"wan": ServiceAddress{
|
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: serviceWANPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: nodeAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
|
|
|
wanPort: serviceWANPort,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
name := name
|
|
|
|
tc := tc
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
addr, port := tc.input.BestAddress(false)
|
|
|
|
require.Equal(t, tc.lanAddr, addr)
|
|
|
|
require.Equal(t, tc.lanPort, port)
|
|
|
|
|
|
|
|
addr, port = tc.input.BestAddress(true)
|
|
|
|
require.Equal(t, tc.wanAddr, addr)
|
|
|
|
require.Equal(t, tc.wanPort, port)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|