Skip to content

Commit

Permalink
Merge pull request #3216 from simonpasquier/investigate-acceptance-te…
Browse files Browse the repository at this point in the history
…st-failures

test: reduce flakiness of acceptance tests
  • Loading branch information
simonpasquier authored Jan 18, 2023
2 parents ecb66f7 + 01e1f54 commit 0ba6441
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 52 deletions.
8 changes: 4 additions & 4 deletions test/cli/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ func NewAcceptanceTest(t *testing.T, opts *AcceptanceOpts) *AcceptanceTest {
opts: opts,
actions: map[float64][]func(){},
}
// TODO: Should this really be set during creation time? Why not do this
// during Run() time, maybe there is something else long happening between
// creation and running.
opts.baseTime = time.Now()

return test
}
Expand Down Expand Up @@ -206,6 +202,10 @@ func (t *AcceptanceTest) Run() {
t.T.Fatal(err)
}

// Set the reference time right before running the test actions to avoid
// test failures due to slow setup of the test environment.
t.opts.baseTime = time.Now()

go t.runActions()

var latest float64
Expand Down
43 changes: 23 additions & 20 deletions test/with_api_v1/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func NewAcceptanceTest(t *testing.T, opts *AcceptanceOpts) *AcceptanceTest {
opts: opts,
actions: map[float64][]func(){},
}
opts.baseTime = time.Now()

return test
}
Expand Down Expand Up @@ -177,6 +176,10 @@ func (t *AcceptanceTest) Run() {
}(am)
}

// Set the reference time right before running the test actions to avoid
// test failures due to slow setup of the test environment.
t.opts.baseTime = time.Now()

go t.runActions()

var latest float64
Expand Down Expand Up @@ -333,28 +336,28 @@ func (am *Alertmanager) cleanup() {
// Push declares alerts that are to be pushed to the Alertmanager
// server at a relative point in time.
func (am *Alertmanager) Push(at float64, alerts ...*TestAlert) {
var cas []APIV1Alert
for i := range alerts {
a := alerts[i].nativeAlert(am.opts)
al := APIV1Alert{
Labels: LabelSet{},
Annotations: LabelSet{},
StartsAt: a.StartsAt,
EndsAt: a.EndsAt,
GeneratorURL: a.GeneratorURL,
}
for n, v := range a.Labels {
al.Labels[LabelName(n)] = LabelValue(v)
}
for n, v := range a.Annotations {
al.Annotations[LabelName(n)] = LabelValue(v)
am.t.Do(at, func() {
var cas []APIV1Alert
for i := range alerts {
a := alerts[i].nativeAlert(am.opts)
al := APIV1Alert{
Labels: LabelSet{},
Annotations: LabelSet{},
StartsAt: a.StartsAt,
EndsAt: a.EndsAt,
GeneratorURL: a.GeneratorURL,
}
for n, v := range a.Labels {
al.Labels[LabelName(n)] = LabelValue(v)
}
for n, v := range a.Annotations {
al.Annotations[LabelName(n)] = LabelValue(v)
}
cas = append(cas, al)
}
cas = append(cas, al)
}

alertAPI := NewAlertAPI(am.client)
alertAPI := NewAlertAPI(am.client)

am.t.Do(at, func() {
if err := alertAPI.Push(context.Background(), cas...); err != nil {
am.t.Errorf("Error pushing %v: %s", cas, err)
}
Expand Down
45 changes: 22 additions & 23 deletions test/with_api_v2/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ func NewAcceptanceTest(t *testing.T, opts *AcceptanceOpts) *AcceptanceTest {
opts: opts,
actions: map[float64][]func(){},
}
// TODO: Should this really be set during creation time? Why not do this
// during Run() time, maybe there is something else long happening between
// creation and running.
opts.baseTime = time.Now()

return test
}

Expand Down Expand Up @@ -185,6 +180,10 @@ func (t *AcceptanceTest) Run() {
t.T.Fatal(err)
}

// Set the reference time right before running the test actions to avoid
// test failures due to slow setup of the test environment.
t.opts.baseTime = time.Now()

go t.runActions()

var latest float64
Expand Down Expand Up @@ -420,26 +419,26 @@ func (amc *AlertmanagerCluster) Push(at float64, alerts ...*TestAlert) {
// Push declares alerts that are to be pushed to the Alertmanager
// server at a relative point in time.
func (am *Alertmanager) Push(at float64, alerts ...*TestAlert) {
var cas models.PostableAlerts
for i := range alerts {
a := alerts[i].nativeAlert(am.opts)
alert := &models.PostableAlert{
Alert: models.Alert{
Labels: a.Labels,
GeneratorURL: a.GeneratorURL,
},
Annotations: a.Annotations,
}
if a.StartsAt != nil {
alert.StartsAt = *a.StartsAt
}
if a.EndsAt != nil {
alert.EndsAt = *a.EndsAt
am.t.Do(at, func() {
var cas models.PostableAlerts
for i := range alerts {
a := alerts[i].nativeAlert(am.opts)
alert := &models.PostableAlert{
Alert: models.Alert{
Labels: a.Labels,
GeneratorURL: a.GeneratorURL,
},
Annotations: a.Annotations,
}
if a.StartsAt != nil {
alert.StartsAt = *a.StartsAt
}
if a.EndsAt != nil {
alert.EndsAt = *a.EndsAt
}
cas = append(cas, alert)
}
cas = append(cas, alert)
}

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

Expand Down
12 changes: 7 additions & 5 deletions test/with_api_v2/acceptance/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ receivers:
- url: 'http://%s'
`

acceptanceOpts := &a.AcceptanceOpts{
Tolerance: 2 * time.Second,
acceptanceOpts := func() *a.AcceptanceOpts {
return &a.AcceptanceOpts{
Tolerance: 2 * time.Second,
}
}

clusterSizes := []int{1, 3}

tests := []*a.AcceptanceTest{
a.NewAcceptanceTest(t, acceptanceOpts),
a.NewAcceptanceTest(t, acceptanceOpts),
a.NewAcceptanceTest(t, acceptanceOpts()),
a.NewAcceptanceTest(t, acceptanceOpts()),
}

collectors := []*a.Collector{}
Expand Down Expand Up @@ -118,7 +120,7 @@ receivers:

wg.Wait()

_, err := a.CompareCollectors(collectors[0], collectors[1], acceptanceOpts)
_, err := a.CompareCollectors(collectors[0], collectors[1], acceptanceOpts())
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 0ba6441

Please sign in to comment.