2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-07-08 17:01:13 +00:00
|
|
|
package peerstream
|
2022-05-19 21:37:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHealthSnapshot(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
name string
|
|
|
|
in []structs.CheckServiceNode
|
|
|
|
expect *healthSnapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
entMeta := acl.DefaultEnterpriseMeta()
|
|
|
|
|
|
|
|
run := func(t *testing.T, tc testcase) {
|
|
|
|
snap := newHealthSnapshot(tc.in, entMeta.PartitionOrEmpty(), "my-peer")
|
|
|
|
require.Equal(t, tc.expect, snap)
|
|
|
|
}
|
|
|
|
|
|
|
|
newNode := func(id, name, peerName string) *structs.Node {
|
|
|
|
return &structs.Node{
|
|
|
|
ID: types.NodeID(id),
|
|
|
|
Node: name,
|
|
|
|
Partition: entMeta.PartitionOrEmpty(),
|
|
|
|
PeerName: peerName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newService := func(id string, port int, peerName string) *structs.NodeService {
|
|
|
|
return &structs.NodeService{
|
|
|
|
ID: id,
|
|
|
|
Service: "xyz",
|
|
|
|
EnterpriseMeta: *entMeta,
|
|
|
|
PeerName: peerName,
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newCheck := func(node, svcID, peerName string) *structs.HealthCheck {
|
|
|
|
return &structs.HealthCheck{
|
|
|
|
Node: node,
|
|
|
|
ServiceID: svcID,
|
|
|
|
ServiceName: "xyz",
|
|
|
|
CheckID: types.CheckID(svcID + ":check"),
|
|
|
|
Name: "check",
|
|
|
|
EnterpriseMeta: *entMeta,
|
|
|
|
PeerName: peerName,
|
|
|
|
Status: "passing",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testcase{
|
|
|
|
{
|
|
|
|
name: "single",
|
|
|
|
in: []structs.CheckServiceNode{
|
|
|
|
{
|
|
|
|
Node: newNode("abc-123", "abc", ""),
|
|
|
|
Service: newService("xyz-123", 8080, ""),
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
newCheck("abc", "xyz-123", ""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: &healthSnapshot{
|
2022-07-15 14:51:38 +00:00
|
|
|
Nodes: map[string]*nodeSnapshot{
|
|
|
|
"abc": {
|
2022-05-19 21:37:52 +00:00
|
|
|
Node: newNode("abc-123", "abc", "my-peer"),
|
|
|
|
Services: map[structs.ServiceID]*serviceSnapshot{
|
|
|
|
structs.NewServiceID("xyz-123", nil): {
|
|
|
|
Service: newService("xyz-123", 8080, "my-peer"),
|
|
|
|
Checks: map[types.CheckID]*structs.HealthCheck{
|
|
|
|
"xyz-123:check": newCheck("abc", "xyz-123", "my-peer"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple",
|
|
|
|
in: []structs.CheckServiceNode{
|
|
|
|
{
|
2022-07-15 14:51:38 +00:00
|
|
|
Node: newNode("", "abc", ""),
|
2022-05-19 21:37:52 +00:00
|
|
|
Service: newService("xyz-123", 8080, ""),
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
newCheck("abc", "xyz-123", ""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-07-15 14:51:38 +00:00
|
|
|
Node: newNode("", "abc", ""),
|
2022-05-19 21:37:52 +00:00
|
|
|
Service: newService("xyz-789", 8181, ""),
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
newCheck("abc", "xyz-789", ""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Node: newNode("def-456", "def", ""),
|
|
|
|
Service: newService("xyz-456", 9090, ""),
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
newCheck("def", "xyz-456", ""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: &healthSnapshot{
|
2022-07-15 14:51:38 +00:00
|
|
|
Nodes: map[string]*nodeSnapshot{
|
|
|
|
"abc": {
|
|
|
|
Node: newNode("", "abc", "my-peer"),
|
2022-05-19 21:37:52 +00:00
|
|
|
Services: map[structs.ServiceID]*serviceSnapshot{
|
|
|
|
structs.NewServiceID("xyz-123", nil): {
|
|
|
|
Service: newService("xyz-123", 8080, "my-peer"),
|
|
|
|
Checks: map[types.CheckID]*structs.HealthCheck{
|
|
|
|
"xyz-123:check": newCheck("abc", "xyz-123", "my-peer"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
structs.NewServiceID("xyz-789", nil): {
|
|
|
|
Service: newService("xyz-789", 8181, "my-peer"),
|
|
|
|
Checks: map[types.CheckID]*structs.HealthCheck{
|
|
|
|
"xyz-789:check": newCheck("abc", "xyz-789", "my-peer"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-15 14:51:38 +00:00
|
|
|
"def": {
|
2022-05-19 21:37:52 +00:00
|
|
|
Node: newNode("def-456", "def", "my-peer"),
|
|
|
|
Services: map[structs.ServiceID]*serviceSnapshot{
|
|
|
|
structs.NewServiceID("xyz-456", nil): {
|
|
|
|
Service: newService("xyz-456", 9090, "my-peer"),
|
|
|
|
Checks: map[types.CheckID]*structs.HealthCheck{
|
|
|
|
"xyz-456:check": newCheck("def", "xyz-456", "my-peer"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|