Skip to content

Commit

Permalink
Implement a check to see if an ISO URL is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Twister915 committed Oct 31, 2018
1 parent 8256665 commit 08a63d7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var settings = []Setting{
{
name: "iso-url",
set: SetString,
validations: []setFn{IsValidURL},
validations: []setFn{IsValidURL, IsURLExists},
},
{
name: config.WantUpdateNotification,
Expand Down
38 changes: 33 additions & 5 deletions cmd/minikube/cmd/config/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ package config

import (
"fmt"
"github.com/docker/go-units"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/constants"
"net"
"net/url"
"os"
"strconv"

units "github.com/docker/go-units"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/constants"
)

func IsValidDriver(string, driver string) error {
Expand Down Expand Up @@ -59,6 +58,35 @@ func IsValidURL(name string, location string) error {
return nil
}

func IsURLExists(name string, location string) error {
parsed, err := url.Parse(location)
if err != nil {
return fmt.Errorf("%s is not a valid URL", location)
}

// we can only validate if local files exist, not other urls
if parsed.Scheme != "file" {
return nil
}

// chop off "file://" from the location, giving us the real system path
sysPath := string([]rune(location[len("file://"):]))
stat, err := os.Stat(sysPath)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s does not exist", location)
}

return err
}

if stat.IsDir() {
return fmt.Errorf("%s is a directory", location)
}

return nil
}

func IsPositive(name string, val string) error {
i, err := strconv.Atoi(val)
if err != nil {
Expand Down
27 changes: 26 additions & 1 deletion cmd/minikube/cmd/config/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

package config

import "testing"
import (
"os"
"testing"
)

type validationTest struct {
value string
Expand Down Expand Up @@ -94,3 +97,25 @@ func TestValidCIDR(t *testing.T) {

runValidations(t, tests, "cidr", IsValidCIDR)
}

func TestIsURLExists(t *testing.T) {

self, err := os.Executable()
if err != nil {
t.Error(err)
}

tests := []validationTest{
{
value: "file://" + self,
shouldErr: false,
},

{
value: "file://" + self + "/subpath-of-file",
shouldErr: true,
},
}

runValidations(t, tests, "url", IsURLExists)
}

0 comments on commit 08a63d7

Please sign in to comment.