Skip to content

fix(acctest): compare in depth request body with cassette body #3135

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

Merged
merged 3 commits into from
Jun 18, 2025
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
59 changes: 0 additions & 59 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,65 +102,6 @@ func extractGeneratedNamePrefix(name string) string {
return name
}

// compareJSONFieldsStrings compare two strings from request JSON bodies
// has special case when string are terraform generated names
func compareJSONFieldsStrings(expected, actual string) bool {
expectedHandled := expected
actualHandled := actual

// Remove s3 url suffix to allow comparison
if strings.HasSuffix(actual, ".s3-website.fr-par.scw.cloud") {
actual = strings.TrimSuffix(actual, ".s3-website.fr-par.scw.cloud")
expected = strings.TrimSuffix(expected, ".s3-website.fr-par.scw.cloud")
}

// Try to parse test generated name
if strings.Contains(actual, "-") {
expectedHandled = extractTestGeneratedNamePrefix(expected)
actualHandled = extractTestGeneratedNamePrefix(actual)
}

// Try provider generated name
if actualHandled == actual && strings.HasPrefix(actual, "tf-") {
expectedHandled = extractGeneratedNamePrefix(expected)
actualHandled = extractGeneratedNamePrefix(actual)
}

return expectedHandled == actualHandled
}

// compareJSONBodies compare two given maps that represent json bodies
// returns true if both json are equivalent
func compareJSONBodies(expected, actual map[string]any) bool {
// Check for each key in actual requests
// Compare its value to cassette content if marshal-able to string
for key := range actual {
expectedValue, exists := expected[key]
if !exists {
// Actual request may contain a field that does not exist in cassette
// New fields can appear in requests with new api features
// We do not want to generate new cassettes for each new features
continue
}

if !compareJSONFields(expectedValue, actual[key]) {
return false
}
}

for key := range expected {
_, exists := actual[key]
if !exists && expected[key] != nil {
// Fails match if cassettes contains a field not in actual requests
// Fields should not disappear from requests unless a sdk breaking change
// We ignore if field is nil in cassette as it could be an old deprecated and unused field
return false
}
}

return true
}

// IsTestResource returns true if given resource identifier is from terraform test
// identifier should be resource name but some resource don't have names
// return true if identifier match regex "tf[-_]test"
Expand Down
Loading
Loading