-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathutil_test.go
More file actions
142 lines (132 loc) · 3.23 KB
/
util_test.go
File metadata and controls
142 lines (132 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package utils
import (
"context"
"os"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/diag"
sdkClients "github.com/stackitcloud/stackit-sdk-go/core/clients"
"github.com/stackitcloud/stackit-sdk-go/core/config"
utils2 "github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/sfs"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
)
const (
testVersion = "1.2.3"
testCustomEndpoint = "https://sfs-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 *sfs.APIClient
}{
{
name: "default endpoint",
args: args{
providerData: &core.ProviderData{
Version: testVersion,
},
},
expected: func() *sfs.APIClient {
apiClient, err := sfs.NewAPIClient(
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,
SfsCustomEndpoint: testCustomEndpoint,
},
},
expected: func() *sfs.APIClient {
apiClient, err := sfs.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)
}
})
}
}
func TestDescribeValidationError(t *testing.T) {
tests := []struct {
name string
err sfs.ValidationError
want string
}{
{
name: "just title",
err: sfs.ValidationError{
Title: utils2.Ptr("nice title"),
},
want: `nice title
`,
},
{
name: "with fields",
err: sfs.ValidationError{
Title: utils2.Ptr("nice title"),
Fields: &[]sfs.ValidationErrorField{
{
Field: utils2.Ptr("field-a"),
Reason: utils2.Ptr("reason-a"),
},
{
Reason: utils2.Ptr("reason-b"),
},
{
Field: utils2.Ptr("field-c"),
},
},
},
want: `nice title
Field: field-a | Reason: reason-a
Field: | Reason: reason-b
Field: field-c | Reason: `,
},
}
for _, tt := range tests {
got := DescribeValidationError(tt.err)
if d := cmp.Diff(got, tt.want); d != "" {
t.Errorf("DescribeValidationError() = got diff: %s", d)
}
}
}