2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-08-05 18:30:35 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-22 21:00:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-08-05 18:30:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFirstHealthyTarget(t *testing.T) {
|
|
|
|
passing := proxycfg.TestUpstreamNodesInStatus(t, "passing")
|
|
|
|
warning := proxycfg.TestUpstreamNodesInStatus(t, "warning")
|
|
|
|
critical := proxycfg.TestUpstreamNodesInStatus(t, "critical")
|
|
|
|
|
2022-08-23 13:13:43 +00:00
|
|
|
warnOnlyPassingTarget := structs.NewDiscoveryTarget(structs.DiscoveryTargetOpts{
|
|
|
|
Service: "all-warn",
|
|
|
|
Namespace: "default",
|
|
|
|
Partition: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
})
|
2019-08-05 18:30:35 +00:00
|
|
|
warnOnlyPassingTarget.Subset.OnlyPassing = true
|
2022-08-23 13:13:43 +00:00
|
|
|
failOnlyPassingTarget := structs.NewDiscoveryTarget(structs.DiscoveryTargetOpts{
|
|
|
|
Service: "all-fail",
|
|
|
|
Namespace: "default",
|
|
|
|
Partition: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
})
|
2019-08-05 18:30:35 +00:00
|
|
|
failOnlyPassingTarget.Subset.OnlyPassing = true
|
|
|
|
|
|
|
|
targets := map[string]*structs.DiscoveryTarget{
|
2022-08-23 13:13:43 +00:00
|
|
|
"all-ok.default.dc1": structs.NewDiscoveryTarget(structs.DiscoveryTargetOpts{
|
|
|
|
Service: "all-ok",
|
|
|
|
Namespace: "default",
|
|
|
|
Partition: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}),
|
|
|
|
"all-warn.default.dc1": structs.NewDiscoveryTarget(structs.DiscoveryTargetOpts{
|
|
|
|
Service: "all-warn",
|
|
|
|
Namespace: "default",
|
|
|
|
Partition: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}),
|
|
|
|
"all-fail.default.default.dc1": structs.NewDiscoveryTarget(structs.DiscoveryTargetOpts{
|
|
|
|
Service: "all-fail",
|
|
|
|
Namespace: "default",
|
|
|
|
Partition: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}),
|
2019-08-05 18:30:35 +00:00
|
|
|
"all-warn-onlypassing.default.dc1": warnOnlyPassingTarget,
|
|
|
|
"all-fail-onlypassing.default.dc1": failOnlyPassingTarget,
|
|
|
|
}
|
|
|
|
targetHealth := map[string]structs.CheckServiceNodes{
|
|
|
|
"all-ok.default.dc1": passing,
|
|
|
|
"all-warn.default.dc1": warning,
|
2021-09-07 20:29:32 +00:00
|
|
|
"all-fail.default.default.dc1": critical,
|
2019-08-05 18:30:35 +00:00
|
|
|
"all-warn-onlypassing.default.dc1": warning,
|
|
|
|
"all-fail-onlypassing.default.dc1": critical,
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
primary string
|
|
|
|
secondary []string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
primary: "all-ok.default.dc1",
|
|
|
|
expect: "all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
primary: "all-warn.default.dc1",
|
|
|
|
expect: "all-warn.default.dc1",
|
|
|
|
},
|
|
|
|
{
|
2021-09-07 20:29:32 +00:00
|
|
|
primary: "all-fail.default.default.dc1",
|
|
|
|
expect: "all-fail.default.default.dc1",
|
2019-08-05 18:30:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
primary: "all-warn-onlypassing.default.dc1",
|
|
|
|
expect: "all-warn-onlypassing.default.dc1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
primary: "all-fail-onlypassing.default.dc1",
|
|
|
|
expect: "all-fail-onlypassing.default.dc1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
primary: "all-ok.default.dc1",
|
|
|
|
secondary: []string{
|
|
|
|
"all-warn.default.dc1",
|
|
|
|
},
|
|
|
|
expect: "all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
primary: "all-warn.default.dc1",
|
|
|
|
secondary: []string{
|
|
|
|
"all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
expect: "all-warn.default.dc1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
primary: "all-warn-onlypassing.default.dc1",
|
|
|
|
secondary: []string{
|
|
|
|
"all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
expect: "all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
{
|
2021-09-07 20:29:32 +00:00
|
|
|
primary: "all-fail.default.default.dc1",
|
2019-08-05 18:30:35 +00:00
|
|
|
secondary: []string{
|
|
|
|
"all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
expect: "all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
primary: "all-fail-onlypassing.default.dc1",
|
|
|
|
secondary: []string{
|
|
|
|
"all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
expect: "all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
{
|
2021-09-07 20:29:32 +00:00
|
|
|
primary: "all-fail.default.default.dc1",
|
2019-08-05 18:30:35 +00:00
|
|
|
secondary: []string{
|
|
|
|
"all-warn-onlypassing.default.dc1",
|
|
|
|
"all-warn.default.dc1",
|
|
|
|
"all-ok.default.dc1",
|
|
|
|
},
|
|
|
|
expect: "all-warn.default.dc1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
name := fmt.Sprintf("%s and %v", tc.primary, tc.secondary)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
targetID := firstHealthyTarget(targets, targetHealth, tc.primary, tc.secondary)
|
|
|
|
require.Equal(t, tc.expect, targetID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|