Skip to content

Commit

Permalink
move leftover test locations to use test.SetupClient
Browse files Browse the repository at this point in the history
  • Loading branch information
thirdeyenick committed Oct 2, 2024
1 parent 17e000b commit f5944ec
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
20 changes: 8 additions & 12 deletions auth/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down Expand Up @@ -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)
}

Expand Down
9 changes: 3 additions & 6 deletions auth/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{}
Expand Down
13 changes: 3 additions & 10 deletions auth/whoami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion internal/test/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

const (
DefaultProject = "default"
FakeJWTToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2ODk2ODkwMDMsImV4cCI6NTE5MjQzMTUwMCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsImVtYWlsIjoianJvY2tldEBleGFtcGxlLmNvbSIsImdyb3VwcyI6WyIvQ3VzdG9tZXJzL3Rlc3QiLCIvQ3VzdG9tZXJzL2JsYSJdfQ.N6pD8DsPhTK5_Eoy83UNiPNMJ5lbvULdEouDSLE3yak"
)

type clientSetup struct {
Expand Down Expand Up @@ -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,
}

Expand Down

0 comments on commit f5944ec

Please sign in to comment.