Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NET-9084] - change peering fetch test to assert blocking query behavior. #1924

Merged
merged 3 commits into from
May 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 66 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,75 @@ 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)
time.Sleep(200 * time.Millisecond)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need this if you're already waiting for 1 minute in the test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty. removed it. it was some copy pasta.


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
Loading