2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
// testClusterID is the Consul cluster ID for testing.
|
|
|
|
//
|
|
|
|
// NOTE: this is explicitly duplicated from agent/connect:TestClusterID
|
|
|
|
const testClusterID = "11111111-2222-3333-4444-555555555555"
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
func TestAPI_DiscoveryChain_Get(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
config_entries := c.ConfigEntries()
|
|
|
|
discoverychain := c.DiscoveryChain()
|
|
|
|
|
2020-01-20 14:56:56 +00:00
|
|
|
s.WaitForActiveCARoot(t)
|
|
|
|
|
2019-08-02 20:34:54 +00:00
|
|
|
require.True(t, t.Run("read default chain", func(t *testing.T) {
|
|
|
|
resp, _, err := discoverychain.Get("web", nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expect := &DiscoveryChainResponse{
|
|
|
|
Chain: &CompiledDiscoveryChain{
|
|
|
|
ServiceName: "web",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Protocol: "tcp",
|
2022-03-30 15:04:18 +00:00
|
|
|
Default: true,
|
2021-09-07 20:29:32 +00:00
|
|
|
StartNode: "resolver:web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Nodes: map[string]*DiscoveryGraphNode{
|
2021-09-07 20:29:32 +00:00
|
|
|
"resolver:web.default.default.dc1": {
|
2019-08-02 20:34:54 +00:00
|
|
|
Type: DiscoveryGraphNodeTypeResolver,
|
2021-09-07 20:29:32 +00:00
|
|
|
Name: "web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Resolver: &DiscoveryResolver{
|
|
|
|
Default: true,
|
|
|
|
ConnectTimeout: 5 * time.Second,
|
2021-09-07 20:29:32 +00:00
|
|
|
Target: "web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: map[string]*DiscoveryTarget{
|
2021-09-07 20:29:32 +00:00
|
|
|
"web.default.default.dc1": {
|
2022-04-07 21:58:21 +00:00
|
|
|
ID: "web.default.default.dc1",
|
|
|
|
Service: "web",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ConnectTimeout: 5 * time.Second,
|
|
|
|
SNI: "web.default.dc1.internal." + testClusterID + ".consul",
|
|
|
|
Name: "web.default.dc1.internal." + testClusterID + ".consul",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, resp)
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.True(t, t.Run("read default chain; evaluate in dc2", func(t *testing.T) {
|
|
|
|
opts := &DiscoveryChainOptions{
|
|
|
|
EvaluateInDatacenter: "dc2",
|
|
|
|
}
|
|
|
|
resp, _, err := discoverychain.Get("web", opts, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expect := &DiscoveryChainResponse{
|
|
|
|
Chain: &CompiledDiscoveryChain{
|
|
|
|
ServiceName: "web",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Protocol: "tcp",
|
2022-03-30 15:04:18 +00:00
|
|
|
Default: true,
|
2021-09-07 20:29:32 +00:00
|
|
|
StartNode: "resolver:web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
Nodes: map[string]*DiscoveryGraphNode{
|
2021-09-07 20:29:32 +00:00
|
|
|
"resolver:web.default.default.dc2": {
|
2019-08-02 20:34:54 +00:00
|
|
|
Type: DiscoveryGraphNodeTypeResolver,
|
2021-09-07 20:29:32 +00:00
|
|
|
Name: "web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
Resolver: &DiscoveryResolver{
|
|
|
|
Default: true,
|
|
|
|
ConnectTimeout: 5 * time.Second,
|
2021-09-07 20:29:32 +00:00
|
|
|
Target: "web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: map[string]*DiscoveryTarget{
|
2021-09-07 20:29:32 +00:00
|
|
|
"web.default.default.dc2": {
|
2022-04-07 21:58:21 +00:00
|
|
|
ID: "web.default.default.dc2",
|
|
|
|
Service: "web",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
ConnectTimeout: 5 * time.Second,
|
|
|
|
SNI: "web.default.dc2.internal." + testClusterID + ".consul",
|
|
|
|
Name: "web.default.dc2.internal." + testClusterID + ".consul",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, resp)
|
|
|
|
}))
|
|
|
|
|
|
|
|
{ // Now create one config entry.
|
|
|
|
ok, _, err := config_entries.Set(&ServiceResolverConfigEntry{
|
|
|
|
Kind: ServiceResolver,
|
|
|
|
Name: "web",
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, t.Run("read modified chain", func(t *testing.T) {
|
|
|
|
resp, _, err := discoverychain.Get("web", nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expect := &DiscoveryChainResponse{
|
|
|
|
Chain: &CompiledDiscoveryChain{
|
|
|
|
ServiceName: "web",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Protocol: "tcp",
|
2021-09-07 20:29:32 +00:00
|
|
|
StartNode: "resolver:web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Nodes: map[string]*DiscoveryGraphNode{
|
2021-09-07 20:29:32 +00:00
|
|
|
"resolver:web.default.default.dc1": {
|
2019-08-02 20:34:54 +00:00
|
|
|
Type: DiscoveryGraphNodeTypeResolver,
|
2021-09-07 20:29:32 +00:00
|
|
|
Name: "web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
Resolver: &DiscoveryResolver{
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
2021-09-07 20:29:32 +00:00
|
|
|
Target: "web.default.default.dc1",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: map[string]*DiscoveryTarget{
|
2021-09-07 20:29:32 +00:00
|
|
|
"web.default.default.dc1": {
|
2022-04-07 21:58:21 +00:00
|
|
|
ID: "web.default.default.dc1",
|
|
|
|
Service: "web",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ConnectTimeout: 33 * time.Second,
|
|
|
|
SNI: "web.default.dc1.internal." + testClusterID + ".consul",
|
|
|
|
Name: "web.default.dc1.internal." + testClusterID + ".consul",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, resp)
|
|
|
|
}))
|
|
|
|
|
|
|
|
require.True(t, t.Run("read modified chain in dc2 with overrides", func(t *testing.T) {
|
|
|
|
opts := &DiscoveryChainOptions{
|
|
|
|
EvaluateInDatacenter: "dc2",
|
|
|
|
OverrideMeshGateway: MeshGatewayConfig{
|
|
|
|
Mode: MeshGatewayModeLocal,
|
|
|
|
},
|
|
|
|
OverrideProtocol: "grpc",
|
|
|
|
OverrideConnectTimeout: 22 * time.Second,
|
|
|
|
}
|
|
|
|
resp, _, err := discoverychain.Get("web", opts, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expect := &DiscoveryChainResponse{
|
|
|
|
Chain: &CompiledDiscoveryChain{
|
|
|
|
ServiceName: "web",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Protocol: "grpc",
|
|
|
|
CustomizationHash: "98809527",
|
2021-09-07 20:29:32 +00:00
|
|
|
StartNode: "resolver:web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
Nodes: map[string]*DiscoveryGraphNode{
|
2021-09-07 20:29:32 +00:00
|
|
|
"resolver:web.default.default.dc2": {
|
2019-08-02 20:34:54 +00:00
|
|
|
Type: DiscoveryGraphNodeTypeResolver,
|
2021-09-07 20:29:32 +00:00
|
|
|
Name: "web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
Resolver: &DiscoveryResolver{
|
|
|
|
ConnectTimeout: 22 * time.Second,
|
2021-09-07 20:29:32 +00:00
|
|
|
Target: "web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Targets: map[string]*DiscoveryTarget{
|
2021-09-07 20:29:32 +00:00
|
|
|
"web.default.default.dc2": {
|
|
|
|
ID: "web.default.default.dc2",
|
2019-08-02 20:34:54 +00:00
|
|
|
Service: "web",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
MeshGateway: MeshGatewayConfig{
|
|
|
|
Mode: MeshGatewayModeLocal,
|
|
|
|
},
|
2022-04-07 21:58:21 +00:00
|
|
|
ConnectTimeout: 22 * time.Second,
|
|
|
|
SNI: "web.default.dc2.internal." + testClusterID + ".consul",
|
|
|
|
Name: "web.default.dc2.internal." + testClusterID + ".consul",
|
2019-08-02 20:34:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, resp)
|
|
|
|
}))
|
|
|
|
}
|