Skip to content

Commit

Permalink
fix(test): using context.TODO() in test steps to reduce flakiness of …
Browse files Browse the repository at this point in the history
…test

Signed-off-by: Alex Castilio dos Santos <[email protected]>
  • Loading branch information
alexcastilio committed Feb 10, 2025
1 parent 63996c7 commit 9af5950
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 42 deletions.
8 changes: 3 additions & 5 deletions test/e2e/framework/kubernetes/check-pod-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
)

const (
RetryTimeoutPodsReady = 5 * time.Minute
RetryIntervalPodsReady = 5 * time.Second
timeoutWaitForPodsSeconds = 1200
RetryTimeoutPodsReady = 5 * time.Minute
RetryIntervalPodsReady = 5 * time.Second

printInterval = 5 // print to stdout every 5 iterations
)
Expand Down Expand Up @@ -49,8 +48,7 @@ func (w *WaitPodsReady) Run() error {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), timeoutWaitForPodsSeconds*time.Second)
defer cancel()
ctx := context.TODO()

return WaitForPodReady(ctx, clientset, w.Namespace, w.LabelSelector)
}
Expand Down
4 changes: 1 addition & 3 deletions test/e2e/framework/kubernetes/create-namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kubernetes
import (
"context"
"fmt"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -52,8 +51,7 @@ func CreateNamespaceFn(kubeconfigpath, namespace string) error {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second)
defer cancel()
ctx := context.TODO()

_, err = clientset.CoreV1().Namespaces().Create(ctx, &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/framework/kubernetes/delete-namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func (d *DeleteNamespace) Run() error {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Second)
defer cancel()
ctx := context.TODO()

err = clientset.CoreV1().Namespaces().Delete(ctx, d.Namespace, metaV1.DeleteOptions{})
if err != nil {
Expand Down
8 changes: 1 addition & 7 deletions test/e2e/framework/scaletest/add-shared-labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"log"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -51,8 +50,7 @@ func (a *AddSharedLabelsToAllPods) Run() error {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := contextToLabelAllPods()
defer cancel()
ctx := context.TODO()

resources, err := clientset.CoreV1().Pods(a.Namespace).List(ctx, metav1.ListOptions{})
if err != nil {
Expand Down Expand Up @@ -109,7 +107,3 @@ func getSharedLabelsPatch(numLabels int) ([]byte, error) {

return b, nil
}

func contextToLabelAllPods() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), 120*time.Minute)
}
4 changes: 2 additions & 2 deletions test/e2e/framework/scaletest/add-unique-labels.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scaletest

import (
"context"
"encoding/json"
"fmt"

Expand Down Expand Up @@ -41,8 +42,7 @@ func (a *AddUniqueLabelsToAllPods) Run() error {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := contextToLabelAllPods()
defer cancel()
ctx := context.TODO()

resources, err := clientset.CoreV1().Pods(a.Namespace).List(ctx, metav1.ListOptions{})
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions test/e2e/framework/scaletest/create-network-policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scaletest
import (
"context"
"fmt"
"time"

e2ekubernetes "github.com/microsoft/retina/test/e2e/framework/kubernetes"

Expand Down Expand Up @@ -45,8 +44,7 @@ func (c *CreateNetworkPolicies) Run() error {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second)
defer cancel()
ctx := context.TODO()

networkPolicies := c.generateNetworkPolicies(c.NumNetworkPolicies)

Expand Down
4 changes: 1 addition & 3 deletions test/e2e/framework/scaletest/create-resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"time"

e2ekubernetes "github.com/microsoft/retina/test/e2e/framework/kubernetes"
"github.com/microsoft/retina/test/retry"
Expand Down Expand Up @@ -49,8 +48,7 @@ func (c *CreateResources) Run() error {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 1800*time.Second)
defer cancel()
ctx := context.TODO()

retrier := retry.Retrier{Attempts: defaultRetryAttempts, Delay: defaultRetryDelay}

Expand Down
9 changes: 1 addition & 8 deletions test/e2e/framework/scaletest/delete-and-re-add-labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ func (d *DeleteAndReAddLabels) Run() error {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ctx := context.TODO()

labelsToDelete := `"shared-lab-00000": null, "shared-lab-00001": null, "shared-lab-00002": null`
labelsToAdd := `"shared-lab-00000": "val", "shared-lab-00001": "val", "shared-lab-00002": "val"`
Expand All @@ -74,9 +73,6 @@ func (d *DeleteAndReAddLabels) Run() error {

patch := fmt.Sprintf(`{"metadata": {"labels": {%s}}}`, labelsToDelete)

ctx, cancel = contextToLabelAllPods()
defer cancel()

err = d.deleteLabels(ctx, clientset, pods, patch)
if err != nil {
return fmt.Errorf("error deleting labels: %w", err)
Expand All @@ -89,9 +85,6 @@ func (d *DeleteAndReAddLabels) Run() error {

patch = fmt.Sprintf(`{"metadata": {"labels": {%s}}}`, labelsToAdd)

ctx, cancel = contextToLabelAllPods()
defer cancel()

err = d.addLabels(ctx, clientset, pods, patch)
if err != nil {
return fmt.Errorf("error adding labels: %w", err)
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/framework/scaletest/get-publish-metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ func (g *GetAndPublishMetrics) Prevalidate() error {

func (g *GetAndPublishMetrics) getAndPublishMetrics() error {

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second)
defer cancel()
ctx := context.TODO()

labelSelector := labels.Set(g.Labels).String()

Expand Down
8 changes: 1 addition & 7 deletions test/e2e/framework/scaletest/validate-nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scaletest
import (
"context"
"fmt"
"time"

"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -12,10 +11,6 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

const (
defaultTimeoutSeconds = 300
)

type ValidateNumOfNodes struct {
NumNodesRequired int
Label map[string]string
Expand Down Expand Up @@ -43,8 +38,7 @@ func (v *ValidateNumOfNodes) Run() error {
return fmt.Errorf("error creating Kubernetes client: %w", err)
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeoutSeconds*time.Second)
defer cancel()
ctx := context.TODO()

labelSelector := labels.Set(v.Label).String()
nodes, err := clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
Expand Down

0 comments on commit 9af5950

Please sign in to comment.