Add retries to all `obj`

pull/5867/head
Sarah Christoff 2019-05-21 13:31:37 -05:00
parent b96d9b01bd
commit 73d73e0e20
1 changed files with 69 additions and 49 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/consul/testrpc" "github.com/hashicorp/consul/testrpc"
"github.com/hashicorp/serf/coordinate" "github.com/hashicorp/serf/coordinate"
) )
@ -80,6 +81,7 @@ func TestCoordinate_Nodes(t *testing.T) {
// Make sure an empty list is non-nil. // Make sure an empty list is non-nil.
req, _ := http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil) req, _ := http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
retry.Run(t, func(r *retry.R) {
obj, err := a.srv.CoordinateNodes(resp, req) obj, err := a.srv.CoordinateNodes(resp, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -94,6 +96,7 @@ func TestCoordinate_Nodes(t *testing.T) {
if len(coordinates) != 0 { if len(coordinates) != 0 {
t.Fatalf("coordinates should be empty, received: %v", coordinates) t.Fatalf("coordinates should be empty, received: %v", coordinates)
} }
})
// Register the nodes. // Register the nodes.
nodes := []string{"foo", "bar"} 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. // Query back and check the nodes are present and sorted correctly.
req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil) req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil)
resp = httptest.NewRecorder() resp = httptest.NewRecorder()
obj, err = a.srv.CoordinateNodes(resp, req) retry.Run(t, func(r *retry.R) {
obj, err := a.srv.CoordinateNodes(resp, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
coordinates = obj.(structs.Coordinates) coordinates, ok := obj.(structs.Coordinates)
if !ok {
t.Fatalf("expected: structs.Coordinates, received: %v", coordinates)
}
if len(coordinates) != 2 || if len(coordinates) != 2 ||
coordinates[0].Node != "bar" || coordinates[0].Node != "bar" ||
coordinates[1].Node != "foo" { coordinates[1].Node != "foo" {
t.Fatalf("bad: %v", coordinates) t.Fatalf("expected: bar, foo recieved: %v", coordinates)
} }
})
// Filter on a nonexistent node segment // Filter on a nonexistent node segment
req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?segment=nope", nil) req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?segment=nope", nil)
resp = httptest.NewRecorder() resp = httptest.NewRecorder()
obj, err = a.srv.CoordinateNodes(resp, req) retry.Run(t, func(r *retry.R) {
obj, err := a.srv.CoordinateNodes(resp, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
coordinates = obj.(structs.Coordinates) coordinates, ok := obj.(structs.Coordinates)
if len(coordinates) != 0 { if !ok {
t.Fatalf("bad: %v", coordinates) 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 // Filter on a real node segment
req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?segment=alpha", nil) req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?segment=alpha", nil)
resp = httptest.NewRecorder() resp = httptest.NewRecorder()
obj, err = a.srv.CoordinateNodes(resp, req) retry.Run(t, func(r *retry.R) {
obj, err := a.srv.CoordinateNodes(resp, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
coordinates = obj.(structs.Coordinates) coordinates, ok := obj.(structs.Coordinates)
if len(coordinates) != 1 || coordinates[0].Node != "foo" { if !ok {
t.Fatalf("bad: %v", coordinates) 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 // Make sure the empty filter works
req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?segment=", nil) req, _ = http.NewRequest("GET", "/v1/coordinate/nodes?segment=", nil)
resp = httptest.NewRecorder() resp = httptest.NewRecorder()
obj, err = a.srv.CoordinateNodes(resp, req) retry.Run(t, func(r *retry.R) {
obj, err := a.srv.CoordinateNodes(resp, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
coordinates = obj.(structs.Coordinates) coordinates, ok := obj.(structs.Coordinates)
if len(coordinates) != 1 || coordinates[0].Node != "bar" { if !ok {
t.Fatalf("bad: %v", coordinates) 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) { func TestCoordinate_Node(t *testing.T) {