Skip to content

Commit

Permalink
apiexportendpointslice: use framework.Eventually
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com>
  • Loading branch information
ncdc committed Feb 1, 2023
1 parent bc8fb6a commit 0b403cb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -99,26 +101,38 @@ 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")
slice.Spec.Partition = partition.Name
_, 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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")

Expand All @@ -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.
Expand All @@ -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)

Expand All @@ -260,31 +290,40 @@ 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")
shard.Labels["region"] = "apiexportendpointslice-test-region"
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")
Expand All @@ -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))
}

0 comments on commit 0b403cb

Please sign in to comment.