// Copyright (c) STACKIT package utils import ( "context" "os" "reflect" "testing" "github.com/hashicorp/terraform-plugin-framework/diag" sdkClients "github.com/stackitcloud/stackit-sdk-go/core/clients" "github.com/stackitcloud/stackit-sdk-go/core/config" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha" ) const ( testVersion = "1.2.3" testCustomEndpoint = "https://postgresflex-custom-endpoint.api.stackit.cloud" ) func TestConfigureClient(t *testing.T) { /* mock authentication by setting service account token env variable */ os.Clearenv() err := os.Setenv(sdkClients.ServiceAccountToken, "mock-val") if err != nil { t.Errorf("error setting env variable: %v", err) } type args struct { providerData *core.ProviderData } tests := []struct { name string args args wantErr bool expected *postgresflex.APIClient }{ { name: "default endpoint", args: args{ providerData: &core.ProviderData{ Version: testVersion, }, }, expected: func() *postgresflex.APIClient { apiClient, err := postgresflex.NewAPIClient( config.WithRegion("eu01"), utils.UserAgentConfigOption(testVersion), ) if err != nil { t.Errorf("error configuring client: %v", err) } return apiClient }(), wantErr: false, }, { name: "custom endpoint", args: args{ providerData: &core.ProviderData{ Version: testVersion, PostgresFlexCustomEndpoint: testCustomEndpoint, }, }, expected: func() *postgresflex.APIClient { apiClient, err := postgresflex.NewAPIClient( utils.UserAgentConfigOption(testVersion), config.WithEndpoint(testCustomEndpoint), ) if err != nil { t.Errorf("error configuring client: %v", err) } return apiClient }(), wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ctx := context.Background() diags := diag.Diagnostics{} actual := ConfigureClient(ctx, tt.args.providerData, &diags) if diags.HasError() != tt.wantErr { t.Errorf("ConfigureClient() error = %v, want %v", diags.HasError(), tt.wantErr) } if !reflect.DeepEqual(actual, tt.expected) { t.Errorf("ConfigureClient() = %v, want %v", actual, tt.expected) } }) } }