2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2019-03-22 19:37:14 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
|
|
|
envoy_endpoint_v3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
|
2019-06-18 00:52:01 +00:00
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
2019-03-22 19:37:14 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-08-17 18:43:21 +00:00
|
|
|
"github.com/hashicorp/consul/agent/xds/response"
|
2023-08-17 19:55:54 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/mitchellh/copystructure"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-22 19:37:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_makeLoadAssignment(t *testing.T) {
|
|
|
|
|
|
|
|
testCheckServiceNodes := structs.CheckServiceNodes{
|
|
|
|
structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
ID: "node1-id",
|
|
|
|
Node: "node1",
|
|
|
|
Address: "10.10.10.10",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "web",
|
|
|
|
Port: 1234,
|
|
|
|
},
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Node: "node1",
|
|
|
|
CheckID: "serfHealth",
|
|
|
|
Status: "passing",
|
|
|
|
},
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Node: "node1",
|
|
|
|
ServiceID: "web",
|
|
|
|
CheckID: "web:check",
|
|
|
|
Status: "passing",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
structs.CheckServiceNode{
|
|
|
|
Node: &structs.Node{
|
|
|
|
ID: "node2-id",
|
|
|
|
Node: "node2",
|
|
|
|
Address: "10.10.10.20",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "web",
|
|
|
|
Port: 1234,
|
|
|
|
},
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Node: "node2",
|
|
|
|
CheckID: "serfHealth",
|
|
|
|
Status: "passing",
|
|
|
|
},
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Node: "node2",
|
|
|
|
ServiceID: "web",
|
|
|
|
CheckID: "web:check",
|
|
|
|
Status: "passing",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testWeightedCheckServiceNodesRaw, err := copystructure.Copy(testCheckServiceNodes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
testWeightedCheckServiceNodes := testWeightedCheckServiceNodesRaw.(structs.CheckServiceNodes)
|
|
|
|
|
|
|
|
testWeightedCheckServiceNodes[0].Service.Weights = &structs.Weights{
|
|
|
|
Passing: 10,
|
|
|
|
Warning: 1,
|
|
|
|
}
|
|
|
|
testWeightedCheckServiceNodes[1].Service.Weights = &structs.Weights{
|
|
|
|
Passing: 5,
|
|
|
|
Warning: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
testWarningCheckServiceNodesRaw, err := copystructure.Copy(testWeightedCheckServiceNodes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
testWarningCheckServiceNodes := testWarningCheckServiceNodesRaw.(structs.CheckServiceNodes)
|
|
|
|
|
|
|
|
testWarningCheckServiceNodes[0].Checks[0].Status = "warning"
|
|
|
|
testWarningCheckServiceNodes[1].Checks[0].Status = "warning"
|
|
|
|
|
2019-07-24 01:20:24 +00:00
|
|
|
// TODO(rb): test onlypassing
|
2019-03-22 19:37:14 +00:00
|
|
|
tests := []struct {
|
2019-08-02 20:34:54 +00:00
|
|
|
name string
|
|
|
|
clusterName string
|
2023-06-21 16:39:53 +00:00
|
|
|
locality *structs.Locality
|
2019-08-02 20:34:54 +00:00
|
|
|
endpoints []loadAssignmentEndpointGroup
|
2021-02-26 22:23:15 +00:00
|
|
|
want *envoy_endpoint_v3.ClusterLoadAssignment
|
2019-03-22 19:37:14 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no instances",
|
|
|
|
clusterName: "service:test",
|
2019-07-24 01:20:24 +00:00
|
|
|
endpoints: []loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: nil},
|
2019-07-02 03:10:51 +00:00
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
want: &envoy_endpoint_v3.ClusterLoadAssignment{
|
2019-03-22 19:37:14 +00:00
|
|
|
ClusterName: "service:test",
|
2021-02-26 22:23:15 +00:00
|
|
|
Endpoints: []*envoy_endpoint_v3.LocalityLbEndpoints{{
|
|
|
|
LbEndpoints: []*envoy_endpoint_v3.LbEndpoint{},
|
2019-03-22 19:37:14 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "instances, no weights",
|
|
|
|
clusterName: "service:test",
|
2019-07-24 01:20:24 +00:00
|
|
|
endpoints: []loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: testCheckServiceNodes},
|
2019-07-02 03:10:51 +00:00
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
want: &envoy_endpoint_v3.ClusterLoadAssignment{
|
2019-03-22 19:37:14 +00:00
|
|
|
ClusterName: "service:test",
|
2021-02-26 22:23:15 +00:00
|
|
|
Endpoints: []*envoy_endpoint_v3.LocalityLbEndpoints{{
|
|
|
|
LbEndpoints: []*envoy_endpoint_v3.LbEndpoint{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 18:43:21 +00:00
|
|
|
Address: response.MakeAddress("10.10.10.10", 1234),
|
2019-06-07 12:10:43 +00:00
|
|
|
}},
|
2021-02-26 22:23:15 +00:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 18:43:21 +00:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(1),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 18:43:21 +00:00
|
|
|
Address: response.MakeAddress("10.10.10.20", 1234),
|
2019-06-07 12:10:43 +00:00
|
|
|
}},
|
2021-02-26 22:23:15 +00:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 18:43:21 +00:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(1),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "instances, healthy weights",
|
|
|
|
clusterName: "service:test",
|
2019-07-24 01:20:24 +00:00
|
|
|
endpoints: []loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: testWeightedCheckServiceNodes},
|
2019-07-02 03:10:51 +00:00
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
want: &envoy_endpoint_v3.ClusterLoadAssignment{
|
2019-03-22 19:37:14 +00:00
|
|
|
ClusterName: "service:test",
|
2021-02-26 22:23:15 +00:00
|
|
|
Endpoints: []*envoy_endpoint_v3.LocalityLbEndpoints{{
|
|
|
|
LbEndpoints: []*envoy_endpoint_v3.LbEndpoint{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 18:43:21 +00:00
|
|
|
Address: response.MakeAddress("10.10.10.10", 1234),
|
2019-06-07 12:10:43 +00:00
|
|
|
}},
|
2021-02-26 22:23:15 +00:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 18:43:21 +00:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(10),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 18:43:21 +00:00
|
|
|
Address: response.MakeAddress("10.10.10.20", 1234),
|
2019-06-07 12:10:43 +00:00
|
|
|
}},
|
2021-02-26 22:23:15 +00:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 18:43:21 +00:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(5),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "instances, warning weights",
|
|
|
|
clusterName: "service:test",
|
2019-07-24 01:20:24 +00:00
|
|
|
endpoints: []loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: testWarningCheckServiceNodes},
|
2019-07-02 03:10:51 +00:00
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
want: &envoy_endpoint_v3.ClusterLoadAssignment{
|
2019-03-22 19:37:14 +00:00
|
|
|
ClusterName: "service:test",
|
2021-02-26 22:23:15 +00:00
|
|
|
Endpoints: []*envoy_endpoint_v3.LocalityLbEndpoints{{
|
|
|
|
LbEndpoints: []*envoy_endpoint_v3.LbEndpoint{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 18:43:21 +00:00
|
|
|
Address: response.MakeAddress("10.10.10.10", 1234),
|
2019-06-07 12:10:43 +00:00
|
|
|
}},
|
2021-02-26 22:23:15 +00:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 18:43:21 +00:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(1),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2021-02-26 22:23:15 +00:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 18:43:21 +00:00
|
|
|
Address: response.MakeAddress("10.10.10.20", 1234),
|
2019-06-07 12:10:43 +00:00
|
|
|
}},
|
2021-02-26 22:23:15 +00:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_UNHEALTHY,
|
2023-08-17 18:43:21 +00:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(1),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-07-24 01:20:24 +00:00
|
|
|
got := makeLoadAssignment(
|
2023-08-10 18:00:44 +00:00
|
|
|
hclog.NewNullLogger(),
|
2023-06-21 16:39:53 +00:00
|
|
|
&proxycfg.ConfigSnapshot{ServiceLocality: tt.locality},
|
2019-07-24 01:20:24 +00:00
|
|
|
tt.clusterName,
|
2023-06-21 16:39:53 +00:00
|
|
|
nil,
|
2019-07-24 01:20:24 +00:00
|
|
|
tt.endpoints,
|
2021-10-29 00:41:58 +00:00
|
|
|
proxycfg.GatewayKey{Datacenter: "dc1"},
|
2019-07-24 01:20:24 +00:00
|
|
|
)
|
2019-03-22 19:37:14 +00:00
|
|
|
require.Equal(t, tt.want, got)
|
2023-06-21 16:39:53 +00:00
|
|
|
|
|
|
|
if tt.locality == nil {
|
|
|
|
got := makeLoadAssignment(
|
2023-08-10 18:00:44 +00:00
|
|
|
hclog.NewNullLogger(),
|
2023-06-21 16:39:53 +00:00
|
|
|
&proxycfg.ConfigSnapshot{ServiceLocality: &structs.Locality{Region: "us-west-1", Zone: "us-west-1a"}},
|
|
|
|
tt.clusterName,
|
|
|
|
nil,
|
|
|
|
tt.endpoints,
|
|
|
|
proxycfg.GatewayKey{Datacenter: "dc1"},
|
|
|
|
)
|
|
|
|
require.Equal(t, tt.want, got)
|
|
|
|
}
|
2019-03-22 19:37:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|