Add retries to all `obj`

This commit is contained in:
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,20 +81,22 @@ 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()
obj, err := a.srv.CoordinateNodes(resp, req) retry.Run(t, func(r *retry.R) {
if err != nil { obj, err := a.srv.CoordinateNodes(resp, req)
t.Fatalf("err: %v", err) if err != nil {
} t.Fatalf("err: %v", err)
}
// Check that coordinates are empty before registering a node // Check that coordinates are empty before registering a node
coordinates, ok := obj.(structs.Coordinates) coordinates, ok := obj.(structs.Coordinates)
if !ok { if !ok {
t.Fatalf("expected: structs.Coordinates, received: %v", coordinates) t.Fatalf("expected: structs.Coordinates, received: %v", coordinates)
} }
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) {
if err != nil { obj, err := a.srv.CoordinateNodes(resp, req)
t.Fatalf("err: %v", err) 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)
}
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 // 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) {
if err != nil { obj, err := a.srv.CoordinateNodes(resp, req)
t.Fatalf("err: %v", err) if err != nil {
} t.Fatalf("err: %v", err)
}
coordinates = obj.(structs.Coordinates)
if len(coordinates) != 0 {
t.Fatalf("bad: %v", coordinates)
}
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 // 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) {
if err != nil { obj, err := a.srv.CoordinateNodes(resp, req)
t.Fatalf("err: %v", err) 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)
}
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 // 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) {
if err != nil { obj, err := a.srv.CoordinateNodes(resp, req)
t.Fatalf("err: %v", err) if err != nil {
} 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) {