open-consul/agent/structs/prepared_query_test.go
Michael Wilkerson 40dd8ce65b
Add sameness group field to prepared queries (#17089)
* added method for converting SamenessGroupConfigEntry
- added new method `ToQueryFailoverTargets` for converting a SamenessGroupConfigEntry's members to a list of QueryFailoverTargets
- renamed `ToFailoverTargets` ToServiceResolverFailoverTargets to distinguish it from `ToQueryFailoverTargets`

* Added SamenessGroup to PreparedQuery
- exposed Service.Partition to API when defining a prepared query
- added a method for determining if a QueryFailoverOptions is empty
- This will be useful for validation
- added unit tests

* added method for retrieving a SamenessGroup to state store

* added logic for using PQ with SamenessGroup
- added branching path for SamenessGroup handling in execute. It will be handled separate from the normal PQ case
- added a new interface so that the `GetSamenessGroupFailoverTargets` can be properly tested
- separated the execute logic into a `targetSelector` function so that it can be used for both failover and sameness group PQs
- split OSS only methods into new PQ OSS files
- added validation that `samenessGroup` is an enterprise only feature

* added documentation for PQ SamenessGroup
2023-04-24 13:21:28 -07:00

92 lines
1.9 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package structs
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStructs_PreparedQuery_GetACLPrefix(t *testing.T) {
ephemeral := &PreparedQuery{}
if prefix, ok := ephemeral.GetACLPrefix(); ok {
t.Fatalf("bad: %s", prefix)
}
named := &PreparedQuery{
Name: "hello",
}
if prefix, ok := named.GetACLPrefix(); !ok || prefix != "hello" {
t.Fatalf("bad: ok=%v, prefix=%#v", ok, prefix)
}
tmpl := &PreparedQuery{
Name: "",
Template: QueryTemplateOptions{
Type: QueryTemplateTypeNamePrefixMatch,
},
}
if prefix, ok := tmpl.GetACLPrefix(); !ok || prefix != "" {
t.Fatalf("bad: ok=%v prefix=%#v", ok, prefix)
}
}
func TestPreparedQueryExecuteRequest_CacheInfoKey(t *testing.T) {
// TODO: should these fields be included in the key?
ignored := []string{"Agent", "QueryOptions"}
assertCacheInfoKeyIsComplete(t, &PreparedQueryExecuteRequest{}, ignored...)
}
func TestQueryFailoverOptions_IsEmpty(t *testing.T) {
tests := []struct {
name string
query QueryFailoverOptions
isExpectedEmpty bool
}{
{
name: "expect empty",
query: QueryFailoverOptions{},
isExpectedEmpty: true,
},
{
name: "expect not empty NearestN",
query: QueryFailoverOptions{
NearestN: 1,
},
isExpectedEmpty: false,
},
{
name: "expect not empty NearestN negative",
query: QueryFailoverOptions{
NearestN: -1,
},
isExpectedEmpty: false,
},
{
name: "expect not empty datacenters",
query: QueryFailoverOptions{
Datacenters: []string{"dc"},
},
isExpectedEmpty: false,
},
{
name: "expect not empty targets",
query: QueryFailoverOptions{
Targets: []QueryFailoverTarget{
{
Peer: "peer",
},
},
},
isExpectedEmpty: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.isExpectedEmpty, tt.query.IsEmpty())
})
}
}