From 0b403cb84de5aba23b277ccce48f9c5efcb5cea2 Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Wed, 1 Feb 2023 14:26:16 -0500 Subject: [PATCH] apiexportendpointslice: use framework.Eventually Signed-off-by: Andy Goldstein --- go.mod | 2 +- .../apiexportendpointslice_test.go | 90 ++++++++++++++----- 2 files changed, 67 insertions(+), 25 deletions(-) diff --git a/go.mod b/go.mod index 0499ada2611..1f0f234d4ac 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/aojea/rwconn v0.1.1 github.com/coredns/caddy v1.1.1 github.com/coredns/coredns v1.9.3 + github.com/davecgh/go-spew v1.1.1 github.com/egymgmbh/go-prefix-writer v0.0.0-20180609083313-7326ea162eca github.com/emicklei/go-restful v2.9.5+incompatible github.com/evanphx/json-patch v5.6.0+incompatible @@ -73,7 +74,6 @@ require ( github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/cyphar/filepath-securejoin v0.2.3 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect github.com/dnstap/golang-dnstap v0.4.0 // indirect github.com/docker/distribution v2.8.1+incompatible // indirect github.com/dustin/go-humanize v1.0.0 // indirect diff --git a/test/e2e/reconciler/apiexportendpointslice/apiexportendpointslice_test.go b/test/e2e/reconciler/apiexportendpointslice/apiexportendpointslice_test.go index 078f95bd836..95a6d611c7f 100644 --- a/test/e2e/reconciler/apiexportendpointslice/apiexportendpointslice_test.go +++ b/test/e2e/reconciler/apiexportendpointslice/apiexportendpointslice_test.go @@ -18,9 +18,11 @@ package apiexportendpointslice import ( "context" + "fmt" "testing" "time" + "github.com/davecgh/go-spew/spew" "github.com/stretchr/testify/require" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -99,15 +101,23 @@ func TestAPIExportEndpointSliceWithPartition(t *testing.T) { require.NoError(t, err, "error creating APIExport") t.Logf("Retrying to create the APIExportEndpointSlice after the APIExport has been created") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { slice, err = sliceClient.Cluster(partitionClusterPath).Create(ctx, slice, metav1.CreateOptions{}) - return err == nil + if err != nil { + return false, err.Error() + } + return true, "" }, wait.ForeverTestTimeout, 100*time.Millisecond, "expected APIExportEndpointSlice creation to succeed") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { slice, err = kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return conditions.IsTrue(slice, apisv1alpha1.APIExportValid) && conditions.IsTrue(slice, apisv1alpha1.APIExportEndpointSliceURLsReady) + + if conditions.IsTrue(slice, apisv1alpha1.APIExportValid) && conditions.IsTrue(slice, apisv1alpha1.APIExportEndpointSliceURLsReady) { + return true, "" + } + + return false, spew.Sdump(slice.Status.Conditions) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expected valid APIExport") t.Logf("Adding a Partition to the APIExportEndpointSlice") @@ -115,10 +125,14 @@ func TestAPIExportEndpointSliceWithPartition(t *testing.T) { _, err = kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Update(ctx, slice, metav1.UpdateOptions{}) require.NoError(t, err, "error updating APIExportEndpointSlice") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { slice, err = kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return conditions.IsFalse(slice, apisv1alpha1.PartitionValid) && conditions.GetReason(slice, apisv1alpha1.PartitionValid) == apisv1alpha1.PartitionInvalidReferenceReason + if conditions.IsFalse(slice, apisv1alpha1.PartitionValid) && conditions.GetReason(slice, apisv1alpha1.PartitionValid) == apisv1alpha1.PartitionInvalidReferenceReason { + return true, "" + } + + return false, spew.Sdump(slice.Status.Conditions) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expected missing Partition") require.True(t, len(slice.Status.APIExportEndpoints) == 0, "not expecting any endpoint") require.True(t, conditions.IsFalse(slice, apisv1alpha1.APIExportEndpointSliceURLsReady), "expecting URLs not ready condition") @@ -128,10 +142,14 @@ func TestAPIExportEndpointSliceWithPartition(t *testing.T) { _, err = partitionClient.Cluster(partitionClusterPath).Create(ctx, partition, metav1.CreateOptions{}) require.NoError(t, err, "error creating Partition") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { slice, err = kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return conditions.IsTrue(slice, apisv1alpha1.PartitionValid) + if conditions.IsTrue(slice, apisv1alpha1.PartitionValid) { + return true, "" + } + + return false, spew.Sdump(slice.Status.Conditions) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expected valid Partition") t.Logf("Checking that no endpoint has been populated") @@ -197,10 +215,13 @@ func TestAPIExportEndpointSliceWithPartitionPrivate(t *testing.T) { slice, err = sliceClient.Cluster(partitionClusterPath).Create(ctx, slice, metav1.CreateOptions{}) require.NoError(t, err) - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { slice, err = kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return conditions.IsTrue(slice, apisv1alpha1.APIExportValid) + if conditions.IsTrue(slice, apisv1alpha1.APIExportValid) { + return true, "" + } + return false, spew.Sdump(slice.Status.Conditions) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expected valid APIExport") require.True(t, conditions.IsTrue(slice, apisv1alpha1.APIExportEndpointSliceURLsReady), "expecting URLs ready condition") @@ -214,18 +235,24 @@ func TestAPIExportEndpointSliceWithPartitionPrivate(t *testing.T) { _, err = kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Update(ctx, slice, metav1.UpdateOptions{}) require.NoError(t, err, "error updating APIExportEndpointSlice") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { s, err := kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return conditions.IsTrue(s, apisv1alpha1.PartitionValid) + if conditions.IsTrue(s, apisv1alpha1.PartitionValid) { + return true, "" + } + return false, spew.Sdump(s.Status.Conditions) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expected valid Partition") require.True(t, conditions.IsTrue(slice, apisv1alpha1.APIExportEndpointSliceURLsReady), "expecting URLs ready condition") t.Logf("Checking that no endpoint has been populated") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { s, err := kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return len(s.Status.APIExportEndpoints) == 0 + if len(s.Status.APIExportEndpoints) == 0 { + return true, "" + } + return false, fmt.Sprintf("expected 0 endpoints, but got: %#v", s.Status.APIExportEndpoints) }, wait.ForeverTestTimeout, 100*time.Millisecond, "not expecting any endpoint") // Endpoint tests require the edition of shards. @@ -248,10 +275,13 @@ func TestAPIExportEndpointSliceWithPartitionPrivate(t *testing.T) { shard, err = shardClient.Cluster(core.RootCluster.Path()).Create(ctx, shard, metav1.CreateOptions{}) require.NoError(t, err, "error creating Shard") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { slice, err = kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return len(slice.Status.APIExportEndpoints) == 1 + if len(slice.Status.APIExportEndpoints) == 1 { + return true, "" + } + return false, fmt.Sprintf("expected 1 endpoint, but got: %#v", slice.Status.APIExportEndpoints) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expecting a single endpoint") require.Contains(t, slice.Status.APIExportEndpoints[0].URL, export.Name) @@ -260,10 +290,13 @@ func TestAPIExportEndpointSliceWithPartitionPrivate(t *testing.T) { shard, err = shardClient.Cluster(core.RootCluster.Path()).Update(ctx, shard, metav1.UpdateOptions{}) require.NoError(t, err, "error updating Shard") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { s, err := kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return len(s.Status.APIExportEndpoints) == 0 + if len(s.Status.APIExportEndpoints) == 0 { + return true, "" + } + return false, fmt.Sprintf("expected 0 endpoints, but got: %#v", s.Status.APIExportEndpoints) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expecting no endpoint") t.Logf("Setting back the correct label") @@ -271,20 +304,26 @@ func TestAPIExportEndpointSliceWithPartitionPrivate(t *testing.T) { shard, err = shardClient.Cluster(core.RootCluster.Path()).Update(ctx, shard, metav1.UpdateOptions{}) require.NoError(t, err, "error updating Shard") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { s, err := kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return len(s.Status.APIExportEndpoints) == 1 + if len(s.Status.APIExportEndpoints) == 1 { + return true, "" + } + return false, fmt.Sprintf("expected 1 endpoint, but got: %#v", s.Status.APIExportEndpoints) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expecting a single endpoint") t.Logf("Deleting the shard") err = shardClient.Cluster(core.RootCluster.Path()).Delete(ctx, shard.Name, metav1.DeleteOptions{}) require.NoError(t, err, "error deleting Shard") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { s, err := kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, slice.Name, metav1.GetOptions{}) require.NoError(t, err) - return len(s.Status.APIExportEndpoints) == 0 + if len(s.Status.APIExportEndpoints) == 0 { + return true, "" + } + return false, fmt.Sprintf("expected 0 endpoints, but got: %#v", s.Status.APIExportEndpoints) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expecting no endpoint") t.Logf("Creating a slice without partition") @@ -302,9 +341,12 @@ func TestAPIExportEndpointSliceWithPartitionPrivate(t *testing.T) { sliceWithAll, err = sliceClient.Cluster(partitionClusterPath).Create(ctx, sliceWithAll, metav1.CreateOptions{}) require.NoError(t, err, "error creating APIExportEndpointSlice") - require.Eventually(t, func() bool { + framework.Eventually(t, func() (bool, string) { sliceWithAll, err := kcpClusterClient.Cluster(partitionClusterPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, sliceWithAll.Name, metav1.GetOptions{}) require.NoError(t, err) - return len(sliceWithAll.Status.APIExportEndpoints) == 1 + if len(sliceWithAll.Status.APIExportEndpoints) == 1 { + return true, "" + } + return false, fmt.Sprintf("expected 1 endpoint, but got: %#v", sliceWithAll.Status.APIExportEndpoints) }, wait.ForeverTestTimeout, 100*time.Millisecond, "expecting a single endpoint for the root shard, got %d", len(sliceWithAll.Status.APIExportEndpoints)) }