added user flag validation and improved some test messages

pull/10106/head
Steven Powell 2021-01-22 15:39:15 -07:00
parent 82f7df5b56
commit 14bcd9ddec
8 changed files with 77 additions and 8 deletions

View File

@ -37,6 +37,7 @@ import (
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/translate"
)
@ -64,6 +65,10 @@ var RootCmd = &cobra.Command{
exit.Error(reason.HostHomeMkdir, "Error creating minikube directory", err)
}
}
if !config.UserNameValid(viper.GetString(config.UserFlag)) {
out.WarningT("User name '{{.username}}' is not valid", out.V{"username": audit.UserName()})
exit.Message(reason.Usage, "User name must be 60 chars or less.")
}
},
}

View File

@ -27,8 +27,8 @@ import (
"k8s.io/minikube/pkg/minikube/config"
)
// username pulls the user flag, if empty gets the os username.
func username() string {
// UserName pulls the user flag, if empty gets the os username.
func UserName() string {
u := viper.GetString(config.UserFlag)
if u != "" {
return u
@ -54,7 +54,7 @@ func Log(startTime time.Time) {
if !shouldLog() {
return
}
e := newEntry(os.Args[1], args(), username(), startTime, time.Now())
e := newEntry(os.Args[1], args(), UserName(), startTime, time.Now())
if err := appendToLog(e); err != nil {
klog.Error(err)
}

View File

@ -49,7 +49,7 @@ func TestAudit(t *testing.T) {
for _, test := range tests {
viper.Set(config.UserFlag, test.userFlag)
got := username()
got := UserName()
if got != test.want {
t.Errorf("userFlag = %q; username() = %q; want %q", test.userFlag, got, test.want)

View File

@ -0,0 +1,22 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
// UserNameValid checks if the user name is valid.
func UserNameValid(name string) bool {
return len(name) <= 60
}

View File

@ -0,0 +1,42 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"strings"
"testing"
)
func TestUser(t *testing.T) {
t.Run("Length", func(t *testing.T) {
tests := []struct {
in string
want bool
}{
{strings.Repeat("a", 60), true},
{strings.Repeat("a", 61), false},
}
for _, tt := range tests {
got := UserNameValid(tt.in)
if got != tt.want {
t.Errorf("UserNameValid(%q, length: %d) = %t; want %t", tt.in, len(tt.in), got, tt.want)
}
}
})
}

View File

@ -243,7 +243,7 @@ func validateStartWithProxy(ctx context.Context, t *testing.T, profile string) {
t.Run("Audit", func(t *testing.T) {
got, err := auditContains(profile)
if err != nil {
t.Fatal(err)
t.Fatalf("failed to check audit log: %v", err)
}
if !got {
t.Errorf("audit.json does not contain the profile %q", profile)
@ -285,7 +285,7 @@ func validateSoftStart(ctx context.Context, t *testing.T, profile string) {
t.Run("Audit", func(t *testing.T) {
got, err := auditContains(profile)
if err != nil {
t.Fatal(err)
t.Fatalf("failed to check audit log: %v", err)
}
if !got {
t.Errorf("audit.json does not contain the profile %q", profile)

View File

@ -70,7 +70,7 @@ func TestJSONOutput(t *testing.T) {
t.Run("Audit", func(t *testing.T) {
got, err := auditContains("testUser")
if err != nil {
t.Fatal(err)
t.Fatalf("failed to check audit log: %v", err)
}
if !got {
t.Errorf("audit.json does not contain the user testUser")

View File

@ -67,7 +67,7 @@ func UniqueProfileName(prefix string) string {
func auditContains(substr string) (bool, error) {
f, err := os.Open(localpath.AuditLog())
if err != nil {
return false, err
return false, fmt.Errorf("Unable to open file %s: %v", localpath.AuditLog(), err)
}
defer f.Close()