Skip to content

Commit e052f9f

Browse files
authored
fix(acctest): compare in depth request body with cassette body (#3135)
* fix(acctest): compare in depth request body with cassette body * feat(acctest): compare slice of map[string]interface{} * tradeoff matching record
1 parent 7731e65 commit e052f9f

File tree

4 files changed

+810
-157
lines changed

4 files changed

+810
-157
lines changed

internal/acctest/acctest.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -102,65 +102,6 @@ func extractGeneratedNamePrefix(name string) string {
102102
return name
103103
}
104104

105-
// compareJSONFieldsStrings compare two strings from request JSON bodies
106-
// has special case when string are terraform generated names
107-
func compareJSONFieldsStrings(expected, actual string) bool {
108-
expectedHandled := expected
109-
actualHandled := actual
110-
111-
// Remove s3 url suffix to allow comparison
112-
if strings.HasSuffix(actual, ".s3-website.fr-par.scw.cloud") {
113-
actual = strings.TrimSuffix(actual, ".s3-website.fr-par.scw.cloud")
114-
expected = strings.TrimSuffix(expected, ".s3-website.fr-par.scw.cloud")
115-
}
116-
117-
// Try to parse test generated name
118-
if strings.Contains(actual, "-") {
119-
expectedHandled = extractTestGeneratedNamePrefix(expected)
120-
actualHandled = extractTestGeneratedNamePrefix(actual)
121-
}
122-
123-
// Try provider generated name
124-
if actualHandled == actual && strings.HasPrefix(actual, "tf-") {
125-
expectedHandled = extractGeneratedNamePrefix(expected)
126-
actualHandled = extractGeneratedNamePrefix(actual)
127-
}
128-
129-
return expectedHandled == actualHandled
130-
}
131-
132-
// compareJSONBodies compare two given maps that represent json bodies
133-
// returns true if both json are equivalent
134-
func compareJSONBodies(expected, actual map[string]any) bool {
135-
// Check for each key in actual requests
136-
// Compare its value to cassette content if marshal-able to string
137-
for key := range actual {
138-
expectedValue, exists := expected[key]
139-
if !exists {
140-
// Actual request may contain a field that does not exist in cassette
141-
// New fields can appear in requests with new api features
142-
// We do not want to generate new cassettes for each new features
143-
continue
144-
}
145-
146-
if !compareJSONFields(expectedValue, actual[key]) {
147-
return false
148-
}
149-
}
150-
151-
for key := range expected {
152-
_, exists := actual[key]
153-
if !exists && expected[key] != nil {
154-
// Fails match if cassettes contains a field not in actual requests
155-
// Fields should not disappear from requests unless a sdk breaking change
156-
// We ignore if field is nil in cassette as it could be an old deprecated and unused field
157-
return false
158-
}
159-
}
160-
161-
return true
162-
}
163-
164105
// IsTestResource returns true if given resource identifier is from terraform test
165106
// identifier should be resource name but some resource don't have names
166107
// return true if identifier match regex "tf[-_]test"

0 commit comments

Comments
 (0)