File tree Expand file tree Collapse file tree 4 files changed +71
-7
lines changed
cli-runtime/pkg/genericclioptions
client-go/tools/clientcmd Expand file tree Collapse file tree 4 files changed +71
-7
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,6 @@ limitations under the License.
17
17
package genericclioptions
18
18
19
19
import (
20
- "errors"
21
20
"os"
22
21
"path/filepath"
23
22
"regexp"
@@ -58,8 +57,7 @@ const (
58
57
59
58
var (
60
59
defaultCacheDir = filepath .Join (homedir .HomeDir (), ".kube" , "http-cache" )
61
-
62
- ErrEmptyConfig = errors .New (`Missing or incomplete configuration info. Please point to an existing, complete config file:
60
+ ErrEmptyConfig = clientcmd .NewEmptyConfigError (`Missing or incomplete configuration info. Please point to an existing, complete config file:
63
61
64
62
1. Via the command-line flag --kubeconfig
65
63
2. Via the KUBECONFIG environment variable
Original file line number Diff line number Diff line change @@ -30,11 +30,24 @@ import (
30
30
31
31
var (
32
32
ErrNoContext = errors .New ("no context chosen" )
33
- ErrEmptyConfig = errors . New ("no configuration has been provided, try setting KUBERNETES_MASTER environment variable" )
33
+ ErrEmptyConfig = NewEmptyConfigError ("no configuration has been provided, try setting KUBERNETES_MASTER environment variable" )
34
34
// message is for consistency with old behavior
35
35
ErrEmptyCluster = errors .New ("cluster has no server defined" )
36
36
)
37
37
38
+ // NewEmptyConfigError returns an error wrapping the given message which IsEmptyConfig() will recognize as an empty config error
39
+ func NewEmptyConfigError (message string ) error {
40
+ return & errEmptyConfig {message }
41
+ }
42
+
43
+ type errEmptyConfig struct {
44
+ message string
45
+ }
46
+
47
+ func (e * errEmptyConfig ) Error () string {
48
+ return e .message
49
+ }
50
+
38
51
type errContextNotFound struct {
39
52
ContextName string
40
53
}
@@ -60,9 +73,14 @@ func IsContextNotFound(err error) bool {
60
73
func IsEmptyConfig (err error ) bool {
61
74
switch t := err .(type ) {
62
75
case errConfigurationInvalid :
63
- return len (t ) == 1 && t [0 ] == ErrEmptyConfig
76
+ if len (t ) != 1 {
77
+ return false
78
+ }
79
+ _ , ok := t [0 ].(* errEmptyConfig )
80
+ return ok
64
81
}
65
- return err == ErrEmptyConfig
82
+ _ , ok := err .(* errEmptyConfig )
83
+ return ok
66
84
}
67
85
68
86
// errConfigurationInvalid is a set of errors indicating the configuration is invalid.
Original file line number Diff line number Diff line change 1
- load ("@io_bazel_rules_go//go:def.bzl" , "go_library" )
1
+ load ("@io_bazel_rules_go//go:def.bzl" , "go_library" , "go_test" )
2
2
3
3
go_library (
4
4
name = "go_default_library" ,
@@ -20,6 +20,16 @@ go_library(
20
20
],
21
21
)
22
22
23
+ go_test (
24
+ name = "go_default_test" ,
25
+ srcs = ["version_test.go" ],
26
+ embed = [":go_default_library" ],
27
+ deps = [
28
+ "//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library" ,
29
+ "//staging/src/k8s.io/kubectl/pkg/cmd/util:go_default_library" ,
30
+ ],
31
+ )
32
+
23
33
filegroup (
24
34
name = "package-srcs" ,
25
35
srcs = glob (["**" ]),
Original file line number Diff line number Diff line change
1
+ /*
2
+ Copyright 2020 The Kubernetes Authors.
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 version
18
+
19
+ import (
20
+ "strings"
21
+ "testing"
22
+
23
+ "k8s.io/cli-runtime/pkg/genericclioptions"
24
+ cmdutil "k8s.io/kubectl/pkg/cmd/util"
25
+ )
26
+
27
+ func TestNewCmdVersionWithoutConfigFile (t * testing.T ) {
28
+ tf := cmdutil .NewFactory (& genericclioptions.ConfigFlags {})
29
+ streams , _ , buf , _ := genericclioptions .NewTestIOStreams ()
30
+ cmd := NewCmdVersion (tf , streams )
31
+ cmd .SetOutput (buf )
32
+ if err := cmd .Execute (); err != nil {
33
+ t .Errorf ("Cannot execute version command: %v" , err )
34
+ }
35
+ if ! strings .Contains (buf .String (), "Client Version" ) {
36
+ t .Errorf ("unexpected output: %s" , buf .String ())
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments