2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-10-06 20:54:56 +00:00
|
|
|
package pbservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbcommon"
|
2020-10-06 20:54:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCheckServiceNode_UniqueID(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
2022-03-30 16:51:56 +00:00
|
|
|
csn *CheckServiceNode
|
2020-10-06 20:54:56 +00:00
|
|
|
expected string
|
|
|
|
}
|
2022-03-30 16:51:56 +00:00
|
|
|
fn := func(t *testing.T, tc *testCase) {
|
2020-10-06 20:54:56 +00:00
|
|
|
require.Equal(t, tc.expected, tc.csn.UniqueID())
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCases = []testCase{
|
|
|
|
{
|
|
|
|
name: "full",
|
2022-03-30 16:51:56 +00:00
|
|
|
csn: &CheckServiceNode{
|
2022-05-20 20:27:01 +00:00
|
|
|
Node: &Node{
|
|
|
|
Node: "the-node-name",
|
|
|
|
PeerName: "my-peer",
|
|
|
|
Partition: "the-partition",
|
|
|
|
},
|
2020-10-06 20:54:56 +00:00
|
|
|
Service: &NodeService{
|
2022-05-20 20:27:01 +00:00
|
|
|
ID: "the-service-id",
|
|
|
|
EnterpriseMeta: &pbcommon.EnterpriseMeta{
|
|
|
|
Partition: "the-partition",
|
|
|
|
Namespace: "the-namespace",
|
|
|
|
},
|
|
|
|
PeerName: "my-peer",
|
2020-10-06 20:54:56 +00:00
|
|
|
},
|
|
|
|
},
|
2022-05-20 20:27:01 +00:00
|
|
|
expected: "the-partition/my-peer/the-node-name/the-namespace/the-service-id",
|
2020-10-06 20:54:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without node",
|
2022-03-30 16:51:56 +00:00
|
|
|
csn: &CheckServiceNode{
|
2020-10-06 20:54:56 +00:00
|
|
|
Service: &NodeService{
|
2022-05-20 20:27:01 +00:00
|
|
|
ID: "the-service-id",
|
|
|
|
EnterpriseMeta: &pbcommon.EnterpriseMeta{
|
|
|
|
Partition: "the-partition",
|
|
|
|
Namespace: "the-namespace",
|
|
|
|
},
|
|
|
|
PeerName: "my-peer",
|
2020-10-06 20:54:56 +00:00
|
|
|
},
|
|
|
|
},
|
2022-05-20 20:27:01 +00:00
|
|
|
expected: "the-partition/my-peer/the-namespace/the-service-id",
|
2020-10-06 20:54:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without service",
|
2022-03-30 16:51:56 +00:00
|
|
|
csn: &CheckServiceNode{
|
2022-05-20 20:27:01 +00:00
|
|
|
Node: &Node{
|
|
|
|
Node: "the-node-name",
|
|
|
|
PeerName: "my-peer",
|
|
|
|
Partition: "the-partition",
|
|
|
|
},
|
2020-10-06 20:54:56 +00:00
|
|
|
},
|
2022-05-20 20:27:01 +00:00
|
|
|
expected: "the-partition/my-peer/the-node-name/",
|
2020-10-06 20:54:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without namespace",
|
2022-03-30 16:51:56 +00:00
|
|
|
csn: &CheckServiceNode{
|
2022-05-20 20:27:01 +00:00
|
|
|
Node: &Node{
|
|
|
|
Node: "the-node-name",
|
|
|
|
PeerName: "my-peer",
|
|
|
|
Partition: "the-partition",
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
ID: "the-service-id",
|
|
|
|
PeerName: "my-peer",
|
|
|
|
EnterpriseMeta: &pbcommon.EnterpriseMeta{
|
|
|
|
Partition: "the-partition",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "the-partition/my-peer/the-node-name//the-service-id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without peer name",
|
|
|
|
csn: &CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Node: "the-node-name",
|
|
|
|
Partition: "the-partition",
|
|
|
|
},
|
2020-10-06 20:54:56 +00:00
|
|
|
Service: &NodeService{
|
|
|
|
ID: "the-service-id",
|
2022-05-20 20:27:01 +00:00
|
|
|
EnterpriseMeta: &pbcommon.EnterpriseMeta{
|
|
|
|
Partition: "the-partition",
|
|
|
|
Namespace: "the-namespace",
|
|
|
|
},
|
2020-10-06 20:54:56 +00:00
|
|
|
},
|
|
|
|
},
|
2022-05-20 20:27:01 +00:00
|
|
|
expected: "the-partition//the-node-name/the-namespace/the-service-id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without partition",
|
|
|
|
csn: &CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Node: "the-node-name",
|
|
|
|
PeerName: "my-peer",
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
ID: "the-service-id",
|
|
|
|
PeerName: "my-peer",
|
|
|
|
EnterpriseMeta: &pbcommon.EnterpriseMeta{
|
|
|
|
Namespace: "the-namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "/my-peer/the-node-name/the-namespace/the-service-id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without partition or namespace",
|
|
|
|
csn: &CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Node: "the-node-name",
|
|
|
|
PeerName: "my-peer",
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
ID: "the-service-id",
|
|
|
|
PeerName: "my-peer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "/my-peer/the-node-name//the-service-id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "without partition or namespace or peer name",
|
|
|
|
csn: &CheckServiceNode{
|
|
|
|
Node: &Node{
|
|
|
|
Node: "the-node-name",
|
|
|
|
},
|
|
|
|
Service: &NodeService{
|
|
|
|
ID: "the-service-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "//the-node-name//the-service-id",
|
2020-10-06 20:54:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-30 16:51:56 +00:00
|
|
|
fn(t, &tc)
|
2020-10-06 20:54:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|