Run a test of `NodeServices()` with a NodeID as an argument.

pull/2702/head
Sean Chittenden 8 years ago
parent 854f2b2bfc
commit 295ca817f3
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16

@ -725,11 +725,12 @@ func (s *StateStore) NodeServices(ws memdb.WatchSet, nodeNameOrID string) (uint6
} }
node := n.(*structs.Node) node := n.(*structs.Node)
nodeName := node.Node
// Read all of the services // Read all of the services
services, err := tx.Get("services", "node", nodeNameOrID) services, err := tx.Get("services", "node", nodeName)
if err != nil { if err != nil {
return 0, nil, fmt.Errorf("failed querying services for node %q: %s", nodeNameOrID, err) return 0, nil, fmt.Errorf("failed querying services for node %q: %s", nodeName, err)
} }
ws.Add(services.WatchCh()) ws.Add(services.WatchCh())

@ -204,6 +204,8 @@ func TestStateStore_EnsureRegistration_Restore(t *testing.T) {
Node: "node1", Node: "node1",
Address: "1.2.3.4", Address: "1.2.3.4",
} }
nodeID := string(req.ID)
nodeName := string(req.Node)
restore := s.Restore() restore := s.Restore()
if err := restore.Registration(1, req); err != nil { if err := restore.Registration(1, req); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -211,17 +213,26 @@ func TestStateStore_EnsureRegistration_Restore(t *testing.T) {
restore.Commit() restore.Commit()
// Retrieve the node and verify its contents. // Retrieve the node and verify its contents.
verifyNode := func() { verifyNode := func(nodeLookup string) {
_, out, err := s.GetNode("node1") _, out, err := s.GetNode(nodeLookup)
if err != nil {
t.Fatalf("err: %s", err)
}
if out == nil {
_, out, err = s.GetNodeID(types.NodeID(nodeLookup))
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
if out.Node != "node1" || out.Address != "1.2.3.4" || }
if out == nil || out.Address != "1.2.3.4" ||
!(out.Node == nodeLookup || string(out.ID) == nodeLookup) ||
out.CreateIndex != 1 || out.ModifyIndex != 1 { out.CreateIndex != 1 || out.ModifyIndex != 1 {
t.Fatalf("bad node returned: %#v", out) t.Fatalf("bad node returned: %#v", out)
} }
} }
verifyNode() verifyNode(nodeID)
verifyNode(nodeName)
// Add in a service definition. // Add in a service definition.
req.Service = &structs.NodeService{ req.Service = &structs.NodeService{
@ -237,8 +248,8 @@ func TestStateStore_EnsureRegistration_Restore(t *testing.T) {
restore.Commit() restore.Commit()
// Verify that the service got registered. // Verify that the service got registered.
verifyService := func() { verifyService := func(nodeLookup string) {
idx, out, err := s.NodeServices(nil, "node1") idx, out, err := s.NodeServices(nil, nodeLookup)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -258,7 +269,7 @@ func TestStateStore_EnsureRegistration_Restore(t *testing.T) {
// Add in a top-level check. // Add in a top-level check.
req.Check = &structs.HealthCheck{ req.Check = &structs.HealthCheck{
Node: "node1", Node: nodeName,
CheckID: "check1", CheckID: "check1",
Name: "check", Name: "check",
} }
@ -270,7 +281,7 @@ func TestStateStore_EnsureRegistration_Restore(t *testing.T) {
// Verify that the check got registered. // Verify that the check got registered.
verifyCheck := func() { verifyCheck := func() {
idx, out, err := s.NodeChecks(nil, "node1") idx, out, err := s.NodeChecks(nil, nodeName)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -281,19 +292,21 @@ func TestStateStore_EnsureRegistration_Restore(t *testing.T) {
t.Fatalf("bad: %#v", out) t.Fatalf("bad: %#v", out)
} }
c := out[0] c := out[0]
if c.Node != "node1" || c.CheckID != "check1" || c.Name != "check" || if c.Node != nodeName || c.CheckID != "check1" || c.Name != "check" ||
c.CreateIndex != 3 || c.ModifyIndex != 3 { c.CreateIndex != 3 || c.ModifyIndex != 3 {
t.Fatalf("bad check returned: %#v", c) t.Fatalf("bad check returned: %#v", c)
} }
} }
verifyNode() verifyNode(nodeID)
verifyService() verifyNode(nodeName)
verifyService(nodeID)
verifyService(nodeName)
verifyCheck() verifyCheck()
// Add in another check via the slice. // Add in another check via the slice.
req.Checks = structs.HealthChecks{ req.Checks = structs.HealthChecks{
&structs.HealthCheck{ &structs.HealthCheck{
Node: "node1", Node: nodeName,
CheckID: "check2", CheckID: "check2",
Name: "check", Name: "check",
}, },
@ -305,10 +318,12 @@ func TestStateStore_EnsureRegistration_Restore(t *testing.T) {
restore.Commit() restore.Commit()
// Verify that the additional check got registered. // Verify that the additional check got registered.
verifyNode() verifyNode(nodeID)
verifyService() verifyNode(nodeName)
verifyService(nodeID)
verifyService(nodeName)
func() { func() {
idx, out, err := s.NodeChecks(nil, "node1") idx, out, err := s.NodeChecks(nil, nodeName)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -319,13 +334,13 @@ func TestStateStore_EnsureRegistration_Restore(t *testing.T) {
t.Fatalf("bad: %#v", out) t.Fatalf("bad: %#v", out)
} }
c1 := out[0] c1 := out[0]
if c1.Node != "node1" || c1.CheckID != "check1" || c1.Name != "check" || if c1.Node != nodeName || c1.CheckID != "check1" || c1.Name != "check" ||
c1.CreateIndex != 3 || c1.ModifyIndex != 4 { c1.CreateIndex != 3 || c1.ModifyIndex != 4 {
t.Fatalf("bad check returned: %#v", c1) t.Fatalf("bad check returned: %#v", c1)
} }
c2 := out[1] c2 := out[1]
if c2.Node != "node1" || c2.CheckID != "check2" || c2.Name != "check" || if c2.Node != nodeName || c2.CheckID != "check2" || c2.Name != "check" ||
c2.CreateIndex != 4 || c2.ModifyIndex != 4 { c2.CreateIndex != 4 || c2.ModifyIndex != 4 {
t.Fatalf("bad check returned: %#v", c2) t.Fatalf("bad check returned: %#v", c2)
} }

Loading…
Cancel
Save