Skip to content

Commit 9c078c8

Browse files
add unit tests for diff suppress funcs (#5050) (#3496)
* add unit test for optionalPrefixSuppress * add more unit tests * add more diff suppress unit tests * add new line Signed-off-by: Modular Magician <[email protected]>
1 parent 37ca20f commit 9c078c8

File tree

4 files changed

+292
-3
lines changed

4 files changed

+292
-3
lines changed

.changelog/5050.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```

google-beta/common_diff_suppress.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ func locationDiffSuppressHelper(a, b string) bool {
142142
// For managed SSL certs, if new is an absolute FQDN (trailing '.') but old isn't, treat them as equals.
143143
func absoluteDomainSuppress(k, old, new string, _ *schema.ResourceData) bool {
144144
if strings.HasPrefix(k, "managed.0.domains.") {
145-
return old == strings.TrimRight(new, ".")
145+
return old == strings.TrimRight(new, ".") || new == strings.TrimRight(old, ".")
146146
}
147-
return old == new
147+
return false
148148
}
149149

150150
func timestampDiffSuppress(format string) schema.SchemaDiffSuppressFunc {
Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
// Contains common diff suppress functions.
2+
3+
package google
4+
5+
import "testing"
6+
7+
func TestOptionalPrefixSuppress(t *testing.T) {
8+
cases := map[string]struct {
9+
Old, New string
10+
Prefix string
11+
ExpectDiffSuppress bool
12+
}{
13+
"with same prefix": {
14+
Old: "my-folder",
15+
New: "folders/my-folder",
16+
Prefix: "folders/",
17+
ExpectDiffSuppress: true,
18+
},
19+
"with different prefix": {
20+
Old: "folders/my-folder",
21+
New: "organizations/my-folder",
22+
Prefix: "folders/",
23+
ExpectDiffSuppress: false,
24+
},
25+
"same without prefix": {
26+
Old: "my-folder",
27+
New: "my-folder",
28+
Prefix: "folders/",
29+
ExpectDiffSuppress: false,
30+
},
31+
"different without prefix": {
32+
Old: "my-folder",
33+
New: "my-new-folder",
34+
Prefix: "folders/",
35+
ExpectDiffSuppress: false,
36+
},
37+
}
38+
39+
for tn, tc := range cases {
40+
if optionalPrefixSuppress(tc.Prefix)("folder", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
41+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
42+
}
43+
}
44+
}
45+
46+
func TestIgnoreMissingKeyInMap(t *testing.T) {
47+
cases := map[string]struct {
48+
Old, New string
49+
Key string
50+
ExpectDiffSuppress bool
51+
}{
52+
"missing key in map": {
53+
Old: "",
54+
New: "v1",
55+
Key: "x-goog-version",
56+
ExpectDiffSuppress: true,
57+
},
58+
"different values": {
59+
Old: "v1",
60+
New: "v2",
61+
Key: "x-goog-version",
62+
ExpectDiffSuppress: false,
63+
},
64+
"same values": {
65+
Old: "v1",
66+
New: "v1",
67+
Key: "x-goog-version",
68+
ExpectDiffSuppress: false,
69+
},
70+
}
71+
72+
for tn, tc := range cases {
73+
if ignoreMissingKeyInMap(tc.Key)("push_config.0.attributes."+tc.Key, tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
74+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
75+
}
76+
}
77+
}
78+
79+
func TestOptionalSurroundingSpacesSuppress(t *testing.T) {
80+
cases := map[string]struct {
81+
Old, New string
82+
ExpectDiffSuppress bool
83+
}{
84+
"surrounding spaces": {
85+
Old: "value",
86+
New: " value ",
87+
ExpectDiffSuppress: true,
88+
},
89+
"no surrounding spaces": {
90+
Old: "value",
91+
New: "value",
92+
ExpectDiffSuppress: true,
93+
},
94+
"one space each": {
95+
Old: " value",
96+
New: "value ",
97+
ExpectDiffSuppress: true,
98+
},
99+
"different values": {
100+
Old: " different",
101+
New: "values ",
102+
ExpectDiffSuppress: false,
103+
},
104+
}
105+
106+
for tn, tc := range cases {
107+
if optionalSurroundingSpacesSuppress("filter", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
108+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
109+
}
110+
}
111+
}
112+
113+
func TestCaseDiffSuppress(t *testing.T) {
114+
cases := map[string]struct {
115+
Old, New string
116+
ExpectDiffSuppress bool
117+
}{
118+
"differents cases": {
119+
Old: "Value",
120+
New: "value",
121+
ExpectDiffSuppress: true,
122+
},
123+
"different values": {
124+
Old: "value",
125+
New: "NewValue",
126+
ExpectDiffSuppress: false,
127+
},
128+
"same cases": {
129+
Old: "value",
130+
New: "value",
131+
ExpectDiffSuppress: true,
132+
},
133+
}
134+
135+
for tn, tc := range cases {
136+
if caseDiffSuppress("key", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
137+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
138+
}
139+
}
140+
}
141+
142+
func TestPortRangeDiffSuppress(t *testing.T) {
143+
cases := map[string]struct {
144+
Old, New string
145+
ExpectDiffSuppress bool
146+
}{
147+
"different single values": {
148+
Old: "80-80",
149+
New: "443",
150+
ExpectDiffSuppress: false,
151+
},
152+
"different ranges": {
153+
Old: "80-80",
154+
New: "443-444",
155+
ExpectDiffSuppress: false,
156+
},
157+
"same single values": {
158+
Old: "80-80",
159+
New: "80",
160+
ExpectDiffSuppress: true,
161+
},
162+
"same ranges": {
163+
Old: "80-80",
164+
New: "80-80",
165+
ExpectDiffSuppress: false,
166+
},
167+
}
168+
169+
for tn, tc := range cases {
170+
if portRangeDiffSuppress("ports", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
171+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
172+
}
173+
}
174+
}
175+
176+
func TestLocationDiffSuppress(t *testing.T) {
177+
cases := map[string]struct {
178+
Old, New string
179+
ExpectDiffSuppress bool
180+
}{
181+
"locations to zones": {
182+
Old: "projects/x/locations/y/resource/z",
183+
New: "projects/x/zones/y/resource/z",
184+
ExpectDiffSuppress: true,
185+
},
186+
"regions to locations": {
187+
Old: "projects/x/regions/y/resource/z",
188+
New: "projects/x/locations/y/resource/z",
189+
ExpectDiffSuppress: true,
190+
},
191+
"locations to locations": {
192+
Old: "projects/x/locations/y/resource/z",
193+
New: "projects/x/locations/y/resource/z",
194+
ExpectDiffSuppress: false,
195+
},
196+
"zones to regions": {
197+
Old: "projects/x/zones/y/resource/z",
198+
New: "projects/x/regions/y/resource/z",
199+
ExpectDiffSuppress: false,
200+
},
201+
"different locations": {
202+
Old: "projects/x/locations/a/resource/z",
203+
New: "projects/x/locations/b/resource/z",
204+
ExpectDiffSuppress: false,
205+
},
206+
}
207+
208+
for tn, tc := range cases {
209+
if locationDiffSuppress("policy_uri", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
210+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
211+
}
212+
}
213+
}
214+
215+
func TestAbsoluteDomainSuppress(t *testing.T) {
216+
cases := map[string]struct {
217+
Old, New string
218+
ExpectDiffSuppress bool
219+
}{
220+
"new trailing dot": {
221+
Old: "sslcert.tf-test.club",
222+
New: "sslcert.tf-test.club.",
223+
ExpectDiffSuppress: true,
224+
},
225+
"old trailing dot": {
226+
Old: "sslcert.tf-test.club.",
227+
New: "sslcert.tf-test.club",
228+
ExpectDiffSuppress: true,
229+
},
230+
"same trailing dot": {
231+
Old: "sslcert.tf-test.club.",
232+
New: "sslcert.tf-test.club.",
233+
ExpectDiffSuppress: false,
234+
},
235+
"different trailing dot": {
236+
Old: "sslcert.tf-test.club.",
237+
New: "sslcert.tf-test.clubs.",
238+
ExpectDiffSuppress: false,
239+
},
240+
"different no trailing dot": {
241+
Old: "sslcert.tf-test.club",
242+
New: "sslcert.tf-test.clubs",
243+
ExpectDiffSuppress: false,
244+
},
245+
}
246+
247+
for tn, tc := range cases {
248+
if absoluteDomainSuppress("managed.0.domains.", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
249+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
250+
}
251+
}
252+
}
253+
254+
func TestDurationDiffSuppress(t *testing.T) {
255+
cases := map[string]struct {
256+
Old, New string
257+
ExpectDiffSuppress bool
258+
}{
259+
"different values": {
260+
Old: "60s",
261+
New: "65s",
262+
ExpectDiffSuppress: false,
263+
},
264+
"same values": {
265+
Old: "60s",
266+
New: "60s",
267+
ExpectDiffSuppress: true,
268+
},
269+
"different values, different formats": {
270+
Old: "65s",
271+
New: "60.0s",
272+
ExpectDiffSuppress: false,
273+
},
274+
"same values, different formats": {
275+
Old: "60.0s",
276+
New: "60s",
277+
ExpectDiffSuppress: true,
278+
},
279+
}
280+
281+
for tn, tc := range cases {
282+
if durationDiffSuppress("duration", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
283+
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
284+
}
285+
}
286+
}

google-beta/resource_gke_hub_feature_membership_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8-
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
8+
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
99
gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

0 commit comments

Comments
 (0)