-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathmodel_test.go
121 lines (111 loc) · 3.78 KB
/
model_test.go
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
package searchdeployment_test
import (
"reflect"
"testing"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment"
// TODO: update before merging to master: "go.mongodb.org/atlas-sdk/v20250219001/admin"
"github.com/mongodb/atlas-sdk-go/admin"
)
type sdkToTFModelTestCase struct {
SDKResp *admin.ApiSearchDeploymentResponse
expectedTFModel *searchdeployment.TFSearchDeploymentRSModel
name string
clusterName string
}
const (
dummyDeploymentID = "111111111111111111111111"
dummyProjectID = "222222222222222222222222"
stateName = "IDLE"
clusterName = "Cluster0"
instanceSize = "S20_HIGHCPU_NVME"
nodeCount = 2
earProvider = "AWS"
)
func TestSearchDeploymentSDKToTFModel(t *testing.T) {
testCases := []sdkToTFModelTestCase{
{
name: "Complete SDK response",
clusterName: clusterName,
SDKResp: &admin.ApiSearchDeploymentResponse{
Id: admin.PtrString(dummyDeploymentID),
GroupId: admin.PtrString(dummyProjectID),
StateName: admin.PtrString(stateName),
Specs: &[]admin.ApiSearchDeploymentSpec{
{
InstanceSize: instanceSize,
NodeCount: nodeCount,
},
},
EncryptionAtRestProvider: admin.PtrString(earProvider),
},
expectedTFModel: &searchdeployment.TFSearchDeploymentRSModel{
ID: types.StringValue(dummyDeploymentID),
ClusterName: types.StringValue(clusterName),
ProjectID: types.StringValue(dummyProjectID),
StateName: types.StringValue(stateName),
Specs: tfSpecsList(t, instanceSize, nodeCount),
EncryptionAtRestProvider: types.StringValue(earProvider),
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resultModel, diags := searchdeployment.NewTFSearchDeployment(t.Context(), tc.clusterName, tc.SDKResp, nil, false)
if diags.HasError() {
t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary())
}
if !reflect.DeepEqual(resultModel, tc.expectedTFModel) {
t.Errorf("created terraform model did not match expected output")
}
})
}
}
func TestSearchDeploymentTFModelToSDK(t *testing.T) {
testCases := []struct {
name string
tfModel *searchdeployment.TFSearchDeploymentRSModel
expectedSDKReq admin.ApiSearchDeploymentRequest
}{
{
name: "Complete TF state",
tfModel: &searchdeployment.TFSearchDeploymentRSModel{
ID: types.StringValue(dummyDeploymentID),
ClusterName: types.StringValue(clusterName),
ProjectID: types.StringValue(dummyProjectID),
StateName: types.StringValue(stateName),
Specs: tfSpecsList(t, instanceSize, nodeCount),
},
expectedSDKReq: admin.ApiSearchDeploymentRequest{
Specs: []admin.ApiSearchDeploymentSpec{
{
InstanceSize: instanceSize,
NodeCount: nodeCount,
},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
apiReqResult := searchdeployment.NewSearchDeploymentReq(t.Context(), tc.tfModel)
if !reflect.DeepEqual(apiReqResult, tc.expectedSDKReq) {
t.Errorf("created sdk model did not match expected output")
}
})
}
}
func tfSpecsList(t *testing.T, instanceSize string, nodeCount int64) basetypes.ListValue {
t.Helper()
tfSpecsList, diags := types.ListValueFrom(t.Context(), searchdeployment.SpecObjectType, []searchdeployment.TFSearchNodeSpecModel{
{
InstanceSize: types.StringValue(instanceSize),
NodeCount: types.Int64Value(nodeCount),
},
})
if diags.HasError() {
t.Errorf("failed to create terraform spec lists model: %s", diags.Errors()[0].Summary())
}
return tfSpecsList
}