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

Warn when a user tries to set a profile name that is unlikely to be valid #4999

Merged
merged 6 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions cmd/minikube/cmd/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/config"
pkgConfig "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
Expand Down Expand Up @@ -226,3 +227,31 @@ func EnableOrDisableStorageClasses(name, val string) error {

return EnableOrDisableAddon(name, val)
}

//ValidateProfile checks if the profile user is trying to switch exists, else throws error
bhanu-lab marked this conversation as resolved.
Show resolved Hide resolved
func ValidateProfile(profile string) error {
var profileFound bool
bhanu-lab marked this conversation as resolved.
Show resolved Hide resolved
validProfiles, invalidProfiles, err := pkgConfig.ListProfiles()
if err != nil {
exit.WithError("Not able to retrieve profile list", err)
bhanu-lab marked this conversation as resolved.
Show resolved Hide resolved
}

// handling invalid profiles
for _, invalidProf := range invalidProfiles {
if profile == invalidProf.Name {
return errors.New("You are trying to switch to invalid profile")
bhanu-lab marked this conversation as resolved.
Show resolved Hide resolved
}
}

// valid profiles if found, setting profileFound to true
for _, prof := range validProfiles {
if prof.Name == profile {
profileFound = true
break
}
}
if !profileFound {
return errors.New("Profile you are trying to switch is not found")
bhanu-lab marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}
24 changes: 24 additions & 0 deletions cmd/minikube/cmd/config/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package config

import (
"fmt"
"testing"

"k8s.io/minikube/pkg/minikube/assets"
Expand Down Expand Up @@ -111,3 +112,26 @@ func TestIsAddonAlreadySet(t *testing.T) {
}
}
}

func TestValidateProfile(t *testing.T) {
testCases := []struct {
profileName string
expectErr string
}{
{
profileName: "not_so_common",
expectErr: "Profile you are trying to switch is not found",
},
}

for _, test := range testCases {
profileNam := test.profileName
expectedMsg := test.expectErr

err := ValidateProfile(profileNam)
fmt.Println(err.Error())
if err.Error() != expectedMsg {
bhanu-lab marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("Didnt receive expected message")
}
}
}