Skip to content

Commit

Permalink
Refactor type cases
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardstudy committed Jul 17, 2019
1 parent 1be16cf commit 0bdbd06
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 23 deletions.
7 changes: 6 additions & 1 deletion pkg/kn/commands/namespaced.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 21 additions & 8 deletions pkg/kn/commands/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"io"
"os"
"path/filepath"

serving_kn_v1alpha1 "github.com/knative/client/pkg/serving/v1alpha1"
Expand Down Expand Up @@ -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
}
63 changes: 49 additions & 14 deletions pkg/kn/commands/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,92 @@
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{
KubeCfgPath: tc.kubeCfgPath,
}

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)
}
}
}

0 comments on commit 0bdbd06

Please sign in to comment.