From f948bf81368eb13afc7270e4ad9fd9a4126c6ff2 Mon Sep 17 00:00:00 2001 From: Amim Knabben Date: Fri, 28 Dec 2018 09:53:52 -0200 Subject: [PATCH 1/2] Adding ReportError test and HTTP Test server reuse --- cmd/util/util_test.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/util/util_test.go b/cmd/util/util_test.go index 1fcb4b55c06b..6305fd4a6145 100644 --- a/cmd/util/util_test.go +++ b/cmd/util/util_test.go @@ -31,6 +31,16 @@ import ( "github.com/spf13/viper" ) +func startTestHTTPServer(returnError bool, response string) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if returnError { + http.Error(w, response, 400) + } else { + fmt.Fprintf(w, response) + } + })) +} + func TestFormatError(t *testing.T) { var testErr error if _, err := FormatError(testErr); err == nil { @@ -70,22 +80,26 @@ func TestUploadError(t *testing.T) { errMsg, _ := FormatError(testErr) jsonErrMsg, _ := MarshallError(errMsg, "default", version.GetVersion()) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello, world!") - })) - + server := startTestHTTPServer(false, "http test") if err := UploadError(jsonErrMsg, server.URL); err != nil { t.Fatalf("Unexpected error: %v", err) } - server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - http.Error(w, "failed to write report", 400) - })) + server = startTestHTTPServer(true, "failed to write report") if err := UploadError(jsonErrMsg, server.URL); err == nil { t.Fatalf("UploadError should have errored from a 400 response") } } +func TestReportError(t *testing.T) { + testErr := errors.New("TestError 1") + + server := startTestHTTPServer(false, "http test") + if err := ReportError(testErr, server.URL); err != nil { + t.Fatalf("ReportError") + } +} + func revertLookPath(l LookPath) { lookPath = l } From 6762fb0096198a0dd2b3a7eff51a76d7dc907e27 Mon Sep 17 00:00:00 2001 From: Amim Knabben Date: Fri, 28 Dec 2018 10:44:34 -0200 Subject: [PATCH 2/2] Adding GetkubeConfig path test --- cmd/util/util_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cmd/util/util_test.go b/cmd/util/util_test.go index 6305fd4a6145..ca57397966e2 100644 --- a/cmd/util/util_test.go +++ b/cmd/util/util_test.go @@ -19,8 +19,10 @@ package util import ( "bytes" "fmt" + "k8s.io/client-go/tools/clientcmd" "net/http" "net/http/httptest" + "os" "strings" "testing" @@ -177,3 +179,26 @@ func TestKubectlDownloadMsg(t *testing.T) { }) } } + +func TestGetKubeConfigPath(t *testing.T) { + var tests = []struct { + input string + want string + }{ + { + input: "/home/fake/.kube/.kubeconfig", + want: "/home/fake/.kube/.kubeconfig", + }, + { + input: "/home/fake/.kube/.kubeconfig:/home/fake2/.kubeconfig", + want: "/home/fake/.kube/.kubeconfig", + }, + } + + for _, test := range tests { + os.Setenv(clientcmd.RecommendedConfigPathEnvVar, test.input) + if result := GetKubeConfigPath(); result != test.want { + t.Errorf("Expected first splitted chunk, got: %s", result) + } + } +}