2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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"
|
2022-05-12 15:08:50 +00:00
|
|
|
"math/rand"
|
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"
|
2020-02-07 21:50:24 +00:00
|
|
|
"time"
|
2016-06-07 20:24:51 +00:00
|
|
|
|
2020-10-06 23:08:36 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-01-13 00:45:46 +00:00
|
|
|
fuzz "github.com/google/gofuzz"
|
|
|
|
|
2020-10-06 23:08:36 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
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"
|
2020-08-18 22:48:05 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2020-05-01 16:56:34 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2016-06-07 20:24:51 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
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-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) {
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
type testcase struct {
|
|
|
|
name string
|
|
|
|
setup func(*RegisterRequest)
|
|
|
|
expect bool
|
|
|
|
}
|
2016-12-09 00:01:01 +00:00
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
cases := []testcase{
|
|
|
|
{
|
|
|
|
name: "id",
|
|
|
|
setup: func(r *RegisterRequest) {
|
|
|
|
r.ID = "nope"
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "name",
|
|
|
|
setup: func(r *RegisterRequest) {
|
|
|
|
r.Node = "nope"
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "name casing",
|
|
|
|
setup: func(r *RegisterRequest) {
|
|
|
|
r.Node = "TeSt"
|
|
|
|
},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "address",
|
|
|
|
setup: func(r *RegisterRequest) {
|
|
|
|
r.Address = "127.0.0.2"
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dc",
|
|
|
|
setup: func(r *RegisterRequest) {
|
|
|
|
r.Datacenter = "dc2"
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tagged addresses",
|
|
|
|
setup: func(r *RegisterRequest) {
|
|
|
|
r.TaggedAddresses["wan"] = "nope"
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "node meta",
|
|
|
|
setup: func(r *RegisterRequest) {
|
|
|
|
r.NodeMeta["invalid"] = "nope"
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
run := func(t *testing.T, tc testcase) {
|
|
|
|
req := &RegisterRequest{
|
|
|
|
ID: types.NodeID("40e4a748-2192-161a-0510-9bf59fe950b5"),
|
|
|
|
Node: "test",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
TaggedAddresses: make(map[string]string),
|
|
|
|
NodeMeta: map[string]string{
|
|
|
|
"role": "server",
|
|
|
|
},
|
2016-12-09 00:01:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 22:01:46 +00:00
|
|
|
if req.ChangesNode(node) {
|
2022-02-24 22:54:47 +00:00
|
|
|
t.Fatalf("should not change")
|
2017-03-23 22:01:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
tc.setup(req)
|
2017-03-23 22:01:46 +00:00
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
if tc.expect {
|
|
|
|
if !req.ChangesNode(node) {
|
|
|
|
t.Fatalf("should change")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if req.ChangesNode(node) {
|
|
|
|
t.Fatalf("should not change")
|
|
|
|
}
|
2016-12-09 00:01:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
t.Run("skip node update", func(t *testing.T) {
|
|
|
|
req.SkipNodeUpdate = true
|
|
|
|
if req.ChangesNode(node) {
|
|
|
|
t.Fatalf("should skip")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-12-09 00:01:01 +00:00
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
2016-12-09 00:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
2020-06-16 17:19:31 +00:00
|
|
|
"lan": {
|
2019-06-17 14:51:50 +00:00
|
|
|
Address: "127.0.0.2",
|
|
|
|
Port: 8080,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan": {
|
2019-06-17 14:51:50 +00:00
|
|
|
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),
|
|
|
|
ServiceConnect: ServiceConnect{
|
|
|
|
Native: true,
|
|
|
|
},
|
2015-10-14 20:41:04 +00:00
|
|
|
}
|
2015-10-19 20:55:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 22:48:05 +00:00
|
|
|
func TestRegisterRequest_UnmarshalJSON_WithConnectNilDoesNotPanic(t *testing.T) {
|
|
|
|
in := `
|
|
|
|
{
|
|
|
|
"ID": "",
|
|
|
|
"Node": "k8s-sync",
|
|
|
|
"Address": "127.0.0.1",
|
|
|
|
"TaggedAddresses": null,
|
|
|
|
"NodeMeta": {
|
|
|
|
"external-source": "kubernetes"
|
|
|
|
},
|
|
|
|
"Datacenter": "",
|
|
|
|
"Service": {
|
|
|
|
"Kind": "",
|
|
|
|
"ID": "test-service-f8fd5f0f4e6c",
|
|
|
|
"Service": "test-service",
|
|
|
|
"Tags": [
|
|
|
|
"k8s"
|
|
|
|
],
|
|
|
|
"Meta": {
|
|
|
|
"external-k8s-ns": "",
|
|
|
|
"external-source": "kubernetes",
|
|
|
|
"port-stats": "18080"
|
|
|
|
},
|
|
|
|
"Port": 8080,
|
|
|
|
"Address": "192.0.2.10",
|
|
|
|
"EnableTagOverride": false,
|
|
|
|
"CreateIndex": 0,
|
|
|
|
"ModifyIndex": 0,
|
|
|
|
"Connect": null
|
|
|
|
},
|
|
|
|
"Check": null,
|
|
|
|
"SkipNodeUpdate": true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var req RegisterRequest
|
|
|
|
err := lib.DecodeJSON(strings.NewReader(in), &req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2022-02-24 22:54:47 +00:00
|
|
|
|
|
|
|
type testcase struct {
|
|
|
|
name string
|
|
|
|
setup func(*Node)
|
|
|
|
expect bool
|
|
|
|
}
|
|
|
|
cases := []testcase{
|
|
|
|
{
|
|
|
|
name: "id",
|
|
|
|
setup: func(n *Node) {
|
|
|
|
n.ID = types.NodeID("")
|
|
|
|
},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "node",
|
|
|
|
setup: func(n *Node) {
|
|
|
|
n.Node = "other"
|
|
|
|
},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "node casing",
|
|
|
|
setup: func(n *Node) {
|
|
|
|
n.Node = "MyNoDe1"
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dc",
|
|
|
|
setup: func(n *Node) {
|
|
|
|
n.Datacenter = "dcX"
|
|
|
|
},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "address",
|
|
|
|
setup: func(n *Node) {
|
|
|
|
n.Address = "127.0.0.1"
|
|
|
|
},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tagged addresses",
|
|
|
|
setup: func(n *Node) {
|
|
|
|
n.TaggedAddresses = map[string]string{"my": "address"}
|
|
|
|
},
|
|
|
|
expect: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "meta",
|
|
|
|
setup: func(n *Node) {
|
|
|
|
n.Meta = map[string]string{"my": "meta"}
|
|
|
|
},
|
|
|
|
expect: false,
|
2018-10-11 11:42:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
run := func(t *testing.T, tc testcase) {
|
|
|
|
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,
|
|
|
|
},
|
2018-10-11 11:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !n.IsSame(other) || !other.IsSame(n) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
2022-02-24 22:54:47 +00:00
|
|
|
|
|
|
|
tc.setup(other)
|
|
|
|
|
|
|
|
if tc.expect {
|
|
|
|
if !n.IsSame(other) || !other.IsSame(n) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if n.IsSame(other) || other.IsSame(n) {
|
|
|
|
t.Fatalf("should be different, was %#v VS %#v", n, other)
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 11:42:39 +00:00
|
|
|
}
|
2022-02-24 22:54:47 +00:00
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
2018-10-11 11:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructs_ServiceNode_IsSameService(t *testing.T) {
|
2022-02-24 22:54:47 +00:00
|
|
|
const (
|
|
|
|
nodeName = "node1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testcase struct {
|
|
|
|
name string
|
|
|
|
setup func(*ServiceNode)
|
|
|
|
expect bool
|
|
|
|
}
|
|
|
|
cases := []testcase{
|
|
|
|
{
|
|
|
|
name: "ServiceID",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceID = "66fb695a-c782-472f-8d36-4f3edd754b37"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Node",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.Node = "other"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Node casing",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.Node = "NoDe1"
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceAddress",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceAddress = "1.2.3.4"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceEnableTagOverride",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceEnableTagOverride = !sn.ServiceEnableTagOverride
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceKind",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceKind = "newKind"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceMeta",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceMeta = map[string]string{"my": "meta"}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceName",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceName = "duck"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServicePort",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServicePort = 65534
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceTags",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceTags = []string{"new", "tags"}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceWeights",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceWeights = Weights{Passing: 42, Warning: 41}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceProxy",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceProxy = ConnectProxyConfig{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceConnect",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceConnect = ServiceConnect{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceTaggedAddresses",
|
|
|
|
setup: func(sn *ServiceNode) {
|
|
|
|
sn.ServiceTaggedAddresses = nil
|
|
|
|
},
|
|
|
|
},
|
2018-10-11 11:42:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
run := func(t *testing.T, tc testcase) {
|
|
|
|
sn := testServiceNode(t)
|
|
|
|
sn.ServiceWeights = Weights{Passing: 2, Warning: 1}
|
|
|
|
n := sn.ToNodeService().ToServiceNode(nodeName)
|
|
|
|
other := sn.ToNodeService().ToServiceNode(nodeName)
|
|
|
|
|
2018-10-11 11:42:39 +00:00
|
|
|
if !n.IsSameService(other) || !other.IsSameService(n) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
tc.setup(other)
|
2018-10-11 11:42:39 +00:00
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
if tc.expect {
|
|
|
|
if !n.IsSameService(other) || !other.IsSameService(n) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if n.IsSameService(other) || other.IsSameService(n) {
|
|
|
|
t.Fatalf("should be different, was %#v VS %#v", n, other)
|
|
|
|
}
|
2018-10-11 11:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
|
|
|
}
|
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{
|
2020-06-16 17:19:31 +00:00
|
|
|
"valid": {
|
2019-06-18 00:52:01 +00:00
|
|
|
func(x *NodeService) {},
|
|
|
|
"",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"zero-port": {
|
2019-06-18 00:52:01 +00:00
|
|
|
func(x *NodeService) { x.Port = 0 },
|
|
|
|
"Port must be non-zero",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"sidecar-service": {
|
2019-06-18 00:52:01 +00:00
|
|
|
func(x *NodeService) { x.Connect.SidecarService = &ServiceDefinition{} },
|
|
|
|
"cannot have a sidecar service",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-destination-name": {
|
2019-06-18 00:52:01 +00:00
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceName = "foo" },
|
|
|
|
"Proxy.DestinationServiceName configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-destination-id": {
|
2019-06-18 00:52:01 +00:00
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceID = "foo" },
|
|
|
|
"Proxy.DestinationServiceID configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-local-address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
func(x *NodeService) { x.Proxy.LocalServiceAddress = "127.0.0.1" },
|
|
|
|
"Proxy.LocalServiceAddress configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-local-port": {
|
2019-06-18 00:52:01 +00:00
|
|
|
func(x *NodeService) { x.Proxy.LocalServicePort = 36 },
|
|
|
|
"Proxy.LocalServicePort configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-upstreams": {
|
|
|
|
func(x *NodeService) { x.Proxy.Upstreams = []Upstream{{}} },
|
2019-06-18 00:52:01 +00:00
|
|
|
"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))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 16:20:56 +00:00
|
|
|
func TestStructs_NodeService_ValidateTerminatingGateway(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
Modify func(*NodeService)
|
|
|
|
Err string
|
|
|
|
}
|
2020-03-31 16:59:10 +00:00
|
|
|
|
2020-03-26 16:20:56 +00:00
|
|
|
cases := map[string]testCase{
|
2020-06-16 17:19:31 +00:00
|
|
|
"valid": {
|
2020-03-26 16:20:56 +00:00
|
|
|
func(x *NodeService) {},
|
|
|
|
"",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"sidecar-service": {
|
2020-03-26 16:20:56 +00:00
|
|
|
func(x *NodeService) { x.Connect.SidecarService = &ServiceDefinition{} },
|
|
|
|
"cannot have a sidecar service",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-destination-name": {
|
2020-03-26 16:20:56 +00:00
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceName = "foo" },
|
|
|
|
"Proxy.DestinationServiceName configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-destination-id": {
|
2020-03-26 16:20:56 +00:00
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceID = "foo" },
|
|
|
|
"Proxy.DestinationServiceID configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-local-address": {
|
2020-03-26 16:20:56 +00:00
|
|
|
func(x *NodeService) { x.Proxy.LocalServiceAddress = "127.0.0.1" },
|
|
|
|
"Proxy.LocalServiceAddress configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-local-port": {
|
2020-03-26 16:20:56 +00:00
|
|
|
func(x *NodeService) { x.Proxy.LocalServicePort = 36 },
|
|
|
|
"Proxy.LocalServicePort configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-upstreams": {
|
|
|
|
func(x *NodeService) { x.Proxy.Upstreams = []Upstream{{}} },
|
2020-03-26 16:20:56 +00:00
|
|
|
"Proxy.Upstreams configuration is invalid",
|
|
|
|
},
|
2022-05-05 16:13:30 +00:00
|
|
|
"port": {
|
|
|
|
func(x *NodeService) { x.Port = 0 },
|
|
|
|
"Port must be non-zero",
|
|
|
|
},
|
2020-03-26 16:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
ns := TestNodeServiceTerminatingGateway(t, "10.0.0.5")
|
|
|
|
tc.Modify(ns)
|
|
|
|
|
|
|
|
err := ns.Validate()
|
2020-03-31 16:59:10 +00:00
|
|
|
if tc.Err == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructs_NodeService_ValidateIngressGateway(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
Modify func(*NodeService)
|
|
|
|
Err string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
2020-06-16 17:19:31 +00:00
|
|
|
"valid": {
|
2020-03-31 16:59:10 +00:00
|
|
|
func(x *NodeService) {},
|
|
|
|
"",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"sidecar-service": {
|
2020-03-31 16:59:10 +00:00
|
|
|
func(x *NodeService) { x.Connect.SidecarService = &ServiceDefinition{} },
|
|
|
|
"cannot have a sidecar service",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-destination-name": {
|
2020-03-31 16:59:10 +00:00
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceName = "foo" },
|
|
|
|
"Proxy.DestinationServiceName configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-destination-id": {
|
2020-03-31 16:59:10 +00:00
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceID = "foo" },
|
|
|
|
"Proxy.DestinationServiceID configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-local-address": {
|
2020-03-31 16:59:10 +00:00
|
|
|
func(x *NodeService) { x.Proxy.LocalServiceAddress = "127.0.0.1" },
|
|
|
|
"Proxy.LocalServiceAddress configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-local-port": {
|
2020-03-31 16:59:10 +00:00
|
|
|
func(x *NodeService) { x.Proxy.LocalServicePort = 36 },
|
|
|
|
"Proxy.LocalServicePort configuration is invalid",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"proxy-upstreams": {
|
|
|
|
func(x *NodeService) { x.Proxy.Upstreams = []Upstream{{}} },
|
2020-03-31 16:59:10 +00:00
|
|
|
"Proxy.Upstreams configuration is invalid",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
ns := TestNodeServiceIngressGateway(t, "10.0.0.5")
|
|
|
|
tc.Modify(ns)
|
|
|
|
|
|
|
|
err := ns.Validate()
|
2020-03-26 16:20:56 +00:00
|
|
|
if tc.Err == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
func TestStructs_NodeService_ValidateExposeConfig(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
Modify func(*NodeService)
|
|
|
|
Err string
|
|
|
|
}
|
|
|
|
cases := map[string]testCase{
|
|
|
|
"valid": {
|
2021-06-14 20:04:11 +00:00
|
|
|
Modify: func(x *NodeService) {},
|
|
|
|
Err: "",
|
2019-09-26 02:55:52 +00:00
|
|
|
},
|
|
|
|
"empty path": {
|
2021-06-14 20:04:11 +00:00
|
|
|
Modify: func(x *NodeService) { x.Proxy.Expose.Paths[0].Path = "" },
|
|
|
|
Err: "empty path exposed",
|
2019-09-26 02:55:52 +00:00
|
|
|
},
|
|
|
|
"invalid port negative": {
|
2021-06-14 20:04:11 +00:00
|
|
|
Modify: func(x *NodeService) { x.Proxy.Expose.Paths[0].ListenerPort = -1 },
|
|
|
|
Err: "invalid listener port",
|
2019-09-26 02:55:52 +00:00
|
|
|
},
|
|
|
|
"invalid port too large": {
|
2021-06-14 20:04:11 +00:00
|
|
|
Modify: func(x *NodeService) { x.Proxy.Expose.Paths[0].ListenerPort = 65536 },
|
|
|
|
Err: "invalid listener port",
|
2019-09-26 02:55:52 +00:00
|
|
|
},
|
2021-06-14 20:04:11 +00:00
|
|
|
"duplicate paths are allowed": {
|
|
|
|
Modify: func(x *NodeService) {
|
|
|
|
x.Proxy.Expose.Paths[0].Path = "/healthz"
|
|
|
|
x.Proxy.Expose.Paths[1].Path = "/healthz"
|
2019-09-26 02:55:52 +00:00
|
|
|
},
|
2021-06-14 20:04:11 +00:00
|
|
|
Err: "",
|
2019-09-26 02:55:52 +00:00
|
|
|
},
|
2021-06-14 20:04:11 +00:00
|
|
|
"duplicate listener ports are not allowed": {
|
|
|
|
Modify: func(x *NodeService) {
|
2019-09-26 02:55:52 +00:00
|
|
|
x.Proxy.Expose.Paths[0].ListenerPort = 21600
|
|
|
|
x.Proxy.Expose.Paths[1].ListenerPort = 21600
|
|
|
|
},
|
2021-06-14 20:04:11 +00:00
|
|
|
Err: "duplicate listener ports exposed",
|
2019-09-26 02:55:52 +00:00
|
|
|
},
|
|
|
|
"protocol not supported": {
|
2021-06-14 20:04:11 +00:00
|
|
|
Modify: func(x *NodeService) { x.Proxy.Expose.Paths[0].Protocol = "foo" },
|
|
|
|
Err: "protocol 'foo' not supported for path",
|
2019-09-26 02:55:52 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
ns := TestNodeServiceExpose(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) {},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2019-08-09 19:19:30 +00:00
|
|
|
"connect-proxy: no Proxy.DestinationServiceName",
|
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
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2019-08-09 19:19:30 +00:00
|
|
|
"connect-proxy: whitespace Proxy.DestinationServiceName",
|
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
|
|
|
},
|
|
|
|
|
2021-03-20 02:56:02 +00:00
|
|
|
{
|
|
|
|
"connect-proxy: wildcard Proxy.DestinationServiceName",
|
|
|
|
func(x *NodeService) { x.Proxy.DestinationServiceName = "*" },
|
|
|
|
"Proxy.DestinationServiceName must not be",
|
|
|
|
},
|
|
|
|
|
2018-03-09 06:13:35 +00:00
|
|
|
{
|
2019-08-09 19:19:30 +00:00
|
|
|
"connect-proxy: valid Proxy.DestinationServiceName",
|
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 },
|
2022-05-05 16:13:30 +00:00
|
|
|
fmt.Sprintf("Port or SocketPath must be set for a %s", ServiceKindConnectProxy),
|
2018-03-09 06:13:35 +00:00
|
|
|
},
|
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",
|
|
|
|
},
|
2019-08-01 18:26:02 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"connect-proxy: upstream missing type (defaulted)",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{{
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
}}
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: upstream invalid type",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{{
|
|
|
|
DestinationType: "garbage",
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
}}
|
|
|
|
},
|
|
|
|
"unknown upstream destination type",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: upstream empty name",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
}}
|
|
|
|
},
|
|
|
|
"upstream destination name cannot be empty",
|
|
|
|
},
|
2021-03-20 02:56:02 +00:00
|
|
|
{
|
|
|
|
"connect-proxy: upstream wildcard name",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: WildcardSpecifier,
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
}}
|
|
|
|
},
|
|
|
|
"upstream destination name cannot be a wildcard",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: upstream can have wildcard name when centrally configured",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: WildcardSpecifier,
|
|
|
|
CentrallyConfigured: true,
|
|
|
|
}}
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
2019-08-01 18:26:02 +00:00
|
|
|
{
|
|
|
|
"connect-proxy: upstream empty bind port",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 0,
|
|
|
|
}}
|
|
|
|
},
|
2021-03-26 20:00:44 +00:00
|
|
|
"upstream local bind port or local socket path must be defined and nonzero",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: upstream bind port and path defined",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 1,
|
|
|
|
LocalBindSocketPath: "/tmp/socket",
|
|
|
|
}}
|
|
|
|
},
|
|
|
|
"only one of upstream local bind port or local socket path can be defined and nonzero",
|
2019-08-01 18:26:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams almost-but-not-quite-duplicated in various ways",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{ // baseline
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
{ // different bind address
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "bar",
|
|
|
|
LocalBindAddress: "127.0.0.2",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
{ // different datacenter
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
LocalBindPort: 5001,
|
|
|
|
},
|
|
|
|
{ // explicit default namespace
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationNamespace: "default",
|
|
|
|
LocalBindPort: 5003,
|
|
|
|
},
|
|
|
|
{ // different namespace
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationNamespace: "alternate",
|
|
|
|
LocalBindPort: 5002,
|
|
|
|
},
|
|
|
|
{ // different type
|
|
|
|
DestinationType: UpstreamDestTypePreparedQuery,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 5004,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
2021-09-07 20:29:32 +00:00
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams non default partition another dc",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{ // baseline
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationPartition: "foo",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstreams cannot target another datacenter in non default partition",
|
|
|
|
},
|
2019-08-01 18:26:02 +00:00
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams duplicated by port",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstreams cannot contain duplicates",
|
|
|
|
},
|
2021-03-20 01:57:23 +00:00
|
|
|
{
|
|
|
|
"connect-proxy: Centrally configured upstreams can have duplicate ip/port",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
CentrallyConfigured: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "bar",
|
|
|
|
CentrallyConfigured: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
2019-08-01 18:26:02 +00:00
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams duplicated by ip and port",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindAddress: "127.0.0.2",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "bar",
|
|
|
|
LocalBindAddress: "127.0.0.2",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstreams cannot contain duplicates",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams duplicated by ip and port with ip defaulted in one",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindAddress: "127.0.0.1",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstreams cannot contain duplicates",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams duplicated by name",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
LocalBindPort: 5001,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstreams cannot contain duplicates",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams duplicated by name and datacenter",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
LocalBindPort: 5001,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstreams cannot contain duplicates",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams duplicated by name and namespace",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationNamespace: "alternate",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationNamespace: "alternate",
|
|
|
|
LocalBindPort: 5001,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstreams cannot contain duplicates",
|
|
|
|
},
|
2022-04-29 22:12:51 +00:00
|
|
|
{
|
|
|
|
"connect-proxy: valid Upstream.PeerDestination",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationPeer: "peer1",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
2018-03-09 06:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
ns := TestNodeServiceProxy(t)
|
|
|
|
tc.Modify(ns)
|
|
|
|
|
|
|
|
err := ns.Validate()
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
assert.Equal(t, err != nil, tc.Err != "", err)
|
2018-09-27 13:33:12 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
assert.Contains(t, strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
2018-09-27 13:33:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:31:06 +00:00
|
|
|
func TestStructs_NodeService_ValidateConnectProxyWithAgentAutoAssign(t *testing.T) {
|
|
|
|
t.Run("connect-proxy: no port set", func(t *testing.T) {
|
|
|
|
ns := TestNodeServiceProxy(t)
|
|
|
|
ns.Port = 0
|
|
|
|
|
|
|
|
err := ns.ValidateForAgent()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-07 20:29:32 +00:00
|
|
|
func TestStructs_NodeService_ValidateConnectProxy_In_Partition(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Modify func(*NodeService)
|
|
|
|
Err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid",
|
|
|
|
func(x *NodeService) {},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams non default partition another dc",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{ // baseline
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationPartition: "foo",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstreams cannot target another datacenter in non default partition",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: Upstreams non default partition same dc",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{ // baseline
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationPartition: "foo",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
2022-04-29 22:12:51 +00:00
|
|
|
{
|
|
|
|
"connect-proxy: Upstream with peer targets partition different from NodeService",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
DestinationPartition: "part1",
|
|
|
|
DestinationPeer: "peer1",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"upstreams must target peers in the same partition as the service",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"connect-proxy: Upstream with peer defaults to NodeService's peer",
|
|
|
|
func(x *NodeService) {
|
|
|
|
x.Proxy.Upstreams = Upstreams{
|
|
|
|
{
|
|
|
|
DestinationType: UpstreamDestTypeService,
|
|
|
|
DestinationName: "foo",
|
|
|
|
// No DestinationPartition here but we assert that it defaults to "bar" and not "default"
|
|
|
|
DestinationPeer: "peer1",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
2021-09-07 20:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
ns := TestNodeServiceProxyInPartition(t, "bar")
|
|
|
|
tc.Modify(ns)
|
|
|
|
|
|
|
|
err := ns.Validate()
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
assert.Equal(t, err != nil, tc.Err != "", err)
|
2021-09-07 20:29:32 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
assert.Contains(t, strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
2021-09-07 20:29:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 13:33:12 +00:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
ns := TestNodeServiceSidecar(t)
|
|
|
|
tc.Modify(ns)
|
|
|
|
|
|
|
|
err := ns.Validate()
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
assert.Equal(t, err != nil, tc.Err != "", err)
|
2018-03-09 06:13:35 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
assert.Contains(t, strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
2018-03-09 06:13:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 16:13:30 +00:00
|
|
|
func TestStructs_NodeService_ConnectNativeEmptyPortError(t *testing.T) {
|
|
|
|
ns := TestNodeService(t)
|
|
|
|
ns.Connect.Native = true
|
|
|
|
ns.Port = 0
|
|
|
|
err := ns.Validate()
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "Port or SocketPath must be set for a Connect native service.")
|
|
|
|
}
|
|
|
|
|
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{
|
2020-06-16 17:19:31 +00:00
|
|
|
"lan": {
|
2019-06-17 14:51:50 +00:00
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 3456,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan": {
|
2019-06-17 14:51:50 +00:00
|
|
|
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{
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan": {
|
2019-06-17 14:51:50 +00:00
|
|
|
Address: "198.18.0.1",
|
|
|
|
Port: 1234,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"lan": {
|
2019-06-17 14:51:50 +00:00
|
|
|
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) {
|
2022-02-24 22:54:47 +00:00
|
|
|
type testcase struct {
|
|
|
|
name string
|
|
|
|
setup func(*HealthCheck)
|
|
|
|
expect bool
|
2015-10-28 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
cases := []testcase{
|
|
|
|
{
|
|
|
|
name: "Node",
|
|
|
|
setup: func(hc *HealthCheck) {
|
|
|
|
hc.Node = "XXX"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Node casing",
|
|
|
|
setup: func(hc *HealthCheck) {
|
|
|
|
hc.Node = "NoDe1"
|
|
|
|
},
|
|
|
|
expect: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "CheckID",
|
|
|
|
setup: func(hc *HealthCheck) {
|
|
|
|
hc.CheckID = "XXX"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Name",
|
|
|
|
setup: func(hc *HealthCheck) {
|
|
|
|
hc.Name = "XXX"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Status",
|
|
|
|
setup: func(hc *HealthCheck) {
|
|
|
|
hc.Status = "XXX"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Notes",
|
|
|
|
setup: func(hc *HealthCheck) {
|
|
|
|
hc.Notes = "XXX"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Output",
|
|
|
|
setup: func(hc *HealthCheck) {
|
|
|
|
hc.Output = "XXX"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceID",
|
|
|
|
setup: func(hc *HealthCheck) {
|
|
|
|
hc.ServiceID = "XXX"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceName",
|
|
|
|
setup: func(hc *HealthCheck) {
|
|
|
|
hc.ServiceName = "XXX"
|
|
|
|
},
|
2015-10-28 21:32:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
run := func(t *testing.T, tc testcase) {
|
|
|
|
hc := &HealthCheck{
|
|
|
|
Node: "node1",
|
|
|
|
CheckID: "check1",
|
|
|
|
Name: "thecheck",
|
|
|
|
Status: api.HealthPassing,
|
|
|
|
Notes: "it's all good",
|
|
|
|
Output: "lgtm",
|
|
|
|
ServiceID: "service1",
|
|
|
|
ServiceName: "theservice",
|
|
|
|
ServiceTags: []string{"foo"},
|
2016-06-07 20:24:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
if !hc.IsSame(hc) {
|
|
|
|
t.Fatalf("should be equal to itself")
|
2016-06-07 20:24:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
other := &HealthCheck{
|
|
|
|
Node: "node1",
|
|
|
|
CheckID: "check1",
|
|
|
|
Name: "thecheck",
|
|
|
|
Status: api.HealthPassing,
|
|
|
|
Notes: "it's all good",
|
|
|
|
Output: "lgtm",
|
|
|
|
ServiceID: "service1",
|
|
|
|
ServiceName: "theservice",
|
|
|
|
ServiceTags: []string{"foo"},
|
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: 1,
|
|
|
|
ModifyIndex: 2,
|
|
|
|
},
|
2016-06-07 20:24:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 21:32:00 +00:00
|
|
|
if !hc.IsSame(other) || !other.IsSame(hc) {
|
2022-02-24 22:54:47 +00:00
|
|
|
t.Fatalf("should not care about Raft fields")
|
2015-10-28 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
tc.setup(hc)
|
2015-10-28 21:32:00 +00:00
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
if tc.expect {
|
|
|
|
if !hc.IsSame(other) || !other.IsSame(hc) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if hc.IsSame(other) || other.IsSame(hc) {
|
|
|
|
t.Fatalf("should not be the same")
|
|
|
|
}
|
2015-10-28 21:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 22:54:47 +00:00
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 23:08:36 +00:00
|
|
|
func TestCheckServiceNodes_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 23:08:36 +00:00
|
|
|
func TestCheckServiceNodes_Filter(t *testing.T) {
|
2015-11-07 00:59:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-30 18:07:32 +00:00
|
|
|
func TestCheckServiceNode_CanRead(t *testing.T) {
|
2020-10-06 23:08:36 +00:00
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
csn CheckServiceNode
|
|
|
|
authz acl.Authorizer
|
|
|
|
expected acl.EnforcementDecision
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(t *testing.T, tc testCase) {
|
|
|
|
actual := tc.csn.CanRead(tc.authz)
|
|
|
|
require.Equal(t, tc.expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCases = []testCase{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
expected: acl.Deny,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "node read not authorized",
|
|
|
|
csn: CheckServiceNode{
|
|
|
|
Node: &Node{Node: "name"},
|
|
|
|
Service: &NodeService{Service: "service-name"},
|
|
|
|
},
|
2022-11-14 18:36:27 +00:00
|
|
|
authz: aclAuthorizerCheckServiceNode{allowLocalService: true},
|
2020-10-06 23:08:36 +00:00
|
|
|
expected: acl.Deny,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "service read not authorized",
|
|
|
|
csn: CheckServiceNode{
|
|
|
|
Node: &Node{Node: "name"},
|
|
|
|
Service: &NodeService{Service: "service-name"},
|
|
|
|
},
|
2022-11-14 18:36:27 +00:00
|
|
|
authz: aclAuthorizerCheckServiceNode{allowLocalNode: true},
|
2020-10-06 23:08:36 +00:00
|
|
|
expected: acl.Deny,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "read authorized",
|
|
|
|
csn: CheckServiceNode{
|
|
|
|
Node: &Node{Node: "name"},
|
|
|
|
Service: &NodeService{Service: "service-name"},
|
|
|
|
},
|
|
|
|
authz: acl.AllowAll(),
|
|
|
|
expected: acl.Allow,
|
|
|
|
},
|
2022-11-09 21:02:58 +00:00
|
|
|
{
|
2022-11-14 18:36:27 +00:00
|
|
|
name: "can read imported csn if can read imported data",
|
2022-11-09 21:02:58 +00:00
|
|
|
csn: CheckServiceNode{
|
|
|
|
Node: &Node{Node: "name", PeerName: "cluster-2"},
|
|
|
|
Service: &NodeService{Service: "service-name", PeerName: "cluster-2"},
|
|
|
|
},
|
2022-11-14 18:36:27 +00:00
|
|
|
authz: aclAuthorizerCheckServiceNode{allowImported: true},
|
2022-11-09 21:02:58 +00:00
|
|
|
expected: acl.Allow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "can't read imported csn with authz for local services and nodes",
|
|
|
|
csn: CheckServiceNode{
|
|
|
|
Node: &Node{Node: "name", PeerName: "cluster-2"},
|
|
|
|
Service: &NodeService{Service: "service-name", PeerName: "cluster-2"},
|
|
|
|
},
|
2022-11-14 18:36:27 +00:00
|
|
|
authz: aclAuthorizerCheckServiceNode{allowLocalService: true, allowLocalNode: true},
|
2022-11-09 21:02:58 +00:00
|
|
|
expected: acl.Deny,
|
|
|
|
},
|
2020-10-06 23:08:36 +00:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
fn(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type aclAuthorizerCheckServiceNode struct {
|
|
|
|
acl.Authorizer
|
2022-11-14 18:36:27 +00:00
|
|
|
allowLocalNode bool
|
|
|
|
allowLocalService bool
|
|
|
|
allowImported bool
|
2020-10-06 23:08:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 18:36:27 +00:00
|
|
|
func (a aclAuthorizerCheckServiceNode) ServiceRead(_ string, ctx *acl.AuthorizerContext) acl.EnforcementDecision {
|
|
|
|
if ctx.Peer != "" {
|
|
|
|
if a.allowImported {
|
|
|
|
return acl.Allow
|
|
|
|
}
|
|
|
|
return acl.Deny
|
2020-10-06 23:08:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 18:36:27 +00:00
|
|
|
if a.allowLocalService {
|
2020-10-06 23:08:36 +00:00
|
|
|
return acl.Allow
|
|
|
|
}
|
|
|
|
return acl.Deny
|
|
|
|
}
|
|
|
|
|
2022-11-14 18:36:27 +00:00
|
|
|
func (a aclAuthorizerCheckServiceNode) NodeRead(_ string, ctx *acl.AuthorizerContext) acl.EnforcementDecision {
|
|
|
|
if ctx.Peer != "" {
|
|
|
|
if a.allowImported {
|
|
|
|
return acl.Allow
|
|
|
|
}
|
|
|
|
return acl.Deny
|
2022-11-09 21:02:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 18:36:27 +00:00
|
|
|
if a.allowLocalNode {
|
2022-11-09 21:02:58 +00:00
|
|
|
return acl.Allow
|
|
|
|
}
|
|
|
|
return acl.Deny
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
func TestStructs_ValidateServiceAndNodeMetadata(t *testing.T) {
|
|
|
|
tooMuchMeta := make(map[string]string)
|
2017-01-23 23:53:45 +00:00
|
|
|
for i := 0; i < metaMaxKeyPairs+1; i++ {
|
2020-09-24 10:14:07 +00:00
|
|
|
tooMuchMeta[fmt.Sprint(i)] = "value"
|
2017-01-23 23:53:45 +00:00
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
type testcase struct {
|
|
|
|
Meta map[string]string
|
|
|
|
AllowConsulPrefix bool
|
|
|
|
NodeError string
|
|
|
|
ServiceError string
|
|
|
|
GatewayError string
|
2017-09-01 00:39:46 +00:00
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
cases := map[string]testcase{
|
|
|
|
"should succeed": {
|
|
|
|
map[string]string{
|
|
|
|
"key1": "value1",
|
|
|
|
"key2": "value2",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
"invalid key": {
|
|
|
|
map[string]string{
|
|
|
|
"": "value1",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"Couldn't load metadata pair",
|
|
|
|
"Couldn't load metadata pair",
|
|
|
|
"Couldn't load metadata pair",
|
|
|
|
},
|
|
|
|
"too many keys": {
|
|
|
|
tooMuchMeta,
|
|
|
|
false,
|
|
|
|
"cannot contain more than",
|
|
|
|
"cannot contain more than",
|
|
|
|
"cannot contain more than",
|
|
|
|
},
|
|
|
|
"reserved key prefix denied": {
|
|
|
|
map[string]string{
|
2021-12-02 23:42:47 +00:00
|
|
|
MetaKeyReservedPrefix + "key": "value1",
|
2020-03-09 20:59:02 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
"reserved for internal use",
|
|
|
|
"reserved for internal use",
|
|
|
|
"reserved for internal use",
|
|
|
|
},
|
|
|
|
"reserved key prefix allowed": {
|
|
|
|
map[string]string{
|
2021-12-02 23:42:47 +00:00
|
|
|
MetaKeyReservedPrefix + "key": "value1",
|
2020-03-09 20:59:02 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
},
|
2020-05-29 18:19:16 +00:00
|
|
|
"reserved key prefix allowed via an allowlist just for gateway - " + MetaWANFederationKey: {
|
2020-03-09 20:59:02 +00:00
|
|
|
map[string]string{
|
|
|
|
MetaWANFederationKey: "value1",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"reserved for internal use",
|
|
|
|
"reserved for internal use",
|
|
|
|
"",
|
|
|
|
},
|
2017-09-01 00:39:46 +00:00
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
t.Run("ValidateNodeMetadata", func(t *testing.T) {
|
|
|
|
err := ValidateNodeMetadata(tc.Meta, tc.AllowConsulPrefix)
|
|
|
|
if tc.NodeError == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
2020-05-01 16:56:34 +00:00
|
|
|
testutil.RequireErrorContains(t, err, tc.NodeError)
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("ValidateServiceMetadata - typical", func(t *testing.T) {
|
|
|
|
err := ValidateServiceMetadata(ServiceKindTypical, tc.Meta, tc.AllowConsulPrefix)
|
|
|
|
if tc.ServiceError == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
2020-05-01 16:56:34 +00:00
|
|
|
testutil.RequireErrorContains(t, err, tc.ServiceError)
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("ValidateServiceMetadata - mesh-gateway", func(t *testing.T) {
|
|
|
|
err := ValidateServiceMetadata(ServiceKindMeshGateway, tc.Meta, tc.AllowConsulPrefix)
|
|
|
|
if tc.GatewayError == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
2020-05-01 16:56:34 +00:00
|
|
|
testutil.RequireErrorContains(t, err, tc.GatewayError)
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2017-09-01 00:39:46 +00:00
|
|
|
}
|
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
|
2020-03-09 20:59:02 +00:00
|
|
|
AllowConsulKeys map[string]struct{}
|
2017-01-23 23:53:45 +00:00
|
|
|
}{
|
|
|
|
// valid pair
|
2020-03-09 20:59:02 +00:00
|
|
|
{"key", "value", "", false, nil},
|
2017-01-23 23:53:45 +00:00
|
|
|
// invalid, blank key
|
2020-03-09 20:59:02 +00:00
|
|
|
{"", "value", "cannot be blank", false, nil},
|
2017-01-23 23:53:45 +00:00
|
|
|
// allowed special chars in key name
|
2020-03-09 20:59:02 +00:00
|
|
|
{"k_e-y", "value", "", false, nil},
|
2017-01-23 23:53:45 +00:00
|
|
|
// disallowed special chars in key name
|
2020-03-09 20:59:02 +00:00
|
|
|
{"(%key&)", "value", "invalid characters", false, nil},
|
2017-01-23 23:53:45 +00:00
|
|
|
// key too long
|
2020-03-09 20:59:02 +00:00
|
|
|
{longKey, "value", "Key is too long", false, nil},
|
2017-01-23 23:53:45 +00:00
|
|
|
// reserved prefix
|
2021-12-02 23:42:47 +00:00
|
|
|
{MetaKeyReservedPrefix + "key", "value", "reserved for internal use", false, nil},
|
2017-09-01 00:39:46 +00:00
|
|
|
// reserved prefix, allowed
|
2021-12-02 23:42:47 +00:00
|
|
|
{MetaKeyReservedPrefix + "key", "value", "", true, nil},
|
2020-05-29 18:19:16 +00:00
|
|
|
// reserved prefix, not allowed via an allowlist
|
2021-12-02 23:42:47 +00:00
|
|
|
{MetaKeyReservedPrefix + "bad", "value", "reserved for internal use", false, map[string]struct{}{MetaKeyReservedPrefix + "good": {}}},
|
2020-05-29 18:19:16 +00:00
|
|
|
// reserved prefix, allowed via an allowlist
|
2021-12-02 23:42:47 +00:00
|
|
|
{MetaKeyReservedPrefix + "good", "value", "", true, map[string]struct{}{MetaKeyReservedPrefix + "good": {}}},
|
2017-01-23 23:53:45 +00:00
|
|
|
// value too long
|
2020-03-09 20:59:02 +00:00
|
|
|
{"key", longValue, "Value is too long", false, nil},
|
2017-01-23 23:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pair := range pairs {
|
2020-03-09 20:59:02 +00:00
|
|
|
err := validateMetaPair(pair.Key, pair.Value, pair.AllowConsulPrefix, pair.AllowConsulKeys)
|
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
|
|
|
|
2021-01-13 00:20:20 +00:00
|
|
|
func TestDCSpecificRequest_CacheInfoKey(t *testing.T) {
|
2021-05-21 17:31:18 +00:00
|
|
|
assertCacheInfoKeyIsComplete(t, &DCSpecificRequest{})
|
2021-01-13 00:45:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 00:20:20 +00:00
|
|
|
func TestNodeSpecificRequest_CacheInfoKey(t *testing.T) {
|
2021-05-21 17:31:18 +00:00
|
|
|
assertCacheInfoKeyIsComplete(t, &NodeSpecificRequest{})
|
2021-01-13 00:45:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 00:20:20 +00:00
|
|
|
func TestServiceSpecificRequest_CacheInfoKey(t *testing.T) {
|
2021-05-31 21:22:16 +00:00
|
|
|
assertCacheInfoKeyIsComplete(t, &ServiceSpecificRequest{})
|
2021-01-13 00:45:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 00:20:20 +00:00
|
|
|
func TestServiceDumpRequest_CacheInfoKey(t *testing.T) {
|
2021-05-21 17:31:18 +00:00
|
|
|
// ServiceKind is only included when UseServiceKind=true
|
|
|
|
assertCacheInfoKeyIsComplete(t, &ServiceDumpRequest{}, "ServiceKind")
|
2021-01-13 00:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cacheInfoIgnoredFields are fields that can be ignored in all cache.Request types
|
|
|
|
// because the cache itself includes these values in the cache key, or because
|
|
|
|
// they are options used to specify the cache operation, and are not part of the
|
|
|
|
// cache entry value.
|
|
|
|
var cacheInfoIgnoredFields = map[string]bool{
|
|
|
|
// Datacenter is part of the cache key added by the cache itself.
|
|
|
|
"Datacenter": true,
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
// PeerName is part of the cache key added by the cache itself.
|
|
|
|
"PeerName": true,
|
2021-05-31 21:22:16 +00:00
|
|
|
// QuerySource is always the same for every request from a single agent, so it
|
2021-01-13 00:45:46 +00:00
|
|
|
// is excluded from the key.
|
|
|
|
"Source": true,
|
|
|
|
// EnterpriseMeta is an empty struct, so can not be included.
|
|
|
|
enterpriseMetaField: true,
|
|
|
|
}
|
|
|
|
|
2021-05-21 17:31:18 +00:00
|
|
|
// assertCacheInfoKeyIsComplete is an assertion to verify that all fields on a request
|
|
|
|
// struct are considered as part of the cache key. It is used to prevent regressions
|
|
|
|
// when new fields are added to the struct. If a field is not included in the cache
|
|
|
|
// key it can lead to API requests or DNS requests returning the wrong value
|
|
|
|
// because a request matches the wrong entry in the agent/cache.Cache.
|
|
|
|
func assertCacheInfoKeyIsComplete(t *testing.T, request cache.Request, ignoredFields ...string) {
|
2021-01-13 00:20:20 +00:00
|
|
|
t.Helper()
|
2021-05-21 17:31:18 +00:00
|
|
|
|
|
|
|
ignored := make(map[string]bool, len(ignoredFields))
|
|
|
|
for _, f := range ignoredFields {
|
|
|
|
ignored[f] = true
|
|
|
|
}
|
|
|
|
|
2021-01-13 00:45:46 +00:00
|
|
|
fuzzer := fuzz.NewWithSeed(time.Now().UnixNano())
|
|
|
|
fuzzer.Funcs(randQueryOptions)
|
|
|
|
fuzzer.Fuzz(request)
|
|
|
|
requestValue := reflect.ValueOf(request).Elem()
|
|
|
|
|
|
|
|
for i := 0; i < requestValue.NumField(); i++ {
|
|
|
|
originalKey := request.CacheInfo().Key
|
|
|
|
field := requestValue.Field(i)
|
|
|
|
fieldName := requestValue.Type().Field(i).Name
|
|
|
|
originalValue := field.Interface()
|
|
|
|
|
2021-05-21 17:31:18 +00:00
|
|
|
if cacheInfoIgnoredFields[fieldName] || ignored[fieldName] {
|
2021-01-13 00:45:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; reflect.DeepEqual(originalValue, field.Interface()) && i < 20; i++ {
|
|
|
|
fuzzer.Fuzz(field.Addr().Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
key := request.CacheInfo().Key
|
|
|
|
if originalKey == key {
|
2021-05-31 21:22:16 +00:00
|
|
|
t.Fatalf("expected field %v to be represented in the CacheInfo.Key, %v change to %v (key: %v)",
|
2021-01-13 00:45:46 +00:00
|
|
|
fieldName,
|
|
|
|
originalValue,
|
2021-05-31 21:22:16 +00:00
|
|
|
field.Interface(),
|
|
|
|
key)
|
2021-01-13 00:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randQueryOptions(o *QueryOptions, c fuzz.Continue) {
|
|
|
|
c.Fuzz(&o.Filter)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
2020-12-18 17:43:16 +00:00
|
|
|
{
|
|
|
|
name: "with integress=true",
|
|
|
|
req: ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "my-service",
|
|
|
|
},
|
|
|
|
mutate: func(req *ServiceSpecificRequest) {
|
|
|
|
req.Ingress = true
|
|
|
|
},
|
|
|
|
},
|
2019-01-07 21:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
input Node
|
|
|
|
lanAddr string
|
|
|
|
wanAddr string
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeAddr := "10.1.2.3"
|
|
|
|
nodeWANAddr := "198.18.19.20"
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
2020-06-16 17:19:31 +00:00
|
|
|
"address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: nodeAddr,
|
|
|
|
wanAddr: nodeAddr,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan-address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
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) {
|
|
|
|
|
|
|
|
require.Equal(t, tc.lanAddr, tc.input.BestAddress(false))
|
|
|
|
require.Equal(t, tc.wanAddr, tc.input.BestAddress(true))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeService_BestAddress(t *testing.T) {
|
|
|
|
|
|
|
|
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{
|
2020-06-16 17:19:31 +00:00
|
|
|
"no-address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: NodeService{
|
|
|
|
Port: servicePort,
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: "",
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: "",
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service-address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceAddr,
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service-wan-address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan": {
|
2019-06-18 00:52:01 +00:00
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: serviceWANPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
|
|
|
wanPort: serviceWANPort,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service-wan-address-default-port": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan": {
|
2019-06-18 00:52:01 +00:00
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service-wan-address-node-lan": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: NodeService{
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan": {
|
2019-06-18 00:52:01 +00:00
|
|
|
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) {
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
input CheckServiceNode
|
|
|
|
lanAddr string
|
|
|
|
lanPort int
|
2022-01-28 06:49:06 +00:00
|
|
|
lanIdx uint64
|
2019-06-18 00:52:01 +00:00
|
|
|
wanAddr string
|
|
|
|
wanPort int
|
2022-01-28 06:49:06 +00:00
|
|
|
wanIdx uint64
|
2019-06-18 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeAddr := "10.1.2.3"
|
|
|
|
nodeWANAddr := "198.18.19.20"
|
2022-01-28 06:49:06 +00:00
|
|
|
nodeIdx := uint64(11)
|
2019-06-18 00:52:01 +00:00
|
|
|
serviceAddr := "10.2.3.4"
|
|
|
|
servicePort := 1234
|
2022-01-28 06:49:06 +00:00
|
|
|
serviceIdx := uint64(22)
|
2019-06-18 00:52:01 +00:00
|
|
|
serviceWANAddr := "198.19.20.21"
|
|
|
|
serviceWANPort := 987
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
2020-06-16 17:19:31 +00:00
|
|
|
"node-address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: nodeIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Port: servicePort,
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: serviceIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: nodeAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
lanIdx: nodeIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: nodeAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
wanIdx: nodeIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"node-wan-address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: nodeIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Port: servicePort,
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: serviceIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: nodeAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
lanIdx: nodeIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: nodeWANAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
wanIdx: nodeIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service-address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
// this will be ignored
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: nodeIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: serviceIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
lanIdx: serviceIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
wanIdx: serviceIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service-wan-address": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
// this will be ignored
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: nodeIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan": {
|
2019-06-18 00:52:01 +00:00
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: serviceWANPort,
|
|
|
|
},
|
|
|
|
},
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: serviceIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
lanIdx: serviceIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
wanIdx: serviceIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
wanPort: serviceWANPort,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service-wan-address-default-port": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
// this will be ignored
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: nodeIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Address: serviceAddr,
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan": {
|
2019-06-18 00:52:01 +00:00
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: 0,
|
|
|
|
},
|
|
|
|
},
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: serviceIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: serviceAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
lanIdx: serviceIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
wanIdx: serviceIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
wanPort: servicePort,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service-wan-address-node-lan": {
|
2019-06-18 00:52:01 +00:00
|
|
|
input: CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Address: nodeAddr,
|
|
|
|
// this will be ignored
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": nodeWANAddr,
|
|
|
|
},
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: nodeIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
Port: servicePort,
|
|
|
|
TaggedAddresses: map[string]ServiceAddress{
|
2020-06-16 17:19:31 +00:00
|
|
|
"wan": {
|
2019-06-18 00:52:01 +00:00
|
|
|
Address: serviceWANAddr,
|
|
|
|
Port: serviceWANPort,
|
|
|
|
},
|
|
|
|
},
|
2022-01-28 06:49:06 +00:00
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
ModifyIndex: serviceIdx,
|
|
|
|
},
|
2019-06-18 00:52:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
lanAddr: nodeAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
lanIdx: nodeIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
lanPort: servicePort,
|
|
|
|
wanAddr: serviceWANAddr,
|
2022-01-28 06:49:06 +00:00
|
|
|
wanIdx: serviceIdx,
|
2019-06-18 00:52:01 +00:00
|
|
|
wanPort: serviceWANPort,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
name := name
|
|
|
|
tc := tc
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
2022-01-28 06:49:06 +00:00
|
|
|
idx, addr, port := tc.input.BestAddress(false)
|
2019-06-18 00:52:01 +00:00
|
|
|
require.Equal(t, tc.lanAddr, addr)
|
|
|
|
require.Equal(t, tc.lanPort, port)
|
2022-01-28 06:49:06 +00:00
|
|
|
require.Equal(t, tc.lanIdx, idx)
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2022-01-28 06:49:06 +00:00
|
|
|
idx, addr, port = tc.input.BestAddress(true)
|
2019-06-18 00:52:01 +00:00
|
|
|
require.Equal(t, tc.wanAddr, addr)
|
|
|
|
require.Equal(t, tc.wanPort, port)
|
2022-01-28 06:49:06 +00:00
|
|
|
require.Equal(t, tc.wanIdx, idx)
|
2019-06-18 00:52:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 16:57:31 +00:00
|
|
|
|
|
|
|
func TestNodeService_JSON_Marshal(t *testing.T) {
|
|
|
|
ns := &NodeService{
|
|
|
|
Service: "foo",
|
|
|
|
Proxy: ConnectProxyConfig{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"bind_addresses": map[string]interface{}{
|
|
|
|
"default": map[string]interface{}{
|
|
|
|
"Address": "0.0.0.0",
|
|
|
|
"Port": "443",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(ns)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var out NodeService
|
|
|
|
require.NoError(t, json.Unmarshal(buf, &out))
|
|
|
|
require.Equal(t, *ns, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceNode_JSON_Marshal(t *testing.T) {
|
|
|
|
sn := &ServiceNode{
|
|
|
|
Node: "foo",
|
|
|
|
ServiceName: "foo",
|
|
|
|
ServiceProxy: ConnectProxyConfig{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"bind_addresses": map[string]interface{}{
|
|
|
|
"default": map[string]interface{}{
|
|
|
|
"Address": "0.0.0.0",
|
|
|
|
"Port": "443",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(sn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var out ServiceNode
|
|
|
|
require.NoError(t, json.Unmarshal(buf, &out))
|
|
|
|
require.Equal(t, *sn, out)
|
|
|
|
}
|
2020-02-07 21:50:24 +00:00
|
|
|
|
|
|
|
// frankensteinStruct is an amalgamation of all of the different kinds of
|
|
|
|
// fields you could have on struct defined in the agent/structs package that we
|
|
|
|
// send through msgpack
|
|
|
|
type frankensteinStruct struct {
|
|
|
|
Child *monsterStruct
|
|
|
|
ChildSlice []*monsterStruct
|
|
|
|
ChildMap map[string]*monsterStruct
|
|
|
|
}
|
|
|
|
type monsterStruct struct {
|
|
|
|
Bool bool
|
|
|
|
Int int
|
|
|
|
Uint8 uint8
|
|
|
|
Uint64 uint64
|
|
|
|
Float32 float32
|
|
|
|
Float64 float64
|
|
|
|
String string
|
|
|
|
|
|
|
|
Hash []byte
|
|
|
|
Uint32Slice []uint32
|
|
|
|
Float64Slice []float64
|
|
|
|
StringSlice []string
|
|
|
|
|
|
|
|
MapInt map[string]int
|
|
|
|
MapString map[string]string
|
|
|
|
MapStringSlice map[string][]string
|
|
|
|
|
|
|
|
// We explicitly DO NOT try to test the following types that involve
|
|
|
|
// interface{} as the TestMsgpackEncodeDecode test WILL fail.
|
|
|
|
//
|
|
|
|
// These are tested elsewhere for the very specific scenario in question,
|
|
|
|
// which usually takes a secondary trip through mapstructure during decode
|
|
|
|
// which papers over some of the additional conversions necessary to finish
|
|
|
|
// decoding.
|
|
|
|
// MapIface map[string]interface{}
|
|
|
|
// MapMapIface map[string]map[string]interface{}
|
|
|
|
|
|
|
|
Dur time.Duration
|
|
|
|
DurPtr *time.Duration
|
|
|
|
Time time.Time
|
|
|
|
TimePtr *time.Time
|
|
|
|
|
|
|
|
RaftIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeFrank() *frankensteinStruct {
|
|
|
|
return &frankensteinStruct{
|
|
|
|
Child: makeMonster(),
|
|
|
|
ChildSlice: []*monsterStruct{
|
|
|
|
makeMonster(),
|
|
|
|
makeMonster(),
|
|
|
|
},
|
|
|
|
ChildMap: map[string]*monsterStruct{
|
|
|
|
"one": makeMonster(), // only put one key in here so the map order is fixed
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeMonster() *monsterStruct {
|
|
|
|
var d time.Duration = 9 * time.Hour
|
|
|
|
var t time.Time = time.Date(2008, 1, 2, 3, 4, 5, 0, time.UTC)
|
|
|
|
|
|
|
|
return &monsterStruct{
|
|
|
|
Bool: true,
|
|
|
|
Int: -8,
|
|
|
|
Uint8: 5,
|
|
|
|
Uint64: 9,
|
|
|
|
Float32: 5.25,
|
|
|
|
Float64: 99.5,
|
|
|
|
String: "strval",
|
|
|
|
|
|
|
|
Hash: []byte("hello"),
|
|
|
|
Uint32Slice: []uint32{1, 2, 3, 4},
|
|
|
|
Float64Slice: []float64{9.2, 6.25},
|
|
|
|
StringSlice: []string{"foo", "bar"},
|
|
|
|
|
|
|
|
// // MapIface will hold an amalgam of what AuthMethods and
|
|
|
|
// // CAConfigurations use in 'Config'
|
|
|
|
// MapIface: map[string]interface{}{
|
|
|
|
// "Name": "inner",
|
|
|
|
// "Dur": "5s",
|
|
|
|
// "Bool": true,
|
|
|
|
// "Float": 15.25,
|
|
|
|
// "Int": int64(94),
|
|
|
|
// "Nested": map[string]string{ // this doesn't survive
|
|
|
|
// "foo": "bar",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// // MapMapIface map[string]map[string]interface{}
|
|
|
|
|
|
|
|
MapInt: map[string]int{
|
|
|
|
"int": 5,
|
|
|
|
},
|
|
|
|
MapString: map[string]string{
|
|
|
|
"aaa": "bbb",
|
|
|
|
},
|
|
|
|
MapStringSlice: map[string][]string{
|
2020-06-16 17:19:31 +00:00
|
|
|
"aaa": {"bbb"},
|
2020-02-07 21:50:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Dur: 5 * time.Second,
|
|
|
|
DurPtr: &d,
|
|
|
|
Time: t.Add(-5 * time.Hour),
|
|
|
|
TimePtr: &t,
|
|
|
|
|
|
|
|
RaftIndex: RaftIndex{
|
|
|
|
CreateIndex: 1,
|
|
|
|
ModifyIndex: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStructs_MsgpackEncodeDecode_Monolith(t *testing.T) {
|
|
|
|
t.Run("monster", func(t *testing.T) {
|
|
|
|
in := makeMonster()
|
|
|
|
TestMsgpackEncodeDecode(t, in, false)
|
|
|
|
})
|
|
|
|
t.Run("frankenstein", func(t *testing.T) {
|
|
|
|
in := makeFrank()
|
|
|
|
TestMsgpackEncodeDecode(t, in, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSnapshotRequestResponse_MsgpackEncodeDecode(t *testing.T) {
|
|
|
|
t.Run("request", func(t *testing.T) {
|
|
|
|
in := &SnapshotRequest{
|
|
|
|
Datacenter: "foo",
|
|
|
|
Token: "blah",
|
|
|
|
AllowStale: true,
|
|
|
|
Op: SnapshotRestore,
|
|
|
|
}
|
|
|
|
TestMsgpackEncodeDecode(t, in, true)
|
|
|
|
})
|
|
|
|
t.Run("response", func(t *testing.T) {
|
|
|
|
in := &SnapshotResponse{
|
|
|
|
Error: "blah",
|
|
|
|
QueryMeta: QueryMeta{
|
2021-12-03 17:11:26 +00:00
|
|
|
Index: 3,
|
|
|
|
LastContact: 5 * time.Second,
|
|
|
|
KnownLeader: true,
|
|
|
|
ConsistencyLevel: "default",
|
|
|
|
ResultsFilteredByACLs: true,
|
2020-02-07 21:50:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
TestMsgpackEncodeDecode(t, in, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2020-04-08 18:37:24 +00:00
|
|
|
func TestGatewayService_IsSame(t *testing.T) {
|
2020-06-12 14:57:41 +00:00
|
|
|
gateway := NewServiceName("gateway", nil)
|
|
|
|
svc := NewServiceName("web", nil)
|
2020-04-08 18:37:24 +00:00
|
|
|
kind := ServiceKindTerminatingGateway
|
|
|
|
ca := "ca.pem"
|
|
|
|
cert := "client.pem"
|
|
|
|
key := "tls.key"
|
2020-04-27 22:25:37 +00:00
|
|
|
sni := "mydomain"
|
|
|
|
wildcard := false
|
2020-04-08 18:37:24 +00:00
|
|
|
|
|
|
|
g := &GatewayService{
|
2020-04-27 22:25:37 +00:00
|
|
|
Gateway: gateway,
|
|
|
|
Service: svc,
|
|
|
|
GatewayKind: kind,
|
|
|
|
CAFile: ca,
|
|
|
|
CertFile: cert,
|
|
|
|
KeyFile: key,
|
|
|
|
SNI: sni,
|
|
|
|
FromWildcard: wildcard,
|
2020-04-08 18:37:24 +00:00
|
|
|
}
|
|
|
|
other := &GatewayService{
|
2020-04-27 22:25:37 +00:00
|
|
|
Gateway: gateway,
|
|
|
|
Service: svc,
|
|
|
|
GatewayKind: kind,
|
|
|
|
CAFile: ca,
|
|
|
|
CertFile: cert,
|
|
|
|
KeyFile: key,
|
|
|
|
SNI: sni,
|
|
|
|
FromWildcard: wildcard,
|
2020-04-08 18:37:24 +00:00
|
|
|
}
|
|
|
|
check := func(twiddle, restore func()) {
|
|
|
|
t.Helper()
|
|
|
|
if !g.IsSame(other) || !other.IsSame(g) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
twiddle()
|
|
|
|
if g.IsSame(other) || other.IsSame(g) {
|
|
|
|
t.Fatalf("should be different, was %#v VS %#v", g, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
restore()
|
|
|
|
if !g.IsSame(other) || !other.IsSame(g) {
|
|
|
|
t.Fatalf("should be the same")
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 14:57:41 +00:00
|
|
|
check(func() { other.Gateway = NewServiceName("other", nil) }, func() { other.Gateway = gateway })
|
|
|
|
check(func() { other.Service = NewServiceName("other", nil) }, func() { other.Service = svc })
|
2020-04-08 18:37:24 +00:00
|
|
|
check(func() { other.GatewayKind = ServiceKindIngressGateway }, func() { other.GatewayKind = kind })
|
|
|
|
check(func() { other.CAFile = "/certs/cert.pem" }, func() { other.CAFile = ca })
|
|
|
|
check(func() { other.CertFile = "/certs/cert.pem" }, func() { other.CertFile = cert })
|
|
|
|
check(func() { other.KeyFile = "/certs/cert.pem" }, func() { other.KeyFile = key })
|
2020-04-27 22:25:37 +00:00
|
|
|
check(func() { other.SNI = "alt-domain" }, func() { other.SNI = sni })
|
|
|
|
check(func() { other.FromWildcard = true }, func() { other.FromWildcard = wildcard })
|
2020-04-08 18:37:24 +00:00
|
|
|
|
|
|
|
if !g.IsSame(other) {
|
|
|
|
t.Fatalf("should be equal, was %#v VS %#v", g, other)
|
|
|
|
}
|
|
|
|
}
|
2022-05-12 15:08:50 +00:00
|
|
|
|
|
|
|
func TestServiceList_Sort(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
name string
|
|
|
|
list []ServiceName
|
|
|
|
expect []ServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
run := func(t *testing.T, tc testcase) {
|
|
|
|
t.Run("written order", func(t *testing.T) {
|
|
|
|
ServiceList(tc.list).Sort()
|
|
|
|
require.Equal(t, tc.expect, tc.list)
|
|
|
|
})
|
|
|
|
t.Run("random order", func(t *testing.T) {
|
|
|
|
rand.Shuffle(len(tc.list), func(i, j int) {
|
|
|
|
tc.list[i], tc.list[j] = tc.list[j], tc.list[i]
|
|
|
|
})
|
|
|
|
ServiceList(tc.list).Sort()
|
|
|
|
require.Equal(t, tc.expect, tc.list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sn := func(name string) ServiceName {
|
|
|
|
return NewServiceName(name, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testcase{
|
|
|
|
{
|
|
|
|
name: "nil",
|
|
|
|
list: nil,
|
|
|
|
expect: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
list: []ServiceName{},
|
|
|
|
expect: []ServiceName{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one",
|
|
|
|
list: []ServiceName{sn("foo")},
|
|
|
|
expect: []ServiceName{sn("foo")},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple",
|
|
|
|
list: []ServiceName{
|
|
|
|
sn("food"),
|
|
|
|
sn("zip"),
|
|
|
|
sn("Bar"),
|
|
|
|
sn("ba"),
|
|
|
|
sn("foo"),
|
|
|
|
sn("bar"),
|
|
|
|
sn("Foo"),
|
|
|
|
sn("Zip"),
|
|
|
|
sn("foo"),
|
|
|
|
sn("bar"),
|
|
|
|
sn("barrier"),
|
|
|
|
},
|
|
|
|
expect: []ServiceName{
|
|
|
|
sn("Bar"),
|
|
|
|
sn("Foo"),
|
|
|
|
sn("Zip"),
|
|
|
|
sn("ba"),
|
|
|
|
sn("bar"),
|
|
|
|
sn("bar"),
|
|
|
|
sn("barrier"),
|
|
|
|
sn("foo"),
|
|
|
|
sn("foo"),
|
|
|
|
sn("food"),
|
|
|
|
sn("zip"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|