Skip to content

Commit

Permalink
test (validations) : Add some unit tests around proxy validation (#4411)
Browse files Browse the repository at this point in the history
Add some unit tests to test various values provided to these validation methods:
- validateHTTPProxy
- validateHTTPSProxy
- validateNoProxy

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia authored and praveenkumar committed Jan 23, 2025
1 parent 4c83d7a commit ab8cf0b
Showing 1 changed file with 83 additions and 0 deletions.
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)
}
})
}
}

0 comments on commit ab8cf0b

Please sign in to comment.