Skip to content

Commit

Permalink
chore: replace deprecated wait.Poll
Browse files Browse the repository at this point in the history
  • Loading branch information
marioferh committed Aug 1, 2023
1 parent 13d4e56 commit ebf97fc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 44 deletions.
38 changes: 21 additions & 17 deletions test/e2e/framework/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
)

// default ForeverTestTimeout is 30, some test fails because they take more than 30s
// change to custom in order to let the test finish withouth errors
const CustomForeverTestTimeout = 40 * time.Second

type AssertOption struct {
Expand All @@ -44,7 +46,7 @@ func WithPollInterval(d time.Duration) OptionFn {
}
}

// AssertResourceNeverExists asserts that a statefulset is never created for the duration of CustomForeverTestTimeout
// AssertResourceNeverExists asserts that a statefulset is never created for the duration of customForeverTestTimeout
func (f *Framework) AssertResourceNeverExists(name, namespace string, resource client.Object, fns ...OptionFn) func(t *testing.T) {
option := AssertOption{
PollInterval: 5 * time.Second,
Expand All @@ -55,23 +57,23 @@ func (f *Framework) AssertResourceNeverExists(name, namespace string, resource c
}

return func(t *testing.T) {
if err := wait.Poll(option.PollInterval, option.WaitTimeout, func() (done bool, err error) {
if err := wait.PollUntilContextTimeout(context.Background(), option.PollInterval, option.WaitTimeout, true, func(ctx context.Context) (done bool, err error) {
key := types.NamespacedName{
Name: name,
Namespace: namespace,
}
if err := f.K8sClient.Get(context.Background(), key, resource); errors.IsNotFound(err) {
if err := f.K8sClient.Get(ctx, key, resource); errors.IsNotFound(err) {
return false, nil
}

return true, fmt.Errorf("resource %s/%s should not have been created", namespace, name)
}); err != wait.ErrWaitTimeout {
}); wait.Interrupted(err) {
t.Fatal(err)
}
}
}

// AssertResourceEventuallyExists asserts that a resource is created duration a time period of CustomForeverTestTimeout
// AssertResourceEventuallyExists asserts that a resource is created duration a time period of customForeverTestTimeout
func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resource client.Object, fns ...OptionFn) func(t *testing.T) {
option := AssertOption{
PollInterval: 5 * time.Second,
Expand All @@ -82,7 +84,7 @@ func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resou
}

return func(t *testing.T) {
if err := wait.Poll(option.PollInterval, option.WaitTimeout, func() (done bool, err error) {
if err := wait.PollUntilContextTimeout(context.Background(), option.PollInterval, option.WaitTimeout, true, func(ctx context.Context) (done bool, err error) {
key := types.NamespacedName{
Name: name,
Namespace: namespace,
Expand All @@ -91,7 +93,7 @@ func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resou
return true, nil
}
return false, nil
}); err == wait.ErrWaitTimeout {
}); wait.Interrupted(err) {
t.Fatal(fmt.Errorf("resource %s/%s was never created", namespace, name))
}
}
Expand Down Expand Up @@ -119,7 +121,7 @@ func (f *Framework) AssertStatefulsetReady(name, namespace string, fns ...Option
}

func (f *Framework) GetResourceWithRetry(t *testing.T, name, namespace string, obj client.Object) {
err := wait.Poll(5*time.Second, CustomForeverTestTimeout, func() (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
key := types.NamespacedName{Name: name, Namespace: namespace}

if err := f.K8sClient.Get(context.Background(), key, obj); errors.IsNotFound(err) {
Expand All @@ -130,13 +132,12 @@ func (f *Framework) GetResourceWithRetry(t *testing.T, name, namespace string, o
return true, nil
})

if err == wait.ErrWaitTimeout {
if wait.Interrupted(err) {
t.Fatal(fmt.Errorf("resource %s/%s was never created", namespace, name))
}
}

func assertPromQL(t *testing.T, metrics []byte, query string, expected map[string]float64) {

now := time.Now()
points, err := prom.ParseTextData(metrics, now)
if err != nil {
Expand Down Expand Up @@ -192,7 +193,6 @@ func assertPromQL(t *testing.T, metrics []byte, query string, expected map[strin
// GetOperatorPod gets the operator pod assuming the operator is deployed in
// "operators" namespace.
func (f *Framework) GetOperatorPod(t *testing.T) *v1.Pod {

// get the operator deployment
operator := appsv1.Deployment{}
f.AssertResourceEventuallyExists("observability-operator", "operators", &operator)(t)
Expand Down Expand Up @@ -226,7 +226,7 @@ func (f *Framework) GetOperatorMetrics(t *testing.T) []byte {

stopChan := make(chan struct{})
defer close(stopChan)
if err := wait.Poll(5*time.Second, CustomForeverTestTimeout, func() (bool, error) {
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
err := f.StartPortForward(pod.Name, pod.Namespace, "8080", stopChan)
return err == nil, nil
}); err != nil {
Expand Down Expand Up @@ -264,9 +264,13 @@ func (f *Framework) GetStackWhenAvailable(t *testing.T, name, namespace string)
Name: name,
Namespace: namespace,
}
err := wait.Poll(5*time.Second, CustomForeverTestTimeout, func() (bool, error) {
var lastErr error

err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
lastErr = nil
err := f.K8sClient.Get(context.Background(), key, &ms)
if err != nil {
lastErr = err
return false, nil
}
availableC := getConditionByType(ms.Status.Conditions, v1alpha1.AvailableCondition)
Expand All @@ -276,8 +280,8 @@ func (f *Framework) GetStackWhenAvailable(t *testing.T, name, namespace string)
return false, nil
})

if err == wait.ErrWaitTimeout {
t.Fatal(fmt.Errorf("resource %s/%s was not available", namespace, name))
if wait.Interrupted(err) {
t.Fatal(fmt.Errorf("MonitoringStack %s/%s was not available - err: %w | %v", namespace, name, lastErr, ms.Status.Conditions))
}
return ms
}
Expand All @@ -288,14 +292,14 @@ func (f *Framework) AssertAlertmanagerAbsent(t *testing.T, name, namespace strin
Name: name,
Namespace: namespace,
}
err := wait.Poll(5*time.Second, CustomForeverTestTimeout, func() (bool, error) {
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
err := f.K8sClient.Get(context.Background(), key, &am)
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
})
if err == wait.ErrWaitTimeout {
if wait.Interrupted(err) {
t.Fatal(fmt.Errorf("alertmanager %s/%s is present when expected to be absent", namespace, name))
}
}
Expand Down
31 changes: 17 additions & 14 deletions test/e2e/monitoring_stack_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func assertCRDExists(t *testing.T, crds ...string) {
}

func TestMonitoringStackController(t *testing.T) {
stack.AddToScheme(scheme.Scheme)
err := stack.AddToScheme(scheme.Scheme)
if err != nil {
return
}
assertCRDExists(t,
"prometheuses.monitoring.rhobs",
"alertmanagers.monitoring.rhobs",
Expand Down Expand Up @@ -149,7 +152,7 @@ func nilResrouceSelectorPropagatesToPrometheus(t *testing.T) {
assert.NilError(t, err, "failed to patch monitoring stack with nil resource selector")

prometheus := monv1.Prometheus{}
err = wait.Poll(5*time.Second, framework.CustomForeverTestTimeout, func() (bool, error) {
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
if err := f.K8sClient.Get(context.Background(), types.NamespacedName{Name: updatedMS.Name, Namespace: updatedMS.Namespace}, &prometheus); errors.IsNotFound(err) {
return false, nil
}
Expand All @@ -160,7 +163,7 @@ func nilResrouceSelectorPropagatesToPrometheus(t *testing.T) {
return true, nil
})

if err == wait.ErrWaitTimeout {
if wait.Interrupted(err) {
t.Fatal(fmt.Errorf("nil ResourceSelector did not propagate to Prometheus object"))
}
}
Expand Down Expand Up @@ -297,7 +300,7 @@ func reconcileRevertsManualChanges(t *testing.T) {
err = f.K8sClient.Update(context.Background(), modified)
assert.NilError(t, err, "failed to update a prometheus")

err = wait.Poll(5*time.Second, time.Minute, func() (bool, error) {
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
reconciled := monv1.Prometheus{}
key := types.NamespacedName{Name: ms.Name, Namespace: ms.Namespace}

Expand Down Expand Up @@ -413,7 +416,7 @@ func assertPrometheusScrapesItself(t *testing.T) {

stopChan := make(chan struct{})
defer close(stopChan)
if err := wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
if err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) {
err = f.StartServicePortForward("self-scrape-prometheus", e2eTestNamespace, "9090", stopChan)
return err == nil, nil
}); err != nil {
Expand All @@ -425,7 +428,7 @@ func assertPrometheusScrapesItself(t *testing.T) {
"prometheus_build_info": 2, // scrapes from both endpoints
"alertmanager_build_info": 2,
}
if err := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
if err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
correct := 0
for query, value := range expectedResults {
result, err := promClient.Query(query)
Expand Down Expand Up @@ -530,14 +533,14 @@ func assertAlertmanagerReceivesAlerts(t *testing.T) {

stopChan := make(chan struct{})
defer close(stopChan)
if err := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
err := f.StartServicePortForward("alerting-alertmanager", e2eTestNamespace, "9093", stopChan)
return err == nil, nil
}); err != nil {
t.Fatal(err)
}

if err := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
alerts, err := getAlertmanagerAlerts()
if err != nil {
return false, nil
Expand Down Expand Up @@ -584,7 +587,7 @@ func prometheusScaleDown(t *testing.T) {
ms.Spec.PrometheusConfig.Replicas = &numOfRep
err = f.K8sClient.Update(context.Background(), ms)
assert.NilError(t, err, "failed to update a monitoring stack")
err = wait.Poll(5*time.Second, framework.CustomForeverTestTimeout, func() (bool, error) {
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
if err := f.K8sClient.Get(context.Background(), key, &prom); errors.IsNotFound(err) {
return false, nil
}
Expand All @@ -595,7 +598,7 @@ func prometheusScaleDown(t *testing.T) {
return true, nil
})

if err == wait.ErrWaitTimeout {
if wait.Interrupted(err) {
t.Fatal(fmt.Errorf("Prometheus was not scaled down"))
}
}
Expand Down Expand Up @@ -819,7 +822,7 @@ func newMonitoringStack(t *testing.T, name string, mods ...stackModifier) *stack
}

func waitForStackDeletion(name string) error {
return wait.Poll(5*time.Second, framework.CustomForeverTestTimeout, func() (bool, error) {
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.CustomForeverTestTimeout, true, func(ctx context.Context) (bool, error) {
key := types.NamespacedName{Name: name, Namespace: e2eTestNamespace}
var ms stack.MonitoringStack
err := f.K8sClient.Get(context.Background(), key, &ms)
Expand Down Expand Up @@ -856,16 +859,16 @@ func namespaceSelectorTest(t *testing.T) {

stopChan := make(chan struct{})
defer close(stopChan)
if pollErr := wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
if pollErr := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) {
err := f.StartServicePortForward(ms.Name+"-prometheus", e2eTestNamespace, "9090", stopChan)
return err == nil, nil
}); pollErr != nil {
t.Fatal(pollErr)
}

promClient := framework.NewPrometheusClient("http://localhost:9090")
if pollErr := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
query := `version{pod="prometheus-example-app",namespace=~"test-ns-.*"}`
if pollErr := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
query := `prometheus_build_info{namespace=~"test-ns-.*"}`
result, err := promClient.Query(query)
if err != nil {
return false, nil
Expand Down
3 changes: 0 additions & 3 deletions test/e2e/prometheus_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,14 @@ func TestPrometheusOperatorForNonOwnedResources(t *testing.T) {
name: "Operator should not reconcile resources which it does not own",
scenario: func(t *testing.T) {
t.Run("Prometheus never exists", func(t *testing.T) {
t.Parallel()
f.AssertResourceNeverExists(prometheusStsName, e2eTestNamespace,
&appsv1.StatefulSet{}, framework.WithTimeout(15*time.Second))(t)
})
t.Run("Alertmanager never exists", func(t *testing.T) {
t.Parallel()
f.AssertResourceNeverExists(alertmanagerStsName, e2eTestNamespace,
&appsv1.StatefulSet{}, framework.WithTimeout(15*time.Second))(t)
})
t.Run("Thanos Ruler never exists", func(t *testing.T) {
t.Parallel()
f.AssertResourceNeverExists(thanosRulerStsName, e2eTestNamespace,
&appsv1.StatefulSet{}, framework.WithTimeout(15*time.Second))(t)
})
Expand Down
20 changes: 10 additions & 10 deletions test/e2e/thanos_querier_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ func singleStackWithSidecar(t *testing.T) {
// Assert prometheus instance can be queried
stopChan := make(chan struct{})
defer close(stopChan)
if err := wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) {
err = f.StartServicePortForward(name, e2eTestNamespace, "9090", stopChan)
return err == nil, nil
}); err != nil {
}); wait.Interrupted(err) {
t.Fatal(err)
}

promClient := framework.NewPrometheusClient("http://localhost:9090")
expectedResults := map[string]int{
"prometheus_build_info": 2, // must return from both prometheus pods
}
if err := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
correct := 0
for query, value := range expectedResults {
result, err := promClient.Query(query)
Expand All @@ -111,7 +111,7 @@ func singleStackWithSidecar(t *testing.T) {
}

return correct == len(expectedResults), nil
}); err != nil {
}); wait.Interrupted(err) {
t.Fatal(err)
}
}
Expand All @@ -137,28 +137,28 @@ func newThanosQuerier(t *testing.T, name string, selector map[string]string) *ms
}

func waitForThanosQuerierDeletion(tq *msov1.ThanosQuerier) error {
return wait.Poll(5*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
err := f.K8sClient.Get(context.Background(),
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
err = f.K8sClient.Get(context.Background(),
types.NamespacedName{Name: tq.Name, Namespace: tq.Namespace},
tq)
return errors.IsNotFound(err), nil
})
}

func waitForDeploymentDeletion(name string) error {
return wait.Poll(5*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
var dep appsv1.Deployment
err := f.K8sClient.Get(context.Background(),
err = f.K8sClient.Get(context.Background(),
types.NamespacedName{Name: name, Namespace: e2eTestNamespace},
&dep)
return errors.IsNotFound(err), nil
})
}

func waitForServiceDeletion(name string) error {
return wait.Poll(5*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, wait.ForeverTestTimeout, true, func(ctx context.Context) (done bool, err error) {
var svc corev1.Service
err := f.K8sClient.Get(context.Background(),
err = f.K8sClient.Get(context.Background(),
types.NamespacedName{Name: name, Namespace: e2eTestNamespace},
&svc)
return errors.IsNotFound(err), nil
Expand Down

0 comments on commit ebf97fc

Please sign in to comment.