From 0f295433150b325f2604efe1b31228a8a392aebd Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" Date: Thu, 29 Aug 2019 09:55:05 -0500 Subject: [PATCH] test: add additional http status code assertions in coordinate HTTP API tests (#6410) When this test flakes sometimes this happens: --- FAIL: TestCoordinate_Node (1.69s) panic: interface conversion: interface {} is nil, not structs.Coordinates [recovered] FAIL github.com/hashicorp/consul/agent 19.999s Exit code: 1 panic: interface conversion: interface {} is nil, not structs.Coordinates [recovered] panic: interface conversion: interface {} is nil, not structs.Coordinates There is definitely a bug lurking, but the code seems to imply this can only return nil on 404. The tests previously were not checking the status code. The underlying cause of the flake is unknown, but this should turn the failure into a more normal test failure. --- agent/coordinate_endpoint_test.go | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/agent/coordinate_endpoint_test.go b/agent/coordinate_endpoint_test.go index 208b05551..060db6f6b 100644 --- a/agent/coordinate_endpoint_test.go +++ b/agent/coordinate_endpoint_test.go @@ -63,6 +63,10 @@ func TestCoordinate_Datacenters(t *testing.T) { t.Fatalf("err: %v", err) } + if resp.Code != http.StatusOK { + t.Fatalf("bad: %v", resp.Code) + } + maps := obj.([]structs.DatacenterMap) if len(maps) != 1 || maps[0].Datacenter != "dc1" || @@ -87,6 +91,10 @@ func TestCoordinate_Nodes(t *testing.T) { r.Fatalf("err: %v", err) } + if resp.Code != http.StatusOK { + r.Fatalf("bad: %v", resp.Code) + } + // Check that coordinates are empty before registering a node coordinates, ok := obj.(structs.Coordinates) if !ok { @@ -144,6 +152,10 @@ func TestCoordinate_Nodes(t *testing.T) { r.Fatalf("err: %v", err) } + if resp.Code != http.StatusOK { + r.Fatalf("bad: %v", resp.Code) + } + coordinates, ok := obj.(structs.Coordinates) if !ok { r.Fatalf("expected: structs.Coordinates, received: %+v", obj) @@ -163,6 +175,10 @@ func TestCoordinate_Nodes(t *testing.T) { r.Fatalf("err: %v", err) } + if resp.Code != http.StatusOK { + r.Fatalf("bad: %v", resp.Code) + } + coordinates, ok := obj.(structs.Coordinates) if !ok { r.Fatalf("expected: structs.Coordinates, received: %+v", obj) @@ -180,6 +196,10 @@ func TestCoordinate_Nodes(t *testing.T) { r.Fatalf("err: %v", err) } + if resp.Code != http.StatusOK { + r.Fatalf("bad: %v", resp.Code) + } + coordinates, ok := obj.(structs.Coordinates) if !ok { r.Fatalf("expected: structs.Coordinates, received: %+v", obj) @@ -220,6 +240,7 @@ func TestCoordinate_Node(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } + if resp.Code != http.StatusNotFound { t.Fatalf("bad: %v", resp.Code) } @@ -269,6 +290,10 @@ func TestCoordinate_Node(t *testing.T) { t.Fatalf("err: %v", err) } + if resp.Code != http.StatusOK { + t.Fatalf("bad: %v", resp.Code) + } + coordinates := obj.(structs.Coordinates) if len(coordinates) != 1 || coordinates[0].Node != "foo" { @@ -294,6 +319,10 @@ func TestCoordinate_Node(t *testing.T) { t.Fatalf("err: %v", err) } + if resp.Code != http.StatusOK { + t.Fatalf("bad: %v", resp.Code) + } + coordinates = obj.(structs.Coordinates) if len(coordinates) != 1 || coordinates[0].Node != "foo" { t.Fatalf("bad: %v", coordinates) @@ -342,6 +371,11 @@ func TestCoordinate_Update(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } + + if resp.Code != http.StatusOK { + t.Fatalf("bad: %v", resp.Code) + } + time.Sleep(300 * time.Millisecond) // Query back and check the coordinates are present.