Skip to content

Commit

Permalink
e2e: don't pass client via globvar
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
stlaz committed Nov 25, 2024
1 parent 67fe24b commit 10b0a23
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/oklog/run v1.1.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.28.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.31.0
Expand Down Expand Up @@ -59,6 +60,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/pquerna/cachecontrol v0.1.0 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
Expand Down
45 changes: 22 additions & 23 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,25 @@ limitations under the License.
package e2e

import (
"flag"
"log"
"os"
"fmt"
"testing"

"github.com/stretchr/testify/require"

"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 := newClientConfigForTest(t)
client, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
log.Fatal(err)
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),
Expand All @@ -68,3 +54,16 @@ func Test(t *testing.T) {
t.Run(name, tc)
}
}

// NewClientConfigForTest returns a config configured to connect to the api server
func newClientConfigForTest(t *testing.T) *rest.Config {
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)
}

require.NoError(t, err)
return config
}

0 comments on commit 10b0a23

Please sign in to comment.