-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathdatasource_test.go
More file actions
122 lines (118 loc) · 3.29 KB
/
datasource_test.go
File metadata and controls
122 lines (118 loc) · 3.29 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
package objectstorage
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)
func TestMapDatasourceFields(t *testing.T) {
now := time.Now()
tests := []struct {
description string
input *objectstorage.AccessKey
expected DataSourceModel
isValid bool
}{
{
"default_values",
&objectstorage.AccessKey{},
DataSourceModel{
Id: types.StringValue("pid,cgid,cid"),
ProjectId: types.StringValue("pid"),
CredentialsGroupId: types.StringValue("cgid"),
CredentialId: types.StringValue("cid"),
Name: types.StringNull(),
ExpirationTimestamp: types.StringNull(),
Region: types.StringValue("eu01"),
},
true,
},
{
"simple_values",
&objectstorage.AccessKey{
DisplayName: utils.Ptr("name"),
Expires: utils.Ptr(now.Format(time.RFC3339)),
},
DataSourceModel{
Id: types.StringValue("pid,cgid,cid"),
ProjectId: types.StringValue("pid"),
CredentialsGroupId: types.StringValue("cgid"),
CredentialId: types.StringValue("cid"),
Name: types.StringValue("name"),
ExpirationTimestamp: types.StringValue(now.Format(time.RFC3339)),
Region: types.StringValue("eu01"),
},
true,
},
{
"empty_strings",
&objectstorage.AccessKey{
DisplayName: utils.Ptr(""),
},
DataSourceModel{
Id: types.StringValue("pid,cgid,cid"),
ProjectId: types.StringValue("pid"),
CredentialsGroupId: types.StringValue("cgid"),
CredentialId: types.StringValue("cid"),
Name: types.StringValue(""),
ExpirationTimestamp: types.StringNull(),
Region: types.StringValue("eu01"),
},
true,
},
{
"expiration_timestamp_with_fractional_seconds",
&objectstorage.AccessKey{
Expires: utils.Ptr(now.Format(time.RFC3339Nano)),
},
DataSourceModel{
Id: types.StringValue("pid,cgid,cid"),
ProjectId: types.StringValue("pid"),
CredentialsGroupId: types.StringValue("cgid"),
CredentialId: types.StringValue("cid"),
Name: types.StringNull(),
ExpirationTimestamp: types.StringValue(now.Format(time.RFC3339)),
Region: types.StringValue("eu01"),
},
true,
},
{
"nil_response",
nil,
DataSourceModel{},
false,
},
{
"bad_time",
&objectstorage.AccessKey{
Expires: utils.Ptr("foo-bar"),
},
DataSourceModel{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
model := &DataSourceModel{
ProjectId: tt.expected.ProjectId,
CredentialsGroupId: tt.expected.CredentialsGroupId,
CredentialId: tt.expected.CredentialId,
}
err := mapDataSourceFields(tt.input, model, "eu01")
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(model, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}