diff --git a/agent/consul/state/catalog_oss_test.go b/agent/consul/state/catalog_oss_test.go new file mode 100644 index 000000000..270489da1 --- /dev/null +++ b/agent/consul/state/catalog_oss_test.go @@ -0,0 +1,26 @@ +// +build !consulent + +package state + +import "github.com/hashicorp/consul/agent/structs" + +func testIndexerTableChecks() map[string]indexerTestCase { + return map[string]indexerTestCase{ + indexNodeService: { + read: indexValue{ + source: NodeServiceQuery{ + Node: "NoDe", + Service: "SeRvIcE", + }, + expected: []byte("node\x00service\x00"), + }, + write: indexValue{ + source: &structs.HealthCheck{ + Node: "NoDe", + ServiceID: "SeRvIcE", + }, + expected: []byte("node\x00service\x00"), + }, + }, + } +} diff --git a/agent/consul/state/schema_test.go b/agent/consul/state/schema_test.go index ccb91db0e..572e70eb8 100644 --- a/agent/consul/state/schema_test.go +++ b/agent/consul/state/schema_test.go @@ -14,7 +14,9 @@ import ( "github.com/hashicorp/consul/internal/testing/golden" ) -func TestStateStoreSchema(t *testing.T) { +// TODO: once TestNewDBSchema_Indexers has test cases for all tables and indexes +// it is probably safe to remove this test +func TestNewDBSchema(t *testing.T) { schema := newDBSchema() require.NoError(t, schema.Validate()) @@ -103,3 +105,82 @@ func indexNames(table *memdb.TableSchema) []string { sort.Strings(indexes) return indexes } + +type indexerTestCase struct { + read indexValue + write indexValue + prefix []indexValue + writeMulti indexValueMulti +} + +type indexValue struct { + source interface{} + expected []byte +} + +type indexValueMulti struct { + source interface{} + expected [][]byte +} + +func TestNewDBSchema_Indexers(t *testing.T) { + schema := newDBSchema() + require.NoError(t, schema.Validate()) + + var testcases = map[string]func() map[string]indexerTestCase{ + tableChecks: testIndexerTableChecks, + } + + for _, table := range schema.Tables { + if testcases[table.Name] == nil { + continue + } + t.Run(table.Name, func(t *testing.T) { + tableTCs := testcases[table.Name]() + + for _, index := range table.Indexes { + t.Run(index.Name, func(t *testing.T) { + indexer := index.Indexer + tc, ok := tableTCs[index.Name] + if !ok { + t.Skip("TODO: missing test case") + } + + args := []interface{}{tc.read.source} + if s, ok := tc.read.source.([]interface{}); ok { + // Indexes using memdb.CompoundIndex must be expanded to multiple args + args = s + } + + actual, err := indexer.FromArgs(args...) + require.NoError(t, err) + require.Equal(t, tc.read.expected, actual) + + if i, ok := indexer.(memdb.SingleIndexer); ok { + valid, actual, err := i.FromObject(tc.write.source) + require.NoError(t, err) + require.True(t, valid) + require.Equal(t, tc.write.expected, actual) + } + + if i, ok := indexer.(memdb.PrefixIndexer); ok { + for _, c := range tc.prefix { + t.Run("", func(t *testing.T) { + actual, err := i.PrefixFromArgs(c.source) + require.NoError(t, err) + require.Equal(t, c.expected, actual) + }) + } + } + + if i, ok := indexer.(memdb.MultiIndexer); ok { + valid, actual, err := i.FromObject(tc.writeMulti.source) + require.NoError(t, err) + require.True(t, valid) + require.Equal(t, tc.writeMulti.expected, actual) + } + }) + } + }) + } +}