Skip to content

Commit 3f29722

Browse files
fix: Clean up deprecated functions (#326)
--------- Signed-off-by: Mario Fernandez <[email protected]> Co-authored-by: Daniel Mellado <[email protected]>
1 parent b12c856 commit 3f29722

5 files changed

+52
-77
lines changed

test/e2e/framework/assertions.go

+24-29
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"k8s.io/apimachinery/pkg/util/wait"
2424
)
2525

26-
const CustomForeverTestTimeout = 40 * time.Second
26+
// default ForeverTestTimeout is 30, some test fail because they take more than 30s
27+
// change to custom in order to let the test finish withouth errors
28+
const DefaultTestTimeout = 40 * time.Second
2729

2830
type AssertOption struct {
2931
PollInterval time.Duration
@@ -44,19 +46,19 @@ func WithPollInterval(d time.Duration) OptionFn {
4446
}
4547
}
4648

47-
// AssertResourceNeverExists asserts that a statefulset is never created for the duration of CustomForeverTestTimeout
49+
// AssertResourceNeverExists asserts that a statefulset is never created for the duration of DefaultTestTimeout
4850
func (f *Framework) AssertResourceNeverExists(name, namespace string, resource client.Object, fns ...OptionFn) func(t *testing.T) {
4951
option := AssertOption{
5052
PollInterval: 5 * time.Second,
51-
WaitTimeout: CustomForeverTestTimeout,
53+
WaitTimeout: DefaultTestTimeout,
5254
}
5355
for _, fn := range fns {
5456
fn(&option)
5557
}
5658

5759
return func(t *testing.T) {
5860
//nolint
59-
if err := wait.Poll(option.PollInterval, option.WaitTimeout, func() (done bool, err error) {
61+
if err := wait.Poll(option.PollInterval, option.WaitTimeout, func() (bool, error) {
6062
key := types.NamespacedName{
6163
Name: name,
6264
Namespace: namespace,
@@ -66,26 +68,24 @@ func (f *Framework) AssertResourceNeverExists(name, namespace string, resource c
6668
}
6769

6870
return true, fmt.Errorf("resource %s/%s should not have been created", namespace, name)
69-
//nolint
70-
}); err != wait.ErrWaitTimeout {
71+
}); !wait.Interrupted(err) {
7172
t.Fatal(err)
7273
}
7374
}
7475
}
7576

76-
// AssertResourceEventuallyExists asserts that a resource is created duration a time period of CustomForeverTestTimeout
77+
// AssertResourceEventuallyExists asserts that a resource is created duration a time period of customForeverTestTimeout
7778
func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resource client.Object, fns ...OptionFn) func(t *testing.T) {
7879
option := AssertOption{
7980
PollInterval: 5 * time.Second,
80-
WaitTimeout: CustomForeverTestTimeout,
81+
WaitTimeout: DefaultTestTimeout,
8182
}
8283
for _, fn := range fns {
8384
fn(&option)
8485
}
8586

8687
return func(t *testing.T) {
87-
//nolint
88-
if err := wait.Poll(option.PollInterval, option.WaitTimeout, func() (done bool, err error) {
88+
if err := wait.PollUntilContextTimeout(context.Background(), option.PollInterval, option.WaitTimeout, true, func(ctx context.Context) (bool, error) {
8989
key := types.NamespacedName{
9090
Name: name,
9191
Namespace: namespace,
@@ -94,8 +94,7 @@ func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resou
9494
return true, nil
9595
}
9696
return false, nil
97-
//nolint
98-
}); err == wait.ErrWaitTimeout {
97+
}); wait.Interrupted(err) {
9998
t.Fatal(fmt.Errorf("resource %s/%s was never created", namespace, name))
10099
}
101100
}
@@ -105,7 +104,7 @@ func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resou
105104
func (f *Framework) AssertStatefulsetReady(name, namespace string, fns ...OptionFn) func(t *testing.T) {
106105
option := AssertOption{
107106
PollInterval: 5 * time.Second,
108-
WaitTimeout: CustomForeverTestTimeout,
107+
WaitTimeout: DefaultTestTimeout,
109108
}
110109
for _, fn := range fns {
111110
fn(&option)
@@ -125,7 +124,7 @@ func (f *Framework) AssertStatefulsetReady(name, namespace string, fns ...Option
125124

126125
func (f *Framework) GetResourceWithRetry(t *testing.T, name, namespace string, obj client.Object) {
127126
//nolint
128-
err := wait.Poll(5*time.Second, CustomForeverTestTimeout, func() (bool, error) {
127+
err := wait.Poll(5*time.Second, DefaultTestTimeout, func() (bool, error) {
129128
key := types.NamespacedName{Name: name, Namespace: namespace}
130129

131130
if err := f.K8sClient.Get(context.Background(), key, obj); errors.IsNotFound(err) {
@@ -136,14 +135,12 @@ func (f *Framework) GetResourceWithRetry(t *testing.T, name, namespace string, o
136135
return true, nil
137136
})
138137

139-
//nolint
140-
if err == wait.ErrWaitTimeout {
138+
if wait.Interrupted(err) {
141139
t.Fatal(fmt.Errorf("resource %s/%s was never created", namespace, name))
142140
}
143141
}
144142

145143
func assertPromQL(t *testing.T, metrics []byte, query string, expected map[string]float64) {
146-
147144
now := time.Now()
148145
points, err := prom.ParseTextData(metrics, now)
149146
if err != nil {
@@ -199,7 +196,6 @@ func assertPromQL(t *testing.T, metrics []byte, query string, expected map[strin
199196
// GetOperatorPod gets the operator pod assuming the operator is deployed in
200197
// "operators" namespace.
201198
func (f *Framework) GetOperatorPod(t *testing.T) *v1.Pod {
202-
203199
// get the operator deployment
204200
operator := appsv1.Deployment{}
205201
f.AssertResourceEventuallyExists("observability-operator", "operators", &operator)(t)
@@ -233,8 +229,7 @@ func (f *Framework) GetOperatorMetrics(t *testing.T) []byte {
233229

234230
stopChan := make(chan struct{})
235231
defer close(stopChan)
236-
//nolint
237-
if err := wait.Poll(5*time.Second, CustomForeverTestTimeout, func() (bool, error) {
232+
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
238233
err := f.StartPortForward(pod.Name, pod.Namespace, "8080", stopChan)
239234
return err == nil, nil
240235
}); err != nil {
@@ -272,10 +267,13 @@ func (f *Framework) GetStackWhenAvailable(t *testing.T, name, namespace string)
272267
Name: name,
273268
Namespace: namespace,
274269
}
275-
//nolint
276-
err := wait.Poll(5*time.Second, CustomForeverTestTimeout, func() (bool, error) {
270+
var lastErr error
271+
272+
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
273+
lastErr = nil
277274
err := f.K8sClient.Get(context.Background(), key, &ms)
278275
if err != nil {
276+
lastErr = err
279277
return false, nil
280278
}
281279
availableC := getConditionByType(ms.Status.Conditions, v1alpha1.AvailableCondition)
@@ -285,9 +283,8 @@ func (f *Framework) GetStackWhenAvailable(t *testing.T, name, namespace string)
285283
return false, nil
286284
})
287285

288-
//nolint
289-
if err == wait.ErrWaitTimeout {
290-
t.Fatal(fmt.Errorf("resource %s/%s was not available", namespace, name))
286+
if wait.Interrupted(err) {
287+
t.Fatal(fmt.Errorf("MonitoringStack %s/%s was not available - err: %w | %v", namespace, name, lastErr, ms.Status.Conditions))
291288
}
292289
return ms
293290
}
@@ -298,16 +295,14 @@ func (f *Framework) AssertAlertmanagerAbsent(t *testing.T, name, namespace strin
298295
Name: name,
299296
Namespace: namespace,
300297
}
301-
//nolint
302-
err := wait.Poll(5*time.Second, CustomForeverTestTimeout, func() (bool, error) {
298+
err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
303299
err := f.K8sClient.Get(context.Background(), key, &am)
304300
if errors.IsNotFound(err) {
305301
return true, nil
306302
}
307303
return false, nil
308304
})
309-
//nolint
310-
if err == wait.ErrWaitTimeout {
305+
if wait.Interrupted(err) {
311306
t.Fatal(fmt.Errorf("alertmanager %s/%s is present when expected to be absent", namespace, name))
312307
}
313308
}

test/e2e/monitoring_stack_controller_test.go

+16-28
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func assertCRDExists(t *testing.T, crds ...string) {
4949
}
5050

5151
func TestMonitoringStackController(t *testing.T) {
52-
stack.AddToScheme(scheme.Scheme)
52+
err := stack.AddToScheme(scheme.Scheme)
53+
assert.NilError(t, err, "adding stack to scheme failed")
5354
assertCRDExists(t,
5455
"prometheuses.monitoring.rhobs",
5556
"alertmanagers.monitoring.rhobs",
@@ -148,8 +149,7 @@ func nilResrouceSelectorPropagatesToPrometheus(t *testing.T) {
148149
assert.NilError(t, err, "failed to patch monitoring stack with nil resource selector")
149150

150151
prometheus := monv1.Prometheus{}
151-
//nolint
152-
err = wait.Poll(5*time.Second, framework.CustomForeverTestTimeout, func() (bool, error) {
152+
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
153153
if err := f.K8sClient.Get(context.Background(), types.NamespacedName{Name: updatedMS.Name, Namespace: updatedMS.Namespace}, &prometheus); errors.IsNotFound(err) {
154154
return false, nil
155155
}
@@ -160,8 +160,7 @@ func nilResrouceSelectorPropagatesToPrometheus(t *testing.T) {
160160
return true, nil
161161
})
162162

163-
//nolint
164-
if err == wait.ErrWaitTimeout {
163+
if wait.Interrupted(err) {
165164
t.Fatal(fmt.Errorf("nil ResourceSelector did not propagate to Prometheus object"))
166165
}
167166
}
@@ -298,8 +297,7 @@ func reconcileRevertsManualChanges(t *testing.T) {
298297
err = f.K8sClient.Update(context.Background(), modified)
299298
assert.NilError(t, err, "failed to update a prometheus")
300299

301-
//nolint
302-
err = wait.Poll(5*time.Second, time.Minute, func() (bool, error) {
300+
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
303301
reconciled := monv1.Prometheus{}
304302
key := types.NamespacedName{Name: ms.Name, Namespace: ms.Namespace}
305303

@@ -415,8 +413,7 @@ func assertPrometheusScrapesItself(t *testing.T) {
415413

416414
stopChan := make(chan struct{})
417415
defer close(stopChan)
418-
//nolint
419-
if err := wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
416+
if err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 2*time.Minute, true, func(ctx context.Context) (bool, error) {
420417
err = f.StartServicePortForward("self-scrape-prometheus", e2eTestNamespace, "9090", stopChan)
421418
return err == nil, nil
422419
}); err != nil {
@@ -428,8 +425,7 @@ func assertPrometheusScrapesItself(t *testing.T) {
428425
"prometheus_build_info": 2, // scrapes from both endpoints
429426
"alertmanager_build_info": 2,
430427
}
431-
//nolint
432-
if err := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
428+
if err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
433429
correct := 0
434430
for query, value := range expectedResults {
435431
result, err := promClient.Query(query)
@@ -534,16 +530,14 @@ func assertAlertmanagerReceivesAlerts(t *testing.T) {
534530

535531
stopChan := make(chan struct{})
536532
defer close(stopChan)
537-
//nolint
538-
if err := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
533+
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
539534
err := f.StartServicePortForward("alerting-alertmanager", e2eTestNamespace, "9093", stopChan)
540535
return err == nil, nil
541536
}); err != nil {
542537
t.Fatal(err)
543538
}
544539

545-
//nolint
546-
if err := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
540+
if err := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
547541
alerts, err := getAlertmanagerAlerts()
548542
if err != nil {
549543
return false, nil
@@ -590,8 +584,7 @@ func prometheusScaleDown(t *testing.T) {
590584
ms.Spec.PrometheusConfig.Replicas = &numOfRep
591585
err = f.K8sClient.Update(context.Background(), ms)
592586
assert.NilError(t, err, "failed to update a monitoring stack")
593-
//nolint
594-
err = wait.Poll(5*time.Second, framework.CustomForeverTestTimeout, func() (bool, error) {
587+
err = wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
595588
if err := f.K8sClient.Get(context.Background(), key, &prom); errors.IsNotFound(err) {
596589
return false, nil
597590
}
@@ -602,8 +595,7 @@ func prometheusScaleDown(t *testing.T) {
602595
return true, nil
603596
})
604597

605-
//nolint
606-
if err == wait.ErrWaitTimeout {
598+
if wait.Interrupted(err) {
607599
t.Fatal(fmt.Errorf("Prometheus was not scaled down"))
608600
}
609601
}
@@ -749,8 +741,6 @@ func getAlertmanagerAlerts() ([]alert, error) {
749741
}
750742

751743
func newAlerts(t *testing.T) *monv1.PrometheusRule {
752-
durationTenSec := monv1.Duration("10s")
753-
durationOneSec := monv1.Duration("1s")
754744
rule := &monv1.PrometheusRule{
755745
TypeMeta: metav1.TypeMeta{
756746
APIVersion: monv1.SchemeGroupVersion.String(),
@@ -764,17 +754,17 @@ func newAlerts(t *testing.T) *monv1.PrometheusRule {
764754
Groups: []monv1.RuleGroup{
765755
{
766756
Name: "Test",
767-
Interval: &durationTenSec,
757+
Interval: ptr.To(monv1.Duration("10s")),
768758
Rules: []monv1.Rule{
769759
{
770760
Alert: "AlwaysOn",
771761
Expr: intstr.FromString("vector(1)"),
772-
For: &durationOneSec,
762+
For: ptr.To(monv1.Duration("1s")),
773763
},
774764
{
775765
Alert: "NeverOn",
776766
Expr: intstr.FromString("vector(1) == 0"),
777-
For: &durationOneSec,
767+
For: ptr.To(monv1.Duration("1s")),
778768
},
779769
},
780770
},
@@ -827,8 +817,7 @@ func newMonitoringStack(t *testing.T, name string, mods ...stackModifier) *stack
827817
}
828818

829819
func waitForStackDeletion(name string) error {
830-
//nolint
831-
return wait.Poll(5*time.Second, framework.CustomForeverTestTimeout, func() (bool, error) {
820+
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, framework.DefaultTestTimeout, true, func(ctx context.Context) (bool, error) {
832821
key := types.NamespacedName{Name: name, Namespace: e2eTestNamespace}
833822
var ms stack.MonitoringStack
834823
err := f.K8sClient.Get(context.Background(), key, &ms)
@@ -874,8 +863,7 @@ func namespaceSelectorTest(t *testing.T) {
874863
}
875864

876865
promClient := framework.NewPrometheusClient("http://localhost:9090")
877-
//nolint
878-
if pollErr := wait.Poll(5*time.Second, 5*time.Minute, func() (bool, error) {
866+
if pollErr := wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
879867
query := `version{pod="prometheus-example-app",namespace=~"test-ns-.*"}`
880868
result, err := promClient.Query(query)
881869
if err != nil {

test/e2e/po_admission_webhook_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"gotest.tools/v3/assert"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
"k8s.io/apimachinery/pkg/util/intstr"
11+
"k8s.io/utils/ptr"
1112
)
1213

1314
func TestPrometheusRuleWebhook(t *testing.T) {
@@ -42,7 +43,6 @@ func invalidPrometheusRuleIsRejected(t *testing.T) {
4243
}
4344

4445
func newSinglePrometheusRule(t *testing.T, name, expr string) *monv1.PrometheusRule {
45-
durationFifteenMinutes := monv1.Duration("15m")
4646
rule := &monv1.PrometheusRule{
4747
ObjectMeta: metav1.ObjectMeta{
4848
Name: name,
@@ -54,7 +54,7 @@ func newSinglePrometheusRule(t *testing.T, name, expr string) *monv1.PrometheusR
5454
Rules: []monv1.Rule{{
5555
Alert: "alert name",
5656
Expr: intstr.FromString(expr),
57-
For: &durationFifteenMinutes,
57+
For: ptr.To(monv1.Duration("15m")),
5858
}},
5959
}},
6060
},

test/e2e/prometheus_operator_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,14 @@ func TestPrometheusOperatorForNonOwnedResources(t *testing.T) {
5353
name: "Operator should not reconcile resources which it does not own",
5454
scenario: func(t *testing.T) {
5555
t.Run("Prometheus never exists", func(t *testing.T) {
56-
t.Parallel()
5756
f.AssertResourceNeverExists(prometheusStsName, e2eTestNamespace,
5857
&appsv1.StatefulSet{}, framework.WithTimeout(15*time.Second))(t)
5958
})
6059
t.Run("Alertmanager never exists", func(t *testing.T) {
61-
t.Parallel()
6260
f.AssertResourceNeverExists(alertmanagerStsName, e2eTestNamespace,
6361
&appsv1.StatefulSet{}, framework.WithTimeout(15*time.Second))(t)
6462
})
6563
t.Run("Thanos Ruler never exists", func(t *testing.T) {
66-
t.Parallel()
6764
f.AssertResourceNeverExists(thanosRulerStsName, e2eTestNamespace,
6865
&appsv1.StatefulSet{}, framework.WithTimeout(15*time.Second))(t)
6966
})

0 commit comments

Comments
 (0)