Skip to content

Commit 0405f4d

Browse files
committed
test(target-allocator): guard scrape_protocols defaulting on config load
Add a regression test asserting that loading a Target Allocator config whose static scrape job omits scrape_protocols still yields a non-empty ScrapeProtocols on every loaded scrape config. This is defaulted by the pinned Prometheus library during yaml.UnmarshalStrict into the prometheus Config type, so the distributed /scrape_configs payload is never empty and the agent's prometheus-receiver validation passes. The test fails fast if a future dependency or load-path change drops this defaulting.
1 parent 1376451 commit 0405f4d

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package config
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
// TestScrapeProtocolsDefaultedOnLoad guards against "scrape_protocols cannot be empty" regression.
14+
func TestScrapeProtocolsDefaultedOnLoad(t *testing.T) {
15+
got := CreateDefaultConfig()
16+
err := LoadFromFile("./testdata/scrape_protocols_omitted_test.yaml", &got)
17+
require.NoError(t, err)
18+
19+
require.NotNil(t, got.PromConfig)
20+
require.NotEmpty(t, got.PromConfig.ScrapeConfigs)
21+
22+
for _, sc := range got.PromConfig.ScrapeConfigs {
23+
assert.NotEmpty(t, sc.ScrapeProtocols,
24+
"scrape_protocols must be defaulted for job %q", sc.JobName)
25+
}
26+
27+
// Spot-check the specific static job from the reproduction by name.
28+
found := false
29+
for _, sc := range got.PromConfig.ScrapeConfigs {
30+
if sc.JobName == "prometheus-sample-app" {
31+
found = true
32+
assert.Greater(t, len(sc.ScrapeProtocols), 0)
33+
}
34+
}
35+
require.True(t, found, "expected the 'prometheus-sample-app' job to be present")
36+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Regression fixture: a scrape job that omits scrape_protocols.
2+
# The TA config-load path must default scrape_protocols so the agent does not
3+
# reject the config with "scrape_protocols cannot be empty".
4+
config:
5+
scrape_configs:
6+
- job_name: prometheus-sample-app
7+
kubernetes_sd_configs:
8+
- role: pod
9+
relabel_configs:
10+
- source_labels: [__meta_kubernetes_pod_label_app]
11+
regex: prometheus-sample-app
12+
action: keep
13+
- target_label: label1
14+
replacement: value1

0 commit comments

Comments
 (0)