Skip to content

Commit

Permalink
feat: add checks for tf setup and user credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
MarlenKoch committed Jan 8, 2025
1 parent 7d9f178 commit 55d7bfa
Showing 1 changed file with 67 additions and 9 deletions.
76 changes: 67 additions & 9 deletions internal/clients/tfclient/tfclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}),
}
Expand All @@ -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{
Expand Down Expand Up @@ -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: "<https://cli.server.url>",
},
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)

Check failure on line 117 in internal/clients/tfclient/tfclient_test.go

View workflow job for this annotation

GitHub Actions / run-unit-test / unit-tests

undefined: 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) {
Expand All @@ -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"])
}
})
}
}
Expand Down

0 comments on commit 55d7bfa

Please sign in to comment.