Merge pull request #5867 from hashicorp/bug/test-flaky-TestCoordinate_Node

Update type assertion logic
This commit is contained in:
Sarah Christoff 2019-05-21 14:56:05 -05:00 committed by GitHub
commit 5b6ef7b4da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 70 additions and 45 deletions

View File

@ -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,15 +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)
}
coordinates := obj.(structs.Coordinates)
if coordinates == nil || len(coordinates) != 0 {
t.Fatalf("bad: %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", obj)
}
if len(coordinates) != 0 {
t.Fatalf("coordinates should be empty, received: %v", coordinates)
}
})
// Register the nodes.
nodes := []string{"foo", "bar"}
@ -130,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", obj)
}
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", obj)
}
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", obj)
}
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", obj)
}
if len(coordinates) != 1 || coordinates[0].Node != "bar" {
t.Fatalf("expected: bar received: %v", coordinates)
}
})
}
func TestCoordinate_Node(t *testing.T) {