From 1e3b0d441bd047f68641053160a3552f77b58b8d Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Tue, 31 Oct 2017 13:34:49 -0700 Subject: [PATCH] Factor out registerNodes function --- agent/consul/coordinate_endpoint.go | 3 +- agent/consul/coordinate_endpoint_test.go | 77 +++++++++--------------- agent/consul/state/coordinate.go | 5 +- 3 files changed, 30 insertions(+), 55 deletions(-) diff --git a/agent/consul/coordinate_endpoint.go b/agent/consul/coordinate_endpoint.go index abf255fbcc..5591122217 100644 --- a/agent/consul/coordinate_endpoint.go +++ b/agent/consul/coordinate_endpoint.go @@ -201,8 +201,7 @@ func (c *Coordinate) ListNodes(args *structs.DCSpecificRequest, reply *structs.I }) } -// ListNodes returns the list of nodes with their raw network coordinates (if no -// coordinates are available for a node it won't appear in this list). +// Node returns the raw coordinates for a single node. func (c *Coordinate) Node(args *structs.NodeSpecificRequest, reply *structs.IndexedCoordinates) error { if done, err := c.srv.forward("Coordinate.Node", args, args, reply); done { return err diff --git a/agent/consul/coordinate_endpoint_test.go b/agent/consul/coordinate_endpoint_test.go index 5f23aeb2cb..d697f92348 100644 --- a/agent/consul/coordinate_endpoint_test.go +++ b/agent/consul/coordinate_endpoint_test.go @@ -4,6 +4,7 @@ import ( "fmt" "math" "math/rand" + "net/rpc" "os" "strings" "testing" @@ -50,16 +51,8 @@ func TestCoordinate_Update(t *testing.T) { // Register some nodes. nodes := []string{"node1", "node2"} - for _, node := range nodes { - req := structs.RegisterRequest{ - Datacenter: "dc1", - Node: node, - Address: "127.0.0.1", - } - var reply struct{} - if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &req, &reply); err != nil { - t.Fatalf("err: %v", err) - } + if err := registerNodes(nodes, codec); err != nil { + t.Fatal(err) } // Send an update for the first node. @@ -201,16 +194,8 @@ func TestCoordinate_Update_ACLDeny(t *testing.T) { // Register some nodes. nodes := []string{"node1", "node2"} - for _, node := range nodes { - req := structs.RegisterRequest{ - Datacenter: "dc1", - Node: node, - Address: "127.0.0.1", - } - var reply struct{} - if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &req, &reply); err != nil { - t.Fatalf("err: %v", err) - } + if err := registerNodes(nodes, codec); err != nil { + t.Fatal(err) } // Send an update for the first node. This should go through since we @@ -309,16 +294,8 @@ func TestCoordinate_ListNodes(t *testing.T) { // Register some nodes. nodes := []string{"foo", "bar", "baz"} - for _, node := range nodes { - req := structs.RegisterRequest{ - Datacenter: "dc1", - Node: node, - Address: "127.0.0.1", - } - var reply struct{} - if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &req, &reply); err != nil { - t.Fatalf("err: %v", err) - } + if err := registerNodes(nodes, codec); err != nil { + t.Fatal(err) } // Send coordinate updates for a few nodes. @@ -515,16 +492,8 @@ func TestCoordinate_Node(t *testing.T) { // Register some nodes. nodes := []string{"foo", "bar"} - for _, node := range nodes { - req := structs.RegisterRequest{ - Datacenter: "dc1", - Node: node, - Address: "127.0.0.1", - } - var reply struct{} - if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &req, &reply); err != nil { - t.Fatalf("err: %v", err) - } + if err := registerNodes(nodes, codec); err != nil { + t.Fatal(err) } // Send coordinate updates for each node. @@ -582,16 +551,8 @@ func TestCoordinate_Node_ACLDeny(t *testing.T) { // Register some nodes. nodes := []string{"node1", "node2"} - for _, node := range nodes { - req := structs.RegisterRequest{ - Datacenter: "dc1", - Node: node, - Address: "127.0.0.1", - } - var reply struct{} - if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &req, &reply); err != nil { - t.Fatalf("err: %v", err) - } + if err := registerNodes(nodes, codec); err != nil { + t.Fatal(err) } // Send an update for the first node. This should go through since we @@ -646,3 +607,19 @@ node "node1" { t.Fatalf("err: %v", err) } } + +func registerNodes(nodes []string, codec rpc.ClientCodec) error { + for _, node := range nodes { + req := structs.RegisterRequest{ + Datacenter: "dc1", + Node: node, + Address: "127.0.0.1", + } + var reply struct{} + if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &req, &reply); err != nil { + return err + } + } + + return nil +} diff --git a/agent/consul/state/coordinate.go b/agent/consul/state/coordinate.go index 68087b1cb1..c810a8151f 100644 --- a/agent/consul/state/coordinate.go +++ b/agent/consul/state/coordinate.go @@ -46,8 +46,7 @@ func (s *Store) Coordinate(node string, ws memdb.WatchSet) (uint64, lib.Coordina tx := s.db.Txn(false) defer tx.Abort() - // Get the table index. - idx := maxIndexTxn(tx, "coordinates") + tableIdx := maxIndexTxn(tx, "coordinates") iter, err := tx.Get("coordinates", "node", node) if err != nil { @@ -60,7 +59,7 @@ func (s *Store) Coordinate(node string, ws memdb.WatchSet) (uint64, lib.Coordina coord := raw.(*structs.Coordinate) results[coord.Segment] = coord.Coord } - return idx, results, nil + return tableIdx, results, nil } // Coordinates queries for all nodes with coordinates.