Skip to content

Commit

Permalink
test: remove deprecated helpers in e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tarun Gupta Akirala <[email protected]>
  • Loading branch information
takirala committed Aug 6, 2023
1 parent 81c48ca commit 66af5ab
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 96 deletions.
85 changes: 41 additions & 44 deletions test/common/crudtester.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions test/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func Equivalent(actual, desired runtimeclient.Object) bool {
}

// WaitForNamespace waits for namespace to be created in a cluster.
func WaitForNamespaceOrDie(tl TestLogger, client kubeclientset.Interface, clusterName, namespace string, interval, timeout time.Duration) {
err := wait.PollImmediate(interval, timeout, func() (exist bool, err error) {
_, err = client.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{})
func WaitForNamespace(tl TestLogger, client kubeclientset.Interface, clusterName, namespace string, interval, timeout time.Duration) {
err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(ctx context.Context) (exist bool, err error) {
_, err = client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if errors.IsNotFound(err) {
return false, nil
}
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ func validateCrdCrud(f framework.KubeFedFramework, targetCrdKind string, namespa
}

targetName := targetAPIResource.Name
err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(_ context.Context) (bool, error) {
_, err := kfenable.LookupAPIResource(hostConfig, targetName, targetAPIResource.Version)
if err != nil {
tl.Logf("An error was reported while waiting for target type %q to be published as an available resource: %v", targetName, err)
}
return (err == nil), nil
return err == nil, nil
})
if err != nil {
tl.Fatalf("Timed out waiting for target type %q to be published as an available resource", targetName)
Expand Down Expand Up @@ -239,12 +239,12 @@ func waitForCrd(config *rest.Config, tl common.TestLogger, apiResource metav1.AP
if err != nil {
tl.Fatalf("Error creating client for crd %q: %v", apiResource.Kind, err)
}
err = wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
_, err := client.Resources("invalid").Get(context.Background(), "invalid", metav1.GetOptions{})
err = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) {
_, err := client.Resources("invalid").Get(ctx, "invalid", metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return true, nil
}
return (err == nil), err
return err == nil, err
})
if err != nil {
tl.Fatalf("Error waiting for crd %q to become established: %v", apiResource.Kind, err)
Expand Down
16 changes: 8 additions & 8 deletions test/e2e/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ var _ = Describe("Federated", func() {

By(fmt.Sprintf("Waiting until the status of the %s %q indicates NamespaceNotFederated", kind, qualifiedName))
client := genericclient.NewForConfigOrDie(f.KubeConfig())
err := wait.PollImmediate(framework.PollInterval, wait.ForeverTestTimeout, func() (bool, error) {
genericResource, err := common.GetGenericResource(client, fedObject.GroupVersionKind(), qualifiedName)
err := wait.PollUntilContextTimeout(context.TODO(), framework.PollInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
genericResource, err := common.GetGenericResource(ctx, client, fedObject.GroupVersionKind(), qualifiedName)
if err != nil {
tl.Fatalf("An error occurred retrieving the status of the %s %q: %v", kind, qualifiedName, err)
}
Expand Down Expand Up @@ -157,7 +157,7 @@ var _ = Describe("Federated", func() {

By("Waiting for the test namespace to be created in the selected cluster")
kubeClient := kubeclientset.NewForConfigOrDie(clusterConfig)
common.WaitForNamespaceOrDie(tl, kubeClient, clusterName, targetObject.GetNamespace(),
common.WaitForNamespace(tl, kubeClient, clusterName, targetObject.GetNamespace(),
framework.PollInterval, framework.TestContext.SingleCallTimeout)

By("Creating a labeled resource in the selected cluster")
Expand All @@ -175,10 +175,10 @@ var _ = Describe("Federated", func() {
}()

By("Checking that the labeled resource is unlabeled by the sync controller")
err = wait.PollImmediate(framework.PollInterval, wait.ForeverTestTimeout, func() (bool, error) {
err = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, wait.ForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(labeledObj.GroupVersionKind())
err := clusterClient.Get(context.TODO(), obj, labeledObj.GetNamespace(), labeledObj.GetName())
err := clusterClient.Get(ctx, obj, labeledObj.GetNamespace(), labeledObj.GetName())
if err != nil {
tl.Errorf("Error retrieving labeled resource: %v", err)
return false, nil
Expand Down Expand Up @@ -206,7 +206,7 @@ var _ = Describe("Federated", func() {

By("Waiting for the test namespace to be created in the selected cluster")
kubeClient := kubeclientset.NewForConfigOrDie(clusterConfig)
common.WaitForNamespaceOrDie(tl, kubeClient, clusterName, targetObject.GetNamespace(),
common.WaitForNamespace(tl, kubeClient, clusterName, targetObject.GetNamespace(),
framework.PollInterval, framework.TestContext.SingleCallTimeout)

By("Creating an unlabeled resource in the selected cluster")
Expand Down Expand Up @@ -248,10 +248,10 @@ var _ = Describe("Federated", func() {

waitDuration := 10 * time.Second // Arbitrary amount of time to wait for deletion
By(fmt.Sprintf("Checking that the unlabeled resource is not deleted within %v", waitDuration))
_ = wait.PollImmediate(framework.PollInterval, waitDuration, func() (bool, error) {
_ = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, waitDuration, true, func(ctx context.Context) (bool, error) {
obj := &unstructured.Unstructured{}
obj.SetGroupVersionKind(unlabeledObj.GroupVersionKind())
err := clusterClient.Get(context.TODO(), obj, unlabeledObj.GetNamespace(), unlabeledObj.GetName())
err := clusterClient.Get(ctx, obj, unlabeledObj.GetNamespace(), unlabeledObj.GetName())
if apierrors.IsNotFound(err) {
tl.Fatalf("Unlabeled resource %s %q was deleted", typeConfig.GetTargetType().Kind, util.NewQualifiedName(unlabeledObj))
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/federate.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ func deleteResource(tl common.TestLogger, client util.ResourceClient, qualifiedN
tl.Fatalf("Error deleting %s %q: %v", kind, qualifiedName, err)
}

err = wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
_, err := client.Resources(qualifiedName.Namespace).Get(context.Background(), qualifiedName.Name, metav1.GetOptions{})
err = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) {
_, err := client.Resources(qualifiedName.Namespace).Get(ctx, qualifiedName.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return true, nil
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/framework/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func clusterIsReadyOrFail(tl common.TestLogger, client genericclient.Client,
if util.IsClusterReady(&cluster.Status) {
return
}
err := wait.Poll(interval, timeout, func() (bool, error) {
err := wait.PollUntilContextTimeout(context.TODO(), interval, timeout, true, func(ctx context.Context) (bool, error) {
cluster := &fedv1b1.KubeFedCluster{}
err := client.Get(context.TODO(), cluster, namespace, clusterName)
err := client.Get(ctx, cluster, namespace, clusterName)
if err != nil {
return false, err
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ func CreateNamespace(client kubeclientset.Interface, generateName string) (strin
// Be robust about making the namespace creation call.
// TODO(marun) should all api calls be made 'robustly'?
var namespaceName string
if err := wait.PollImmediate(PollInterval, TestContext.SingleCallTimeout, func() (bool, error) {
namespace, err := client.CoreV1().Namespaces().Create(context.Background(), namespaceObj, metav1.CreateOptions{})
if err := wait.PollUntilContextTimeout(context.Background(), PollInterval, TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) {
namespace, err := client.CoreV1().Namespaces().Create(ctx, namespaceObj, metav1.CreateOptions{})
if err != nil {
Logf("Unexpected error while creating namespace: %v", err)
return false, nil
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/framework/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (t *TestContextType) NamespaceScopedControlPlane() bool {
return t.InMemoryControllers && t.LimitedScopeInMemoryControllers || t.LimitedScope
}

var TestContext *TestContextType = &TestContextType{}
var TestContext = &TestContextType{}

func registerFlags(t *TestContextType) {
flag.BoolVar(&t.InMemoryControllers, "in-memory-controllers", false,
Expand Down
12 changes: 7 additions & 5 deletions test/e2e/framework/unmanaged.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"sort"
"time"

"github.com/pkg/errors"

Expand Down Expand Up @@ -188,8 +189,9 @@ func (f *UnmanagedFramework) ControllerConfig() *util.ControllerConfig {
KubeFedNamespace: TestContext.KubeFedSystemNamespace,
TargetNamespace: f.inMemoryTargetNamespace(),
},
KubeConfig: f.Config,
MinimizeLatency: true,
KubeConfig: f.Config,
MinimizeLatency: true,
CacheSyncTimeout: 15 * time.Minute,
}
controllerCfg.RawResourceStatusCollection = true
return controllerCfg
Expand Down Expand Up @@ -276,7 +278,7 @@ func (f *UnmanagedFramework) ClusterConfigs(userAgent string) map[string]common.
restclient.AddUserAgent(config, userAgent)
clusterConfigs[cluster.Name] = common.TestClusterConfig{
Config: config,
IsPrimary: (cluster.Name == hostClusterName),
IsPrimary: cluster.Name == hostClusterName,
}
}

Expand Down Expand Up @@ -366,8 +368,8 @@ func DeleteNamespace(client kubeclientset.Interface, namespaceName string) {
}

func waitForNamespaceDeletion(client kubeclientset.Interface, namespace string) error {
err := wait.PollImmediate(PollInterval, TestContext.SingleCallTimeout, func() (bool, error) {
if _, err := client.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{}); err != nil {
err := wait.PollUntilContextTimeout(context.Background(), PollInterval, TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) {
if _, err := client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}); err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/framework/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package framework

import (
"bufio"
"context"
"io"
"strings"
"time"
Expand All @@ -34,7 +35,7 @@ func WaitForObject(tl common.TestLogger, namespace, name string, objectGetter fu
var actual runtimeclient.Object
interval := PollInterval
timeout := TestContext.SingleCallTimeout
err := wait.PollImmediate(interval, timeout, func() (exist bool, err error) {
err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(ctx context.Context) (exist bool, err error) {
actual, err = objectGetter(namespace, name)
if err != nil {
if apierrors.IsNotFound(err) {
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/ftccontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ func enableResource(f framework.KubeFedFramework, targetAPIResource *metav1.APIR
}

func waitForTargetCrd(tl common.TestLogger, config *rest.Config, targetName, version string) {
err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) {
_, err := kfenable.LookupAPIResource(config, targetName, version)
if err != nil {
tl.Logf("An error was reported while waiting for target type %q to be published as an available resource: %v", targetName, err)
}
return (err == nil), nil
return err == nil, nil
})
if err != nil {
tl.Fatalf("Timed out waiting for target type %q to be published as an available resource", targetName)
Expand All @@ -192,9 +192,9 @@ func waitForTargetCrd(tl common.TestLogger, config *rest.Config, targetName, ver

// waitForGenerationSynced indicates that sync controller is updated
func waitForGenerationSynced(tl common.TestLogger, client genericclient.Client, namespace, name string) {
err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) {
ftc := fedv1b1.FederatedTypeConfig{}
err := client.Get(context.TODO(), &ftc, namespace, name)
err := client.Get(ctx, &ftc, namespace, name)
if err != nil {
tl.Fatalf("Error retrieving status of FederatedTypeConfig %q: %v", util.QualifiedName{Namespace: namespace, Name: name}, err)
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/not_ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ var _ = Describe("[NOT_READY] Simulated not-ready nodes", func() {
tl.Fatalf("Failed to patch kubefed cluster: %v", err)
}

err = wait.Poll(time.Second*5, time.Second*30, func() (bool, error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second*5, time.Second*30, true, func(ctx context.Context) (bool, error) {
cluster := &fedv1b1.KubeFedCluster{}
err := hostClient.Get(context.TODO(), cluster, hostNamespace, unhealthyCluster)
err := hostClient.Get(ctx, cluster, hostNamespace, unhealthyCluster)
if err != nil {
tl.Fatalf("Failed to retrieve unhealthy cluster: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/placement.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ var _ = Describe("Placement", func() {
if err != nil {
tl.Fatalf("Error creating resource client for %q: %v", targetKind, err)
}
err = wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
_, err := client.Resources(namespace).Get(context.Background(), qualifiedName.Name, metav1.GetOptions{})
err = wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) {
_, err := client.Resources(namespace).Get(ctx, qualifiedName.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return true, nil
}
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ var _ = Describe("Simulated Scale", func() {
// serviceaccount token recreate
saName := kfutil.ClusterServiceAccountName(memberCluster, hostCluster)
var deleteSecret sync.Once
err = wait.PollImmediate(1*time.Second, 10*time.Second, func() (bool, error) {
err = wait.PollUntilContextTimeout(context.Background(), 1*time.Second, 10*time.Second, true, func(ctx context.Context) (bool, error) {
sa, err := client.CoreV1().ServiceAccounts(joiningNamespace).Get(
context.Background(), saName, metav1.GetOptions{},
ctx, saName, metav1.GetOptions{},
)
if err != nil {
return false, nil
Expand All @@ -153,13 +153,13 @@ var _ = Describe("Simulated Scale", func() {
for _, objReference := range sa.Secrets {
saSecretName := objReference.Name
secret, err := client.CoreV1().Secrets(joiningNamespace).Get(
context.Background(), saSecretName, metav1.GetOptions{},
ctx, saSecretName, metav1.GetOptions{},
)
if err != nil {
tl.Fatalf("Error get sa secret %s: %v", saSecretName, err)
}
if secret.Type == corev1.SecretTypeServiceAccountToken {
if err := client.CoreV1().Secrets(joiningNamespace).Delete(context.TODO(), saSecretName, metav1.DeleteOptions{}); err != nil {
if err := client.CoreV1().Secrets(joiningNamespace).Delete(ctx, saSecretName, metav1.DeleteOptions{}); err != nil {
tl.Fatalf("Error delete secret %s: %v", secretName, err)
}
}
Expand All @@ -168,7 +168,7 @@ var _ = Describe("Simulated Scale", func() {
for _, objReference := range sa.Secrets {
saSecretName := objReference.Name
secret, err := client.CoreV1().Secrets(joiningNamespace).Get(
context.Background(), saSecretName, metav1.GetOptions{},
ctx, saSecretName, metav1.GetOptions{},
)
if err != nil {
return false, nil
Expand Down
6 changes: 4 additions & 2 deletions test/e2e/schedulermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package e2e

import (
"context"

"k8s.io/apimachinery/pkg/util/wait"
restclient "k8s.io/client-go/rest"

Expand Down Expand Up @@ -111,7 +113,7 @@ func GetSchedulingTypes(tl common.TestLogger) map[string]schedulingtypes.Schedul
}

func waitForSchedulerDeleted(tl common.TestLogger, controller *schedulingmanager.SchedulingManager) {
err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(_ context.Context) (bool, error) {
scheduler := controller.GetScheduler(schedulingtypes.RSPKind)
if scheduler != nil {
return false, nil
Expand All @@ -125,7 +127,7 @@ func waitForSchedulerDeleted(tl common.TestLogger, controller *schedulingmanager
}

func waitForSchedulerStarted(tl common.TestLogger, controller *schedulingmanager.SchedulingManager, schedulingTypes map[string]schedulingtypes.SchedulerFactory) {
err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(_ context.Context) (bool, error) {
scheduler := controller.GetScheduler(schedulingtypes.RSPKind)
if scheduler == nil {
return false, nil
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/scheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ func waitForMatchingFederatedObject(tl common.TestLogger, typeConfig typeconfig.

expected64 := int32MapToInt64(expected32)

return wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
fedObject, err := client.Resources(namespace).Get(context.Background(), name, metav1.GetOptions{})
return wait.PollUntilContextTimeout(context.Background(), framework.PollInterval, framework.TestContext.SingleCallTimeout, true, func(ctx context.Context) (bool, error) {
fedObject, err := client.Resources(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
tl.Errorf("An error occurred while polling for %s %s/%s: %v", kind, namespace, name, err)
Expand Down Expand Up @@ -348,8 +348,8 @@ func updateClusterLabel(tl common.TestLogger, client genericclient.Client, kubef
Version: fedv1b1.SchemeGroupVersion.Version,
})
// We retry couple of times on conflict
err := wait.PollImmediate(1*time.Second, 10*time.Second, func() (bool, error) {
err := client.Get(context.Background(), fedCluster, kubefedNamespace, clusterName)
err := wait.PollUntilContextTimeout(context.Background(), 1*time.Second, 10*time.Second, true, func(ctx context.Context) (bool, error) {
err := client.Get(ctx, fedCluster, kubefedNamespace, clusterName)
if err != nil {
tl.Fatalf("Cannot get KubeFedCluster %q from namespace %q: %v", clusterName, kubefedNamespace, err)
}
Expand All @@ -359,7 +359,7 @@ func updateClusterLabel(tl common.TestLogger, client genericclient.Client, kubef
} else {
removeLabel(fedCluster, "foo", "bar")
}
err = client.Update(context.TODO(), fedCluster)
err = client.Update(ctx, fedCluster)
if err == nil {
return true, nil
}
Expand Down
Loading

0 comments on commit 66af5ab

Please sign in to comment.