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 10, 2018
1 parent 1977293 commit 98cdcaa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 103 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 @@ -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 {
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
7 changes: 7 additions & 0 deletions api/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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)
Expand Down

0 comments on commit 98cdcaa

Please sign in to comment.