From 55d7bfae2d9530a0c0bad9e16ce6e754e767b4a8 Mon Sep 17 00:00:00 2001 From: MarlenKoch Date: Wed, 8 Jan 2025 09:27:22 +0100 Subject: [PATCH] feat: add checks for tf setup and user credentials --- internal/clients/tfclient/tfclient_test.go | 76 +++++++++++++++++++--- 1 file changed, 67 insertions(+), 9 deletions(-) diff --git a/internal/clients/tfclient/tfclient_test.go b/internal/clients/tfclient/tfclient_test.go index 0e78133..676057e 100644 --- a/internal/clients/tfclient/tfclient_test.go +++ b/internal/clients/tfclient/tfclient_test.go @@ -6,14 +6,18 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/pkg/errors" + v1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" ) func TestTerraformSetupBuilder(t *testing.T) { - kubeStub := func(err error) client.Client { + kubeStub := func(err error, secretData []byte) client.Client { return &test.MockClient{ MockGet: test.NewMockGetFn(err, func(obj client.Object) error { + if secret, ok := obj.(*v1.Secret); ok { + secret.Data = map[string][]byte{"service-account.json": secretData} + } return nil }), } @@ -26,14 +30,19 @@ func TestTerraformSetupBuilder(t *testing.T) { disableTracking bool } type want struct { - err error - setupCreated bool + err error + setupCreated bool + username string + password string + globalAccount string + cliServerUrl string } cases := map[string]struct { - args args - want want - mockErr error + args args + want want + mockSecretData []byte + mockErr error }{ "connect tfclient with tracking": { args: args{ @@ -72,23 +81,59 @@ func TestTerraformSetupBuilder(t *testing.T) { }, mockErr: errors.New(errGetProviderConfig), }, + "connect tfclient with tracking and valid secret data": { + args: args{ + version: "version", + providerSource: "source", + providerVersion: "someVersion", + disableTracking: false, + }, + want: want{ + err: nil, + setupCreated: true, + username: "testUser", + password: "testPassword", + globalAccount: "testAccount", + cliServerUrl: "", + }, + mockSecretData: []byte(`{"username":"testUser","password":"testPass"}`), + }, } for name, tc := range cases { t.Run(name, func(t *testing.T) { - mockClient := kubeStub(tc.mockErr) + mockClient := kubeStub(tc.mockErr, tc.mockSecretData) tfSetup := TerraformSetupBuilder(tc.args.version, tc.args.providerSource, tc.args.providerVersion, tc.args.disableTracking) + ctx := context.Background() if tc.want.setupCreated != (tfSetup != nil) { t.Errorf("expected terraform setup to be created: %t, tfSetup %t", tc.want.setupCreated, tfSetup != nil) } - ctx := context.Background() //mg := TODO, managed resource zum testen - _, err := tfSetup(ctx, mockClient, mg) + setup, err := tfSetup(ctx, mockClient, mg) + + if tc.want.setupCreated { + if setup.Configuration == nil { + t.Errorf("expected setup to be created with configuration, but got nil") + } else { + if username, ok := setup.Configuration["username"]; !ok || username != tc.want.username { + t.Errorf("expected username: %v, got: %v", tc.want.username, username) + } + if password, ok := setup.Configuration["password"]; !ok || password != tc.want.password { + t.Errorf("expected password: %v, got: %v", tc.want.password, password) + } + if globalAccount, ok := setup.Configuration["globalaccount"]; !ok || globalAccount != tc.want.globalAccount { + t.Errorf("expected globalaccount: %v, got: %v", tc.want.globalAccount, globalAccount) + } + if cliServerUrl, ok := setup.Configuration["cli_server_url"]; !ok || cliServerUrl != tc.want.cliServerUrl { + t.Errorf("expected cli_server_url: %v, got: %v", tc.want.cliServerUrl, cliServerUrl) + } + } + } if tc.want.err != nil { if err == nil || !errors.Is(err, tc.want.err) { @@ -97,6 +142,19 @@ func TestTerraformSetupBuilder(t *testing.T) { } else if err != nil { t.Errorf("unexpected error: %v", err) } + + if setup.Configuration["username"] != tc.want.username { + t.Errorf("expected username: %v, got: %v", tc.want.username, setup.Configuration["username"]) + } + if setup.Configuration["password"] != tc.want.password { + t.Errorf("expected password: %v, got: %v", tc.want.password, setup.Configuration["password"]) + } + if setup.Configuration["globalaccount"] != tc.want.globalAccount { + t.Errorf("expected globalaccount: %v, got: %v", tc.want.globalAccount, setup.Configuration["globalaccount"]) + } + if setup.Configuration["cli_server_url"] != tc.want.cliServerUrl { + t.Errorf("expected cli_server_url: %v, got: %v", tc.want.cliServerUrl, setup.Configuration["cli_server_url"]) + } }) } }