From 0bdbd06fdab2d6eb1ac670ae8e6dfed24e9b1053 Mon Sep 17 00:00:00 2001 From: Zida Date: Wed, 3 Jul 2019 23:30:29 +0800 Subject: [PATCH] Refactor type cases --- pkg/kn/commands/namespaced.go | 7 +++- pkg/kn/commands/types.go | 29 +++++++++++----- pkg/kn/commands/types_test.go | 63 +++++++++++++++++++++++++++-------- 3 files changed, 76 insertions(+), 23 deletions(-) diff --git a/pkg/kn/commands/namespaced.go b/pkg/kn/commands/namespaced.go index e37fa3ebf5..414976b170 100644 --- a/pkg/kn/commands/namespaced.go +++ b/pkg/kn/commands/namespaced.go @@ -63,12 +63,17 @@ func (params *KnParams) GetNamespace(cmd *cobra.Command) (string, error) { return namespace, nil } +// CurrentNamespace returns the current namespace which is either provided as option or picked up from kubeconfig func (params *KnParams) CurrentNamespace() (string, error) { + var err error if params.fixedCurrentNamespace != "" { return params.fixedCurrentNamespace, nil } if params.ClientConfig == nil { - params.ClientConfig = params.GetClientConfig() + params.ClientConfig, err = params.GetClientConfig() + if err != nil { + return "", err + } } name, _, err := params.ClientConfig.Namespace() return name, err diff --git a/pkg/kn/commands/types.go b/pkg/kn/commands/types.go index 98850c9fe7..a36a494e7f 100644 --- a/pkg/kn/commands/types.go +++ b/pkg/kn/commands/types.go @@ -18,6 +18,7 @@ import ( "errors" "fmt" "io" + "os" "path/filepath" serving_kn_v1alpha1 "github.com/knative/client/pkg/serving/v1alpha1" @@ -75,13 +76,25 @@ func (params *KnParams) GetConfig() (serving_v1alpha1_client.ServingV1alpha1Inte // GetClientConfig gets ClientConfig from KubeCfgPath func (params *KnParams) GetClientConfig() (clientcmd.ClientConfig, error) { loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() - if len(params.KubeCfgPath) > 0 { - paths := filepath.SplitList(params.KubeCfgPath) - if len(paths) == 1 { - loadingRules.ExplicitPath = paths[0] - } else { - return nil, errors.New(fmt.Sprintf("Config file '%s' could not be found. For configuration lookup path please use the env variable KUBECONFIG", params.KubeCfgPath)) - } + if len(params.KubeCfgPath) == 0 { + return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}), nil + } + + _, err := os.Stat(params.KubeCfgPath) + if err == nil { + loadingRules.ExplicitPath = params.KubeCfgPath + return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}), nil + } + + if !os.IsNotExist(err) { + return nil, err + } + + paths := filepath.SplitList(params.KubeCfgPath) + if len(paths) > 1 { + return nil, errors.New(fmt.Sprintf("Can not find config file. '%s' looks like a combine path. "+ + "Please use the env var KUBECONFIG if you want to check for multiple configuration files", params.KubeCfgPath)) + } else { + return nil, errors.New(fmt.Sprintf("Config file '%s' can not be found", params.KubeCfgPath)) } - return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}), nil } diff --git a/pkg/kn/commands/types_test.go b/pkg/kn/commands/types_test.go index 38792bf8c0..1af5bddd8e 100644 --- a/pkg/kn/commands/types_test.go +++ b/pkg/kn/commands/types_test.go @@ -15,39 +15,75 @@ package commands import ( - "errors" "fmt" "os" - "reflect" + "strings" "testing" + "github.com/knative/client/pkg/util" + "gotest.tools/assert" "k8s.io/client-go/tools/clientcmd" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) +type getConfigTestCase struct { + clientConfig clientcmd.ClientConfig + expectedErrString string +} + +func TestGetConfig(t *testing.T) { + for i, tc := range []getConfigTestCase{ + { + clientcmd.NewDefaultClientConfig(clientcmdapi.Config{}, &clientcmd.ConfigOverrides{}), + "no configuration has been provided", + }, + } { + p := &KnParams{ + ClientConfig: tc.clientConfig} + + _, err := p.GetConfig() + + switch len(tc.expectedErrString) { + case 0: + if err != nil { + t.Errorf("%d: unexpected error: %s", i, err.Error()) + } + default: + if err == nil { + t.Errorf("%d: wrong error detected: %s (expected) != %s (actual)", i, tc.expectedErrString, err) + } + if !strings.Contains(err.Error(), tc.expectedErrString) { + t.Errorf("%d: wrong error detected: %s (expected) != %s (actual)", i, tc.expectedErrString, err.Error()) + } + } + } +} + type typeTestCase struct { kubeCfgPath string explicitPath string - expectedError error + expectedError string } func TestGetClientConfig(t *testing.T) { multiConfigs := fmt.Sprintf("%s%s%s", "/testing/assets/kube-config-01.yml", string(os.PathListSeparator), "/testing/assets/kube-config-02.yml") - for i, tc := range []typeTestCase{ + multiConfigs = multiConfigs + for _, tc := range []typeTestCase{ { "", clientcmd.NewDefaultClientConfigLoadingRules().ExplicitPath, - nil, + "", }, { "/testing/assets/kube-config-01.yml", - "/testing/assets/kube-config-01.yml", - nil, + "", + fmt.Sprintf("Config file '%s' can not be found", "/testing/assets/kube-config-01.yml"), }, { multiConfigs, "", - errors.New(fmt.Sprintf("Config file '%s' could not be found. For configuration lookup path please use the env variable KUBECONFIG", multiConfigs)), + fmt.Sprintf("Can not find config file. '%s' looks like a combine path. Please use the env var KUBECONFIG if you want to check for multiple configuration files", multiConfigs), }, } { p := &KnParams{ @@ -55,17 +91,16 @@ func TestGetClientConfig(t *testing.T) { } clientConfig, err := p.GetClientConfig() - - if !reflect.DeepEqual(err, tc.expectedError) { - t.Errorf("%d: wrong error detected: %s (expected) != %s (actual)", i, tc.expectedError.Error(), err.Error()) + if tc.expectedError != "" { + assert.Assert(t, util.ContainsAll(err.Error(), tc.expectedError)) + } else { + assert.Assert(t, err == nil, err) } if clientConfig != nil { configAccess := clientConfig.ConfigAccess() - if configAccess.GetExplicitFile() != tc.explicitPath { - t.Errorf("%d: wrong explicit file detected: %s (expected) != %s (actual)", i, tc.explicitPath, configAccess.GetExplicitFile()) - } + assert.Assert(t, configAccess.GetExplicitFile() == tc.explicitPath) } } }