diff --git a/config/config.go b/config/config.go index 59eeee9f..b749833c 100644 --- a/config/config.go +++ b/config/config.go @@ -24,6 +24,7 @@ import ( "reflect" "regexp" "runtime" + "slices" "sort" "strconv" "strings" @@ -350,7 +351,7 @@ func (s *Module) UnmarshalYAML(unmarshal func(interface{}) error) error { if err := unmarshal((*plain)(s)); err != nil { return err } - return nil + return s.validate() } // UnmarshalJSON implements the json.Unmarshaler interface. @@ -390,7 +391,7 @@ func (s *Module) UnmarshalJSON(data []byte) error { return err } default: - return fmt.Errorf("invalid duration: %#v", tmp) + return fmt.Errorf("invalid duration '%#v'", tmp) } } *s = Module{ @@ -402,6 +403,13 @@ func (s *Module) UnmarshalJSON(data []byte) error { DNS: tmp.DNS, GRPC: tmp.GRPC, } + return s.validate() +} + +func (s *Module) validate() error { + if !slices.Contains([]string{"http", "tcp", "icmp", "dns", "grpc"}, s.Prober) { + return fmt.Errorf("prober '%s' is invalid", s.Prober) + } return nil } diff --git a/config/config_test.go b/config/config_test.go index 60cd54f7..34c86fdd 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -224,6 +224,11 @@ func TestLoadBadConfigs(t *testing.T) { want: `error parsing config file: setting body and body_file both are not allowed`, format: []string{"yml", "json"}, }, + { + input: "testdata/invalid-module-prober", + want: `error parsing config file: invalid prober 'hTTp'`, + format: []string{"yml", "json"}, + }, } for _, test := range tests { require.NoError(t, yamlToJson(t, test.input)) diff --git a/config/testdata/invalid-module-prober.yml b/config/testdata/invalid-module-prober.yml new file mode 100644 index 00000000..0b8e17b6 --- /dev/null +++ b/config/testdata/invalid-module-prober.yml @@ -0,0 +1,5 @@ +modules: + http_2xx: + prober: hTTp + timeout: 5s + http: