Skip to content

Commit

Permalink
Refactor unit test to not use filesystem (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
alenkacz authored and fabianbaier committed May 23, 2019
1 parent c271412 commit bf67c3a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pkg/kudoctl/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func CmdErrorProcessor(cmd *cobra.Command, args []string) error {
return fmt.Errorf("get flag: %+v", err)
}

err = check.KubeConfigPath()
err = check.ValidateKubeConfigPath()
if err != nil {
return errors.WithMessage(err, "could not check kubeconfig path")
}
Expand Down
30 changes: 19 additions & 11 deletions pkg/kudoctl/util/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,32 @@ const (
defaultGithubCredentialPath = ".git-credentials"
)

// KubeConfigPath checks if the kubeconfig file exists.
func KubeConfigPath() error {
// if KubeConfigPath is not specified, search for the default kubeconfig file under the $HOME/.kube/config.
if len(vars.KubeConfigPath) == 0 {
usr, err := user.Current()
if err != nil {
return errors.Wrap(err, "failed to determine user's home dir")
}
vars.KubeConfigPath = filepath.Join(usr.HomeDir, defaultKubeConfigPath)
// ValidateKubeConfigPath checks if the kubeconfig file exists.
func ValidateKubeConfigPath() error {
path, err := getKubeConfigLocation()
if err != nil {
return err
}

_, err := os.Stat(vars.KubeConfigPath)
if err != nil && os.IsNotExist(err) {
vars.KubeConfigPath = path
if _, err := os.Stat(vars.KubeConfigPath); os.IsNotExist(err) {
return errors.Wrap(err, "failed to find kubeconfig file")
}
return nil
}

func getKubeConfigLocation() (string, error) {
// if vars.KubeConfigPath is not specified, search for the default kubeconfig file under the $HOME/.kube/config.
if len(vars.KubeConfigPath) == 0 {
usr, err := user.Current()
if err != nil {
return "", errors.Wrap(err, "failed to determine user's home dir")
}
return filepath.Join(usr.HomeDir, defaultKubeConfigPath), nil
}
return vars.KubeConfigPath, nil
}

// GithubCredentials checks if the credential file exists.
func GithubCredentials() error {
// if credentials are not specified, search for the default credential file under the $HOME/.git-credentials.
Expand Down
40 changes: 16 additions & 24 deletions pkg/kudoctl/util/check/check_test.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
package check

import (
"os/user"
"path/filepath"
"testing"

"github.com/kudobuilder/kudo/pkg/kudoctl/util/vars"
)

func TestKubeConfigPath(t *testing.T) {
// first we test that the vars.KubeConfigPath is propagated correctly when resolving the path
vars.KubeConfigPath = "/tmp/;"

testNonExisting := []struct {
expected string
}{
{"failed to find kubeconfig file: stat /tmp/;: no such file or directory"}, // 1
location, err := getKubeConfigLocation()
if err != nil {
t.Errorf("expected kubeconfig path '%v' to be propagated from vars, got error instead %v", vars.KubeConfigPath, err)
}

for _, tt := range testNonExisting {
actual := KubeConfigPath()
if actual != nil {
if actual.Error() != tt.expected {
t.Errorf("non existing test:\nexpected: %v\n got: %v", tt.expected, actual)
}
}
if location != vars.KubeConfigPath {
t.Errorf("expected kubeconfig path '%v' to be propagated from vars, kubeconfig path instead resolved as %v", vars.KubeConfigPath, location)
}

// then we test that default is used when no path is provided in vars
vars.KubeConfigPath = ""

testZero := []struct {
expected *string
}{
{nil}, // 1
usr, _ := user.Current()
expectedPath := filepath.Join(usr.HomeDir, defaultKubeConfigPath)
location, err = getKubeConfigLocation()
if err != nil {
t.Errorf("expected kubeconfig path '%v', got error instead %v", expectedPath, err)
}

for _, tt := range testZero {
actual := KubeConfigPath()
if actual != nil {
t.Errorf("empty path test:\nexpected: %v\n got: %v", tt.expected, actual)
}
if location != expectedPath {
t.Errorf("expected kubeconfig path '%v', kubeconfig path instead resolved as %v", expectedPath, location)
}
}

Expand Down

0 comments on commit bf67c3a

Please sign in to comment.