Skip to content

Commit 0ba6441

Browse files
Merge pull request #3216 from simonpasquier/investigate-acceptance-test-failures
test: reduce flakiness of acceptance tests
2 parents ecb66f7 + 01e1f54 commit 0ba6441

File tree

4 files changed

+56
-52
lines changed

4 files changed

+56
-52
lines changed

test/cli/acceptance.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ func NewAcceptanceTest(t *testing.T, opts *AcceptanceOpts) *AcceptanceTest {
9292
opts: opts,
9393
actions: map[float64][]func(){},
9494
}
95-
// TODO: Should this really be set during creation time? Why not do this
96-
// during Run() time, maybe there is something else long happening between
97-
// creation and running.
98-
opts.baseTime = time.Now()
9995

10096
return test
10197
}
@@ -206,6 +202,10 @@ func (t *AcceptanceTest) Run() {
206202
t.T.Fatal(err)
207203
}
208204

205+
// Set the reference time right before running the test actions to avoid
206+
// test failures due to slow setup of the test environment.
207+
t.opts.baseTime = time.Now()
208+
209209
go t.runActions()
210210

211211
var latest float64

test/with_api_v1/acceptance.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func NewAcceptanceTest(t *testing.T, opts *AcceptanceOpts) *AcceptanceTest {
8080
opts: opts,
8181
actions: map[float64][]func(){},
8282
}
83-
opts.baseTime = time.Now()
8483

8584
return test
8685
}
@@ -177,6 +176,10 @@ func (t *AcceptanceTest) Run() {
177176
}(am)
178177
}
179178

179+
// Set the reference time right before running the test actions to avoid
180+
// test failures due to slow setup of the test environment.
181+
t.opts.baseTime = time.Now()
182+
180183
go t.runActions()
181184

182185
var latest float64
@@ -333,28 +336,28 @@ func (am *Alertmanager) cleanup() {
333336
// Push declares alerts that are to be pushed to the Alertmanager
334337
// server at a relative point in time.
335338
func (am *Alertmanager) Push(at float64, alerts ...*TestAlert) {
336-
var cas []APIV1Alert
337-
for i := range alerts {
338-
a := alerts[i].nativeAlert(am.opts)
339-
al := APIV1Alert{
340-
Labels: LabelSet{},
341-
Annotations: LabelSet{},
342-
StartsAt: a.StartsAt,
343-
EndsAt: a.EndsAt,
344-
GeneratorURL: a.GeneratorURL,
345-
}
346-
for n, v := range a.Labels {
347-
al.Labels[LabelName(n)] = LabelValue(v)
348-
}
349-
for n, v := range a.Annotations {
350-
al.Annotations[LabelName(n)] = LabelValue(v)
339+
am.t.Do(at, func() {
340+
var cas []APIV1Alert
341+
for i := range alerts {
342+
a := alerts[i].nativeAlert(am.opts)
343+
al := APIV1Alert{
344+
Labels: LabelSet{},
345+
Annotations: LabelSet{},
346+
StartsAt: a.StartsAt,
347+
EndsAt: a.EndsAt,
348+
GeneratorURL: a.GeneratorURL,
349+
}
350+
for n, v := range a.Labels {
351+
al.Labels[LabelName(n)] = LabelValue(v)
352+
}
353+
for n, v := range a.Annotations {
354+
al.Annotations[LabelName(n)] = LabelValue(v)
355+
}
356+
cas = append(cas, al)
351357
}
352-
cas = append(cas, al)
353-
}
354358

355-
alertAPI := NewAlertAPI(am.client)
359+
alertAPI := NewAlertAPI(am.client)
356360

357-
am.t.Do(at, func() {
358361
if err := alertAPI.Push(context.Background(), cas...); err != nil {
359362
am.t.Errorf("Error pushing %v: %s", cas, err)
360363
}

test/with_api_v2/acceptance.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ func NewAcceptanceTest(t *testing.T, opts *AcceptanceOpts) *AcceptanceTest {
8383
opts: opts,
8484
actions: map[float64][]func(){},
8585
}
86-
// TODO: Should this really be set during creation time? Why not do this
87-
// during Run() time, maybe there is something else long happening between
88-
// creation and running.
89-
opts.baseTime = time.Now()
90-
9186
return test
9287
}
9388

@@ -185,6 +180,10 @@ func (t *AcceptanceTest) Run() {
185180
t.T.Fatal(err)
186181
}
187182

183+
// Set the reference time right before running the test actions to avoid
184+
// test failures due to slow setup of the test environment.
185+
t.opts.baseTime = time.Now()
186+
188187
go t.runActions()
189188

190189
var latest float64
@@ -420,26 +419,26 @@ func (amc *AlertmanagerCluster) Push(at float64, alerts ...*TestAlert) {
420419
// Push declares alerts that are to be pushed to the Alertmanager
421420
// server at a relative point in time.
422421
func (am *Alertmanager) Push(at float64, alerts ...*TestAlert) {
423-
var cas models.PostableAlerts
424-
for i := range alerts {
425-
a := alerts[i].nativeAlert(am.opts)
426-
alert := &models.PostableAlert{
427-
Alert: models.Alert{
428-
Labels: a.Labels,
429-
GeneratorURL: a.GeneratorURL,
430-
},
431-
Annotations: a.Annotations,
432-
}
433-
if a.StartsAt != nil {
434-
alert.StartsAt = *a.StartsAt
435-
}
436-
if a.EndsAt != nil {
437-
alert.EndsAt = *a.EndsAt
422+
am.t.Do(at, func() {
423+
var cas models.PostableAlerts
424+
for i := range alerts {
425+
a := alerts[i].nativeAlert(am.opts)
426+
alert := &models.PostableAlert{
427+
Alert: models.Alert{
428+
Labels: a.Labels,
429+
GeneratorURL: a.GeneratorURL,
430+
},
431+
Annotations: a.Annotations,
432+
}
433+
if a.StartsAt != nil {
434+
alert.StartsAt = *a.StartsAt
435+
}
436+
if a.EndsAt != nil {
437+
alert.EndsAt = *a.EndsAt
438+
}
439+
cas = append(cas, alert)
438440
}
439-
cas = append(cas, alert)
440-
}
441441

442-
am.t.Do(at, func() {
443442
params := alert.PostAlertsParams{}
444443
params.WithContext(context.Background()).WithAlerts(cas)
445444

test/with_api_v2/acceptance/cluster_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,17 @@ receivers:
7777
- url: 'http://%s'
7878
`
7979

80-
acceptanceOpts := &a.AcceptanceOpts{
81-
Tolerance: 2 * time.Second,
80+
acceptanceOpts := func() *a.AcceptanceOpts {
81+
return &a.AcceptanceOpts{
82+
Tolerance: 2 * time.Second,
83+
}
8284
}
8385

8486
clusterSizes := []int{1, 3}
8587

8688
tests := []*a.AcceptanceTest{
87-
a.NewAcceptanceTest(t, acceptanceOpts),
88-
a.NewAcceptanceTest(t, acceptanceOpts),
89+
a.NewAcceptanceTest(t, acceptanceOpts()),
90+
a.NewAcceptanceTest(t, acceptanceOpts()),
8991
}
9092

9193
collectors := []*a.Collector{}
@@ -118,7 +120,7 @@ receivers:
118120

119121
wg.Wait()
120122

121-
_, err := a.CompareCollectors(collectors[0], collectors[1], acceptanceOpts)
123+
_, err := a.CompareCollectors(collectors[0], collectors[1], acceptanceOpts())
122124
if err != nil {
123125
t.Fatal(err)
124126
}

0 commit comments

Comments
 (0)