From 73d73e0e20280a315ba7f68e454547ce6f21aeb3 Mon Sep 17 00:00:00 2001 From: Sarah Christoff Date: Tue, 21 May 2019 13:31:37 -0500 Subject: [PATCH] Add retries to all `obj` --- agent/coordinate_endpoint_test.go | 118 +++++++++++++++++------------- 1 file changed, 69 insertions(+), 49 deletions(-) diff --git a/agent/coordinate_endpoint_test.go b/agent/coordinate_endpoint_test.go index edc5511d35..0f5b55e8db 100644 --- a/agent/coordinate_endpoint_test.go +++ b/agent/coordinate_endpoint_test.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/agent/structs" + "github.com/hashicorp/consul/sdk/testutil/retry" "github.com/hashicorp/consul/testrpc" "github.com/hashicorp/serf/coordinate" ) @@ -80,20 +81,22 @@ func TestCoordinate_Nodes(t *testing.T) { // Make sure an empty list is non-nil. req, _ := http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil) resp := httptest.NewRecorder() - obj, err := a.srv.CoordinateNodes(resp, req) - if err != nil { - t.Fatalf("err: %v", err) - } + retry.Run(t, func(r *retry.R) { + obj, err := a.srv.CoordinateNodes(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } - // Check that coordinates are empty before registering a node - coordinates, ok := obj.(structs.Coordinates) - if !ok { - t.Fatalf("expected: structs.Coordinates, received: %v", coordinates) - } + // Check that coordinates are empty before registering a node + coordinates, ok := obj.(structs.Coordinates) + if !ok { + t.Fatalf("expected: structs.Coordinates, received: %v", coordinates) + } - if len(coordinates) != 0 { - t.Fatalf("coordinates should be empty, received: %v", coordinates) - } + if len(coordinates) != 0 { + t.Fatalf("coordinates should be empty, received: %v", coordinates) + } + }) // Register the nodes. nodes := []string{"foo", "bar"} @@ -135,56 +138,73 @@ func TestCoordinate_Nodes(t *testing.T) { // Query back and check the nodes are present and sorted correctly. req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil) resp = httptest.NewRecorder() - obj, err = a.srv.CoordinateNodes(resp, req) - if err != nil { - t.Fatalf("err: %v", err) - } - - coordinates = obj.(structs.Coordinates) - if len(coordinates) != 2 || - coordinates[0].Node != "bar" || - coordinates[1].Node != "foo" { - t.Fatalf("bad: %v", coordinates) - } + retry.Run(t, func(r *retry.R) { + obj, err := a.srv.CoordinateNodes(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + coordinates, ok := obj.(structs.Coordinates) + if !ok { + t.Fatalf("expected: structs.Coordinates, received: %v", coordinates) + } + if len(coordinates) != 2 || + coordinates[0].Node != "bar" || + coordinates[1].Node != "foo" { + t.Fatalf("expected: bar, foo recieved: %v", coordinates) + } + }) // Filter on a nonexistent node segment req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?segment=nope", nil) resp = httptest.NewRecorder() - obj, err = a.srv.CoordinateNodes(resp, req) - if err != nil { - t.Fatalf("err: %v", err) - } - - coordinates = obj.(structs.Coordinates) - if len(coordinates) != 0 { - t.Fatalf("bad: %v", coordinates) - } + retry.Run(t, func(r *retry.R) { + obj, err := a.srv.CoordinateNodes(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + coordinates, ok := obj.(structs.Coordinates) + if !ok { + t.Fatalf("expected: structs.Coordinates, received: %v", coordinates) + } + if len(coordinates) != 0 { + t.Fatalf("coordinates should be empty, received: %v", coordinates) + } + }) // Filter on a real node segment req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?segment=alpha", nil) resp = httptest.NewRecorder() - obj, err = a.srv.CoordinateNodes(resp, req) - if err != nil { - t.Fatalf("err: %v", err) - } - - coordinates = obj.(structs.Coordinates) - if len(coordinates) != 1 || coordinates[0].Node != "foo" { - t.Fatalf("bad: %v", coordinates) - } + retry.Run(t, func(r *retry.R) { + obj, err := a.srv.CoordinateNodes(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + coordinates, ok := obj.(structs.Coordinates) + if !ok { + t.Fatalf("expected: structs.Coordinates, received: %v", coordinates) + } + if len(coordinates) != 1 || coordinates[0].Node != "foo" { + t.Fatalf("expected: foo received: %v", coordinates) + } + }) // Make sure the empty filter works req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?segment=", nil) resp = httptest.NewRecorder() - obj, err = a.srv.CoordinateNodes(resp, req) - if err != nil { - t.Fatalf("err: %v", err) - } + retry.Run(t, func(r *retry.R) { + obj, err := a.srv.CoordinateNodes(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } - coordinates = obj.(structs.Coordinates) - if len(coordinates) != 1 || coordinates[0].Node != "bar" { - t.Fatalf("bad: %v", coordinates) - } + coordinates, ok := obj.(structs.Coordinates) + if !ok { + t.Fatalf("expected: structs.Coordinates, received: %v", coordinates) + } + if len(coordinates) != 1 || coordinates[0].Node != "bar" { + t.Fatalf("expected: bar received: %v", coordinates) + } + }) } func TestCoordinate_Node(t *testing.T) {