Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit tests for diff suppress funcs #3496

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/5050.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
4 changes: 2 additions & 2 deletions google-beta/common_diff_suppress.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ func locationDiffSuppressHelper(a, b string) bool {
// For managed SSL certs, if new is an absolute FQDN (trailing '.') but old isn't, treat them as equals.
func absoluteDomainSuppress(k, old, new string, _ *schema.ResourceData) bool {
if strings.HasPrefix(k, "managed.0.domains.") {
return old == strings.TrimRight(new, ".")
return old == strings.TrimRight(new, ".") || new == strings.TrimRight(old, ".")
}
return old == new
return false
}

func timestampDiffSuppress(format string) schema.SchemaDiffSuppressFunc {
Expand Down
286 changes: 286 additions & 0 deletions google-beta/common_diff_suppress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
// Contains common diff suppress functions.

package google

import "testing"

func TestOptionalPrefixSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
Prefix string
ExpectDiffSuppress bool
}{
"with same prefix": {
Old: "my-folder",
New: "folders/my-folder",
Prefix: "folders/",
ExpectDiffSuppress: true,
},
"with different prefix": {
Old: "folders/my-folder",
New: "organizations/my-folder",
Prefix: "folders/",
ExpectDiffSuppress: false,
},
"same without prefix": {
Old: "my-folder",
New: "my-folder",
Prefix: "folders/",
ExpectDiffSuppress: false,
},
"different without prefix": {
Old: "my-folder",
New: "my-new-folder",
Prefix: "folders/",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
if optionalPrefixSuppress(tc.Prefix)("folder", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func TestIgnoreMissingKeyInMap(t *testing.T) {
cases := map[string]struct {
Old, New string
Key string
ExpectDiffSuppress bool
}{
"missing key in map": {
Old: "",
New: "v1",
Key: "x-goog-version",
ExpectDiffSuppress: true,
},
"different values": {
Old: "v1",
New: "v2",
Key: "x-goog-version",
ExpectDiffSuppress: false,
},
"same values": {
Old: "v1",
New: "v1",
Key: "x-goog-version",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
if ignoreMissingKeyInMap(tc.Key)("push_config.0.attributes."+tc.Key, tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func TestOptionalSurroundingSpacesSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"surrounding spaces": {
Old: "value",
New: " value ",
ExpectDiffSuppress: true,
},
"no surrounding spaces": {
Old: "value",
New: "value",
ExpectDiffSuppress: true,
},
"one space each": {
Old: " value",
New: "value ",
ExpectDiffSuppress: true,
},
"different values": {
Old: " different",
New: "values ",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
if optionalSurroundingSpacesSuppress("filter", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func TestCaseDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"differents cases": {
Old: "Value",
New: "value",
ExpectDiffSuppress: true,
},
"different values": {
Old: "value",
New: "NewValue",
ExpectDiffSuppress: false,
},
"same cases": {
Old: "value",
New: "value",
ExpectDiffSuppress: true,
},
}

for tn, tc := range cases {
if caseDiffSuppress("key", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func TestPortRangeDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"different single values": {
Old: "80-80",
New: "443",
ExpectDiffSuppress: false,
},
"different ranges": {
Old: "80-80",
New: "443-444",
ExpectDiffSuppress: false,
},
"same single values": {
Old: "80-80",
New: "80",
ExpectDiffSuppress: true,
},
"same ranges": {
Old: "80-80",
New: "80-80",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
if portRangeDiffSuppress("ports", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func TestLocationDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"locations to zones": {
Old: "projects/x/locations/y/resource/z",
New: "projects/x/zones/y/resource/z",
ExpectDiffSuppress: true,
},
"regions to locations": {
Old: "projects/x/regions/y/resource/z",
New: "projects/x/locations/y/resource/z",
ExpectDiffSuppress: true,
},
"locations to locations": {
Old: "projects/x/locations/y/resource/z",
New: "projects/x/locations/y/resource/z",
ExpectDiffSuppress: false,
},
"zones to regions": {
Old: "projects/x/zones/y/resource/z",
New: "projects/x/regions/y/resource/z",
ExpectDiffSuppress: false,
},
"different locations": {
Old: "projects/x/locations/a/resource/z",
New: "projects/x/locations/b/resource/z",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
if locationDiffSuppress("policy_uri", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func TestAbsoluteDomainSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"new trailing dot": {
Old: "sslcert.tf-test.club",
New: "sslcert.tf-test.club.",
ExpectDiffSuppress: true,
},
"old trailing dot": {
Old: "sslcert.tf-test.club.",
New: "sslcert.tf-test.club",
ExpectDiffSuppress: true,
},
"same trailing dot": {
Old: "sslcert.tf-test.club.",
New: "sslcert.tf-test.club.",
ExpectDiffSuppress: false,
},
"different trailing dot": {
Old: "sslcert.tf-test.club.",
New: "sslcert.tf-test.clubs.",
ExpectDiffSuppress: false,
},
"different no trailing dot": {
Old: "sslcert.tf-test.club",
New: "sslcert.tf-test.clubs",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
if absoluteDomainSuppress("managed.0.domains.", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func TestDurationDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"different values": {
Old: "60s",
New: "65s",
ExpectDiffSuppress: false,
},
"same values": {
Old: "60s",
New: "60s",
ExpectDiffSuppress: true,
},
"different values, different formats": {
Old: "65s",
New: "60.0s",
ExpectDiffSuppress: false,
},
"same values, different formats": {
Old: "60.0s",
New: "60s",
ExpectDiffSuppress: true,
},
}

for tn, tc := range cases {
if durationDiffSuppress("duration", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}
2 changes: 1 addition & 1 deletion google-beta/resource_gke_hub_feature_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand Down