Skip to content

Commit 99c3385

Browse files
jor2Jordan-Williams2
andauthored
test: add plan validation test (#491)
Co-authored-by: Jordan-Williams2 <[email protected]>
1 parent ced9660 commit 99c3385

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/pr_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"strings"
1414
"testing"
1515

16+
"github.com/gruntwork-io/terratest/modules/logger"
17+
"github.com/gruntwork-io/terratest/modules/terraform"
1618
"github.com/stretchr/testify/assert"
1719
"github.com/stretchr/testify/require"
1820
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/cloudinfo"
@@ -235,6 +237,63 @@ func TestRunCompleteUpgradeExample(t *testing.T) {
235237
}
236238
}
237239

240+
func TestPlanValidation(t *testing.T) {
241+
options := testhelper.TestOptionsDefault(&testhelper.TestOptions{
242+
Testing: t,
243+
TerraformDir: standardSolutionTerraformDir,
244+
Prefix: "validate-plan",
245+
ResourceGroup: resourceGroup,
246+
Region: "us-south", // skip VPC region picker
247+
})
248+
options.TestSetup()
249+
options.TerraformOptions.NoColor = true
250+
options.TerraformOptions.Logger = logger.Discard
251+
options.TerraformOptions.Vars = map[string]interface{}{
252+
"prefix": options.Prefix,
253+
"region": "us-south",
254+
"mongodb_version": "6.0",
255+
"provider_visibility": "public",
256+
"resource_group_name": options.Prefix,
257+
}
258+
259+
// Test the DA when using an existing KMS instance
260+
var standardSolutionWithExistingKms = map[string]interface{}{
261+
"access_tags": permanentResources["accessTags"],
262+
"existing_kms_instance_crn": permanentResources["hpcs_south_crn"],
263+
}
264+
265+
// Test the DA when using IBM owned encryption key
266+
var standardSolutionWithUseIbmOwnedEncKey = map[string]interface{}{
267+
"use_ibm_owned_encryption_key": true,
268+
}
269+
270+
// Create a map of the variables
271+
tfVarsMap := map[string]map[string]interface{}{
272+
"standardSolutionWithExistingKms": standardSolutionWithExistingKms,
273+
"standardSolutionWithUseIbmOwnedEncKey": standardSolutionWithUseIbmOwnedEncKey,
274+
}
275+
276+
_, initErr := terraform.InitE(t, options.TerraformOptions)
277+
if assert.Nil(t, initErr, "This should not have errored") {
278+
// Iterate over the slice of maps
279+
for name, tfVars := range tfVarsMap {
280+
t.Run(name, func(t *testing.T) {
281+
// Iterate over the keys and values in each map
282+
for key, value := range tfVars {
283+
options.TerraformOptions.Vars[key] = value
284+
}
285+
output, err := terraform.PlanE(t, options.TerraformOptions)
286+
assert.Nil(t, err, "This should not have errored")
287+
assert.NotNil(t, output, "Expected some output")
288+
// Delete the keys from the map
289+
for key := range tfVars {
290+
delete(options.TerraformOptions.Vars, key)
291+
}
292+
})
293+
}
294+
}
295+
}
296+
238297
func GetRandomAdminPassword(t *testing.T) string {
239298
// Generate a 15 char long random string for the admin_pass
240299
randomBytes := make([]byte, 13)

0 commit comments

Comments
 (0)