Skip to content

Commit

Permalink
Merge pull request #325 from stlaz/e2e_kubeconfig
Browse files Browse the repository at this point in the history
e2e: don't pass client via globvar
  • Loading branch information
ibihim authored Nov 26, 2024
2 parents 7b9b6a2 + 7774c73 commit c9f1c49
Show file tree
Hide file tree
Showing 2 changed files with 23 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
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,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),
Expand All @@ -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
}

0 comments on commit c9f1c49

Please sign in to comment.