Skip to content

Commit ebbc7a8

Browse files
authored
test: removed testhelper duplicate code (#646)
Replaced any code in local PR tests that checks for plan consistency with a call to the new public function `testhelper.CheckConsistency()`
1 parent 7439657 commit ebbc7a8

File tree

3 files changed

+19
-144
lines changed

3 files changed

+19
-144
lines changed

tests/go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ toolchain go1.21.3
66

77
require (
88
github.com/gruntwork-io/terratest v0.46.8
9-
github.com/hashicorp/terraform-json v0.18.0
9+
github.com/hashicorp/terraform-json v0.20.0
1010
github.com/stretchr/testify v1.8.4
11-
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.25.7
11+
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.26.2
1212
)
1313

1414
require github.com/IBM/schematics-go-sdk v0.2.2 // indirect
@@ -45,13 +45,13 @@ require (
4545
github.com/go-logr/logr v1.3.0 // indirect
4646
github.com/go-logr/stdr v1.2.2 // indirect
4747
github.com/go-openapi/analysis v0.21.4 // indirect
48-
github.com/go-openapi/errors v0.20.4 // indirect
48+
github.com/go-openapi/errors v0.21.0 // indirect
4949
github.com/go-openapi/jsonpointer v0.20.0 // indirect
5050
github.com/go-openapi/jsonreference v0.20.2 // indirect
5151
github.com/go-openapi/loads v0.21.2 // indirect
5252
github.com/go-openapi/runtime v0.26.0 // indirect
5353
github.com/go-openapi/spec v0.20.11 // indirect
54-
github.com/go-openapi/strfmt v0.21.9 // indirect
54+
github.com/go-openapi/strfmt v0.22.0 // indirect
5555
github.com/go-openapi/swag v0.22.4 // indirect
5656
github.com/go-openapi/validate v0.22.3 // indirect
5757
github.com/go-playground/locales v0.14.1 // indirect
@@ -61,7 +61,7 @@ require (
6161
github.com/golang/protobuf v1.5.3 // indirect
6262
github.com/google/go-cmp v0.6.0 // indirect
6363
github.com/google/s2a-go v0.1.4 // indirect
64-
github.com/google/uuid v1.4.0 // indirect
64+
github.com/google/uuid v1.5.0 // indirect
6565
github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect
6666
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
6767
github.com/hashicorp/errwrap v1.1.0 // indirect
@@ -100,7 +100,7 @@ require (
100100
go.opentelemetry.io/otel v1.16.0 // indirect
101101
go.opentelemetry.io/otel/metric v1.16.0 // indirect
102102
go.opentelemetry.io/otel/trace v1.16.0 // indirect
103-
golang.org/x/crypto v0.16.0 // indirect
103+
golang.org/x/crypto v0.17.0 // indirect
104104
golang.org/x/mod v0.12.0 // indirect
105105
golang.org/x/net v0.19.0 // indirect
106106
golang.org/x/oauth2 v0.10.0 // indirect

tests/go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ github.com/go-openapi/analysis v0.21.4/go.mod h1:4zQ35W4neeZTqh3ol0rv/O8JBbka9Qy
304304
github.com/go-openapi/errors v0.19.8/go.mod h1:cM//ZKUKyO06HSwqAelJ5NsEMMcpa6VpXe8DOa1Mi1M=
305305
github.com/go-openapi/errors v0.20.2/go.mod h1:cM//ZKUKyO06HSwqAelJ5NsEMMcpa6VpXe8DOa1Mi1M=
306306
github.com/go-openapi/errors v0.20.3/go.mod h1:Z3FlZ4I8jEGxjUK+bugx3on2mIAk4txuAOhlsB1FSgk=
307-
github.com/go-openapi/errors v0.20.4 h1:unTcVm6PispJsMECE3zWgvG4xTiKda1LIR5rCRWLG6M=
308-
github.com/go-openapi/errors v0.20.4/go.mod h1:Z3FlZ4I8jEGxjUK+bugx3on2mIAk4txuAOhlsB1FSgk=
307+
github.com/go-openapi/errors v0.21.0 h1:FhChC/duCnfoLj1gZ0BgaBmzhJC2SL/sJr8a2vAobSY=
308+
github.com/go-openapi/errors v0.21.0/go.mod h1:jxNTMUxRCKj65yb/okJGEtahVd7uvWnuWfj53bse4ho=
309309
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
310310
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
311311
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
@@ -324,8 +324,8 @@ github.com/go-openapi/spec v0.20.11/go.mod h1:2OpW+JddWPrpXSCIX8eOx7lZ5iyuWj3RYR
324324
github.com/go-openapi/strfmt v0.21.1/go.mod h1:I/XVKeLc5+MM5oPNN7P6urMOpuLXEcNrCX/rPGuWb0k=
325325
github.com/go-openapi/strfmt v0.21.3/go.mod h1:k+RzNO0Da+k3FrrynSNN8F7n/peCmQQqbbXjtDfvmGg=
326326
github.com/go-openapi/strfmt v0.21.7/go.mod h1:adeGTkxE44sPyLk0JV235VQAO/ZXUr8KAzYjclFs3ew=
327-
github.com/go-openapi/strfmt v0.21.9 h1:LnEGOO9qyEC1v22Bzr323M98G13paIUGPU7yeJtG9Xs=
328-
github.com/go-openapi/strfmt v0.21.9/go.mod h1:0k3v301mglEaZRJdDDGSlN6Npq4VMVU69DE0LUyf7uA=
327+
github.com/go-openapi/strfmt v0.22.0 h1:Ew9PnEYc246TwrEspvBdDHS4BVKXy/AOVsfqGDgAcaI=
328+
github.com/go-openapi/strfmt v0.22.0/go.mod h1:HzJ9kokGIju3/K6ap8jL+OlGAbjpSv27135Yr9OivU4=
329329
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
330330
github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
331331
github.com/go-openapi/swag v0.21.1/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
@@ -432,8 +432,8 @@ github.com/google/s2a-go v0.1.4/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkj
432432
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
433433
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
434434
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
435-
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
436-
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
435+
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
436+
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
437437
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
438438
github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
439439
github.com/googleapis/enterprise-certificate-proxy v0.2.0/go.mod h1:8C0jb7/mgJe/9KK8Lm7X9ctZC2t60YyIpYEI16jx0Qg=
@@ -478,8 +478,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
478478
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
479479
github.com/hashicorp/hcl/v2 v2.17.0 h1:z1XvSUyXd1HP10U4lrLg5e0JMVz6CPaJvAgxM0KNZVY=
480480
github.com/hashicorp/hcl/v2 v2.17.0/go.mod h1:gJyW2PTShkJqQBKpAmPO3yxMxIuoXkOF2TpqXzrQyx4=
481-
github.com/hashicorp/terraform-json v0.18.0 h1:pCjgJEqqDESv4y0Tzdqfxr/edOIGkjs8keY42xfNBwU=
482-
github.com/hashicorp/terraform-json v0.18.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
481+
github.com/hashicorp/terraform-json v0.20.0 h1:cJcvn4gIOTi0SD7pIy+xiofV1zFA3hza+6K+fo52IX8=
482+
github.com/hashicorp/terraform-json v0.20.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
483483
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
484484
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
485485
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
@@ -620,8 +620,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
620620
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
621621
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
622622
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
623-
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.25.7 h1:+TeRzyUZIJ+MRmQnxVPMfcs84adhmjgEL1Tapfo+D2E=
624-
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.25.7/go.mod h1:w9Dlprvtjk0lmVyac+FFUrsy2eLLq/MfYQzpCN/VXg4=
623+
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.26.2 h1:JyA1jmI1Ofbzw6ZOpAaUouyfnyZ9L7LRr1MkvWjmHzw=
624+
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.26.2/go.mod h1:RziMkMMo3B7UEOxU0rx/MQZHFfktil885T1cTRfZHco=
625625
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
626626
github.com/tmccombs/hcl2json v0.5.0 h1:cT2sXStOzKL06c8ZTf9vh+0N8GKGzV7+9RUaY5/iUP8=
627627
github.com/tmccombs/hcl2json v0.5.0/go.mod h1:B0ZpBthAKbQur6yZRKrtaqDmYLCvgnwHOBApE0faCpU=
@@ -685,8 +685,8 @@ golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU
685685
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
686686
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
687687
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
688-
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
689-
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
688+
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
689+
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
690690
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
691691
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
692692
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=

tests/pr_test.go

Lines changed: 1 addition & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"strings"
1111
"testing"
1212

13-
tfjson "github.com/hashicorp/terraform-json"
14-
1513
"github.com/gruntwork-io/terratest/modules/files"
1614
"github.com/gruntwork-io/terratest/modules/logger"
1715
"github.com/gruntwork-io/terratest/modules/random"
@@ -229,28 +227,6 @@ func TestRunUpgradeVpcPattern(t *testing.T) {
229227
}
230228
}
231229

232-
// sanitizeResourceChanges sanitizes the sensitive data in a Terraform JSON Change and returns the sanitized JSON.
233-
func sanitizeResourceChanges(change *tfjson.Change, mergedSensitive map[string]interface{}) (string, error) {
234-
// Marshal the Change to JSON bytes
235-
changesBytes, err := json.MarshalIndent(change, "", " ")
236-
if err != nil {
237-
return "", err
238-
}
239-
changesJson := string(changesBytes)
240-
241-
// Perform sanitization of sensitive data
242-
changesJson, err = common.SanitizeSensitiveData(changesJson, mergedSensitive)
243-
return changesJson, err
244-
}
245-
246-
// handleSanitizationError logs an error message if a sanitization error occurs.
247-
func handleSanitizationError(err error, location string, options *testhelper.TestOptions) {
248-
if err != nil {
249-
errorMessage := fmt.Sprintf("Error sanitizing sensitive data in %s", location)
250-
logger.Log(options.Testing, errorMessage)
251-
}
252-
}
253-
254230
func TestRunOverride(t *testing.T) {
255231
t.Parallel()
256232

@@ -273,108 +249,7 @@ func TestRunOverride(t *testing.T) {
273249

274250
if assert.Nil(t, planErr, "This should not have errored") &&
275251
assert.NotNil(t, planStruct, "Expected some output") {
276-
277-
// defines if at least one resource changed (destroy, update, etc)
278-
resourcesChanged := false
279-
for _, resource := range planStruct.ResourceChangesMap {
280-
// get JSON string of full changes for the logs
281-
changesBytes, changesErr := json.MarshalIndent(resource.Change, "", " ")
282-
// if it errors in the marshall step, just put a placeholder and move on, not important
283-
changesJson := "--UNAVAILABLE--"
284-
if changesErr == nil {
285-
changesJson = string(changesBytes)
286-
}
287-
288-
var resourceDetails string
289-
290-
// Treat all keys in the BeforeSensitive and AfterSensitive maps as sensitive
291-
// Assuming BeforeSensitive and AfterSensitive are of type interface{}
292-
beforeSensitive, beforeSensitiveOK := resource.Change.BeforeSensitive.(map[string]interface{})
293-
afterSensitive, afterSensitiveOK := resource.Change.AfterSensitive.(map[string]interface{})
294-
295-
// Create the mergedSensitive map
296-
mergedSensitive := make(map[string]interface{})
297-
298-
// Check if BeforeSensitive is of the expected type
299-
if beforeSensitiveOK {
300-
// Copy the keys and values from BeforeSensitive to the mergedSensitive map.
301-
for key, value := range beforeSensitive {
302-
mergedSensitive[key] = value
303-
}
304-
}
305-
306-
// Check if AfterSensitive is of the expected type
307-
if afterSensitiveOK {
308-
// Copy the keys and values from AfterSensitive to the mergedSensitive map.
309-
for key, value := range afterSensitive {
310-
mergedSensitive[key] = value
311-
}
312-
}
313-
314-
// Perform sanitization
315-
changesJson, err := sanitizeResourceChanges(resource.Change, mergedSensitive)
316-
if err != nil {
317-
changesJson = "Error sanitizing sensitive data"
318-
logger.Log(options.Testing, changesJson)
319-
}
320-
formatChangesJson, err := common.FormatJsonStringPretty(changesJson)
321-
322-
var formatChangesJsonString string
323-
if err != nil {
324-
logger.Log(options.Testing, "Error formatting JSON, use unformatted")
325-
formatChangesJsonString = changesJson
326-
} else {
327-
formatChangesJsonString = string(formatChangesJson)
328-
}
329-
330-
diff, diffErr := common.GetBeforeAfterDiff(changesJson)
331-
332-
if diffErr != nil {
333-
diff = fmt.Sprintf("Error getting diff: %s", diffErr)
334-
} else {
335-
// Split the changesJson into "Before" and "After" parts
336-
beforeAfter := strings.Split(diff, "After: ")
337-
338-
// Perform sanitization on "After" part
339-
var after string
340-
if len(beforeAfter) > 1 {
341-
after, err = common.SanitizeSensitiveData(beforeAfter[1], mergedSensitive)
342-
handleSanitizationError(err, "after diff", options)
343-
} else {
344-
after = fmt.Sprintf("Could not parse after from diff") // dont print incase diff contains sensitive values
345-
}
346-
347-
// Perform sanitization on "Before" part
348-
var before string
349-
if len(beforeAfter) > 0 {
350-
before, err = common.SanitizeSensitiveData(strings.TrimPrefix(beforeAfter[0], "Before: "), mergedSensitive)
351-
handleSanitizationError(err, "before diff", options)
352-
} else {
353-
before = fmt.Sprintf("Could not parse before from diff") // dont print incase diff contains sensitive values
354-
}
355-
356-
// Reassemble the sanitized diff string
357-
diff = " Before: \n\t" + before + "\n After: \n\t" + after
358-
}
359-
resourceDetails = fmt.Sprintf("\nName: %s\nAddress: %s\nActions: %s\nDIFF:\n%s\n\nChange Detail:\n%s", resource.Name, resource.Address, resource.Change.Actions, diff, formatChangesJsonString)
360-
361-
// build error message
362-
errorMessage := fmt.Sprintf("Resource(s) identified to be destroyed %s", resourceDetails)
363-
364-
// check if current resource is changed
365-
noResourceChange := resource.Change.Actions.NoOp() || resource.Change.Actions.Read()
366-
assert.True(options.Testing, noResourceChange, errorMessage)
367-
368-
// if at least one resource is changed, then save that information
369-
if !resourcesChanged && !noResourceChange {
370-
resourcesChanged = true
371-
}
372-
}
373-
374-
// Run plan again to output the nice human-readable plan if there was a change
375-
if resourcesChanged {
376-
terraform.Plan(options.Testing, options.TerraformOptions)
377-
}
252+
options.CheckConsistency(planStruct)
378253
}
379254
}
380255
options.TestTearDown()

0 commit comments

Comments
 (0)