Skip to content

Commit

Permalink
asset/installconfig: move uri validation to validation package
Browse files Browse the repository at this point in the history
Move the uri validation from pkg/asset/installconfig/libvirt to pkg/validation
for consistency with other validation functions.

https://jira.coreos.com/browse/CORS-850
  • Loading branch information
staebler committed Dec 15, 2018
1 parent ef9282d commit d813360
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
17 changes: 2 additions & 15 deletions pkg/asset/installconfig/libvirt/libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package libvirt

import (
"context"
"fmt"
"net/url"

"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"

"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/types/libvirt"
"github.com/openshift/installer/pkg/validate"
)

const (
Expand Down Expand Up @@ -55,17 +54,5 @@ func Platform() (*libvirt.Platform, error) {
// uriValidator validates if the answer provided in prompt is a valid
// url and has non-empty scheme.
func uriValidator(ans interface{}) error {
return validURI(ans.(string))
}

// validURI validates if the URI is a valid URI with a non-empty scheme.
func validURI(uri string) error {
parsed, err := url.Parse(uri)
if err != nil {
return err
}
if parsed.Scheme == "" {
return fmt.Errorf("invalid URI %q (no scheme)", uri)
}
return nil
return validate.URI(ans.(string))
}
13 changes: 13 additions & 0 deletions pkg/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net"
"net/url"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -220,3 +221,15 @@ func SSHPublicKey(v string) error {
_, _, _, _, err := ssh.ParseAuthorizedKey([]byte(v))
return err
}

// URI validates if the URI is a valid absolute URI.
func URI(uri string) error {
parsed, err := url.Parse(uri)
if err != nil {
return err
}
if !parsed.IsAbs() {
return fmt.Errorf("invalid URI %q (no scheme)", uri)
}
return nil
}
29 changes: 29 additions & 0 deletions pkg/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,32 @@ func TestSSHPublicKey(t *testing.T) {
})
}
}

func TestURI(t *testing.T) {
cases := []struct {
name string
uri string
valid bool
}{
{
name: "valid",
uri: "https://example.com",
valid: true,
},
{
name: "missing scheme",
uri: "example.com",
valid: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := URI(tc.uri)
if tc.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}

0 comments on commit d813360

Please sign in to comment.