From fb9a781a0f46c9052be23a61e65567850fdd50eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Gouteroux?= Date: Thu, 19 Oct 2023 11:06:33 +0200 Subject: [PATCH] enhancement: config file probe type validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: François Gouteroux --- config/testdata/invalid-probe-type-config.yml | 6 +++++ main.go | 22 +++++++++++++++++++ main_test.go | 20 ++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 config/testdata/invalid-probe-type-config.yml diff --git a/config/testdata/invalid-probe-type-config.yml b/config/testdata/invalid-probe-type-config.yml new file mode 100644 index 00000000..42f1fff9 --- /dev/null +++ b/config/testdata/invalid-probe-type-config.yml @@ -0,0 +1,6 @@ +modules: + http_test: + prober: http2 + timeout: 5s + http: + body: "Test body" diff --git a/main.go b/main.go index 0d113b4b..aecc229a 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,7 @@ import ( "syscall" "github.com/alecthomas/kingpin/v2" + "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" @@ -92,6 +93,11 @@ func run() int { } if *configCheck { + err := checkModuleProbeType(sc, logger) + if err != nil { + level.Error(logger).Log("err", err) + return 1 + } level.Info(logger).Log("msg", "Config file is ok exiting...") return 0 } @@ -324,3 +330,19 @@ func computeExternalURL(u, listenAddr string) (*url.URL, error) { return eu, nil } + +func checkModuleProbeType(sc *config.SafeConfig, logger log.Logger) error { + var hasErrors bool + for name, module := range sc.C.Modules { + if _, ok := prober.Probers[module.Prober]; !ok { + if logger != nil { + level.Error(logger).Log("err", fmt.Sprintf("Unknown probe type '%s' for module name '%s'", module.Prober, name)) + } + hasErrors = true + } + } + if hasErrors { + return fmt.Errorf("Config file validation error, one or many invalid probe types found") + } + return nil +} diff --git a/main_test.go b/main_test.go index 45c9abe1..edf49f36 100644 --- a/main_test.go +++ b/main_test.go @@ -13,7 +13,11 @@ package main -import "testing" +import ( + "github.com/prometheus/blackbox_exporter/config" + "github.com/prometheus/client_golang/prometheus" + "testing" +) func TestComputeExternalURL(t *testing.T) { tests := []struct { @@ -67,3 +71,17 @@ func TestComputeExternalURL(t *testing.T) { } } } + +func TestInvalidConfigCheck(t *testing.T) { + sc := config.NewSafeConfig(prometheus.NewRegistry()) + + err := sc.ReloadConfig("config/testdata/invalid-probe-type-config.yml", nil) + if err != nil { + t.Fatal("Invalid config file 'config/testdata/invalid-probe-type-config.yml'") + } + + err = checkProbeTypeModule(sc, nil) + if err == nil { + t.Errorf("Error: test should fail") + } +}