Skip to content

Commit 882ed5b

Browse files
jor2Jordan-Williams2
andauthored
test: add plan validation test (#216)
Co-authored-by: Jordan-Williams2 <[email protected]>
1 parent 645bccf commit 882ed5b

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"
@@ -236,6 +238,63 @@ func TestRunUpgradeCompleteExample(t *testing.T) {
236238
}
237239
}
238240

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

0 commit comments

Comments
 (0)