Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e2e: don't pass client via globvar #325

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading