|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package audit |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "os/user" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/spf13/viper" |
| 25 | + "k8s.io/minikube/pkg/minikube/config" |
| 26 | +) |
| 27 | + |
| 28 | +func TestAudit(t *testing.T) { |
| 29 | + t.Run("Username", func(t *testing.T) { |
| 30 | + u, err := user.Current() |
| 31 | + if err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + |
| 35 | + tests := []struct { |
| 36 | + userFlag string |
| 37 | + want string |
| 38 | + }{ |
| 39 | + { |
| 40 | + "test123", |
| 41 | + "test123", |
| 42 | + }, |
| 43 | + { |
| 44 | + "", |
| 45 | + u.Username, |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + for _, test := range tests { |
| 50 | + viper.Set(config.User, test.userFlag) |
| 51 | + |
| 52 | + got := username() |
| 53 | + |
| 54 | + if got != test.want { |
| 55 | + t.Errorf("userFlag = %q; username() = %q; want %q", test.userFlag, got, test.want) |
| 56 | + } |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("Args", func(t *testing.T) { |
| 61 | + oldArgs := os.Args |
| 62 | + defer func() { os.Args = oldArgs }() |
| 63 | + |
| 64 | + tests := []struct { |
| 65 | + args []string |
| 66 | + want string |
| 67 | + }{ |
| 68 | + { |
| 69 | + []string{"minikube", "start"}, |
| 70 | + "", |
| 71 | + }, |
| 72 | + { |
| 73 | + []string{"minikube", "start", "--user", "test123"}, |
| 74 | + "--user test123", |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, test := range tests { |
| 79 | + os.Args = test.args |
| 80 | + |
| 81 | + got := args() |
| 82 | + |
| 83 | + if got != test.want { |
| 84 | + t.Errorf("os.Args = %q; args() = %q; want %q", os.Args, got, test.want) |
| 85 | + } |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("ShouldLog", func(t *testing.T) { |
| 90 | + oldArgs := os.Args |
| 91 | + defer func() { os.Args = oldArgs }() |
| 92 | + |
| 93 | + tests := []struct { |
| 94 | + args []string |
| 95 | + want bool |
| 96 | + }{ |
| 97 | + { |
| 98 | + []string{"minikube", "start"}, |
| 99 | + true, |
| 100 | + }, |
| 101 | + { |
| 102 | + []string{"minikube", "delete"}, |
| 103 | + true, |
| 104 | + }, |
| 105 | + { |
| 106 | + []string{"minikube", "status"}, |
| 107 | + false, |
| 108 | + }, |
| 109 | + { |
| 110 | + []string{"minikube", "version"}, |
| 111 | + false, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + for _, test := range tests { |
| 116 | + os.Args = test.args |
| 117 | + |
| 118 | + got := shouldLog() |
| 119 | + |
| 120 | + if got != test.want { |
| 121 | + t.Errorf("os.Args = %q; shouldLog() = %t; want %t", os.Args, got, test.want) |
| 122 | + } |
| 123 | + } |
| 124 | + }) |
| 125 | +} |
0 commit comments