From 49b3e86b373eaa4a38a5589c687ce7a7d9ad7fd4 Mon Sep 17 00:00:00 2001 From: Benjamin Pineau Date: Wed, 11 Apr 2018 17:03:26 +0200 Subject: [PATCH] Unit tests for pkg/client --- .goreleaser.yml | 2 +- assets/.kube/config | 13 +++++++ .../Dockerfile.goreleaser | 0 pkg/client/client_test.go | 38 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 assets/.kube/config rename Dockerfile.goreleaser => assets/Dockerfile.goreleaser (100%) create mode 100644 pkg/client/client_test.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 1c364f7..f0074fe 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -25,7 +25,7 @@ dockers: - image: bpineau/katafygio goos: linux goarch: amd64 - dockerfile: Dockerfile.goreleaser + dockerfile: assets/Dockerfile.goreleaser tag_templates: - '{{ .Tag }}' - latest diff --git a/assets/.kube/config b/assets/.kube/config new file mode 100644 index 0000000..1518924 --- /dev/null +++ b/assets/.kube/config @@ -0,0 +1,13 @@ +apiVersion: v1 +clusters: +- cluster: + server: http://127.0.0.1:8080 + name: test-fake-server +contexts: +- context: + cluster: test-fake-server + user: "" + name: test-fake-server +current-context: test-fake-server +kind: Config +preferences: {} diff --git a/Dockerfile.goreleaser b/assets/Dockerfile.goreleaser similarity index 100% rename from Dockerfile.goreleaser rename to assets/Dockerfile.goreleaser diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go new file mode 100644 index 0000000..7140f59 --- /dev/null +++ b/pkg/client/client_test.go @@ -0,0 +1,38 @@ +package client + +import ( + "fmt" + "os" + "testing" +) + +const nonExistentPath = "\\/hopefully/non/existent/path" + +func TestClientSet(t *testing.T) { + here, _ := os.Getwd() + _ = os.Setenv("HOME", here+"/../../assets") + cs, err := NewClientSet("", "") + if err != nil { + t.Fatal(err) + } + if fmt.Sprintf("%T", cs) != "*kubernetes.Clientset" { + t.Errorf("NewClientSet() didn't return a *kubernetes.Clientset: %T", cs) + } + + cs, _ = NewClientSet("http://127.0.0.1", "/dev/null") + if fmt.Sprintf("%T", cs) != "*kubernetes.Clientset" { + t.Errorf("NewClientSet(server) didn't return a *kubernetes.Clientset: %T", cs) + } + + _, err = NewClientSet("http://127.0.0.1", nonExistentPath) + if err == nil { + t.Fatal("NewClientSet() should fail on non existent kubeconfig path") + } + + _ = os.Unsetenv("KUBERNETES_SERVICE_HOST") + _ = os.Setenv("HOME", nonExistentPath) + _, err = NewClientSet("", "") + if err == nil { + t.Fatal("NewClientSet() should fail to load InClusterConfig without kube address env") + } +}