提交 41bd1de6 编写于 作者: J Joey 提交者: Balint Pato

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

* Implement a check to see if an ISO URL is valid

* Use strings.TrimPrefix instead of []rune casting for 'file://' prefix removal

* Handle file permissions error with a user-friendly message
上级 49f038a9
......@@ -90,7 +90,7 @@ var settings = []Setting{
{
name: "iso-url",
set: SetString,
validations: []setFn{IsValidURL},
validations: []setFn{IsValidURL, IsURLExists},
},
{
name: config.WantUpdateNotification,
......
......@@ -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 {
......@@ -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)
}
// 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
}
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 {
......
......@@ -16,7 +16,10 @@ limitations under the License.
package config
import "testing"
import (
"os"
"testing"
)
type validationTest struct {
value string
......@@ -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)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册