@@ -23,7 +23,9 @@ import (
23
23
"k8s.io/apimachinery/pkg/util/wait"
24
24
)
25
25
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
27
29
28
30
type AssertOption struct {
29
31
PollInterval time.Duration
@@ -44,19 +46,19 @@ func WithPollInterval(d time.Duration) OptionFn {
44
46
}
45
47
}
46
48
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
48
50
func (f * Framework ) AssertResourceNeverExists (name , namespace string , resource client.Object , fns ... OptionFn ) func (t * testing.T ) {
49
51
option := AssertOption {
50
52
PollInterval : 5 * time .Second ,
51
- WaitTimeout : CustomForeverTestTimeout ,
53
+ WaitTimeout : DefaultTestTimeout ,
52
54
}
53
55
for _ , fn := range fns {
54
56
fn (& option )
55
57
}
56
58
57
59
return func (t * testing.T ) {
58
60
//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 ) {
60
62
key := types.NamespacedName {
61
63
Name : name ,
62
64
Namespace : namespace ,
@@ -66,26 +68,24 @@ func (f *Framework) AssertResourceNeverExists(name, namespace string, resource c
66
68
}
67
69
68
70
return true , fmt .Errorf ("resource %s/%s should not have been created" , namespace , name )
69
- //nolint
70
- }); err != wait .ErrWaitTimeout {
71
+ }); ! wait .Interrupted (err ) {
71
72
t .Fatal (err )
72
73
}
73
74
}
74
75
}
75
76
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
77
78
func (f * Framework ) AssertResourceEventuallyExists (name , namespace string , resource client.Object , fns ... OptionFn ) func (t * testing.T ) {
78
79
option := AssertOption {
79
80
PollInterval : 5 * time .Second ,
80
- WaitTimeout : CustomForeverTestTimeout ,
81
+ WaitTimeout : DefaultTestTimeout ,
81
82
}
82
83
for _ , fn := range fns {
83
84
fn (& option )
84
85
}
85
86
86
87
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 ) {
89
89
key := types.NamespacedName {
90
90
Name : name ,
91
91
Namespace : namespace ,
@@ -94,8 +94,7 @@ func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resou
94
94
return true , nil
95
95
}
96
96
return false , nil
97
- //nolint
98
- }); err == wait .ErrWaitTimeout {
97
+ }); wait .Interrupted (err ) {
99
98
t .Fatal (fmt .Errorf ("resource %s/%s was never created" , namespace , name ))
100
99
}
101
100
}
@@ -105,7 +104,7 @@ func (f *Framework) AssertResourceEventuallyExists(name, namespace string, resou
105
104
func (f * Framework ) AssertStatefulsetReady (name , namespace string , fns ... OptionFn ) func (t * testing.T ) {
106
105
option := AssertOption {
107
106
PollInterval : 5 * time .Second ,
108
- WaitTimeout : CustomForeverTestTimeout ,
107
+ WaitTimeout : DefaultTestTimeout ,
109
108
}
110
109
for _ , fn := range fns {
111
110
fn (& option )
@@ -125,7 +124,7 @@ func (f *Framework) AssertStatefulsetReady(name, namespace string, fns ...Option
125
124
126
125
func (f * Framework ) GetResourceWithRetry (t * testing.T , name , namespace string , obj client.Object ) {
127
126
//nolint
128
- err := wait .Poll (5 * time .Second , CustomForeverTestTimeout , func () (bool , error ) {
127
+ err := wait .Poll (5 * time .Second , DefaultTestTimeout , func () (bool , error ) {
129
128
key := types.NamespacedName {Name : name , Namespace : namespace }
130
129
131
130
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
136
135
return true , nil
137
136
})
138
137
139
- //nolint
140
- if err == wait .ErrWaitTimeout {
138
+ if wait .Interrupted (err ) {
141
139
t .Fatal (fmt .Errorf ("resource %s/%s was never created" , namespace , name ))
142
140
}
143
141
}
144
142
145
143
func assertPromQL (t * testing.T , metrics []byte , query string , expected map [string ]float64 ) {
146
-
147
144
now := time .Now ()
148
145
points , err := prom .ParseTextData (metrics , now )
149
146
if err != nil {
@@ -199,7 +196,6 @@ func assertPromQL(t *testing.T, metrics []byte, query string, expected map[strin
199
196
// GetOperatorPod gets the operator pod assuming the operator is deployed in
200
197
// "operators" namespace.
201
198
func (f * Framework ) GetOperatorPod (t * testing.T ) * v1.Pod {
202
-
203
199
// get the operator deployment
204
200
operator := appsv1.Deployment {}
205
201
f .AssertResourceEventuallyExists ("observability-operator" , "operators" , & operator )(t )
@@ -233,8 +229,7 @@ func (f *Framework) GetOperatorMetrics(t *testing.T) []byte {
233
229
234
230
stopChan := make (chan struct {})
235
231
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 ) {
238
233
err := f .StartPortForward (pod .Name , pod .Namespace , "8080" , stopChan )
239
234
return err == nil , nil
240
235
}); err != nil {
@@ -272,10 +267,13 @@ func (f *Framework) GetStackWhenAvailable(t *testing.T, name, namespace string)
272
267
Name : name ,
273
268
Namespace : namespace ,
274
269
}
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
277
274
err := f .K8sClient .Get (context .Background (), key , & ms )
278
275
if err != nil {
276
+ lastErr = err
279
277
return false , nil
280
278
}
281
279
availableC := getConditionByType (ms .Status .Conditions , v1alpha1 .AvailableCondition )
@@ -285,9 +283,8 @@ func (f *Framework) GetStackWhenAvailable(t *testing.T, name, namespace string)
285
283
return false , nil
286
284
})
287
285
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 ))
291
288
}
292
289
return ms
293
290
}
@@ -298,16 +295,14 @@ func (f *Framework) AssertAlertmanagerAbsent(t *testing.T, name, namespace strin
298
295
Name : name ,
299
296
Namespace : namespace ,
300
297
}
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 ) {
303
299
err := f .K8sClient .Get (context .Background (), key , & am )
304
300
if errors .IsNotFound (err ) {
305
301
return true , nil
306
302
}
307
303
return false , nil
308
304
})
309
- //nolint
310
- if err == wait .ErrWaitTimeout {
305
+ if wait .Interrupted (err ) {
311
306
t .Fatal (fmt .Errorf ("alertmanager %s/%s is present when expected to be absent" , namespace , name ))
312
307
}
313
308
}
0 commit comments