Skip to content

Commit

Permalink
receiver/prometheus: run e2e tests in parallel to avoid doubling test…
Browse files Browse the repository at this point in the history
… suite runtime

Signed-off-by: Anthony J Mirabella <[email protected]>
  • Loading branch information
Aneurysm9 committed Nov 24, 2021
1 parent 838f7a5 commit 7e04b44
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions receiver/prometheusreceiver/metrics_receiver_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ type testData struct {
validateFunc func(t *testing.T, td *testData, result []*pdata.ResourceMetrics)
}

func (t testData) clone() *testData {
return &testData{name: t.name, pages: t.pages, validateFunc: t.validateFunc}
}

// setupMockPrometheus to create a mocked prometheus based on targets, returning the server and a prometheus exporting
// config
func setupMockPrometheus(tds ...*testData) (*mockPrometheus, *promcfg.Config, error) {
Expand Down Expand Up @@ -277,6 +281,7 @@ type testExpectation func(*testing.T, *pdata.ResourceMetrics)

func doCompare(t *testing.T, name string, want pdata.AttributeMap, got *pdata.ResourceMetrics, expectations []testExpectation) {
t.Run(name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, expectedScrapeMetricCount, countScrapeMetricsRM(got))
assert.Equal(t, want.Len(), got.Resource().Attributes().Len())
for k, v := range want.AsRaw() {
Expand Down Expand Up @@ -448,7 +453,13 @@ func testComponent(t *testing.T, targets []*testData, useStartTimeMetric bool, s
pipelineType = "pdata"
}
t.Run(pipelineType, func(t *testing.T) { // 1. setup mock server
mp, cfg, err := setupMockPrometheus(targets...)
// Clone the target data to allow the tests to run in parallel
tgts := make([]*testData, len(targets))
for i, t := range targets {
tgts[i] = t.clone()
}
t.Parallel()
mp, cfg, err := setupMockPrometheus(tgts...)
require.Nilf(t, err, "Failed to create Prometheus config: %v", err)
defer mp.Close()

Expand All @@ -464,7 +475,7 @@ func testComponent(t *testing.T, targets []*testData, useStartTimeMetric bool, s
require.NoError(t, rcvr.Start(context.Background(), componenttest.NewNopHost()), "Failed to invoke Start: %v", err)
t.Cleanup(func() {
// verify state after shutdown is called
assert.Lenf(t, flattenTargets(rcvr.scrapeManager.TargetsAll()), len(targets), "expected %v targets to be running", len(targets))
assert.Lenf(t, flattenTargets(rcvr.scrapeManager.TargetsAll()), len(tgts), "expected %v targets to be running", len(tgts))
require.NoError(t, rcvr.Shutdown(context.Background()))
assert.Len(t, flattenTargets(rcvr.scrapeManager.TargetsAll()), 0, "expected scrape manager to have no targets")
})
Expand All @@ -479,8 +490,9 @@ func testComponent(t *testing.T, targets []*testData, useStartTimeMetric bool, s
assert.Equalf(t, lep, lres, "want %d targets, but got %v\n", lep, lres)

// loop to validate outputs for each targets
for _, target := range targets {
for _, target := range tgts {
t.Run(target.name, func(t *testing.T) {
t.Parallel()
validScrapes := getValidScrapes(t, pResults[target.name])
target.validateFunc(t, target, validScrapes)
})
Expand All @@ -497,8 +509,14 @@ func testComponentCustomConfig(t *testing.T, targets []*testData, cfgMut func(*p
pipelineType = "pdata"
}
t.Run(pipelineType, func(t *testing.T) {
// Clone the target data to allow the tests to run in parallel
tgts := make([]*testData, len(targets))
for i, t := range targets {
tgts[i] = t.clone()
}
t.Parallel()
ctx := context.Background()
mp, cfg, err := setupMockPrometheus(targets...)
mp, cfg, err := setupMockPrometheus(tgts...)
cfgMut(cfg)
require.Nilf(t, err, "Failed to create Prometheus config: %v", err)
defer mp.Close()
Expand All @@ -525,8 +543,9 @@ func testComponentCustomConfig(t *testing.T, targets []*testData, cfgMut func(*p
assert.Equalf(t, lep, lres, "want %d targets, but got %v\n", lep, lres)

// loop to validate outputs for each targets
for _, target := range targets {
for _, target := range tgts {
t.Run(target.name, func(t *testing.T) {
t.Parallel()
validScrapes := getValidScrapes(t, pResults[target.name])
target.validateFunc(t, target, validScrapes)
})
Expand Down

0 comments on commit 7e04b44

Please sign in to comment.