Skip to content

Commit

Permalink
Update new tests to use the require library
Browse files Browse the repository at this point in the history
  • Loading branch information
adilyse committed Oct 9, 2018
1 parent d49718d commit 84012ce
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 205 deletions.
9 changes: 2 additions & 7 deletions agent/consul/catalog_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1695,13 +1695,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 {
Expand Down
33 changes: 9 additions & 24 deletions agent/consul/health_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand All @@ -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{
Expand All @@ -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) {
Expand Down
97 changes: 25 additions & 72 deletions agent/consul/state/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
63 changes: 20 additions & 43 deletions api/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/consul/testutil"
"github.com/hashicorp/consul/testutil/retry"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)

func TestAPI_CatalogDatacenters(t *testing.T) {
Expand Down Expand Up @@ -230,25 +231,15 @@ func TestAPI_CatalogService_SingleTag(t *testing.T) {
ID: "foo1",
Tags: []string{"bar"},
}
if err := agent.ServiceRegister(reg); err != nil {
t.Fatalf("err: %v", err)
}
require.NoError(t, agent.ServiceRegister(reg))
defer agent.ServiceDeregister("foo1")

retry.Run(t, func(r *retry.R) {
services, meta, err := catalog.Service("foo", "bar", nil)
if err != nil {
r.Fatal(err)
}
if meta.LastIndex == 0 {
r.Fatalf("Bad: %v", meta)
}
if len(services) != 1 {
r.Fatalf("Bad: %v", services)
}
if services[0].ServiceID != "foo1" {
r.Fatalf("Bad: %v", services[0].ServiceID)
}
require.NoError(t, err)
require.NotEqual(t, meta.LastIndex, 0)
require.Len(t, services, 1)
require.Equal(t, services[0].ServiceID, "foo1")
})
}

Expand All @@ -268,52 +259,38 @@ func TestAPI_CatalogService_MultipleTags(t *testing.T) {
ID: "foo1",
Tags: []string{"bar"},
}
if err := agent.ServiceRegister(reg); err != nil {
t.Fatalf("err: %v", err)
}
require.NoError(t, agent.ServiceRegister(reg))
defer agent.ServiceDeregister("foo1")

reg2 := &AgentServiceRegistration{
Name: "foo",
ID: "foo2",
Tags: []string{"bar", "v2"},
}
if err := agent.ServiceRegister(reg2); err != nil {
t.Fatalf("err: %v", err)
}
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 := catalog.ServiceMultipleTags("foo", []string{"bar"}, nil)
if err != nil {
r.Fatal(err)
}
if meta.LastIndex == 0 {
r.Fatalf("Bad: %v", meta)
}

require.NoError(t, err)
require.NotEqual(t, meta.LastIndex, 0)

// Should be 2 services with the `bar` tag
if len(services) != 2 {
r.Fatalf("Bad: %v", services)
}
require.Len(t, services, 2)
})

// Test searching with two tags (one result)
retry.Run(t, func(r *retry.R) {
services, meta, err := catalog.ServiceMultipleTags("foo", []string{"bar", "v2"}, nil)
if err != nil {
r.Fatal(err)
}
if meta.LastIndex == 0 {
r.Fatalf("Bad: %v", meta)
}
// Should be exactly 1 service
if len(services) != 1 {
r.Fatalf("Bad: %v", services)
}
if services[0].ServiceID != "foo2" {
r.Fatalf("Bad: %v", services[0].ServiceID)
}

require.NoError(t, err)
require.NotEqual(t, meta.LastIndex, 0)

// Should be exactly 1 service, named "foo2"
require.Len(t, services, 1)
require.Equal(t, services[0].ServiceID, "foo2")
})
}

Expand Down
Loading

0 comments on commit 84012ce

Please sign in to comment.