2023-03-28 19:39:22 +01:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 09:12:13 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:39:22 +01:00
|
|
|
|
2019-03-22 19:37:14 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-02-26 16:23:15 -06: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-17 20:52:01 -04:00
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
2019-03-22 19:37:14 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-08-17 12:43:21 -06:00
|
|
|
"github.com/hashicorp/consul/agent/xds/response"
|
2023-08-17 13:55:54 -06: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-23 20:20:24 -05:00
|
|
|
// TODO(rb): test onlypassing
|
2019-03-22 19:37:14 +00:00
|
|
|
tests := []struct {
|
2019-08-02 15:34:54 -05:00
|
|
|
name string
|
|
|
|
clusterName string
|
2023-06-21 12:39:53 -04:00
|
|
|
locality *structs.Locality
|
2019-08-02 15:34:54 -05:00
|
|
|
endpoints []loadAssignmentEndpointGroup
|
2021-02-26 16:23:15 -06:00
|
|
|
want *envoy_endpoint_v3.ClusterLoadAssignment
|
2019-03-22 19:37:14 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no instances",
|
|
|
|
clusterName: "service:test",
|
2019-07-23 20:20:24 -05:00
|
|
|
endpoints: []loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: nil},
|
2019-07-01 22:10:51 -05:00
|
|
|
},
|
2021-02-26 16:23:15 -06:00
|
|
|
want: &envoy_endpoint_v3.ClusterLoadAssignment{
|
2019-03-22 19:37:14 +00:00
|
|
|
ClusterName: "service:test",
|
2021-02-26 16:23:15 -06: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-23 20:20:24 -05:00
|
|
|
endpoints: []loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: testCheckServiceNodes},
|
2019-07-01 22:10:51 -05:00
|
|
|
},
|
2021-02-26 16:23:15 -06:00
|
|
|
want: &envoy_endpoint_v3.ClusterLoadAssignment{
|
2019-03-22 19:37:14 +00:00
|
|
|
ClusterName: "service:test",
|
2021-02-26 16:23:15 -06:00
|
|
|
Endpoints: []*envoy_endpoint_v3.LocalityLbEndpoints{{
|
|
|
|
LbEndpoints: []*envoy_endpoint_v3.LbEndpoint{
|
2020-06-16 13:19:31 -04:00
|
|
|
{
|
2021-02-26 16:23:15 -06:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 12:43:21 -06:00
|
|
|
Address: response.MakeAddress("10.10.10.10", 1234),
|
2019-06-07 07:10:43 -05:00
|
|
|
}},
|
2021-02-26 16:23:15 -06:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 12:43:21 -06:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(1),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
2020-06-16 13:19:31 -04:00
|
|
|
{
|
2021-02-26 16:23:15 -06:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 12:43:21 -06:00
|
|
|
Address: response.MakeAddress("10.10.10.20", 1234),
|
2019-06-07 07:10:43 -05:00
|
|
|
}},
|
2021-02-26 16:23:15 -06:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 12:43:21 -06:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(1),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "instances, healthy weights",
|
|
|
|
clusterName: "service:test",
|
2019-07-23 20:20:24 -05:00
|
|
|
endpoints: []loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: testWeightedCheckServiceNodes},
|
2019-07-01 22:10:51 -05:00
|
|
|
},
|
2021-02-26 16:23:15 -06:00
|
|
|
want: &envoy_endpoint_v3.ClusterLoadAssignment{
|
2019-03-22 19:37:14 +00:00
|
|
|
ClusterName: "service:test",
|
2021-02-26 16:23:15 -06:00
|
|
|
Endpoints: []*envoy_endpoint_v3.LocalityLbEndpoints{{
|
|
|
|
LbEndpoints: []*envoy_endpoint_v3.LbEndpoint{
|
2020-06-16 13:19:31 -04:00
|
|
|
{
|
2021-02-26 16:23:15 -06:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 12:43:21 -06:00
|
|
|
Address: response.MakeAddress("10.10.10.10", 1234),
|
2019-06-07 07:10:43 -05:00
|
|
|
}},
|
2021-02-26 16:23:15 -06:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 12:43:21 -06:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(10),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
2020-06-16 13:19:31 -04:00
|
|
|
{
|
2021-02-26 16:23:15 -06:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 12:43:21 -06:00
|
|
|
Address: response.MakeAddress("10.10.10.20", 1234),
|
2019-06-07 07:10:43 -05:00
|
|
|
}},
|
2021-02-26 16:23:15 -06:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 12:43:21 -06:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(5),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "instances, warning weights",
|
|
|
|
clusterName: "service:test",
|
2019-07-23 20:20:24 -05:00
|
|
|
endpoints: []loadAssignmentEndpointGroup{
|
|
|
|
{Endpoints: testWarningCheckServiceNodes},
|
2019-07-01 22:10:51 -05:00
|
|
|
},
|
2021-02-26 16:23:15 -06:00
|
|
|
want: &envoy_endpoint_v3.ClusterLoadAssignment{
|
2019-03-22 19:37:14 +00:00
|
|
|
ClusterName: "service:test",
|
2021-02-26 16:23:15 -06:00
|
|
|
Endpoints: []*envoy_endpoint_v3.LocalityLbEndpoints{{
|
|
|
|
LbEndpoints: []*envoy_endpoint_v3.LbEndpoint{
|
2020-06-16 13:19:31 -04:00
|
|
|
{
|
2021-02-26 16:23:15 -06:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 12:43:21 -06:00
|
|
|
Address: response.MakeAddress("10.10.10.10", 1234),
|
2019-06-07 07:10:43 -05:00
|
|
|
}},
|
2021-02-26 16:23:15 -06:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_HEALTHY,
|
2023-08-17 12:43:21 -06:00
|
|
|
LoadBalancingWeight: response.MakeUint32Value(1),
|
2019-03-22 19:37:14 +00:00
|
|
|
},
|
2020-06-16 13:19:31 -04:00
|
|
|
{
|
2021-02-26 16:23:15 -06:00
|
|
|
HostIdentifier: &envoy_endpoint_v3.LbEndpoint_Endpoint{
|
|
|
|
Endpoint: &envoy_endpoint_v3.Endpoint{
|
2023-08-17 12:43:21 -06:00
|
|
|
Address: response.MakeAddress("10.10.10.20", 1234),
|
2019-06-07 07:10:43 -05:00
|
|
|
}},
|
2021-02-26 16:23:15 -06:00
|
|
|
HealthStatus: envoy_core_v3.HealthStatus_UNHEALTHY,
|
2023-08-17 12:43:21 -06: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-23 20:20:24 -05:00
|
|
|
got := makeLoadAssignment(
|
2023-08-10 14:00:44 -04:00
|
|
|
hclog.NewNullLogger(),
|
2023-06-21 12:39:53 -04:00
|
|
|
&proxycfg.ConfigSnapshot{ServiceLocality: tt.locality},
|
2019-07-23 20:20:24 -05:00
|
|
|
tt.clusterName,
|
2023-06-21 12:39:53 -04:00
|
|
|
nil,
|
2019-07-23 20:20:24 -05:00
|
|
|
tt.endpoints,
|
2021-10-28 18:41:58 -06:00
|
|
|
proxycfg.GatewayKey{Datacenter: "dc1"},
|
2019-07-23 20:20:24 -05:00
|
|
|
)
|
2019-03-22 19:37:14 +00:00
|
|
|
require.Equal(t, tt.want, got)
|
2023-06-21 12:39:53 -04:00
|
|
|
|
|
|
|
if tt.locality == nil {
|
|
|
|
got := makeLoadAssignment(
|
2023-08-10 14:00:44 -04:00
|
|
|
hclog.NewNullLogger(),
|
2023-06-21 12:39:53 -04: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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|