From c9f3d9b47f374fc2e3a1567d5c0b61b196487885 Mon Sep 17 00:00:00 2001 From: machine424 Date: Tue, 3 Dec 2024 19:41:45 +0100 Subject: [PATCH] doc(nomad): adjust sections about nomad_sd_config's server test(nomad): extend TestConfiguredService with more valid/invalid servers configs fixes https://github.com/prometheus/prometheus/issues/12306 Signed-off-by: machine424 --- discovery/nomad/nomad_test.go | 36 +++++++++++++++++++++-------- docs/configuration/configuration.md | 3 ++- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/discovery/nomad/nomad_test.go b/discovery/nomad/nomad_test.go index 32b087524..c08f01749 100644 --- a/discovery/nomad/nomad_test.go +++ b/discovery/nomad/nomad_test.go @@ -127,19 +127,37 @@ func (m *SDMock) HandleServiceHashiCupsGet() { } func TestConfiguredService(t *testing.T) { - conf := &SDConfig{ - Server: "http://localhost:4646", + testCases := []struct { + name string + server string + acceptedURL bool + }{ + {"invalid hostname URL", "http://foo.bar:4646", true}, + {"invalid even though accepted by parsing", "foo.bar:4646", true}, + {"valid address URL", "http://172.30.29.23:4646", true}, + {"invalid URL", "172.30.29.23:4646", false}, } - reg := prometheus.NewRegistry() - refreshMetrics := discovery.NewRefreshMetrics(reg) - metrics := conf.NewDiscovererMetrics(reg, refreshMetrics) - require.NoError(t, metrics.Register()) + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + conf := &SDConfig{ + Server: tc.server, + } - _, err := NewDiscovery(conf, nil, metrics) - require.NoError(t, err) + reg := prometheus.NewRegistry() + refreshMetrics := discovery.NewRefreshMetrics(reg) + metrics := conf.NewDiscovererMetrics(reg, refreshMetrics) + require.NoError(t, metrics.Register()) + defer metrics.Unregister() - metrics.Unregister() + _, err := NewDiscovery(conf, nil, metrics) + if tc.acceptedURL { + require.NoError(t, err) + } else { + require.Error(t, err) + } + }) + } } func TestNomadSDRefresh(t *testing.T) { diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 32da2c61c..746360633 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -2130,7 +2130,8 @@ The following meta labels are available on targets during [relabeling](#relabel_ [ namespace: | default = default ] [ refresh_interval: | default = 60s ] [ region: | default = global ] -[ server: ] +# The URL to connect to the API. +[ server: ] [ tag_separator: | default = ,] # HTTP client settings, including authentication methods (such as basic auth and