mirror of https://github.com/status-im/consul.git
Merge pull request #9958 from hashicorp/dnephin/state-improve-indexer-tests
state: support additional test cases in indexer tests
This commit is contained in:
commit
f0ba6f858a
|
@ -258,6 +258,35 @@ func testIndexerTableServices() map[string]indexerTestCase {
|
||||||
},
|
},
|
||||||
expected: []byte("connectname\x00"),
|
expected: []byte("connectname\x00"),
|
||||||
},
|
},
|
||||||
|
extra: []indexerTestCase{
|
||||||
|
{
|
||||||
|
write: indexValue{
|
||||||
|
source: &structs.ServiceNode{
|
||||||
|
ServiceName: "ServiceName",
|
||||||
|
ServiceKind: structs.ServiceKindConnectProxy,
|
||||||
|
ServiceProxy: structs.ConnectProxyConfig{
|
||||||
|
DestinationServiceName: "ConnectName",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []byte("connectname\x00"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
write: indexValue{
|
||||||
|
source: &structs.ServiceNode{ServiceName: "ServiceName"},
|
||||||
|
expectedIndexMissing: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
write: indexValue{
|
||||||
|
source: &structs.ServiceNode{
|
||||||
|
ServiceName: "ServiceName",
|
||||||
|
ServiceKind: structs.ServiceKindTerminatingGateway,
|
||||||
|
},
|
||||||
|
expectedIndexMissing: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
indexKind: {
|
indexKind: {
|
||||||
read: indexValue{
|
read: indexValue{
|
||||||
|
@ -270,6 +299,17 @@ func testIndexerTableServices() map[string]indexerTestCase {
|
||||||
},
|
},
|
||||||
expected: []byte("connect-proxy\x00"),
|
expected: []byte("connect-proxy\x00"),
|
||||||
},
|
},
|
||||||
|
extra: []indexerTestCase{
|
||||||
|
{
|
||||||
|
write: indexValue{
|
||||||
|
source: &structs.ServiceNode{
|
||||||
|
ServiceName: "ServiceName",
|
||||||
|
ServiceKind: structs.ServiceKindTypical,
|
||||||
|
},
|
||||||
|
expected: []byte("\x00"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,11 +12,18 @@ type indexerTestCase struct {
|
||||||
write indexValue
|
write indexValue
|
||||||
prefix []indexValue
|
prefix []indexValue
|
||||||
writeMulti indexValueMulti
|
writeMulti indexValueMulti
|
||||||
|
// extra test cases can be added if the indexer has special handling for
|
||||||
|
// specific cases.
|
||||||
|
extra []indexerTestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
type indexValue struct {
|
type indexValue struct {
|
||||||
source interface{}
|
source interface{}
|
||||||
expected []byte
|
expected []byte
|
||||||
|
// expectedIndexMissing indicates that this test case should not produce an
|
||||||
|
// expected value. The indexer should report a required value was missing.
|
||||||
|
// This field is only relevant for the writeIndex.
|
||||||
|
expectedIndexMissing bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type indexValueMulti struct {
|
type indexValueMulti struct {
|
||||||
|
@ -54,42 +61,61 @@ func TestNewDBSchema_Indexers(t *testing.T) {
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Skip("TODO: missing test case")
|
t.Skip("TODO: missing test case")
|
||||||
}
|
}
|
||||||
|
tc.run(t, indexer)
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tc indexerTestCase) run(t *testing.T, indexer memdb.Indexer) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
if tc.read.source != nil {
|
||||||
|
t.Run("readIndex", func(t *testing.T) {
|
||||||
|
actual, err := indexer.FromArgs(args...)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, tc.read.expected, actual)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if i, ok := indexer.(memdb.SingleIndexer); ok {
|
||||||
|
t.Run("writeIndex", func(t *testing.T) {
|
||||||
|
valid, actual, err := i.FromObject(tc.write.source)
|
||||||
|
require.NoError(t, err)
|
||||||
|
if tc.write.expectedIndexMissing {
|
||||||
|
require.False(t, valid, "expected the indexer to produce no index value")
|
||||||
|
} else {
|
||||||
|
require.True(t, valid, "indexer was missing a required value")
|
||||||
|
require.Equal(t, tc.write.expected, actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if i, ok := indexer.(memdb.PrefixIndexer); ok {
|
||||||
|
for _, c := range tc.prefix {
|
||||||
|
t.Run("prefixIndex", 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, extra := range tc.extra {
|
||||||
|
t.Run("extra", func(t *testing.T) {
|
||||||
|
extra.run(t, indexer)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue