Skip to content

Commit 27d068f

Browse files
authored
test: Adds unit test to understand replication ID matching (#2358)
* test: Adds unit tests for FlattenAdvancedReplicationSpecs * simplify test cases * address PR comments * address PR comments 2 * test: revert back to []any for region_configs
1 parent 3bf7b72 commit 27d068f

File tree

5 files changed

+129
-9
lines changed

5 files changed

+129
-9
lines changed

internal/service/advancedcluster/data_source_advanced_cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
302302
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pit_enabled", clusterName, err))
303303
}
304304

305-
replicationSpecs, err := flattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connV2)
305+
replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connV2)
306306
if err != nil {
307307
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err))
308308
}

internal/service/advancedcluster/data_source_advanced_clusters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func flattenAdvancedClusters(ctx context.Context, connV2 *admin.APIClient, clust
271271
if err != nil {
272272
log.Printf("[WARN] Error setting `advanced_configuration` for the cluster(%s): %s", cluster.GetId(), err)
273273
}
274-
replicationSpecs, err := flattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), nil, d, connV2)
274+
replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), nil, d, connV2)
275275
if err != nil {
276276
log.Printf("[WARN] Error setting `replication_specs` for the cluster(%s): %s", cluster.GetId(), err)
277277
}

internal/service/advancedcluster/model_advanced_cluster.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func flattenProcessArgs(p *admin.ClusterDescriptionProcessArgs) []map[string]any
434434
}
435435
}
436436

437-
func flattenAdvancedReplicationSpecs(ctx context.Context, apiObjects []admin.ReplicationSpec, tfMapObjects []any,
437+
func FlattenAdvancedReplicationSpecs(ctx context.Context, apiObjects []admin.ReplicationSpec, tfMapObjects []any,
438438
d *schema.ResourceData, connV2 *admin.APIClient) ([]map[string]any, error) {
439439
if len(apiObjects) == 0 {
440440
return nil, nil
@@ -451,11 +451,7 @@ func flattenAdvancedReplicationSpecs(ctx context.Context, apiObjects []admin.Rep
451451
}
452452

453453
for j := 0; j < len(apiObjects); j++ {
454-
if wasAPIObjectUsed[j] {
455-
continue
456-
}
457-
458-
if !doesAdvancedReplicationSpecMatchAPI(tfMapObject, &apiObjects[j]) {
454+
if wasAPIObjectUsed[j] || !doesAdvancedReplicationSpecMatchAPI(tfMapObject, &apiObjects[j]) {
459455
continue
460456
}
461457

internal/service/advancedcluster/model_advanced_cluster_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"net/http"
77
"testing"
88

9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
1011
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/mock"
14+
"github.com/stretchr/testify/require"
1315
"go.mongodb.org/atlas-sdk/v20231115014/admin"
1416
"go.mongodb.org/atlas-sdk/v20231115014/mockadmin"
1517
)
@@ -21,6 +23,128 @@ var (
2123
advancedClusters = []admin.AdvancedClusterDescription{{StateName: conversion.StringPtr("NOT IDLE")}}
2224
)
2325

26+
func TestFlattenReplicationSpecs(t *testing.T) {
27+
var (
28+
regionName = "EU_WEST_1"
29+
providerName = "AWS"
30+
expectedID = "id1"
31+
unexpectedID = "id2"
32+
expectedZoneName = "z1"
33+
unexpectedZoneName = "z2"
34+
regionConfigAdmin = []admin.CloudRegionConfig{{
35+
ProviderName: &providerName,
36+
RegionName: &regionName,
37+
}}
38+
regionConfigTfSameZone = map[string]any{
39+
"provider_name": "AWS",
40+
"region_name": regionName,
41+
}
42+
regionConfigTfDiffZone = map[string]any{
43+
"provider_name": "AWS",
44+
"region_name": regionName,
45+
"zone_name": unexpectedZoneName,
46+
}
47+
apiSpecExpected = admin.ReplicationSpec{Id: &expectedID, ZoneName: &expectedZoneName, RegionConfigs: &regionConfigAdmin}
48+
apiSpecDifferent = admin.ReplicationSpec{Id: &unexpectedID, ZoneName: &unexpectedZoneName, RegionConfigs: &regionConfigAdmin}
49+
testSchema = map[string]*schema.Schema{
50+
"project_id": {Type: schema.TypeString},
51+
}
52+
tfSameIDSameZone = map[string]any{
53+
"id": expectedID,
54+
"num_shards": 1,
55+
"region_configs": []any{regionConfigTfSameZone},
56+
"zone_name": expectedZoneName,
57+
}
58+
tfNoIDSameZone = map[string]any{
59+
"id": nil,
60+
"num_shards": 1,
61+
"region_configs": []any{regionConfigTfSameZone},
62+
"zone_name": expectedZoneName,
63+
}
64+
tfNoIDDiffZone = map[string]any{
65+
"id": nil,
66+
"num_shards": 1,
67+
"region_configs": []any{regionConfigTfDiffZone},
68+
"zone_name": unexpectedZoneName,
69+
}
70+
tfdiffIDDiffZone = map[string]any{
71+
"id": "unique",
72+
"num_shards": 1,
73+
"region_configs": []any{regionConfigTfDiffZone},
74+
"zone_name": unexpectedZoneName,
75+
}
76+
)
77+
testCases := map[string]struct {
78+
adminSpecs []admin.ReplicationSpec
79+
tfInputSpecs []any
80+
expectedLen int
81+
}{
82+
"empty admin spec should return empty list": {
83+
[]admin.ReplicationSpec{},
84+
[]any{tfSameIDSameZone},
85+
0,
86+
},
87+
"existing id, should match admin": {
88+
[]admin.ReplicationSpec{apiSpecExpected},
89+
[]any{tfSameIDSameZone},
90+
1,
91+
},
92+
"existing different id, should change to admin spec": {
93+
[]admin.ReplicationSpec{apiSpecExpected},
94+
[]any{tfdiffIDDiffZone},
95+
1,
96+
},
97+
"missing id, should be set when zone_name matches": {
98+
[]admin.ReplicationSpec{apiSpecExpected},
99+
[]any{tfNoIDSameZone},
100+
1,
101+
},
102+
"missing id and diff zone, should change to admin spec": {
103+
[]admin.ReplicationSpec{apiSpecExpected},
104+
[]any{tfNoIDDiffZone},
105+
1,
106+
},
107+
"existing id, should match correct api spec using `id` and extra api spec added": {
108+
[]admin.ReplicationSpec{apiSpecDifferent, apiSpecExpected},
109+
[]any{tfSameIDSameZone},
110+
2,
111+
},
112+
"missing id, should match correct api spec using `zone_name` and extra api spec added": {
113+
[]admin.ReplicationSpec{apiSpecDifferent, apiSpecExpected},
114+
[]any{tfNoIDSameZone},
115+
2,
116+
},
117+
"two matching specs should be set to api specs": {
118+
[]admin.ReplicationSpec{apiSpecExpected, apiSpecDifferent},
119+
[]any{tfSameIDSameZone, tfdiffIDDiffZone},
120+
2,
121+
},
122+
}
123+
for name, tc := range testCases {
124+
t.Run(name, func(t *testing.T) {
125+
peeringAPI := mockadmin.NetworkPeeringApi{}
126+
127+
peeringAPI.EXPECT().ListPeeringContainerByCloudProviderWithParams(mock.Anything, mock.Anything).Return(admin.ListPeeringContainerByCloudProviderApiRequest{ApiService: &peeringAPI})
128+
containerResult := []admin.CloudProviderContainer{{Id: conversion.StringPtr("c1"), RegionName: &regionName, ProviderName: &providerName}}
129+
peeringAPI.EXPECT().ListPeeringContainerByCloudProviderExecute(mock.Anything).Return(&admin.PaginatedCloudProviderContainer{Results: &containerResult}, nil, nil)
130+
131+
client := &admin.APIClient{
132+
NetworkPeeringApi: &peeringAPI,
133+
}
134+
resourceData := schema.TestResourceDataRaw(t, testSchema, map[string]any{"project_id": "p1"})
135+
136+
tfOutputSpecs, err := advancedcluster.FlattenAdvancedReplicationSpecs(context.Background(), tc.adminSpecs, tc.tfInputSpecs, resourceData, client)
137+
138+
require.NoError(t, err)
139+
assert.Len(t, tfOutputSpecs, tc.expectedLen)
140+
if tc.expectedLen != 0 {
141+
assert.Equal(t, expectedID, tfOutputSpecs[0]["id"])
142+
assert.Equal(t, expectedZoneName, tfOutputSpecs[0]["zone_name"])
143+
}
144+
})
145+
}
146+
}
147+
24148
type Result struct {
25149
response any
26150
error error

internal/service/advancedcluster/resource_advanced_cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
552552
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "pit_enabled", clusterName, err))
553553
}
554554

555-
replicationSpecs, err := flattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connV2)
555+
replicationSpecs, err := FlattenAdvancedReplicationSpecs(ctx, cluster.GetReplicationSpecs(), d.Get("replication_specs").([]any), d, connV2)
556556
if err != nil {
557557
return diag.FromErr(fmt.Errorf(ErrorClusterAdvancedSetting, "replication_specs", clusterName, err))
558558
}

0 commit comments

Comments
 (0)