Skip to content

Commit 44c5c37

Browse files
committed
Fix test asserts
Have a function that can emulate the old GatherAndCompare Follow up to prometheus/client_golang#1424 Signed-off-by: György Krajcsovits <[email protected]>
1 parent 86ff8bd commit 44c5c37

File tree

9 files changed

+106
-72
lines changed

9 files changed

+106
-72
lines changed

pkg/compactor/blocks_cleaner_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,7 @@ func TestBlocksCleaner_ShouldNotCleanupUserThatDoesntBelongToShardAnymore(t *tes
464464
require.ElementsMatch(t, []string{"user-1", "user-2"}, cleaner.lastOwnedUsers)
465465

466466
// But there are no metrics for any user, because we did not in fact clean them.
467-
assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
468-
# HELP cortex_bucket_blocks_count Total number of blocks in the bucket. Includes blocks marked for deletion, but not partial blocks.
469-
# TYPE cortex_bucket_blocks_count gauge
470-
`),
471-
"cortex_bucket_blocks_count",
472-
))
467+
test.AssertGatherAndCompare(t, reg, "", "cortex_bucket_blocks_count")
473468

474469
// Running cleanUsers again will see that users are no longer owned.
475470
require.NoError(t, cleaner.runCleanupWithErr(ctx))

pkg/compactor/compactor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ func TestMultitenantCompactor_ShouldNotCompactBlocksForUsersMarkedForDeletion(t
967967
"cortex_compactor_block_cleanup_started_total", "cortex_compactor_block_cleanup_completed_total", "cortex_compactor_block_cleanup_failed_total",
968968
"cortex_bucket_blocks_count", "cortex_bucket_blocks_marked_for_deletion_count", "cortex_bucket_index_last_successful_update_timestamp_seconds",
969969
}
970-
assert.NoError(t, prom_testutil.GatherAndCompare(registry, strings.NewReader(`
970+
testutil.AssertGatherAndCompare(t, registry, `
971971
# TYPE cortex_compactor_runs_started_total counter
972972
# HELP cortex_compactor_runs_started_total Total number of compaction runs started.
973973
cortex_compactor_runs_started_total 1
@@ -1006,7 +1006,7 @@ func TestMultitenantCompactor_ShouldNotCompactBlocksForUsersMarkedForDeletion(t
10061006
# TYPE cortex_compactor_block_cleanup_failed_total counter
10071007
# HELP cortex_compactor_block_cleanup_failed_total Total number of blocks cleanup runs failed.
10081008
cortex_compactor_block_cleanup_failed_total 0
1009-
`), testedMetrics...))
1009+
`, testedMetrics...)
10101010
}
10111011

10121012
func TestMultitenantCompactor_ShouldCompactAllUsersOnShardingEnabledButOnlyOneInstanceRunning(t *testing.T) {

pkg/distributor/distributor_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func TestDistributor_MetricsCleanup(t *testing.T) {
375375
d.dedupedSamples.WithLabelValues("userA", "cluster1").Inc() // We cannot clean this metric
376376
d.latestSeenSampleTimestampPerUser.WithLabelValues("userA").Set(1111)
377377

378-
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
378+
util_test.AssertGatherAndCompare(t, reg, `
379379
# HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples.
380380
# TYPE cortex_distributor_deduped_samples_total counter
381381
cortex_distributor_deduped_samples_total{cluster="cluster1",user="userA"} 1
@@ -414,11 +414,11 @@ func TestDistributor_MetricsCleanup(t *testing.T) {
414414
# HELP cortex_distributor_exemplars_in_total The total number of exemplars that have come in to the distributor, including rejected or deduped exemplars.
415415
# TYPE cortex_distributor_exemplars_in_total counter
416416
cortex_distributor_exemplars_in_total{user="userA"} 5
417-
`), metrics...))
417+
`, metrics...)
418418

419419
d.cleanupInactiveUser("userA")
420420

