Skip to content

Commit

Permalink
Merge pull request #5624 from nanikjava/f-fix-4883
Browse files Browse the repository at this point in the history
Add validation checking for minikube profile
  • Loading branch information
tstromberg authored Oct 21, 2019
2 parents 523db20 + 824c911 commit c883382
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions cmd/minikube/cmd/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ var ProfileCmd = &cobra.Command{
}

profile := args[0]
/**
we need to add code over here to check whether the profile
name is in the list of reserved keywords
*/
if pkgConfig.ProfileNameInReservedKeywords(profile) {
out.ErrT(out.FailureType, `Profile name "{{.profilename}}" is minikube keyword. To delete profile use command minikube delete -p <profile name> `, out.V{"profilename": profile})
os.Exit(0)
}

if profile == "default" {
profile = "minikube"
} else {
Expand Down
14 changes: 13 additions & 1 deletion pkg/minikube/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/golang/glog"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/util/lock"
)

var keywords = []string{"start", "stop", "status", "delete", "config", "open", "profile", "addons", "cache", "logs"}

// IsValid checks if the profile has the essential info needed for a profile
func (p *Profile) IsValid() bool {
if p.Config == nil {
return false
}

if p.Config.MachineConfig.VMDriver == "" {
return false
}
Expand All @@ -42,6 +44,16 @@ func (p *Profile) IsValid() bool {
return true
}

// check if the profile is an internal keywords
func ProfileNameInReservedKeywords(name string) bool {
for _, v := range keywords {
if strings.EqualFold(v, name) {
return true
}
}
return false
}

// ProfileExists returns true if there is a profile config (regardless of being valid)
func ProfileExists(name string, miniHome ...string) bool {
miniPath := localpath.MiniPath()
Expand Down
26 changes: 26 additions & 0 deletions pkg/minikube/config/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ func TestListProfiles(t *testing.T) {
}
}

func TestProfileNameInReservedKeywords(t *testing.T) {
var testCases = []struct {
name string
expected bool
}{
{"start", true},
{"stop", true},
{"status", true},
{"delete", true},
{"config", true},
{"open", true},
{"profile", true},
{"addons", true},
{"cache", true},
{"logs", true},
{"myprofile", false},
{"log", false},
}
for _, tt := range testCases {
got := ProfileNameInReservedKeywords(tt.name)
if got != tt.expected {
t.Errorf("expected ProfileNameInReservedKeywords(%s)=%t but got %t ", tt.name, tt.expected, got)
}
}
}

func TestProfileExists(t *testing.T) {
miniDir, err := filepath.Abs("./testdata/.minikube2")
if err != nil {
Expand Down

0 comments on commit c883382

Please sign in to comment.