Skip to content

Commit

Permalink
Merge pull request #1924 from hashicorp/jm/NET-9084
Browse files Browse the repository at this point in the history
[NET-9084] - change peering fetch test to assert blocking query behavior.
  • Loading branch information
jmurret authored May 7, 2024
2 parents 14587a4 + 45492cc commit 68d5b4f
Showing 1 changed file with 65 additions and 33 deletions.
98 changes: 65 additions & 33 deletions dependency/consul_peering_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package dependency

import (
"context"
"fmt"
"github.com/hashicorp/consul-template/test"
"github.com/hashicorp/consul/api"
"github.com/stretchr/testify/require"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -59,47 +64,74 @@ func TestListPeeringsQuery(t *testing.T) {
}

func TestListPeeringsQuery_Fetch(t *testing.T) {
cases := []struct {
name string
i string
exp []string
}{
{
"all",
"",
// the peering generated has random IDs,
// we can't assert on the full response,
// we can assert on the peering names though.
[]string{
"bar",
"foo",
},
},
// the peering generated has random IDs,
// we can't assert on the full response,
// we can assert on the peering names though.
expectedPeerNames := []string{
"bar",
"foo",
}

for i, tc := range cases {
t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) {
p, err := NewListPeeringQuery(tc.i)
if err != nil {
t.Fatal(err)
}
p, err := NewListPeeringQuery("")
if err != nil {
t.Fatal(err)
}

res, meta, err := p.Fetch(testClients, nil)
require.NoError(t, err)
require.NotNil(t, res)
peerNames := make([]string, 0)
for _, peering := range res.([]*Peering) {
peerNames = append(peerNames, peering.Name)
}
assert.Equal(t, expectedPeerNames, peerNames)

res, _, err := p.Fetch(testClients, nil)
client := testClients.Consul()
th, err := test.NewTenancyHelper(client)
require.NoError(t, err)
if th.IsConsulEnterprise() {
// set up blocking query with last index
dataCh := make(chan interface{}, 1)
errCh := make(chan error, 1)
go func() {
data, _, err := p.Fetch(testClients, &QueryOptions{WaitIndex: meta.LastIndex})
if err != nil {
t.Fatal(err)
errCh <- err
return
}
dataCh <- data
}()

if res == nil {
t.Fatalf("expected non-nil result")
}
tenancy := th.Tenancy("default.baz")
ap := &api.Partition{Name: tenancy.Partition}
partition, _, err := client.Partitions().Create(context.Background(), ap, nil)
defer func() {
_, _ = client.Partitions().Delete(context.Background(), partition.Name, nil)
}()
require.NoError(t, err)
generateReq := api.PeeringGenerateTokenRequest{PeerName: "baz", Partition: tenancy.Partition}
_, _, err = client.Peerings().GenerateToken(context.Background(), generateReq, &api.WriteOptions{})
require.NoError(t, err)
defer func() {
_, _ = client.Peerings().Delete(context.Background(), generateReq.PeerName, nil)
}()

peerNames := make([]string, 0)
for _, peering := range res.([]*Peering) {
peerNames = append(peerNames, peering.Name)
}
// create another peer
err = testClients.createConsulPeerings(tenancy)
require.NoError(t, err)

assert.Equal(t, tc.exp, peerNames)
})
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 {
require.Equal(t, 3, len(val.([]*Peering)))
}
}
}
}

Expand Down

0 comments on commit 68d5b4f

Please sign in to comment.