421-
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
421+
util_test.AssertGatherAndCompare(t, reg, `
422422
# HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples.
423423
# TYPE cortex_distributor_deduped_samples_total counter
424424
@@ -448,7 +448,7 @@ func TestDistributor_MetricsCleanup(t *testing.T) {
448448
449449
# HELP cortex_distributor_exemplars_in_total The total number of exemplars that have come in to the distributor, including rejected or deduped exemplars.
450450
# TYPE cortex_distributor_exemplars_in_total counter
451-
`), metrics...))
451+
`, metrics...)
452452
}
453453

454454
func TestDistributor_PushRequestRateLimiter(t *testing.T) {
@@ -1678,7 +1678,7 @@ func TestDistributor_ExemplarValidation(t *testing.T) {
16781678
}
16791679

16801680
assert.Equal(t, tc.expectedExemplars, tc.req.Timeseries)
1681-
assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(tc.expectedMetrics), "cortex_discarded_exemplars_total"))
1681+
util_test.AssertGatherAndCompare(t, regs[0], tc.expectedMetrics, "cortex_discarded_exemplars_total")
16821682
})
16831683
}
16841684
}
@@ -7181,15 +7181,15 @@ func TestDistributor_StorageConfigMetrics(t *testing.T) {
71817181
happyIngesters: 3,
71827182
replicationFactor: 3,
71837183
})
7184-
assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(`
7184+
util_test.AssertGatherAndCompare(t, regs[0], `
71857185
# HELP cortex_distributor_replication_factor The configured replication factor.
71867186
# TYPE cortex_distributor_replication_factor gauge
71877187
cortex_distributor_replication_factor 3
71887188
71897189
# HELP cortex_distributor_ingest_storage_enabled Whether writes are being processed via ingest storage. Equal to 1 if ingest storage is enabled, 0 if disabled.
71907190
# TYPE cortex_distributor_ingest_storage_enabled gauge
71917191
cortex_distributor_ingest_storage_enabled 0
7192-
`), "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled"))
7192+
`, "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled")
71937193
})
71947194

71957195
t.Run("migration to ingest storage", func(t *testing.T) {
@@ -7202,15 +7202,15 @@ func TestDistributor_StorageConfigMetrics(t *testing.T) {
72027202
happyIngesters: 3,
72037203
replicationFactor: 3,
72047204
})
7205-
assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(`
7205+
util_test.AssertGatherAndCompare(t, regs[0], `
72067206
# HELP cortex_distributor_replication_factor The configured replication factor.
72077207
# TYPE cortex_distributor_replication_factor gauge
72087208
cortex_distributor_replication_factor 3
72097209
72107210
# HELP cortex_distributor_ingest_storage_enabled Whether writes are being processed via ingest storage. Equal to 1 if ingest storage is enabled, 0 if disabled.
72117211
# TYPE cortex_distributor_ingest_storage_enabled gauge
72127212
cortex_distributor_ingest_storage_enabled 1
7213-
`), "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled"))
7213+
`, "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled")
72147214
})
72157215

72167216
t.Run("ingest storage", func(t *testing.T) {
@@ -7222,11 +7222,11 @@ func TestDistributor_StorageConfigMetrics(t *testing.T) {
72227222
happyIngesters: 3,
72237223
replicationFactor: 3,
72247224
})
7225-
assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(`
7225+
util_test.AssertGatherAndCompare(t, regs[0], `
72267226
# HELP cortex_distributor_ingest_storage_enabled Whether writes are being processed via ingest storage. Equal to 1 if ingest storage is enabled, 0 if disabled.
72277227
# TYPE cortex_distributor_ingest_storage_enabled gauge
72287228
cortex_distributor_ingest_storage_enabled 1
7229-
`), "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled"))
7229+
`, "cortex_distributor_ingest_storage_enabled")
72307230
})
72317231
}
72327232

pkg/frontend/v1/frontend_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/grafana/mimir/pkg/querier/stats"
4141
querier_worker "github.com/grafana/mimir/pkg/querier/worker"
4242
"github.com/grafana/mimir/pkg/scheduler/queue"
43+
util_test "github.com/grafana/mimir/pkg/util/test"
4344
)
4445

4546
const (
@@ -222,10 +223,7 @@ func TestFrontendMetricsCleanup(t *testing.T) {
222223

223224
fr.cleanupInactiveUserMetrics("1")
224225

225-
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
226-
# HELP cortex_query_frontend_queue_length Number of queries in the queue.
227-
# TYPE cortex_query_frontend_queue_length gauge
228-
`), "cortex_query_frontend_queue_length"))
226+
util_test.AssertGatherAndCompare(t, reg, "", "cortex_query_frontend_queue_length")
229227
}
230228

231229
testFrontend(t, defaultFrontendConfig(), handler, test, nil, reg)

0 commit comments

Comments
 (0)