From 2794db5aac0ea00f43509e1c68dd9d2066fb7496 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Fri, 5 May 2017 12:14:43 +0200 Subject: [PATCH] test: Fix badly formatted retry.Run tests --- command/agent/event_endpoint_test.go | 46 +++++----- command/agent/health_endpoint_test.go | 126 ++++++++++++-------------- command/agent/local_test.go | 68 ++++++-------- consul/autopilot_test.go | 55 ++++++----- consul/catalog_endpoint_test.go | 18 ++-- 5 files changed, 141 insertions(+), 172 deletions(-) diff --git a/command/agent/event_endpoint_test.go b/command/agent/event_endpoint_test.go index 6dceeba8d3..5852c6bc3a 100644 --- a/command/agent/event_endpoint_test.go +++ b/command/agent/event_endpoint_test.go @@ -318,31 +318,27 @@ func TestEventList_EventBufOrder(t *testing.T) { t.Fatalf("err: %v", err) } } - retry. - - // Test that the event order is preserved when name - // filtering on a list of > 1 matching event. - Run(t, func(r *retry.R) { - - url := "/v1/event/list?name=foo" - req, err := http.NewRequest("GET", url, nil) - if err != nil { - r.Fatal(err) - } - resp := httptest.NewRecorder() - obj, err := srv.EventList(resp, req) - if err != nil { - r.Fatal(err) - } - list, ok := obj.([]*UserEvent) - if !ok { - r.Fatalf("bad: %#v", obj) - } - if len(list) != 3 || list[2].ID != expected.ID { - r.Fatalf("bad: %#v", list) - } - }) - + // Test that the event order is preserved when name + // filtering on a list of > 1 matching event. + retry.Run(t, func(r *retry.R) { + url := "/v1/event/list?name=foo" + req, err := http.NewRequest("GET", url, nil) + if err != nil { + r.Fatal(err) + } + resp := httptest.NewRecorder() + obj, err := srv.EventList(resp, req) + if err != nil { + r.Fatal(err) + } + list, ok := obj.([]*UserEvent) + if !ok { + r.Fatalf("bad: %#v", obj) + } + if len(list) != 3 || list[2].ID != expected.ID { + r.Fatalf("bad: %#v", list) + } + }) }) } diff --git a/command/agent/health_endpoint_test.go b/command/agent/health_endpoint_test.go index 22a988008f..df2346e2d9 100644 --- a/command/agent/health_endpoint_test.go +++ b/command/agent/health_endpoint_test.go @@ -167,29 +167,25 @@ func TestHealthChecksInState_DistanceSort(t *testing.T) { if err := srv.agent.RPC("Coordinate.Update", &arg, &out); err != nil { t.Fatalf("err: %v", err) } - retry. - - // Retry until foo moves to the front of the line. - Run(t, func(r *retry.R) { - - resp = httptest.NewRecorder() - obj, err = srv.HealthChecksInState(resp, req) - if err != nil { - r.Fatalf("err: %v", err) - } - assertIndex(t, resp) - nodes = obj.(structs.HealthChecks) - if len(nodes) != 2 { - r.Fatalf("bad: %v", nodes) - } - if nodes[0].Node != "foo" { - r.Fatalf("bad: %v", nodes) - } - if nodes[1].Node != "bar" { - r.Fatalf("bad: %v", nodes) - } - }) - + // Retry until foo moves to the front of the line. + retry.Run(t, func(r *retry.R) { + resp = httptest.NewRecorder() + obj, err = srv.HealthChecksInState(resp, req) + if err != nil { + r.Fatalf("err: %v", err) + } + assertIndex(t, resp) + nodes = obj.(structs.HealthChecks) + if len(nodes) != 2 { + r.Fatalf("bad: %v", nodes) + } + if nodes[0].Node != "foo" { + r.Fatalf("bad: %v", nodes) + } + if nodes[1].Node != "bar" { + r.Fatalf("bad: %v", nodes) + } + }) } func TestHealthNodeChecks(t *testing.T) { @@ -428,29 +424,25 @@ func TestHealthServiceChecks_DistanceSort(t *testing.T) { if err := srv.agent.RPC("Coordinate.Update", &arg, &out); err != nil { t.Fatalf("err: %v", err) } - retry. - - // Retry until foo has moved to the front of the line. - Run(t, func(r *retry.R) { - - resp = httptest.NewRecorder() - obj, err = srv.HealthServiceChecks(resp, req) - if err != nil { - r.Fatalf("err: %v", err) - } - assertIndex(t, resp) - nodes = obj.(structs.HealthChecks) - if len(nodes) != 2 { - r.Fatalf("bad: %v", obj) - } - if nodes[0].Node != "foo" { - r.Fatalf("bad: %v", nodes) - } - if nodes[1].Node != "bar" { - r.Fatalf("bad: %v", nodes) - } - }) - + // Retry until foo has moved to the front of the line. + retry.Run(t, func(r *retry.R) { + resp = httptest.NewRecorder() + obj, err = srv.HealthServiceChecks(resp, req) + if err != nil { + r.Fatalf("err: %v", err) + } + assertIndex(t, resp) + nodes = obj.(structs.HealthChecks) + if len(nodes) != 2 { + r.Fatalf("bad: %v", obj) + } + if nodes[0].Node != "foo" { + r.Fatalf("bad: %v", nodes) + } + if nodes[1].Node != "bar" { + r.Fatalf("bad: %v", nodes) + } + }) } func TestHealthServiceNodes(t *testing.T) { @@ -662,29 +654,25 @@ func TestHealthServiceNodes_DistanceSort(t *testing.T) { if err := srv.agent.RPC("Coordinate.Update", &arg, &out); err != nil { t.Fatalf("err: %v", err) } - retry. - - // Retry until foo has moved to the front of the line. - Run(t, func(r *retry.R) { - - resp = httptest.NewRecorder() - obj, err = srv.HealthServiceNodes(resp, req) - if err != nil { - r.Fatalf("err: %v", err) - } - assertIndex(t, resp) - nodes = obj.(structs.CheckServiceNodes) - if len(nodes) != 2 { - r.Fatalf("bad: %v", obj) - } - if nodes[0].Node.Node != "foo" { - r.Fatalf("bad: %v", nodes) - } - if nodes[1].Node.Node != "bar" { - r.Fatalf("bad: %v", nodes) - } - }) - + // Retry until foo has moved to the front of the line. + retry.Run(t, func(r *retry.R) { + resp = httptest.NewRecorder() + obj, err = srv.HealthServiceNodes(resp, req) + if err != nil { + r.Fatalf("err: %v", err) + } + assertIndex(t, resp) + nodes = obj.(structs.CheckServiceNodes) + if len(nodes) != 2 { + r.Fatalf("bad: %v", obj) + } + if nodes[0].Node.Node != "foo" { + r.Fatalf("bad: %v", nodes) + } + if nodes[1].Node.Node != "bar" { + r.Fatalf("bad: %v", nodes) + } + }) } func TestHealthServiceNodes_PassingFilter(t *testing.T) { diff --git a/command/agent/local_test.go b/command/agent/local_test.go index 632cdd98dd..840c7e5883 100644 --- a/command/agent/local_test.go +++ b/command/agent/local_test.go @@ -1289,26 +1289,23 @@ func TestAgentAntiEntropy_NodeInfo(t *testing.T) { Node: agent.config.NodeName, } var services structs.IndexedNodeServices - retry. + // Wait for the sync + retry.Run(t, func(r *retry.R) { + if err := agent.RPC("Catalog.NodeServices", &req, &services); err != nil { + r.Fatalf("err: %v", err) + } - // Wait for the sync - Run(t, func(r *retry.R) { - - if err := agent.RPC("Catalog.NodeServices", &req, &services); err != nil { - r.Fatalf("err: %v", err) - } - - // Make sure we synced our node info - this should have ridden on the - // "consul" service sync - id := services.NodeServices.Node.ID - addrs := services.NodeServices.Node.TaggedAddresses - meta := services.NodeServices.Node.Meta - if id != conf.NodeID || - !reflect.DeepEqual(addrs, conf.TaggedAddresses) || - !reflect.DeepEqual(meta, conf.Meta) { - r.Fatalf("bad: %v", services.NodeServices.Node) - } - }) + // Make sure we synced our node info - this should have ridden on the + // "consul" service sync + id := services.NodeServices.Node.ID + addrs := services.NodeServices.Node.TaggedAddresses + meta := services.NodeServices.Node.Meta + if id != conf.NodeID || + !reflect.DeepEqual(addrs, conf.TaggedAddresses) || + !reflect.DeepEqual(meta, conf.Meta) { + r.Fatalf("bad: %v", services.NodeServices.Node) + } + }) // Blow away the catalog version of the node info if err := agent.RPC("Catalog.Register", args, &out); err != nil { @@ -1317,26 +1314,21 @@ func TestAgentAntiEntropy_NodeInfo(t *testing.T) { // Trigger anti-entropy run and wait agent.StartSync() - retry. - - // Wait for the sync - this should have been a sync of just the - // node info - Run(t, func(r *retry.R) { - - if err := agent.RPC("Catalog.NodeServices", &req, &services); err != nil { - r.Fatalf("err: %v", err) - } - - id := services.NodeServices.Node.ID - addrs := services.NodeServices.Node.TaggedAddresses - meta := services.NodeServices.Node.Meta - if id != conf.NodeID || - !reflect.DeepEqual(addrs, conf.TaggedAddresses) || - !reflect.DeepEqual(meta, conf.Meta) { - r.Fatalf("bad: %v", services.NodeServices.Node) - } - }) + // Wait for the sync - this should have been a sync of just the node info + retry.Run(t, func(r *retry.R) { + if err := agent.RPC("Catalog.NodeServices", &req, &services); err != nil { + r.Fatalf("err: %v", err) + } + id := services.NodeServices.Node.ID + addrs := services.NodeServices.Node.TaggedAddresses + meta := services.NodeServices.Node.Meta + if id != conf.NodeID || + !reflect.DeepEqual(addrs, conf.TaggedAddresses) || + !reflect.DeepEqual(meta, conf.Meta) { + r.Fatalf("bad: %v", services.NodeServices.Node) + } + }) } func TestAgentAntiEntropy_deleteService_fails(t *testing.T) { diff --git a/consul/autopilot_test.go b/consul/autopilot_test.go index 64f4350b8a..d2dd87687d 100644 --- a/consul/autopilot_test.go +++ b/consul/autopilot_test.go @@ -239,37 +239,34 @@ func TestAutopilot_PromoteNonVoter(t *testing.T) { } testrpc.WaitForLeader(t, s1.RPC, "dc1") - retry. + // Wait for the new server to be added as a non-voter, but make sure + // it doesn't get promoted to a voter even after ServerStabilizationTime, + // because that would result in an even-numbered quorum count. + retry.Run(t, func(r *retry.R) { + future := s1.raft.GetConfiguration() + if err := future.Error(); err != nil { + r.Fatal(err) + } - // Wait for the new server to be added as a non-voter, but make sure - // it doesn't get promoted to a voter even after ServerStabilizationTime, - // because that would result in an even-numbered quorum count. - Run(t, func(r *retry.R) { + servers := future.Configuration().Servers - future := s1.raft.GetConfiguration() - if err := future.Error(); err != nil { - r.Fatal(err) - } - - servers := future.Configuration().Servers - - if len(servers) != 2 { - r.Fatalf("bad: %v", servers) - } - if servers[1].Suffrage != raft.Nonvoter { - r.Fatalf("bad: %v", servers) - } - health := s1.getServerHealth(string(servers[1].ID)) - if health == nil { - r.Fatal("nil health") - } - if !health.Healthy { - r.Fatalf("bad: %v", health) - } - if time.Now().Sub(health.StableSince) < s1.config.AutopilotConfig.ServerStabilizationTime { - r.Fatal("stable period not elapsed") - } - }) + if len(servers) != 2 { + r.Fatalf("bad: %v", servers) + } + if servers[1].Suffrage != raft.Nonvoter { + r.Fatalf("bad: %v", servers) + } + health := s1.getServerHealth(string(servers[1].ID)) + if health == nil { + r.Fatal("nil health") + } + if !health.Healthy { + r.Fatalf("bad: %v", health) + } + if time.Now().Sub(health.StableSince) < s1.config.AutopilotConfig.ServerStabilizationTime { + r.Fatal("stable period not elapsed") + } + }) // Now add another server and make sure they both get promoted to voters after stabilization dir3, s3 := testServerWithConfig(t, func(c *Config) { diff --git a/consul/catalog_endpoint_test.go b/consul/catalog_endpoint_test.go index c4b562db73..747f4efde0 100644 --- a/consul/catalog_endpoint_test.go +++ b/consul/catalog_endpoint_test.go @@ -676,17 +676,13 @@ func TestCatalog_ListNodes_NodeMetaFilter(t *testing.T) { if err != nil { t.Fatalf("err: %v", err) } - retry. - - // Should get an empty list of nodes back - Run(t, func(r *retry.R) { - - msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out) - if len(out.Nodes) != 0 { - r.Fatal(nil) - } - }) - + // Should get an empty list of nodes back + retry.Run(t, func(r *retry.R) { + msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out) + if len(out.Nodes) != 0 { + r.Fatal(nil) + } + }) } func TestCatalog_ListNodes_StaleRaad(t *testing.T) {