1
0
mirror of https://github.com/openshift/installer.git synced 2026-02-05 15:47:14 +01:00

asset/installconfig: move uri validation to validation package

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
This commit is contained in:
staebler
2018-11-21 15:07:20 -05:00
parent ef9282d50e
commit d813360470
3 changed files with 44 additions and 15 deletions

View File

@@ -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 (
@@ -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))
}

View File

@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net"
"net/url"
"regexp"
"strconv"
"strings"
@@ -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
}

View File

@@ -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)
}
})
}
}