From 98cdcaa55083eed4ee800c47a3988ddd28ca9bae Mon Sep 17 00:00:00 2001 From: Rebecca Zanzig Date: Sun, 7 Oct 2018 22:03:22 -0700 Subject: [PATCH] Update new tests to use the `require` library --- agent/consul/catalog_endpoint_test.go | 9 +-- agent/consul/health_endpoint_test.go | 33 +++------ agent/consul/state/catalog_test.go | 97 +++++++-------------------- api/health_test.go | 7 ++ 4 files changed, 43 insertions(+), 103 deletions(-) diff --git a/agent/consul/catalog_endpoint_test.go b/agent/consul/catalog_endpoint_test.go index 2fa87fa604f9..d7569fa9961d 100644 --- a/agent/consul/catalog_endpoint_test.go +++ b/agent/consul/catalog_endpoint_test.go @@ -1728,13 +1728,8 @@ func TestCatalog_ListServiceNodes_NodeMetaFilter(t *testing.T) { TagFilter: len(tc.tags) > 0, } var out structs.IndexedServiceNodes - if err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &args, &out); err != nil { - t.Fatalf("err: %v", err) - } - - if len(out.ServiceNodes) != len(tc.services) { - t.Fatalf("bad: %v", out) - } + require.NoError(t, msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &args, &out)) + require.Len(t, out.ServiceNodes, len(tc.services)) for i, serviceNode := range out.ServiceNodes { if serviceNode.Node != tc.services[i].Node || serviceNode.ServiceID != tc.services[i].ServiceID { diff --git a/agent/consul/health_endpoint_test.go b/agent/consul/health_endpoint_test.go index e62fbb1f5a0e..cbe1141f2e90 100644 --- a/agent/consul/health_endpoint_test.go +++ b/agent/consul/health_endpoint_test.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/consul/testrpc" "github.com/hashicorp/net-rpc-msgpackrpc" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestHealth_ChecksInState(t *testing.T) { @@ -628,9 +629,7 @@ func TestHealth_ServiceNodes_MultipleServiceTags(t *testing.T) { }, } var out struct{} - if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil { - t.Fatalf("err: %v", err) - } + require.NoError(t, msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)) arg = structs.RegisterRequest{ Datacenter: "dc1", @@ -647,9 +646,7 @@ func TestHealth_ServiceNodes_MultipleServiceTags(t *testing.T) { ServiceID: "db", }, } - if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil { - t.Fatalf("err: %v", err) - } + require.NoError(t, msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)) var out2 structs.IndexedCheckServiceNodes req := structs.ServiceSpecificRequest{ @@ -658,26 +655,14 @@ func TestHealth_ServiceNodes_MultipleServiceTags(t *testing.T) { ServiceTags: []string{"master", "v2"}, TagFilter: true, } - if err := msgpackrpc.CallWithCodec(codec, "Health.ServiceNodes", &req, &out2); err != nil { - t.Fatalf("err: %v", err) - } + require.NoError(t, msgpackrpc.CallWithCodec(codec, "Health.ServiceNodes", &req, &out2)) nodes := out2.Nodes - if len(nodes) != 1 { - t.Fatalf("Bad: %v", nodes) - } - if nodes[0].Node.Node != "foo" { - t.Fatalf("Bad: %v", nodes[0]) - } - if !lib.StrContains(nodes[0].Service.Tags, "v2") { - t.Fatalf("Bad: %v", nodes[0]) - } - if !lib.StrContains(nodes[0].Service.Tags, "master") { - t.Fatalf("Bad: %v", nodes[0]) - } - if nodes[0].Checks[0].Status != api.HealthPassing { - t.Fatalf("Bad: %v", nodes[0]) - } + require.Len(t, nodes, 1) + require.Equal(t, nodes[0].Node.Node, "foo") + require.Contains(t, nodes[0].Service.Tags, "v2") + require.Contains(t, nodes[0].Service.Tags, "master") + require.Equal(t, nodes[0].Checks[0].Status, api.HealthPassing) } func TestHealth_ServiceNodes_NodeMetaFilter(t *testing.T) { diff --git a/agent/consul/state/catalog_test.go b/agent/consul/state/catalog_test.go index f4f49da98792..f27b9c118e0a 100644 --- a/agent/consul/state/catalog_test.go +++ b/agent/consul/state/catalog_test.go @@ -15,6 +15,7 @@ import ( uuid "github.com/hashicorp/go-uuid" "github.com/pascaldekloe/goe/verify" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func makeRandomNodeID(t *testing.T) types.NodeID { @@ -1904,85 +1905,37 @@ func TestStateStore_ServiceTagNodes_MultipleTags(t *testing.T) { } idx, nodes, err := s.ServiceTagNodes(nil, "db", []string{"master"}) - if err != nil { - t.Fatalf("err: %s", err) - } - if idx != 19 { - t.Fatalf("bad: %v", idx) - } - if len(nodes) != 1 { - t.Fatalf("bad: %v", nodes) - } - if nodes[0].Node != "foo" { - t.Fatalf("bad: %v", nodes) - } - if nodes[0].Address != "127.0.0.1" { - t.Fatalf("bad: %v", nodes) - } - if !lib.StrContains(nodes[0].ServiceTags, "master") { - t.Fatalf("bad: %v", nodes) - } - if nodes[0].ServicePort != 8000 { - t.Fatalf("bad: %v", nodes) - } + require.NoError(t, err) + require.Equal(t, int(idx), 19) + require.Len(t, nodes, 1) + require.Equal(t, nodes[0].Node, "foo") + require.Equal(t, nodes[0].Address, "127.0.0.1") + require.Contains(t, nodes[0].ServiceTags, "master") + require.Equal(t, nodes[0].ServicePort, 8000) idx, nodes, err = s.ServiceTagNodes(nil, "db", []string{"v2"}) - if err != nil { - t.Fatalf("err: %s", err) - } - if idx != 19 { - t.Fatalf("bad: %v", idx) - } - if len(nodes) != 3 { - t.Fatalf("bad: %v", nodes) - } + require.NoError(t, err) + require.Equal(t, int(idx), 19) + require.Len(t, nodes, 3) // Test filtering on multiple tags idx, nodes, err = s.ServiceTagNodes(nil, "db", []string{"v2", "slave"}) - if err != nil { - t.Fatalf("err: %s", err) - } - if idx != 19 { - t.Fatalf("bad: %v", idx) - } - if len(nodes) != 2 { - t.Fatalf("bad: %v", nodes) - } - if !lib.StrContains(nodes[0].ServiceTags, "v2") { - t.Fatalf("bad: %v", nodes) - } - if !lib.StrContains(nodes[0].ServiceTags, "slave") { - t.Fatalf("bad: %v", nodes) - } - if !lib.StrContains(nodes[1].ServiceTags, "v2") { - t.Fatalf("bad: %v", nodes) - } - if !lib.StrContains(nodes[1].ServiceTags, "slave") { - t.Fatalf("bad: %v", nodes) - } + require.NoError(t, err) + require.Equal(t, int(idx), 19) + require.Len(t, nodes, 2) + require.Contains(t, nodes[0].ServiceTags, "v2") + require.Contains(t, nodes[0].ServiceTags, "slave") + require.Contains(t, nodes[1].ServiceTags, "v2") + require.Contains(t, nodes[1].ServiceTags, "slave") idx, nodes, err = s.ServiceTagNodes(nil, "db", []string{"dev"}) - if err != nil { - t.Fatalf("err: %s", err) - } - if idx != 19 { - t.Fatalf("bad: %v", idx) - } - if len(nodes) != 1 { - t.Fatalf("bad: %v", nodes) - } - if nodes[0].Node != "foo" { - t.Fatalf("bad: %v", nodes) - } - if nodes[0].Address != "127.0.0.1" { - t.Fatalf("bad: %v", nodes) - } - if !lib.StrContains(nodes[0].ServiceTags, "dev") { - t.Fatalf("bad: %v", nodes) - } - if nodes[0].ServicePort != 8001 { - t.Fatalf("bad: %v", nodes) - } + require.NoError(t, err) + require.Equal(t, int(idx), 19) + require.Len(t, nodes, 1) + require.Equal(t, nodes[0].Node, "foo") + require.Equal(t, nodes[0].Address, "127.0.0.1") + require.Contains(t, nodes[0].ServiceTags, "dev") + require.Equal(t, nodes[0].ServicePort, 8001) } func TestStateStore_DeleteService(t *testing.T) { diff --git a/api/health_test.go b/api/health_test.go index 1ecd3cfc85dd..2663e59f7075 100644 --- a/api/health_test.go +++ b/api/health_test.go @@ -316,8 +316,10 @@ func TestAPI_HealthService_MultipleTags(t *testing.T) { conf.NodeName = "node123" }) defer s.Stop() + agent := c.Agent() health := c.Health() + // Make two services with a check reg := &AgentServiceRegistration{ Name: "foo", @@ -330,6 +332,7 @@ func TestAPI_HealthService_MultipleTags(t *testing.T) { } require.NoError(t, agent.ServiceRegister(reg)) defer agent.ServiceDeregister("foo1") + reg2 := &AgentServiceRegistration{ Name: "foo", ID: "foo2", @@ -341,16 +344,20 @@ func TestAPI_HealthService_MultipleTags(t *testing.T) { } require.NoError(t, agent.ServiceRegister(reg2)) defer agent.ServiceDeregister("foo2") + // Test searching with one tag (two results) retry.Run(t, func(r *retry.R) { services, meta, err := health.ServiceMultipleTags("foo", []string{"bar"}, true, nil) + require.NoError(t, err) require.NotEqual(t, meta.LastIndex, 0) require.Len(t, services, 2) }) + // Test searching with two tags (one result) retry.Run(t, func(r *retry.R) { services, meta, err := health.ServiceMultipleTags("foo", []string{"bar", "v2"}, true, nil) + require.NoError(t, err) require.NotEqual(t, meta.LastIndex, 0) require.Len(t, services, 1)