From b48666f7e5e089ef7f7ddf820c7b54e5028e14f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C3=B6rgy=20Krajcsovits?= Date: Fri, 30 Aug 2024 19:55:30 +0200 Subject: [PATCH] More test asserts fixed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to https://github.com/prometheus/client_golang/pull/1424 Signed-off-by: György Krajcsovits --- pkg/distributor/distributor_test.go | 41 ++++--------------- pkg/frontend/v1/frontend_test.go | 5 +-- .../tenantfederation/merge_queryable_test.go | 4 +- 3 files changed, 11 insertions(+), 39 deletions(-) diff --git a/pkg/distributor/distributor_test.go b/pkg/distributor/distributor_test.go index 0c322f3ba06..3ac04c6c97b 100644 --- a/pkg/distributor/distributor_test.go +++ b/pkg/distributor/distributor_test.go @@ -418,37 +418,7 @@ func TestDistributor_MetricsCleanup(t *testing.T) { d.cleanupInactiveUser("userA") - require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(` - # HELP cortex_distributor_deduped_samples_total The total number of deduplicated samples. - # TYPE cortex_distributor_deduped_samples_total counter - - # HELP cortex_distributor_latest_seen_sample_timestamp_seconds Unix timestamp of latest received sample per user. - # TYPE cortex_distributor_latest_seen_sample_timestamp_seconds gauge - - # HELP cortex_distributor_metadata_in_total The total number of metadata the have come in to the distributor, including rejected. - # TYPE cortex_distributor_metadata_in_total counter - - # HELP cortex_distributor_non_ha_samples_received_total The total number of received samples for a user that has HA tracking turned on, but the sample didn't contain both HA labels. - # TYPE cortex_distributor_non_ha_samples_received_total counter - - # HELP cortex_distributor_received_metadata_total The total number of received metadata, excluding rejected. - # TYPE cortex_distributor_received_metadata_total counter - cortex_distributor_received_metadata_total{user="userB"} 10 - - # HELP cortex_distributor_received_samples_total The total number of received samples, excluding rejected and deduped samples. - # TYPE cortex_distributor_received_samples_total counter - cortex_distributor_received_samples_total{user="userB"} 10 - - # HELP cortex_distributor_received_exemplars_total The total number of received exemplars, excluding rejected and deduped exemplars. - # TYPE cortex_distributor_received_exemplars_total counter - cortex_distributor_received_exemplars_total{user="userB"} 10 - - # HELP cortex_distributor_samples_in_total The total number of samples that have come in to the distributor, including rejected or deduped samples. - # TYPE cortex_distributor_samples_in_total counter - - # HELP cortex_distributor_exemplars_in_total The total number of exemplars that have come in to the distributor, including rejected or deduped exemplars. - # TYPE cortex_distributor_exemplars_in_total counter - `), metrics...)) + require.ErrorContains(t, testutil.GatherAndCompare(reg, strings.NewReader(""), metrics...), "expected metric name(s) not found") } func TestDistributor_PushRequestRateLimiter(t *testing.T) { @@ -1678,7 +1648,11 @@ func TestDistributor_ExemplarValidation(t *testing.T) { } assert.Equal(t, tc.expectedExemplars, tc.req.Timeseries) - assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(tc.expectedMetrics), "cortex_discarded_exemplars_total")) + if len(tc.expectedMetrics) > 0 { + assert.NoError(t, testutil.GatherAndCompare(regs[0], strings.NewReader(tc.expectedMetrics), "cortex_discarded_exemplars_total")) + } else { + assert.ErrorContains(t, testutil.GatherAndCompare(regs[0], strings.NewReader(""), "cortex_discarded_exemplars_total"), "expected metric name(s) not found") + } }) } } @@ -7226,7 +7200,8 @@ func TestDistributor_StorageConfigMetrics(t *testing.T) { # 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. # TYPE cortex_distributor_ingest_storage_enabled gauge cortex_distributor_ingest_storage_enabled 1 - `), "cortex_distributor_replication_factor", "cortex_distributor_ingest_storage_enabled")) + `), "cortex_distributor_ingest_storage_enabled")) + assert.ErrorContains(t, testutil.GatherAndCompare(regs[0], strings.NewReader(""), "cortex_distributor_replication_factor"), "expected metric name(s) not found") }) } diff --git a/pkg/frontend/v1/frontend_test.go b/pkg/frontend/v1/frontend_test.go index f4c55b02e8b..25a5c119484 100644 --- a/pkg/frontend/v1/frontend_test.go +++ b/pkg/frontend/v1/frontend_test.go @@ -222,10 +222,7 @@ func TestFrontendMetricsCleanup(t *testing.T) { fr.cleanupInactiveUserMetrics("1") - require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(` - # HELP cortex_query_frontend_queue_length Number of queries in the queue. - # TYPE cortex_query_frontend_queue_length gauge - `), "cortex_query_frontend_queue_length")) + require.ErrorContains(t, testutil.GatherAndCompare(reg, strings.NewReader(""), "cortex_query_frontend_queue_length"), "expected metric name(s) not found") } testFrontend(t, defaultFrontendConfig(), handler, test, nil, reg) diff --git a/pkg/querier/tenantfederation/merge_queryable_test.go b/pkg/querier/tenantfederation/merge_queryable_test.go index 2e23fb5f32a..7917039942c 100644 --- a/pkg/querier/tenantfederation/merge_queryable_test.go +++ b/pkg/querier/tenantfederation/merge_queryable_test.go @@ -554,7 +554,7 @@ func TestMergeQueryable_Select(t *testing.T) { require.EqualError(t, seriesSet.Err(), tc.expectedQueryErr.Error()) } else { require.NoError(t, seriesSet.Err()) - assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(tc.expectedMetrics), "cortex_querier_federation_sample_tenants_queried")) + assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(tc.expectedMetrics), "cortex_querier_federation_tenants_queried")) assertEqualWarnings(t, tc.expectedWarnings, seriesSet.Warnings()) } @@ -709,7 +709,7 @@ func TestMergeQueryable_LabelNames(t *testing.T) { } else { require.NoError(t, err) assert.Equal(t, scenario.labelNamesTestCase.expectedLabelNames, labelNames) - assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(scenario.labelNamesTestCase.expectedMetrics), "cortex_querier_federation_sample_tenants_queried")) + assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(scenario.labelNamesTestCase.expectedMetrics), "cortex_querier_federation_tenants_queried")) assertEqualWarnings(t, scenario.labelNamesTestCase.expectedWarnings, warnings) } })