diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go index 5ded3e98ba8..b8940bbb19b 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags.go @@ -17,7 +17,6 @@ limitations under the License. package genericclioptions import ( - "errors" "os" "path/filepath" "regexp" @@ -58,8 +57,7 @@ const ( var ( defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "http-cache") - - ErrEmptyConfig = errors.New(`Missing or incomplete configuration info. Please point to an existing, complete config file: + ErrEmptyConfig = clientcmd.NewEmptyConfigError(`Missing or incomplete configuration info. Please point to an existing, complete config file: 1. Via the command-line flag --kubeconfig 2. Via the KUBECONFIG environment variable diff --git a/staging/src/k8s.io/client-go/tools/clientcmd/validation.go b/staging/src/k8s.io/client-go/tools/clientcmd/validation.go index afe6f80b39e..a480bdf0d6b 100644 --- a/staging/src/k8s.io/client-go/tools/clientcmd/validation.go +++ b/staging/src/k8s.io/client-go/tools/clientcmd/validation.go @@ -30,11 +30,24 @@ import ( var ( ErrNoContext = errors.New("no context chosen") - ErrEmptyConfig = errors.New("no configuration has been provided, try setting KUBERNETES_MASTER environment variable") + ErrEmptyConfig = NewEmptyConfigError("no configuration has been provided, try setting KUBERNETES_MASTER environment variable") // message is for consistency with old behavior ErrEmptyCluster = errors.New("cluster has no server defined") ) +// NewEmptyConfigError returns an error wrapping the given message which IsEmptyConfig() will recognize as an empty config error +func NewEmptyConfigError(message string) error { + return &errEmptyConfig{message} +} + +type errEmptyConfig struct { + message string +} + +func (e *errEmptyConfig) Error() string { + return e.message +} + type errContextNotFound struct { ContextName string } @@ -60,9 +73,14 @@ func IsContextNotFound(err error) bool { func IsEmptyConfig(err error) bool { switch t := err.(type) { case errConfigurationInvalid: - return len(t) == 1 && t[0] == ErrEmptyConfig + if len(t) != 1 { + return false + } + _, ok := t[0].(*errEmptyConfig) + return ok } - return err == ErrEmptyConfig + _, ok := err.(*errEmptyConfig) + return ok } // errConfigurationInvalid is a set of errors indicating the configuration is invalid. diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/version/BUILD b/staging/src/k8s.io/kubectl/pkg/cmd/version/BUILD index 7bbb72c6013..622835ebfc3 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/version/BUILD +++ b/staging/src/k8s.io/kubectl/pkg/cmd/version/BUILD @@ -1,4 +1,4 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "go_default_library", @@ -20,6 +20,16 @@ go_library( ], ) +go_test( + name = "go_default_test", + srcs = ["version_test.go"], + embed = [":go_default_library"], + deps = [ + "//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library", + "//staging/src/k8s.io/kubectl/pkg/cmd/util:go_default_library", + ], +) + filegroup( name = "package-srcs", srcs = glob(["**"]), diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/version/version_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/version/version_test.go new file mode 100644 index 00000000000..376602412a1 --- /dev/null +++ b/staging/src/k8s.io/kubectl/pkg/cmd/version/version_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2020 The Kubernetes Authors. + +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 version + +import ( + "strings" + "testing" + + "k8s.io/cli-runtime/pkg/genericclioptions" + cmdutil "k8s.io/kubectl/pkg/cmd/util" +) + +func TestNewCmdVersionWithoutConfigFile(t *testing.T) { + tf := cmdutil.NewFactory(&genericclioptions.ConfigFlags{}) + streams, _, buf, _ := genericclioptions.NewTestIOStreams() + cmd := NewCmdVersion(tf, streams) + cmd.SetOutput(buf) + if err := cmd.Execute(); err != nil { + t.Errorf("Cannot execute version command: %v", err) + } + if !strings.Contains(buf.String(), "Client Version") { + t.Errorf("unexpected output: %s", buf.String()) + } +}