state: add tests for gateway-service table indexers

This commit is contained in:
Daniel Nephin 2021-03-16 14:20:11 -04:00
parent 11b4de719c
commit 4d1d19ed46
3 changed files with 56 additions and 12 deletions

View File

@ -2524,7 +2524,7 @@ func updateGatewayNamespace(tx WriteTxn, idx uint64, service *structs.GatewaySer
continue continue
} }
existing, err := tx.First(tableGatewayServices, "id", service.Gateway, sn.CompoundServiceName(), service.Port) existing, err := tx.First(tableGatewayServices, indexID, service.Gateway, sn.CompoundServiceName(), service.Port)
if err != nil { if err != nil {
return fmt.Errorf("gateway service lookup failed: %s", err) return fmt.Errorf("gateway service lookup failed: %s", err)
} }
@ -2559,7 +2559,7 @@ func updateGatewayNamespace(tx WriteTxn, idx uint64, service *structs.GatewaySer
func updateGatewayService(tx WriteTxn, idx uint64, mapping *structs.GatewayService) error { func updateGatewayService(tx WriteTxn, idx uint64, mapping *structs.GatewayService) error {
// Check if mapping already exists in table if it's already in the table // Check if mapping already exists in table if it's already in the table
// Avoid insert if nothing changed // Avoid insert if nothing changed
existing, err := tx.First(tableGatewayServices, "id", mapping.Gateway, mapping.Service, mapping.Port) existing, err := tx.First(tableGatewayServices, indexID, mapping.Gateway, mapping.Service, mapping.Port)
if err != nil { if err != nil {
return fmt.Errorf("gateway service lookup failed: %s", err) return fmt.Errorf("gateway service lookup failed: %s", err)
} }
@ -2658,7 +2658,7 @@ func (s *Store) DumpGatewayServices(ws memdb.WatchSet) (uint64, structs.GatewayS
tx := s.db.ReadTxn() tx := s.db.ReadTxn()
defer tx.Abort() defer tx.Abort()
iter, err := tx.Get(tableGatewayServices, "id") iter, err := tx.Get(tableGatewayServices, indexID)
if err != nil { if err != nil {
return 0, nil, fmt.Errorf("failed to dump gateway-services: %s", err) return 0, nil, fmt.Errorf("failed to dump gateway-services: %s", err)
} }

View File

@ -62,8 +62,7 @@ func testIndexerTableMeshTopology() map[string]indexerTestCase {
}, },
indexUpstream: { indexUpstream: {
read: indexValue{ read: indexValue{
source: structs.ServiceName{Name: "UpStReAm"}, source: structs.ServiceName{Name: "UpStReAm"},
expected: []byte("upstream\x00"), expected: []byte("upstream\x00"),
}, },
write: indexValue{ write: indexValue{
@ -73,8 +72,7 @@ func testIndexerTableMeshTopology() map[string]indexerTestCase {
}, },
indexDownstream: { indexDownstream: {
read: indexValue{ read: indexValue{
source: structs.ServiceName{Name: "DownStream"}, source: structs.ServiceName{Name: "DownStream"},
expected: []byte("downstream\x00"), expected: []byte("downstream\x00"),
}, },
write: indexValue{ write: indexValue{
@ -85,6 +83,51 @@ func testIndexerTableMeshTopology() map[string]indexerTestCase {
} }
} }
func testIndexerTableGatewayServices() map[string]indexerTestCase {
obj := &structs.GatewayService{
Gateway: structs.ServiceName{Name: "GateWay"},
Service: structs.ServiceName{Name: "SerVice"},
Port: 50123,
}
encodedPort := string([]byte{0x96, 0x8f, 0x06, 0, 0, 0, 0, 0, 0, 0})
return map[string]indexerTestCase{
indexID: {
read: indexValue{
source: []interface{}{
structs.ServiceName{Name: "GateWay"},
structs.ServiceName{Name: "SerVice"},
50123,
},
expected: []byte("gateway\x00service\x00" + encodedPort),
},
write: indexValue{
source: obj,
expected: []byte("gateway\x00service\x00" + encodedPort),
},
},
indexGateway: {
read: indexValue{
source: structs.ServiceName{Name: "GateWay"},
expected: []byte("gateway\x00"),
},
write: indexValue{
source: obj,
expected: []byte("gateway\x00"),
},
},
indexService: {
read: indexValue{
source: structs.ServiceName{Name: "SerVice"},
expected: []byte("service\x00"),
},
write: indexValue{
source: obj,
expected: []byte("service\x00"),
},
},
}
}
func testIndexerTableNodes() map[string]indexerTestCase { func testIndexerTableNodes() map[string]indexerTestCase {
return map[string]indexerTestCase{ return map[string]indexerTestCase{
indexID: { indexID: {

View File

@ -128,11 +128,12 @@ func TestNewDBSchema_Indexers(t *testing.T) {
require.NoError(t, schema.Validate()) require.NoError(t, schema.Validate())
var testcases = map[string]func() map[string]indexerTestCase{ var testcases = map[string]func() map[string]indexerTestCase{
tableChecks: testIndexerTableChecks, tableChecks: testIndexerTableChecks,
tableServices: testIndexerTableServices, tableServices: testIndexerTableServices,
tableNodes: testIndexerTableNodes, tableNodes: testIndexerTableNodes,
tableConfigEntries: testIndexerTableConfigEntries, tableConfigEntries: testIndexerTableConfigEntries,
tableMeshTopology: testIndexerTableMeshTopology, tableMeshTopology: testIndexerTableMeshTopology,
tableGatewayServices: testIndexerTableGatewayServices,
} }
for _, table := range schema.Tables { for _, table := range schema.Tables {