From ce96b2c69dbc496e88e01d4916d85b538008f1e4 Mon Sep 17 00:00:00 2001 From: Paul Banks Date: Tue, 25 Apr 2023 15:25:26 +0100 Subject: [PATCH] De-flake snapshot test (#17120) --- .../test/snapshot/snapshot_restore_test.go | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/test/integration/consul-container/test/snapshot/snapshot_restore_test.go b/test/integration/consul-container/test/snapshot/snapshot_restore_test.go index d472f6efc..fa26ff924 100644 --- a/test/integration/consul-container/test/snapshot/snapshot_restore_test.go +++ b/test/integration/consul-container/test/snapshot/snapshot_restore_test.go @@ -5,14 +5,17 @@ package snapshot import ( "fmt" + "io" + "testing" + "time" + + "github.com/stretchr/testify/require" + "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/sdk/testutil/retry" libcluster "github.com/hashicorp/consul/test/integration/consul-container/libs/cluster" libtopology "github.com/hashicorp/consul/test/integration/consul-container/libs/topology" "github.com/hashicorp/consul/test/integration/consul-container/libs/utils" - "github.com/stretchr/testify/require" - "io" - "testing" ) func TestSnapshotRestore(t *testing.T) { @@ -105,11 +108,27 @@ func testSnapShotRestoreForLogStore(t *testing.T, logStore libcluster.LogStore) require.Equal(r, LeaderLogIndex, followerLogIndex) }) - for i := 0; i < 100; i++ { + // Follower might not have finished loading snapshot yet which means attempts + // could return nil or "key not found" for a while. + failer := func() *retry.Timer { + return &retry.Timer{Timeout: 10 * time.Second, Wait: 100 * time.Millisecond} + } + + retry.RunWith(failer(), t, func(r *retry.R) { + kv, _, err := fc.KV().Get(fmt.Sprintf("key-%d", 1), &api.QueryOptions{AllowStale: true}) + require.NoError(t, err) + require.NotNil(t, kv) + require.Equal(t, kv.Key, fmt.Sprintf("key-%d", 1)) + require.Equal(t, kv.Value, []byte(fmt.Sprintf("value-%d", 1))) + }) + + // Now we have at least one non-nil key, the snapshot must be loaded so check + // we can read all the rest of them too. + for i := 2; i < 100; i++ { kv, _, err := fc.KV().Get(fmt.Sprintf("key-%d", i), &api.QueryOptions{AllowStale: true}) require.NoError(t, err) + require.NotNil(t, kv) require.Equal(t, kv.Key, fmt.Sprintf("key-%d", i)) require.Equal(t, kv.Value, []byte(fmt.Sprintf("value-%d", i))) } - }