Skip to content

Commit

Permalink
fix: adds tests for ip address case given
Browse files Browse the repository at this point in the history
  • Loading branch information
faiq committed Jan 4, 2024
1 parent a01439d commit 8881105
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
25 changes: 22 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -218,12 +219,30 @@ func validateAndSanitizePrismCentralInfoAddress(address string) (string, error)
if address == "" {
return "", ErrPrismAddressNotSet
}
u, err := url.Parse(address)
if err != nil {
return "", fmt.Errorf("failed to parse url from given address %w", err)
u, urlParseErr := url.Parse(address)
if urlParseErr != nil {
ipHost, ipErr := parseIP(address)
if ipErr != nil {
return "", fmt.Errorf("failed to resolve %s as url or ip addr %w", address, ipErr)

Check warning on line 226 in pkg/client/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/client.go#L226

Added line #L226 was not covered by tests
}
return ipHost, nil
}
if u.Scheme != "" || u.Port() != "" {
return u.Hostname(), nil
}
return address, nil
}

func parseIP(s string) (string, error) {
ip, _, err := net.SplitHostPort(s)
if err == nil {
return ip, nil
}

ip2 := net.ParseIP(s)
if ip2 == nil {
return "", fmt.Errorf("invalid IP %s", ip2)

Check warning on line 244 in pkg/client/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/client.go#L242-L244

Added lines #L242 - L244 were not covered by tests
}

return ip2.String(), nil

Check warning on line 247 in pkg/client/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/client.go#L247

Added line #L247 was not covered by tests
}
24 changes: 24 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ func Test_validateAndSanitizePrismCentralInfoAddress(t *testing.T) {
"",
ErrPrismAddressNotSet,
},
{
"using a IP with scheme and port",
"https://1.2.3.4:9440",
"1.2.3.4",
nil,
},
{
"using a IP with scheme",
"https://1.2.3.4",
"1.2.3.4",
nil,
},
{
"using a IP with port",
"1.2.3.4:9440",
"1.2.3.4",
nil,
},
{
"just an IP",
"1.2.3.4",
"1.2.3.4",
nil,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 8881105

Please sign in to comment.