Skip to content

Commit

Permalink
add blocking query tests for sameness groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmurret committed Jun 3, 2024
1 parent 6469bf5 commit b800110
Showing 1 changed file with 64 additions and 33 deletions.
97 changes: 64 additions & 33 deletions dependency/health_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"
"reflect"
"testing"
"time"
"unsafe"

"github.com/hashicorp/consul-template/test"
Expand Down Expand Up @@ -690,6 +691,9 @@ func TestHealthServiceQuery_Fetch(t *testing.T) {
}
}

// TestHealthServiceQuery_Fetch_SamenessGroup tests fetching services with sameness group
// and asserts that failover works when a service in the sameness group fails and consul-template
// reacts to the change.
func TestHealthServiceQuery_Fetch_SamenessGroup(t *testing.T) {
// Arrange - set up test data
catalog := testClients.Consul().Catalog()
Expand Down Expand Up @@ -738,8 +742,8 @@ func TestHealthServiceQuery_Fetch_SamenessGroup(t *testing.T) {
}
}

// set up partition one to be unhealthy so that it will fall back to partition two
registerSvc(svcName, nodeOne, partitionOne, "critical")
// set up service in each partition
registerSvc(svcName, nodeOne, partitionOne, "passing")
registerSvc(svcName, nodeTwo, partitionTwo, "passing")

// Act - fetch the service
Expand All @@ -749,45 +753,72 @@ func TestHealthServiceQuery_Fetch_SamenessGroup(t *testing.T) {
t.Fatal(err)
}

act, _, err := d.Fetch(testClients, nil)
svcs, meta, err := d.Fetch(testClients, nil)
if err != nil {
t.Fatal(err)
}

if act != nil {
for _, v := range act.([]*HealthService) {
v.NodeID = ""
v.Checks = nil
// delete any version data from ServiceMeta
v.ServiceMeta = filterVersionMeta(v.ServiceMeta)
v.NodeTaggedAddresses = filterAddresses(
v.NodeTaggedAddresses)
v.NodeMeta = filterVersionMeta(v.NodeMeta)
healthServices := svcs.([]*HealthService)
require.True(t, len(healthServices) == 1)
require.Equal(t, "node"+partitionOne, healthServices[0].Node)

// set up blocking query with last index
dataCh := make(chan interface{}, 1)
errCh := make(chan error, 1)
go func() {
data, _, err := d.Fetch(testClients, &QueryOptions{WaitIndex: meta.LastIndex})
if err != nil {
errCh <- err
return
}
}
dataCh <- data
}()

// Assert - verify the results
expectedResult := []*HealthService{
{
Node: nodeTwo,
NodeAddress: testConsul.Config.Bind,
ServiceMeta: map[string]string{},
Address: testConsul.Config.Bind,
NodeTaggedAddresses: map[string]string{},
NodeMeta: map[string]string{},
Port: 12345,
ID: svcName,
Name: svcName,
Tags: []string{},
Status: HealthPassing,
Weights: api.AgentWeights{
Passing: 1,
Warning: 1,
},
},
// update partition one to initiate failover
registerSvc(svcName, nodeOne, partitionOne, "critical")

select {
case err := <-errCh:
if err != ErrStopped {
t.Fatal(err)
}
case <-time.After(1 * time.Minute):
t.Errorf("did not stop")
case val := <-dataCh:
if val != nil {
for _, v := range val.([]*HealthService) {
v.NodeID = ""
v.Checks = nil
// delete any version data from ServiceMeta
v.ServiceMeta = filterVersionMeta(v.ServiceMeta)
v.NodeTaggedAddresses = filterAddresses(
v.NodeTaggedAddresses)
v.NodeMeta = filterVersionMeta(v.NodeMeta)
}
}

// Assert - verify the results
expectedResult := []*HealthService{
{
Node: nodeTwo,
NodeAddress: testConsul.Config.Bind,
ServiceMeta: map[string]string{},
Address: testConsul.Config.Bind,
NodeTaggedAddresses: map[string]string{},
NodeMeta: map[string]string{},
Port: 12345,
ID: svcName,
Name: svcName,
Tags: []string{},
Status: HealthPassing,
Weights: api.AgentWeights{
Passing: 1,
Warning: 1,
},
},
}
assert.Equal(t, expectedResult, val)
}
assert.Equal(t, expectedResult, act)
}

func TestHealthServiceQuery_String(t *testing.T) {
Expand Down

0 comments on commit b800110

Please sign in to comment.