Skip to content

Commit

Permalink
enhancement: config file probe type validation
Browse files Browse the repository at this point in the history
Signed-off-by: François Gouteroux <[email protected]>
  • Loading branch information
fgouteroux committed Jun 10, 2024
1 parent a45f3e1 commit fb9a781
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config/testdata/invalid-probe-type-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
modules:
http_test:
prober: http2
timeout: 5s
http:
body: "Test body"
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
20 changes: 19 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
}

0 comments on commit fb9a781

Please sign in to comment.