Skip to content

Commit

Permalink
add tests for ValidateConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Świątek committed May 24, 2023
1 parent 9c2cbf6 commit c09cce3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/otel-allocator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func ParseCLI() (CLIConfig, error) {

// ValidateConfig validates the cli and file configs together.
func ValidateConfig(config *Config, cliConfig *CLIConfig) error {
scrapeConfigsPresent := (config.Config != nil || len(config.Config.ScrapeConfigs) > 0)
scrapeConfigsPresent := (config.Config != nil && len(config.Config.ScrapeConfigs) > 0)
if !(*cliConfig.PromCRWatcherConf.Enabled || scrapeConfigsPresent) {
return fmt.Errorf("at least one scrape config must be defined, or Prometheus CR watching must be enabled")
}
Expand Down
53 changes: 53 additions & 0 deletions cmd/otel-allocator/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,56 @@ func TestLoad(t *testing.T) {
})
}
}

func TestValidateConfig(t *testing.T) {
enabled := true
disabled := false
testCases := []struct {
name string
cliConfig CLIConfig
fileConfig Config
expectedErr error
}{
{
name: "promCR enabled, no Prometheus config",
cliConfig: CLIConfig{PromCRWatcherConf: PrometheusCRWatcherConfig{Enabled: &enabled}},
fileConfig: Config{Config: nil},
expectedErr: nil,
},
{
name: "promCR disabled, no Prometheus config",
cliConfig: CLIConfig{PromCRWatcherConf: PrometheusCRWatcherConfig{Enabled: &disabled}},
fileConfig: Config{Config: nil},
expectedErr: fmt.Errorf("at least one scrape config must be defined, or Prometheus CR watching must be enabled"),
},
{
name: "promCR disabled, Prometheus config present, no scrapeConfigs",
cliConfig: CLIConfig{PromCRWatcherConf: PrometheusCRWatcherConfig{Enabled: &disabled}},
fileConfig: Config{Config: &promconfig.Config{}},
expectedErr: fmt.Errorf("at least one scrape config must be defined, or Prometheus CR watching must be enabled"),
},
{
name: "promCR disabled, Prometheus config present, scrapeConfigs present",
cliConfig: CLIConfig{PromCRWatcherConf: PrometheusCRWatcherConfig{Enabled: &disabled}},
fileConfig: Config{
Config: &promconfig.Config{ScrapeConfigs: []*promconfig.ScrapeConfig{{}}},
},
expectedErr: nil,
},
{
name: "promCR enabled, Prometheus config present, scrapeConfigs present",
cliConfig: CLIConfig{PromCRWatcherConf: PrometheusCRWatcherConfig{Enabled: &enabled}},
fileConfig: Config{
Config: &promconfig.Config{ScrapeConfigs: []*promconfig.ScrapeConfig{{}}},
},
expectedErr: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := ValidateConfig(&tc.fileConfig, &tc.cliConfig)
assert.Equal(t, tc.expectedErr, err)
})
}
}

0 comments on commit c09cce3

Please sign in to comment.