Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test (validations) : Add some unit tests around proxy validation (#4411) #4580

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions pkg/crc/config/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,86 @@ func TestValidationPreset_WhenOKDProvidedOnArmArchitecture_thenValidationFailure
assert.Equal(t, false, validationPass)
assert.Equal(t, fmt.Sprintf("preset 'okd' is not supported on %s architecture, please use different preset value", runtime.GOARCH), validationMessage)
}

func TestValidateHTTPProxy(t *testing.T) {
tests := []struct {
name string
noProxyValue string
expectedValidationResult bool
expectedValidationStr string
}{
{"empty value", "", true, ""},
{"valid http url", "http://proxy.example.com", true, ""},
{"valid https url", "https://proxy.example.com", false, "HTTP proxy URL 'https://proxy.example.com' is not valid: url should start with http://"},
{"valid socks5 url", "socks5://proxy.example.com", false, "HTTP proxy URL 'socks5://proxy.example.com' is not valid: url should start with http://"},
{"type in http scheme", "htp://proxy.example.com", false, "HTTP proxy URL 'htp://proxy.example.com' is not valid: url should start with http://"},
{"no scheme", "proxy.example.com", false, "HTTP proxy URL 'proxy.example.com' is not valid: url should start with http://"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualValidationResult, actualValidationStr := validateHTTPProxy(tt.noProxyValue)
if actualValidationStr != tt.expectedValidationStr {
t.Errorf("validateHTTPProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationStr, tt.expectedValidationStr)
}
if actualValidationResult != tt.expectedValidationResult {
t.Errorf("validateHTTPProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationResult, tt.expectedValidationResult)
}
})
}
}

func TestValidateHTTPSProxy(t *testing.T) {
tests := []struct {
name string
noProxyValue string
expectedValidationResult bool
expectedValidationStr string
}{
{"empty value", "", true, ""},
{"valid https url", "https://proxy.example.com", true, ""},
{"valid http url", "http://proxy.example.com", true, ""},
{"valid socks5 url", "socks5://proxy.example.com", false, "HTTPS proxy URL 'socks5://proxy.example.com' is not valid: url should start with http:// or https://"},
{"type in https scheme", "htps://proxy.example.com", false, "HTTPS proxy URL 'htps://proxy.example.com' is not valid: url should start with http:// or https://"},
{"no scheme", "proxy.example.com", false, "HTTPS proxy URL 'proxy.example.com' is not valid: url should start with http:// or https://"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualValidationResult, actualValidationStr := validateHTTPSProxy(tt.noProxyValue)
if actualValidationStr != tt.expectedValidationStr {
t.Errorf("validateHTTPSProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationStr, tt.expectedValidationStr)
}
if actualValidationResult != tt.expectedValidationResult {
t.Errorf("validateHTTPSProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationResult, tt.expectedValidationResult)
}
})
}
}

func TestValidateNoProxy(t *testing.T) {
tests := []struct {
name string
noProxyValue string
expectedValidationResult bool
expectedValidationStr string
}{
{"empty value", "", true, ""},
{"valid single", "example.com", true, ""},
{"valid multiple", "localhost,127.0.0.1,example.com", true, ""},
{"space in single entry", "example .com", false, "NoProxy string can't contain spaces"},
{"space in between multiple entries", "localhost, , example.com", false, "NoProxy string can't contain spaces"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualValidationResult, actualValidationStr := validateNoProxy(tt.noProxyValue)
if actualValidationStr != tt.expectedValidationStr {
t.Errorf("validateNoProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationStr, tt.expectedValidationStr)
}
if actualValidationResult != tt.expectedValidationResult {
t.Errorf("validateNoProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationResult, tt.expectedValidationResult)
}
})
}
}
Loading