From 7774c73ec239c4fb0183acb5f8e68b14f53a5cca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanislav=20L=C3=A1zni=C4=8Dka?= Date: Mon, 25 Nov 2024 13:12:51 +0100 Subject: [PATCH] e2e: don't pass client via globvar Use the kube-native way to construct a client by using the KUBECONFIG env var. That was done in the tests anyway (see the change to the Makefile). --- Makefile | 2 +- test/e2e/main_test.go | 45 +++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 4e6e1ce43..7e878baf4 100644 --- a/Makefile +++ b/Makefile @@ -97,7 +97,7 @@ test-unit: go test -v -race -count=1 $(PKGS) test-e2e: - go test -timeout 55m -v ./test/e2e/ $(TEST_RUN_ARGS) --kubeconfig=$(KUBECONFIG) + go test -timeout 55m -v ./test/e2e/ $(TEST_RUN_ARGS) test-local-setup: VERSION = local test-local-setup: VERSION_SEMVER = $(shell cat VERSION) diff --git a/test/e2e/main_test.go b/test/e2e/main_test.go index 79bd11969..fbaa9a167 100644 --- a/test/e2e/main_test.go +++ b/test/e2e/main_test.go @@ -17,39 +17,26 @@ limitations under the License. package e2e import ( - "flag" - "log" - "os" + "fmt" "testing" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" "github.com/brancz/kube-rbac-proxy/test/kubetest" ) -// Sadly there's no way to pass the k8s client from TestMain to Test, -// so we need this global instance -var client kubernetes.Interface - -// TestMain adds the kubeconfig flag to our tests -func TestMain(m *testing.M) { - kubeconfig := flag.String( - "kubeconfig", - "", - "path to kubeconfig", - ) - flag.Parse() - - var err error - client, err = kubetest.NewClientFromKubeconfig(*kubeconfig) +func Test(t *testing.T) { + clientConfig, err := newClientConfigForTest() if err != nil { - log.Fatal(err) + t.Fatalf("failed retrieving kubernetes client config: %v", err) + } + client, err := kubernetes.NewForConfig(clientConfig) + if err != nil { + t.Fatalf("failed to setup a client for the tests: %v", err) } - os.Exit(m.Run()) -} - -func Test(t *testing.T) { tests := map[string]kubetest.TestSuite{ "Basics": testBasics(client), "H2CUpstream": testH2CUpstream(client), @@ -68,3 +55,15 @@ func Test(t *testing.T) { t.Run(name, tc) } } + +// NewClientConfigForTest returns a config configured to connect to the api server +func newClientConfigForTest() (*rest.Config, error) { + loader := clientcmd.NewDefaultClientConfigLoadingRules() + clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loader, &clientcmd.ConfigOverrides{}) + config, err := clientConfig.ClientConfig() + if err == nil { + fmt.Printf("Found configuration for host %v.\n", config.Host) + } + + return config, err +}