2024-01-17 23:46:18 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
2024-01-29 22:33:45 +00:00
|
|
|
"context"
|
2024-01-17 23:46:18 +00:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2024-01-29 16:40:10 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2024-02-02 23:29:38 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
2024-01-17 23:46:18 +00:00
|
|
|
cachetype "github.com/hashicorp/consul/agent/cache-types"
|
|
|
|
"github.com/hashicorp/consul/agent/config"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
)
|
|
|
|
|
2024-01-30 22:34:35 +00:00
|
|
|
// Test_FetchVirtualIP tests the FetchVirtualIP method in scenarios where the RPC
|
2024-01-17 23:46:18 +00:00
|
|
|
// call succeeds and fails.
|
|
|
|
func Test_FetchVirtualIP(t *testing.T) {
|
|
|
|
// set these to confirm that RPC call does not use them for this particular RPC
|
|
|
|
rc := &config.RuntimeConfig{
|
|
|
|
DNSAllowStale: true,
|
|
|
|
DNSMaxStale: 100,
|
|
|
|
DNSUseCache: true,
|
|
|
|
DNSCacheMaxAge: 100,
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
queryPayload *QueryPayload
|
|
|
|
context Context
|
|
|
|
expectedResult *Result
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "FetchVirtualIP returns result",
|
|
|
|
queryPayload: &QueryPayload{
|
|
|
|
Name: "db",
|
|
|
|
Tenancy: QueryTenancy{
|
2024-02-02 23:29:38 +00:00
|
|
|
Peer: "test-peer",
|
|
|
|
Namespace: defaultTestNamespace,
|
|
|
|
Partition: defaultTestPartition,
|
2024-01-17 23:46:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
context: Context{
|
|
|
|
Token: "test-token",
|
|
|
|
},
|
|
|
|
expectedResult: &Result{
|
2024-02-03 03:23:52 +00:00
|
|
|
Service: &Location{
|
|
|
|
Name: "db",
|
|
|
|
Address: "192.168.10.10",
|
|
|
|
},
|
|
|
|
Type: ResultTypeVirtual,
|
2024-01-17 23:46:18 +00:00
|
|
|
},
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FetchVirtualIP returns error",
|
|
|
|
queryPayload: &QueryPayload{
|
|
|
|
Name: "db",
|
|
|
|
Tenancy: QueryTenancy{
|
2024-02-02 23:29:38 +00:00
|
|
|
Peer: "test-peer",
|
|
|
|
Namespace: defaultTestNamespace,
|
|
|
|
Partition: defaultTestPartition},
|
2024-01-17 23:46:18 +00:00
|
|
|
},
|
|
|
|
context: Context{
|
|
|
|
Token: "test-token",
|
|
|
|
},
|
|
|
|
expectedResult: nil,
|
|
|
|
expectedErr: errors.New("test-error"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
logger := testutil.Logger(t)
|
|
|
|
mockRPC := cachetype.NewMockRPC(t)
|
|
|
|
mockRPC.On("RPC", mock.Anything, "Catalog.VirtualIPForService", mock.Anything, mock.Anything).
|
|
|
|
Return(tc.expectedErr).
|
|
|
|
Run(func(args mock.Arguments) {
|
|
|
|
req := args.Get(2).(*structs.ServiceSpecificRequest)
|
|
|
|
|
|
|
|
// validate RPC options are not set from config for the VirtuaLIPForService RPC
|
|
|
|
require.False(t, req.AllowStale)
|
|
|
|
require.Equal(t, time.Duration(0), req.MaxStaleDuration)
|
|
|
|
require.False(t, req.UseCache)
|
|
|
|
require.Equal(t, time.Duration(0), req.MaxAge)
|
|
|
|
|
|
|
|
// validate RPC options are set correctly from the queryPayload and context
|
|
|
|
require.Equal(t, tc.queryPayload.Tenancy.Peer, req.PeerName)
|
2024-02-02 23:29:38 +00:00
|
|
|
require.Equal(t, tc.queryPayload.Tenancy.Namespace, req.EnterpriseMeta.NamespaceOrEmpty())
|
|
|
|
require.Equal(t, tc.queryPayload.Tenancy.Partition, req.EnterpriseMeta.PartitionOrEmpty())
|
2024-01-17 23:46:18 +00:00
|
|
|
require.Equal(t, tc.context.Token, req.QueryOptions.Token)
|
|
|
|
|
|
|
|
if tc.expectedErr == nil {
|
|
|
|
// set the out parameter to ensure that it is used to formulate the result.Address
|
|
|
|
reply := args.Get(3).(*string)
|
2024-02-03 03:23:52 +00:00
|
|
|
*reply = tc.expectedResult.Service.Address
|
2024-01-17 23:46:18 +00:00
|
|
|
}
|
|
|
|
})
|
2024-01-29 22:33:45 +00:00
|
|
|
translateServicePortFunc := func(dc string, port int, taggedAddresses map[string]structs.ServiceAddress) int { return 0 }
|
|
|
|
rpcFuncForServiceNodes := func(ctx context.Context, req structs.ServiceSpecificRequest) (structs.IndexedCheckServiceNodes, cache.ResultMeta, error) {
|
|
|
|
return structs.IndexedCheckServiceNodes{}, cache.ResultMeta{}, nil
|
|
|
|
}
|
|
|
|
rpcFuncForSamenessGroup := func(ctx context.Context, req *structs.ConfigEntryQuery) (structs.SamenessGroupConfigEntry, cache.ResultMeta, error) {
|
|
|
|
return structs.SamenessGroupConfigEntry{}, cache.ResultMeta{}, nil
|
|
|
|
}
|
|
|
|
getFromCacheFunc := func(ctx context.Context, t string, r cache.Request) (interface{}, cache.ResultMeta, error) {
|
|
|
|
return nil, cache.ResultMeta{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
df := NewV1DataFetcher(rc, acl.DefaultEnterpriseMeta(), getFromCacheFunc, mockRPC.RPC, rpcFuncForServiceNodes, rpcFuncForSamenessGroup, translateServicePortFunc, logger)
|
2024-01-17 23:46:18 +00:00
|
|
|
|
|
|
|
result, err := df.FetchVirtualIP(tc.context, tc.queryPayload)
|
|
|
|
require.Equal(t, tc.expectedErr, err)
|
|
|
|
require.Equal(t, tc.expectedResult, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-01-30 22:34:35 +00:00
|
|
|
|
|
|
|
// Test_FetchEndpoints tests the FetchEndpoints method in scenarios where the RPC
|
|
|
|
// call succeeds and fails.
|
|
|
|
func Test_FetchEndpoints(t *testing.T) {
|
|
|
|
// set these to confirm that RPC call does not use them for this particular RPC
|
|
|
|
rc := &config.RuntimeConfig{
|
2024-04-22 18:30:43 +00:00
|
|
|
Datacenter: "dc2",
|
2024-01-30 22:34:35 +00:00
|
|
|
DNSAllowStale: true,
|
|
|
|
DNSMaxStale: 100,
|
|
|
|
DNSUseCache: true,
|
|
|
|
DNSCacheMaxAge: 100,
|
|
|
|
}
|
2024-02-03 03:23:52 +00:00
|
|
|
ctx := Context{
|
|
|
|
Token: "test-token",
|
|
|
|
}
|
|
|
|
expectedResults := []*Result{
|
2024-01-30 22:34:35 +00:00
|
|
|
{
|
2024-02-03 03:23:52 +00:00
|
|
|
Node: &Location{
|
2024-02-12 19:27:25 +00:00
|
|
|
Name: "node-name",
|
|
|
|
Address: "node-address",
|
|
|
|
TaggedAddresses: map[string]*TaggedAddress{},
|
2024-01-30 22:34:35 +00:00
|
|
|
},
|
2024-02-03 03:23:52 +00:00
|
|
|
Service: &Location{
|
2024-02-12 19:27:25 +00:00
|
|
|
Name: "service-name",
|
|
|
|
Address: "service-address",
|
|
|
|
TaggedAddresses: map[string]*TaggedAddress{},
|
2024-01-30 22:34:35 +00:00
|
|
|
},
|
2024-02-09 16:26:02 +00:00
|
|
|
Type: ResultTypeService,
|
|
|
|
DNS: DNSConfig{
|
|
|
|
Weight: 1,
|
|
|
|
},
|
2024-02-09 22:41:40 +00:00
|
|
|
Ports: []Port{
|
|
|
|
{
|
|
|
|
Number: 0,
|
|
|
|
},
|
|
|
|
},
|
2024-02-12 19:25:45 +00:00
|
|
|
Tenancy: ResultTenancy{
|
|
|
|
PeerName: "test-peer",
|
|
|
|
},
|
2024-01-30 22:34:35 +00:00
|
|
|
},
|
2024-02-03 03:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger := testutil.Logger(t)
|
|
|
|
mockRPC := cachetype.NewMockRPC(t)
|
|
|
|
translateServicePortFunc := func(dc string, port int, taggedAddresses map[string]structs.ServiceAddress) int { return 0 }
|
|
|
|
rpcFuncForSamenessGroup := func(ctx context.Context, req *structs.ConfigEntryQuery) (structs.SamenessGroupConfigEntry, cache.ResultMeta, error) {
|
|
|
|
return structs.SamenessGroupConfigEntry{}, cache.ResultMeta{}, nil
|
|
|
|
}
|
|
|
|
getFromCacheFunc := func(ctx context.Context, t string, r cache.Request) (interface{}, cache.ResultMeta, error) {
|
|
|
|
return nil, cache.ResultMeta{}, nil
|
|
|
|
}
|
|
|
|
rpcFuncForServiceNodes := func(ctx context.Context, req structs.ServiceSpecificRequest) (structs.IndexedCheckServiceNodes, cache.ResultMeta, error) {
|
|
|
|
return structs.IndexedCheckServiceNodes{
|
|
|
|
Nodes: []structs.CheckServiceNode{
|
2024-01-30 22:34:35 +00:00
|
|
|
{
|
2024-02-03 03:23:52 +00:00
|
|
|
Node: &structs.Node{
|
|
|
|
Address: "node-address",
|
|
|
|
Node: "node-name",
|
2024-02-02 23:29:38 +00:00
|
|
|
},
|
2024-02-03 03:23:52 +00:00
|
|
|
Service: &structs.NodeService{
|
2024-05-14 13:32:49 +00:00
|
|
|
Address: "service-address",
|
|
|
|
Service: "service-name",
|
|
|
|
PeerName: "test-peer",
|
2024-02-02 23:29:38 +00:00
|
|
|
},
|
2024-01-30 22:34:35 +00:00
|
|
|
},
|
|
|
|
},
|
2024-02-03 03:23:52 +00:00
|
|
|
}, cache.ResultMeta{}, nil
|
|
|
|
}
|
|
|
|
queryPayload := &QueryPayload{
|
|
|
|
Name: "service-name",
|
|
|
|
Tenancy: QueryTenancy{
|
|
|
|
Peer: "test-peer",
|
|
|
|
Namespace: defaultTestNamespace,
|
|
|
|
Partition: defaultTestPartition,
|
2024-01-30 22:34:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-02-03 03:23:52 +00:00
|
|
|
df := NewV1DataFetcher(rc, acl.DefaultEnterpriseMeta(), getFromCacheFunc, mockRPC.RPC, rpcFuncForServiceNodes, rpcFuncForSamenessGroup, translateServicePortFunc, logger)
|
2024-01-30 22:34:35 +00:00
|
|
|
|
2024-02-03 03:23:52 +00:00
|
|
|
results, err := df.FetchEndpoints(ctx, queryPayload, LookupTypeService)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedResults, results)
|
2024-01-30 22:34:35 +00:00
|
|
|
}
|