From f5944ec0a54e33e2bcf65480626e1c42a7bca331 Mon Sep 17 00:00:00 2001 From: Sebastian Nickel Date: Wed, 2 Oct 2024 10:37:21 +0200 Subject: [PATCH] move leftover test locations to use test.SetupClient --- auth/cluster_test.go | 20 ++++++++------------ auth/login_test.go | 9 +++------ auth/whoami_test.go | 13 +++---------- internal/test/client.go | 3 ++- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/auth/cluster_test.go b/auth/cluster_test.go index ea5eed5..aa11559 100644 --- a/auth/cluster_test.go +++ b/auth/cluster_test.go @@ -8,12 +8,11 @@ import ( "testing" infrastructure "github.com/ninech/apis/infrastructure/v1alpha1" - "github.com/ninech/nctl/api" "github.com/ninech/nctl/api/config" + "github.com/ninech/nctl/internal/test" + "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/clientcmd" - - "sigs.k8s.io/controller-runtime/pkg/client/fake" ) const existingKubeconfig = ` @@ -43,19 +42,16 @@ func TestClusterCmd(t *testing.T) { t.Fatal(err) } - // prepare a fake client with some static objects (a KubernetesCluster and - // a Secret) that the cluster cmd expects. - scheme, err := api.NewScheme() - if err != nil { - t.Fatal(err) - } - cluster := newCluster() - client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cluster).Build() + apiClient, err := test.SetupClient( + test.WithObjects(cluster), + ) + require.NoError(t, err) + apiClient.KubeconfigPath = kubeconfig.Name() // we run without the execPlugin, that would be something for an e2e test cmd := &ClusterCmd{Name: config.ContextName(cluster), ExecPlugin: false} - if err := cmd.Run(context.TODO(), &api.Client{WithWatch: client, KubeconfigPath: kubeconfig.Name()}); err != nil { + if err := cmd.Run(context.TODO(), apiClient); err != nil { t.Fatal(err) } diff --git a/auth/login_test.go b/auth/login_test.go index 8fd9f75..273381d 100644 --- a/auth/login_test.go +++ b/auth/login_test.go @@ -8,18 +8,15 @@ import ( "path" "testing" + "github.com/ninech/nctl/internal/test" "k8s.io/client-go/tools/clientcmd" clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) -const ( - FakeJWTToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2ODk2ODkwMDMsImV4cCI6NTE5MjQzMTUwMCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsImVtYWlsIjoianJvY2tldEBleGFtcGxlLmNvbSIsImdyb3VwcyI6WyIvQ3VzdG9tZXJzL3Rlc3QiLCIvQ3VzdG9tZXJzL2JsYSJdfQ.N6pD8DsPhTK5_Eoy83UNiPNMJ5lbvULdEouDSLE3yak" -) - type fakeTokenGetter struct{} func (f *fakeTokenGetter) GetTokenString(ctx context.Context, issuerURL, clientID string, usePKCE bool) (string, error) { - return FakeJWTToken, nil + return test.FakeJWTToken, nil } func TestLoginCmd(t *testing.T) { @@ -70,7 +67,7 @@ func TestLoginStaticToken(t *testing.T) { os.Setenv(clientcmd.RecommendedConfigPathEnvVar, kubeconfig.Name()) apiHost := "api.example.org" - token := FakeJWTToken + token := test.FakeJWTToken cmd := &LoginCmd{APIURL: "https://" + apiHost, APIToken: token, Organization: "test"} tk := &fakeTokenGetter{} diff --git a/auth/whoami_test.go b/auth/whoami_test.go index dd53d10..8486a4e 100644 --- a/auth/whoami_test.go +++ b/auth/whoami_test.go @@ -4,25 +4,18 @@ package auth_test import ( "context" - "os" "testing" - "github.com/ninech/nctl/api" "github.com/ninech/nctl/auth" "github.com/ninech/nctl/internal/test" "github.com/stretchr/testify/require" - "k8s.io/client-go/rest" - "sigs.k8s.io/controller-runtime/pkg/client/fake" ) func TestWhoAmICmd_Run(t *testing.T) { - client := fake.NewClientBuilder().Build() - apiClient := &api.Client{WithWatch: client, Project: "default", KubeconfigPath: "*-kubeconfig.yaml"} - apiClient.Config = &rest.Config{BearerToken: auth.FakeJWTToken} - - kubeconfig, err := test.CreateTestKubeconfig(apiClient, "test") + apiClient, err := test.SetupClient( + test.WithKubeconfig(t), + ) require.NoError(t, err) - defer os.Remove(kubeconfig) s := &auth.WhoAmICmd{ IssuerURL: "https://auth.nine.ch/auth/realms/pub", diff --git a/internal/test/client.go b/internal/test/client.go index 0e89961..1c44bc5 100644 --- a/internal/test/client.go +++ b/internal/test/client.go @@ -21,6 +21,7 @@ import ( const ( DefaultProject = "default" + FakeJWTToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2ODk2ODkwMDMsImV4cCI6NTE5MjQzMTUwMCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsImVtYWlsIjoianJvY2tldEBleGFtcGxlLmNvbSIsImdyb3VwcyI6WyIvQ3VzdG9tZXJzL3Rlc3QiLCIvQ3VzdG9tZXJzL2JsYSJdfQ.N6pD8DsPhTK5_Eoy83UNiPNMJ5lbvULdEouDSLE3yak" ) type clientSetup struct { @@ -156,7 +157,7 @@ func SetupClient(opts ...ClientSetupOption) (*api.Client, error) { client := clientBuilder.Build() c := &api.Client{ - Config: &rest.Config{BearerToken: "fake"}, + Config: &rest.Config{BearerToken: FakeJWTToken}, WithWatch: client, Project: setup.defaultProject, }