-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathdatasource_test.go
More file actions
164 lines (153 loc) · 5.07 KB
/
datasource_test.go
File metadata and controls
164 lines (153 loc) · 5.07 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package accounts
import (
"regexp"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types"
serviceaccount "github.com/stackitcloud/stackit-sdk-go/services/serviceaccount/v2api"
)
func TestMapDataSourceFields(t *testing.T) {
projectId := "test-project-id"
emailA := "sa-a-1234567@sa.stackit.cloud"
emailB := "sa-b-1234567@sa.stackit.cloud"
emailC := "sa-c-1234567@ske.sa.stackit.cloud"
idA := "550e8400-e29b-41d4-a716-446655440001"
idB := "550e8400-e29b-41d4-a716-446655440002"
idC := "550e8400-e29b-41d4-a716-446655440003"
nameA := "sa-a"
nameB := "sa-b"
nameC := "sa-c"
tests := []struct {
description string
apiItems []serviceaccount.ServiceAccount
initialModel ServiceAccountsModel
regexStr string
expectedModel ServiceAccountsModel
isValid bool
}{
{
description: "default_sort_descending",
apiItems: []serviceaccount.ServiceAccount{
{Email: emailA, Id: idA},
{Email: emailC, Id: idC},
{Email: emailB, Id: idB},
},
initialModel: ServiceAccountsModel{
ProjectId: types.StringValue(projectId),
SortAscending: types.BoolNull(), // Default should trigger descending sort
},
expectedModel: ServiceAccountsModel{
Id: types.StringValue(projectId),
ProjectId: types.StringValue(projectId),
SortAscending: types.BoolNull(),
Items: []ServiceAccountItem{
{Email: types.StringValue(emailC), Name: types.StringValue(nameC), ServiceAccountId: types.StringValue(idC)},
{Email: types.StringValue(emailB), Name: types.StringValue(nameB), ServiceAccountId: types.StringValue(idB)},
{Email: types.StringValue(emailA), Name: types.StringValue(nameA), ServiceAccountId: types.StringValue(idA)},
},
},
isValid: true,
},
{
description: "sort_ascending",
apiItems: []serviceaccount.ServiceAccount{
{Email: emailC, Id: idC},
{Email: emailA, Id: idA},
{Email: emailB, Id: idB},
},
initialModel: ServiceAccountsModel{
ProjectId: types.StringValue(projectId),
SortAscending: types.BoolValue(true),
},
expectedModel: ServiceAccountsModel{
Id: types.StringValue(projectId),
ProjectId: types.StringValue(projectId),
SortAscending: types.BoolValue(true),
Items: []ServiceAccountItem{
{Email: types.StringValue(emailA), Name: types.StringValue(nameA), ServiceAccountId: types.StringValue(idA)},
{Email: types.StringValue(emailB), Name: types.StringValue(nameB), ServiceAccountId: types.StringValue(idB)},
{Email: types.StringValue(emailC), Name: types.StringValue(nameC), ServiceAccountId: types.StringValue(idC)},
},
},
isValid: true,
},
{
description: "regex_filter_match",
apiItems: []serviceaccount.ServiceAccount{
{Email: emailA, Id: idA},
{Email: emailB, Id: idB},
{Email: emailC, Id: idC},
},
initialModel: ServiceAccountsModel{
ProjectId: types.StringValue(projectId),
EmailRegex: types.StringValue(`.*-b-.*`),
SortAscending: types.BoolValue(true),
},
regexStr: `.*-b-.*`,
expectedModel: ServiceAccountsModel{
Id: types.StringValue(projectId),
ProjectId: types.StringValue(projectId),
EmailRegex: types.StringValue(`.*-b-.*`),
SortAscending: types.BoolValue(true),
Items: []ServiceAccountItem{
{Email: types.StringValue(emailB), Name: types.StringValue(nameB), ServiceAccountId: types.StringValue(idB)},
},
},
isValid: true,
},
{
description: "suffix_filter_match",
apiItems: []serviceaccount.ServiceAccount{
{Email: emailA, Id: idA},
{Email: emailB, Id: idB},
{Email: emailC, Id: idC},
},
initialModel: ServiceAccountsModel{
ProjectId: types.StringValue(projectId),
EmailSuffix: types.StringValue(`@ske.sa.stackit.cloud`),
},
expectedModel: ServiceAccountsModel{
Id: types.StringValue(projectId),
ProjectId: types.StringValue(projectId),
EmailSuffix: types.StringValue(`@ske.sa.stackit.cloud`),
Items: []ServiceAccountItem{
{Email: types.StringValue(emailC), Name: types.StringValue(nameC), ServiceAccountId: types.StringValue(idC)},
},
},
isValid: true,
},
{
description: "nil_model",
apiItems: []serviceaccount.ServiceAccount{},
initialModel: ServiceAccountsModel{},
isValid: false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
var compiledRegex *regexp.Regexp
if tt.regexStr != "" {
compiledRegex = regexp.MustCompile(tt.regexStr)
}
// Handle nil model scenario
var modelPtr *ServiceAccountsModel
if tt.description != "nil_model" {
modelCopy := tt.initialModel
modelPtr = &modelCopy
}
err := mapDataSourceFields(tt.apiItems, modelPtr, compiledRegex)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(*modelPtr, tt.expectedModel, cmp.AllowUnexported(types.String{}, types.Bool{}))
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}