cmd/ghalistener/config: export Validate (#3870)

Co-authored-by: Han-Wen Nienhuys <hanwenn@gmail.com>
This commit is contained in:
Han-Wen Nienhuys 2025-01-17 12:25:33 +01:00 committed by GitHub
parent 66172ab0bd
commit f673a085b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 8 deletions

View File

@ -46,14 +46,15 @@ func Read(path string) (Config, error) {
return Config{}, fmt.Errorf("failed to decode config: %w", err) return Config{}, fmt.Errorf("failed to decode config: %w", err)
} }
if err := config.validate(); err != nil { if err := config.Validate(); err != nil {
return Config{}, fmt.Errorf("failed to validate config: %w", err) return Config{}, fmt.Errorf("failed to validate config: %w", err)
} }
return config, nil return config, nil
} }
func (c *Config) validate() error { // Validate checks the configuration for errors.
func (c *Config) Validate() error {
if len(c.ConfigureUrl) == 0 { if len(c.ConfigureUrl) == 0 {
return fmt.Errorf("GitHubConfigUrl is not provided") return fmt.Errorf("GitHubConfigUrl is not provided")
} }

View File

@ -17,7 +17,7 @@ func TestConfigValidationMinMax(t *testing.T) {
MaxRunners: 2, MaxRunners: 2,
Token: "token", Token: "token",
} }
err := config.validate() err := config.Validate()
assert.ErrorContains(t, err, "MinRunners '5' cannot be greater than MaxRunners '2", "Expected error about MinRunners > MaxRunners") assert.ErrorContains(t, err, "MinRunners '5' cannot be greater than MaxRunners '2", "Expected error about MinRunners > MaxRunners")
} }
@ -28,7 +28,7 @@ func TestConfigValidationMissingToken(t *testing.T) {
EphemeralRunnerSetName: "deployment", EphemeralRunnerSetName: "deployment",
RunnerScaleSetId: 1, RunnerScaleSetId: 1,
} }
err := config.validate() err := config.Validate()
expectedError := fmt.Sprintf("GitHub auth credential is missing, token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(config.Token), config.AppID, config.AppInstallationID, len(config.AppPrivateKey)) expectedError := fmt.Sprintf("GitHub auth credential is missing, token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(config.Token), config.AppID, config.AppInstallationID, len(config.AppPrivateKey))
assert.ErrorContains(t, err, expectedError, "Expected error about missing auth") assert.ErrorContains(t, err, expectedError, "Expected error about missing auth")
} }
@ -42,7 +42,7 @@ func TestConfigValidationAppKey(t *testing.T) {
EphemeralRunnerSetName: "deployment", EphemeralRunnerSetName: "deployment",
RunnerScaleSetId: 1, RunnerScaleSetId: 1,
} }
err := config.validate() err := config.Validate()
expectedError := fmt.Sprintf("GitHub auth credential is missing, token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(config.Token), config.AppID, config.AppInstallationID, len(config.AppPrivateKey)) expectedError := fmt.Sprintf("GitHub auth credential is missing, token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(config.Token), config.AppID, config.AppInstallationID, len(config.AppPrivateKey))
assert.ErrorContains(t, err, expectedError, "Expected error about missing auth") assert.ErrorContains(t, err, expectedError, "Expected error about missing auth")
} }
@ -58,7 +58,7 @@ func TestConfigValidationOnlyOneTypeOfCredentials(t *testing.T) {
EphemeralRunnerSetName: "deployment", EphemeralRunnerSetName: "deployment",
RunnerScaleSetId: 1, RunnerScaleSetId: 1,
} }
err := config.validate() err := config.Validate()
expectedError := fmt.Sprintf("only one GitHub auth method supported at a time. Have both PAT and App auth: token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(config.Token), config.AppID, config.AppInstallationID, len(config.AppPrivateKey)) expectedError := fmt.Sprintf("only one GitHub auth method supported at a time. Have both PAT and App auth: token length: '%d', appId: '%d', installationId: '%d', private key length: '%d", len(config.Token), config.AppID, config.AppInstallationID, len(config.AppPrivateKey))
assert.ErrorContains(t, err, expectedError, "Expected error about missing auth") assert.ErrorContains(t, err, expectedError, "Expected error about missing auth")
} }
@ -74,7 +74,7 @@ func TestConfigValidation(t *testing.T) {
Token: "asdf", Token: "asdf",
} }
err := config.validate() err := config.Validate()
assert.NoError(t, err, "Expected no error") assert.NoError(t, err, "Expected no error")
} }
@ -86,7 +86,7 @@ func TestConfigValidationConfigUrl(t *testing.T) {
RunnerScaleSetId: 1, RunnerScaleSetId: 1,
} }
err := config.validate() err := config.Validate()
assert.ErrorContains(t, err, "GitHubConfigUrl is not provided", "Expected error about missing ConfigureUrl") assert.ErrorContains(t, err, "GitHubConfigUrl is not provided", "Expected error about missing ConfigureUrl")
} }