state: add tests for checks.ID indexer

This commit is contained in:
Daniel Nephin 2021-03-22 17:29:58 -04:00
parent 30281a5332
commit 8743e925d5
7 changed files with 45 additions and 21 deletions

View File

@ -194,7 +194,7 @@ func ensureNoNodeWithSimilarNameTxn(tx ReadTxn, node *structs.Node, allowClashWi
if strings.EqualFold(node.Node, enode.Node) && node.ID != enode.ID { if strings.EqualFold(node.Node, enode.Node) && node.ID != enode.ID {
// Look up the existing node's Serf health check to see if it's failed. // Look up the existing node's Serf health check to see if it's failed.
// If it is, the node can be renamed. // If it is, the node can be renamed.
enodeCheck, err := tx.First(tableChecks, indexID, NodeCheckID{EnterpriseMeta: *structs.DefaultEnterpriseMeta(), Node: enode.Node, CheckID: string(structs.SerfCheckID)}) enodeCheck, err := tx.First(tableChecks, indexID, NodeCheckQuery{EnterpriseMeta: *structs.DefaultEnterpriseMeta(), Node: enode.Node, CheckID: string(structs.SerfCheckID)})
if err != nil { if err != nil {
return fmt.Errorf("Cannot get status of node %s: %s", enode.Node, err) return fmt.Errorf("Cannot get status of node %s: %s", enode.Node, err)
} }
@ -1470,7 +1470,7 @@ func (s *Store) ensureCheckCASTxn(tx WriteTxn, idx uint64, hc *structs.HealthChe
// checks with no matching node or service. // checks with no matching node or service.
func (s *Store) ensureCheckTxn(tx WriteTxn, idx uint64, preserveIndexes bool, hc *structs.HealthCheck) error { func (s *Store) ensureCheckTxn(tx WriteTxn, idx uint64, preserveIndexes bool, hc *structs.HealthCheck) error {
// Check if we have an existing health check // Check if we have an existing health check
existing, err := tx.First(tableChecks, indexID, NodeCheckID{EnterpriseMeta: hc.EnterpriseMeta, Node: hc.Node, CheckID: string(hc.CheckID)}) existing, err := tx.First(tableChecks, indexID, NodeCheckQuery{EnterpriseMeta: hc.EnterpriseMeta, Node: hc.Node, CheckID: string(hc.CheckID)})
if err != nil { if err != nil {
return fmt.Errorf("failed health check lookup: %s", err) return fmt.Errorf("failed health check lookup: %s", err)
} }
@ -1583,7 +1583,7 @@ func getNodeCheckTxn(tx ReadTxn, nodeName string, checkID types.CheckID, entMeta
} }
// Return the check. // Return the check.
check, err := tx.First(tableChecks, indexID, NodeCheckID{EnterpriseMeta: *entMeta, Node: nodeName, CheckID: string(checkID)}) check, err := tx.First(tableChecks, indexID, NodeCheckQuery{EnterpriseMeta: *entMeta, Node: nodeName, CheckID: string(checkID)})
if err != nil { if err != nil {
return 0, nil, fmt.Errorf("failed check lookup: %s", err) return 0, nil, fmt.Errorf("failed check lookup: %s", err)
} }
@ -1809,7 +1809,7 @@ func (s *Store) deleteCheckTxn(tx WriteTxn, idx uint64, node string, checkID typ
} }
// Try to retrieve the existing health check. // Try to retrieve the existing health check.
hc, err := tx.First(tableChecks, indexID, NodeCheckID{EnterpriseMeta: *entMeta, Node: node, CheckID: string(checkID)}) hc, err := tx.First(tableChecks, indexID, NodeCheckQuery{EnterpriseMeta: *entMeta, Node: node, CheckID: string(checkID)})
if err != nil { if err != nil {
return fmt.Errorf("check lookup failed: %s", err) return fmt.Errorf("check lookup failed: %s", err)
} }

View File

@ -120,9 +120,9 @@ func indexFromHealthCheck(raw interface{}) ([]byte, error) {
} }
func indexFromNodeCheckID(raw interface{}) ([]byte, error) { func indexFromNodeCheckID(raw interface{}) ([]byte, error) {
hc, ok := raw.(NodeCheckID) hc, ok := raw.(NodeCheckQuery)
if !ok { if !ok {
return nil, fmt.Errorf("unexpected type %T for NodeCheckID index", raw) return nil, fmt.Errorf("unexpected type %T for NodeCheckQuery index", raw)
} }
if hc.Node == "" || hc.CheckID == "" { if hc.Node == "" || hc.CheckID == "" {

View File

@ -2,10 +2,40 @@
package state package state
import "github.com/hashicorp/consul/agent/structs" import (
"github.com/hashicorp/consul/agent/structs"
)
func testIndexerTableChecks() map[string]indexerTestCase { func testIndexerTableChecks() map[string]indexerTestCase {
obj := &structs.HealthCheck{
Node: "NoDe",
ServiceID: "SeRvIcE",
CheckID: "CheckID",
}
return map[string]indexerTestCase{ return map[string]indexerTestCase{
indexID: {
read: indexValue{
source: NodeCheckQuery{
Node: "NoDe",
CheckID: "CheckId",
},
expected: []byte("node\x00checkid\x00"),
},
write: indexValue{
source: obj,
expected: []byte("node\x00checkid\x00"),
},
prefix: []indexValue{
{
source: structs.EnterpriseMeta{},
expected: nil,
},
{
source: Query{Value: "nOdE"},
expected: []byte("node\x00"),
},
},
},
indexNodeService: { indexNodeService: {
read: indexValue{ read: indexValue{
source: NodeServiceQuery{ source: NodeServiceQuery{
@ -15,10 +45,7 @@ func testIndexerTableChecks() map[string]indexerTestCase {
expected: []byte("node\x00service\x00"), expected: []byte("node\x00service\x00"),
}, },
write: indexValue{ write: indexValue{
source: &structs.HealthCheck{ source: obj,
Node: "NoDe",
ServiceID: "SeRvIcE",
},
expected: []byte("node\x00service\x00"), expected: []byte("node\x00service\x00"),
}, },
}, },
@ -30,10 +57,7 @@ func testIndexerTableChecks() map[string]indexerTestCase {
expected: []byte("node\x00"), expected: []byte("node\x00"),
}, },
write: indexValue{ write: indexValue{
source: &structs.HealthCheck{ source: obj,
Node: "NoDe",
ServiceID: "SeRvIcE",
},
expected: []byte("node\x00"), expected: []byte("node\x00"),
}, },
}, },

View File

@ -325,8 +325,8 @@ type upstreamDownstream struct {
structs.RaftIndex structs.RaftIndex
} }
// NodeCheckID is used to query the ID index of the checks table. // NodeCheckQuery is used to query the ID index of the checks table.
type NodeCheckID struct { type NodeCheckQuery struct {
Node string Node string
CheckID string CheckID string
structs.EnterpriseMeta structs.EnterpriseMeta

View File

@ -1307,7 +1307,7 @@ func TestStateStore_DeleteNode(t *testing.T) {
} }
// Associated health check was removed. // Associated health check was removed.
checks, err := tx.Get(tableChecks, indexID, NodeCheckID{Node: "node1", CheckID: "check1"}) checks, err := tx.Get(tableChecks, indexID, NodeCheckQuery{Node: "node1", CheckID: "check1"})
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -2067,7 +2067,7 @@ func TestStateStore_DeleteService(t *testing.T) {
// that it actually is removed in the state store. // that it actually is removed in the state store.
tx := s.db.Txn(false) tx := s.db.Txn(false)
defer tx.Abort() defer tx.Abort()
check, err := tx.First(tableChecks, indexID, NodeCheckID{Node: "node1", CheckID: "check1"}) check, err := tx.First(tableChecks, indexID, NodeCheckQuery{Node: "node1", CheckID: "check1"})
if err != nil || check != nil { if err != nil || check != nil {
t.Fatalf("bad: %#v (err: %s)", check, err) t.Fatalf("bad: %#v (err: %s)", check, err)
} }

View File

@ -108,7 +108,7 @@ func sessionMaxIndex(tx ReadTxn, entMeta *structs.EnterpriseMeta) uint64 {
func validateSessionChecksTxn(tx *txn, session *structs.Session) error { func validateSessionChecksTxn(tx *txn, session *structs.Session) error {
// Go over the session checks and ensure they exist. // Go over the session checks and ensure they exist.
for _, checkID := range session.CheckIDs() { for _, checkID := range session.CheckIDs() {
check, err := tx.First(tableChecks, indexID, NodeCheckID{Node: session.Node, CheckID: string(checkID)}) check, err := tx.First(tableChecks, indexID, NodeCheckQuery{Node: session.Node, CheckID: string(checkID)})
if err != nil { if err != nil {
return fmt.Errorf("failed check lookup: %s", err) return fmt.Errorf("failed check lookup: %s", err)
} }

View File

@ -163,7 +163,7 @@ func testRegisterCheck(t *testing.T, s *Store, idx uint64,
tx := s.db.Txn(false) tx := s.db.Txn(false)
defer tx.Abort() defer tx.Abort()
c, err := tx.First(tableChecks, indexID, NodeCheckID{Node: nodeID, CheckID: string(checkID)}) c, err := tx.First(tableChecks, indexID, NodeCheckQuery{Node: nodeID, CheckID: string(checkID)})
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }