diff --git a/agent/cache-types/discovery_chain_test.go b/agent/cache-types/discovery_chain_test.go new file mode 100644 index 0000000000..9bc365b838 --- /dev/null +++ b/agent/cache-types/discovery_chain_test.go @@ -0,0 +1,66 @@ +package cachetype + +import ( + "testing" + "time" + + "github.com/hashicorp/consul/agent/cache" + "github.com/hashicorp/consul/agent/consul/discoverychain" + "github.com/hashicorp/consul/agent/structs" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestCompiledDiscoveryChain(t *testing.T) { + rpc := TestRPC(t) + typ := &CompiledDiscoveryChain{RPC: rpc} + + // just do the default chain + entries := structs.NewDiscoveryChainConfigEntries() + chain := discoverychain.TestCompileConfigEntries(t, "web", "default", "dc1") + + // Expect the proper RPC call. This also sets the expected value + // since that is return-by-pointer in the arguments. + var resp *structs.DiscoveryChainResponse + rpc.On("RPC", "ConfigEntry.ReadDiscoveryChain", mock.Anything, mock.Anything).Return(nil). + Run(func(args mock.Arguments) { + req := args.Get(1).(*structs.DiscoveryChainRequest) + require.Equal(t, uint64(24), req.QueryOptions.MinQueryIndex) + require.Equal(t, 1*time.Second, req.QueryOptions.MaxQueryTime) + require.True(t, req.AllowStale) + + reply := args.Get(2).(*structs.DiscoveryChainResponse) + reply.ConfigEntries = entries + reply.Chain = chain + reply.QueryMeta.Index = 48 + resp = reply + }) + + // Fetch + resultA, err := typ.Fetch(cache.FetchOptions{ + MinIndex: 24, + Timeout: 1 * time.Second, + }, &structs.DiscoveryChainRequest{ + Name: "web", + Datacenter: "dc1", + }) + require.NoError(t, err) + require.Equal(t, cache.FetchResult{ + Value: resp, + Index: 48, + }, resultA) + + rpc.AssertExpectations(t) +} + +func TestCompiledDiscoveryChain_badReqType(t *testing.T) { + rpc := TestRPC(t) + typ := &CompiledDiscoveryChain{RPC: rpc} + + // Fetch + _, err := typ.Fetch(cache.FetchOptions{}, cache.TestRequest( + t, cache.RequestInfo{Key: "foo", MinIndex: 64})) + require.Error(t, err) + require.Contains(t, err.Error(), "wrong type") + rpc.AssertExpectations(t) +} diff --git a/agent/consul/discoverychain/testing.go b/agent/consul/discoverychain/testing.go new file mode 100644 index 0000000000..2925d76b89 --- /dev/null +++ b/agent/consul/discoverychain/testing.go @@ -0,0 +1,29 @@ +package discoverychain + +import ( + "github.com/hashicorp/consul/agent/structs" + "github.com/mitchellh/go-testing-interface" + "github.com/stretchr/testify/require" +) + +func TestCompileConfigEntries( + t testing.T, + serviceName string, + currentNamespace string, + currentDatacenter string, + entries ...structs.ConfigEntry, +) *structs.CompiledDiscoveryChain { + set := structs.NewDiscoveryChainConfigEntries() + + set.AddEntries(entries...) + + chain, err := Compile(CompileRequest{ + ServiceName: serviceName, + CurrentNamespace: currentNamespace, + CurrentDatacenter: currentDatacenter, + InferDefaults: true, + Entries: set, + }) + require.NoError(t, err) + return chain +} diff --git a/agent/proxycfg/manager_test.go b/agent/proxycfg/manager_test.go index a42983a0c0..4ea99708f1 100644 --- a/agent/proxycfg/manager_test.go +++ b/agent/proxycfg/manager_test.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/consul/agent/cache" cachetype "github.com/hashicorp/consul/agent/cache-types" + "github.com/hashicorp/consul/agent/consul/discoverychain" "github.com/hashicorp/consul/agent/local" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/token" @@ -62,7 +63,7 @@ func TestManager_BasicLifecycle(t *testing.T) { Datacenter: "dc1", } dbDefaultChain := func() *structs.CompiledDiscoveryChain { - return TestCompileConfigEntries(t, "db", "default", "dc1", + return discoverychain.TestCompileConfigEntries(t, "db", "default", "dc1", &structs.ServiceResolverConfigEntry{ Kind: structs.ServiceResolver, Name: "db", @@ -70,7 +71,7 @@ func TestManager_BasicLifecycle(t *testing.T) { ) } dbSplitChain := func() *structs.CompiledDiscoveryChain { - return TestCompileConfigEntries(t, "db", "default", "dc1", + return discoverychain.TestCompileConfigEntries(t, "db", "default", "dc1", &structs.ProxyConfigEntry{ Kind: structs.ProxyDefaults, Name: structs.ProxyConfigGlobal, diff --git a/agent/proxycfg/state_test.go b/agent/proxycfg/state_test.go index fcae4b5574..ee24664f87 100644 --- a/agent/proxycfg/state_test.go +++ b/agent/proxycfg/state_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/consul/agent/cache" cachetype "github.com/hashicorp/consul/agent/cache-types" + "github.com/hashicorp/consul/agent/consul/discoverychain" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/sdk/testutil" "github.com/stretchr/testify/require" @@ -403,14 +404,14 @@ func TestState_WatchesAndUpdates(t *testing.T) { cache.UpdateEvent{ CorrelationID: "discovery-chain:api", Result: &structs.DiscoveryChainResponse{ - Chain: TestCompileConfigEntries(t, "api", "default", "dc1"), + Chain: discoverychain.TestCompileConfigEntries(t, "api", "default", "dc1"), }, Err: nil, }, cache.UpdateEvent{ CorrelationID: "discovery-chain:api", Result: &structs.DiscoveryChainResponse{ - Chain: TestCompileConfigEntries(t, "api-dc2", "default", "dc1", + Chain: discoverychain.TestCompileConfigEntries(t, "api-dc2", "default", "dc1", &structs.ServiceResolverConfigEntry{ Kind: structs.ServiceResolver, Name: "api-dc2", diff --git a/agent/proxycfg/testing.go b/agent/proxycfg/testing.go index 4fb38d97aa..b15e59c191 100644 --- a/agent/proxycfg/testing.go +++ b/agent/proxycfg/testing.go @@ -528,7 +528,7 @@ func testConfigSnapshotDiscoveryChain(t testing.T, variation string, additionalE entries = append(entries, additionalEntries...) } - dbChain := TestCompileConfigEntries(t, "db", "default", "dc1", entries...) + dbChain := discoverychain.TestCompileConfigEntries(t, "db", "default", "dc1", entries...) dbTarget := structs.DiscoveryTarget{ Service: "db", @@ -644,28 +644,6 @@ func TestConfigSnapshotMeshGateway(t testing.T) *ConfigSnapshot { } } -func TestCompileConfigEntries( - t testing.T, - serviceName string, - currentNamespace string, - currentDatacenter string, - entries ...structs.ConfigEntry, -) *structs.CompiledDiscoveryChain { - set := structs.NewDiscoveryChainConfigEntries() - - set.AddEntries(entries...) - - chain, err := discoverychain.Compile(discoverychain.CompileRequest{ - ServiceName: serviceName, - CurrentNamespace: currentNamespace, - CurrentDatacenter: currentDatacenter, - InferDefaults: true, - Entries: set, - }) - require.NoError(t, err) - return chain -} - // ControllableCacheType is a cache.Type that simulates a typical blocking RPC // but lets us control the responses and when they are delivered easily. type ControllableCacheType struct {