Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a check to see if an ISO URL is valid #3287

Merged
merged 3 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 38 additions & 5 deletions cmd/minikube/cmd/config/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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"
"strings"
)

func IsValidDriver(string, driver string) error {
Expand Down Expand Up @@ -59,6 +59,39 @@ 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)
tstromberg marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 := strings.TrimPrefix(location, "file://")
stat, err := os.Stat(sysPath)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s does not exist", location)
}

if os.IsPermission(err) {
return fmt.Errorf("%s could not be opened (permission error: %s)", location, err.Error())
}

return err
}

Twister915 marked this conversation as resolved.
Show resolved Hide resolved
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)
}