@@ -202,104 +202,236 @@ func TestRunExistingResourcesInstancesFullyConfigurable(t *testing.T) {
202202func TestRunExistingSMInstanceFullyConfigurable (t * testing.T ) {
203203 t .Parallel ()
204204
205- options := testschematic .TestSchematicOptionsDefault (& testschematic.TestSchematicOptions {
206- Testing : t ,
207- TarIncludePatterns : []string {
208- "*.tf" ,
209- fmt .Sprintf ("%s/*.tf" , fullyConfigurableTerraformDir ),
210- fmt .Sprintf ("%s/*.tf" , "modules/secrets" ),
211- fmt .Sprintf ("%s/*.tf" , "modules/fscloud" ),
205+ // ------------------------------------------------------------------------------------
206+ // Provision new RG
207+ // ------------------------------------------------------------------------------------
208+ region := validRegions [rand .Intn (len (validRegions ))]
209+ prefix := fmt .Sprintf ("ex-scm-%s" , strings .ToLower (random .UniqueId ()))
210+ realTerraformDir := ".."
211+ tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
212+
213+ // Verify ibmcloud_api_key variable is set
214+ checkVariable := "TF_VAR_ibmcloud_api_key"
215+ val , present := os .LookupEnv (checkVariable )
216+ require .True (t , present , checkVariable + " environment variable not set" )
217+ require .NotEqual (t , "" , val , checkVariable + " environment variable is empty" )
218+ logger .Log (t , "Tempdir: " , tempTerraformDir )
219+ existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
220+ TerraformDir : tempTerraformDir + "/tests/new-resources" ,
221+ Vars : map [string ]interface {}{
222+ "prefix" : prefix ,
223+ "region" : region ,
224+ "provision_secrets_manager" : true ,
212225 },
213- TemplateFolder : fullyConfigurableTerraformDir ,
214- ResourceGroup : resourceGroup ,
215- Prefix : "ex-scm" ,
216- Tags : []string {"test-schematic" },
217- DeleteWorkspaceOnFail : false ,
218- WaitJobCompleteMinutes : 60 ,
226+ // Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
227+ // This is the same as setting the -upgrade=true flag with terraform.
228+ Upgrade : true ,
219229 })
220230
221- options .TerraformVars = []testschematic.TestSchematicTerraformVar {
222- {Name : "ibmcloud_api_key" , Value : options .RequiredEnvironmentVars ["TF_VAR_ibmcloud_api_key" ], DataType : "string" , Secure : true },
223- {Name : "prefix" , Value : options .Prefix , DataType : "string" },
224- {Name : "region" , Value : validRegions [rand .Intn (len (validRegions ))], DataType : "string" },
225- {Name : "existing_resource_group_name" , Value : resourceGroup , DataType : "string" },
226- {Name : "existing_secrets_manager_crn" , Value : permanentResources ["secretsManagerCRN" ], DataType : "string" },
227- {Name : "service_plan" , Value : "trial" , DataType : "string" },
228- {Name : "secret_groups" , Value : _secret_group_config (options .Prefix ), DataType : "list(object)" },
231+ terraform .WorkspaceSelectOrNew (t , existingTerraformOptions , prefix )
232+ _ , existErr := terraform .InitAndApplyE (t , existingTerraformOptions )
233+ if existErr != nil {
234+ assert .True (t , existErr == nil , "Init and Apply of new resources failed failed" )
235+ } else {
236+ options := testschematic .TestSchematicOptionsDefault (& testschematic.TestSchematicOptions {
237+ Testing : t ,
238+ TarIncludePatterns : []string {
239+ "*.tf" ,
240+ fmt .Sprintf ("%s/*.tf" , fullyConfigurableTerraformDir ),
241+ fmt .Sprintf ("%s/*.tf" , "modules/secrets" ),
242+ fmt .Sprintf ("%s/*.tf" , "modules/fscloud" ),
243+ },
244+ TemplateFolder : fullyConfigurableTerraformDir ,
245+ ResourceGroup : resourceGroup ,
246+ Prefix : "ex-scm" ,
247+ Tags : []string {"test-schematic" },
248+ DeleteWorkspaceOnFail : false ,
249+ WaitJobCompleteMinutes : 60 ,
250+ })
251+
252+ options .TerraformVars = []testschematic.TestSchematicTerraformVar {
253+ {Name : "ibmcloud_api_key" , Value : options .RequiredEnvironmentVars ["TF_VAR_ibmcloud_api_key" ], DataType : "string" , Secure : true },
254+ {Name : "prefix" , Value : options .Prefix , DataType : "string" },
255+ {Name : "region" , Value : validRegions [rand .Intn (len (validRegions ))], DataType : "string" },
256+ {Name : "existing_resource_group_name" , Value : terraform .Output (t , existingTerraformOptions , "resource_group_name" ), DataType : "string" },
257+ {Name : "existing_secrets_manager_crn" , Value : terraform .Output (t , existingTerraformOptions , "secrets_manager_crn" ), DataType : "string" },
258+ {Name : "service_plan" , Value : "trial" , DataType : "string" },
259+ {Name : "secret_groups" , Value : _secret_group_config (options .Prefix ), DataType : "list(object)" },
260+ }
261+
262+ err := options .RunSchematicTest ()
263+ assert .NoError (t , err , "Schematic Test had unexpected error" )
229264 }
230265
231- err := options .RunSchematicTest ()
232- assert .NoError (t , err , "Schematic Test had unexpected error" )
266+ // Check if "DO_NOT_DESTROY_ON_FAILURE" is set
267+ envVal , _ := os .LookupEnv ("DO_NOT_DESTROY_ON_FAILURE" )
268+ // Destroy the temporary existing resources if required
269+ if t .Failed () && strings .ToLower (envVal ) == "true" {
270+ fmt .Println ("Terratest failed. Debug the test and delete resources manually." )
271+ } else {
272+ logger .Log (t , "START: Destroy (existing resources)" )
273+ terraform .Destroy (t , existingTerraformOptions )
274+ terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
275+ logger .Log (t , "END: Destroy (existing resources)" )
276+ }
233277}
234278
235279func TestRunSecurityEnforcedSchematics (t * testing.T ) {
236280 t .Parallel ()
237281
238- // Set up a schematics test
239- options := testschematic .TestSchematicOptionsDefault (& testschematic.TestSchematicOptions {
240- Testing : t ,
241- TarIncludePatterns : []string {
242- "*.tf" ,
243- fmt .Sprintf ("%s/*.tf" , securityEnforcedTerraformDir ),
244- fmt .Sprintf ("%s/*.tf" , fullyConfigurableTerraformDir ),
245- fmt .Sprintf ("%s/*.tf" , fscloudExampleTerraformDir ),
246- fmt .Sprintf ("%s/*.tf" , "modules/secrets" ),
247- fmt .Sprintf ("%s/*.tf" , "modules/fscloud" ),
282+ // ------------------------------------------------------------------------------------
283+ // Provision new RG
284+ // ------------------------------------------------------------------------------------
285+ region := validRegions [rand .Intn (len (validRegions ))]
286+ prefix := fmt .Sprintf ("sm-se-%s" , strings .ToLower (random .UniqueId ()))
287+ realTerraformDir := ".."
288+ tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
289+
290+ // Verify ibmcloud_api_key variable is set
291+ checkVariable := "TF_VAR_ibmcloud_api_key"
292+ val , present := os .LookupEnv (checkVariable )
293+ require .True (t , present , checkVariable + " environment variable not set" )
294+ require .NotEqual (t , "" , val , checkVariable + " environment variable is empty" )
295+ logger .Log (t , "Tempdir: " , tempTerraformDir )
296+ existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
297+ TerraformDir : tempTerraformDir + "/tests/new-resources" ,
298+ Vars : map [string ]interface {}{
299+ "prefix" : prefix ,
300+ "region" : region ,
248301 },
249- TemplateFolder : securityEnforcedTerraformDir ,
250- ResourceGroup : resourceGroup ,
251- Prefix : "sm-se" ,
252- Tags : []string {"test-schematic" },
253- DeleteWorkspaceOnFail : false ,
254- WaitJobCompleteMinutes : 60 ,
302+ // Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
303+ // This is the same as setting the -upgrade=true flag with terraform.
304+ Upgrade : true ,
255305 })
256306
257- options .TerraformVars = []testschematic.TestSchematicTerraformVar {
258- {Name : "ibmcloud_api_key" , Value : options .RequiredEnvironmentVars ["TF_VAR_ibmcloud_api_key" ], DataType : "string" , Secure : true },
259- {Name : "prefix" , Value : options .Prefix , DataType : "string" },
260- {Name : "region" , Value : validRegions [rand .Intn (len (validRegions ))], DataType : "string" },
261- {Name : "existing_resource_group_name" , Value : resourceGroup , DataType : "string" },
262- {Name : "service_plan" , Value : "trial" , DataType : "string" },
263- {Name : "existing_kms_instance_crn" , Value : permanentResources ["hpcs_south_crn" ], DataType : "string" },
264- {Name : "secret_groups" , Value : _secret_group_config (options .Prefix ), DataType : "list(object)" },
307+ terraform .WorkspaceSelectOrNew (t , existingTerraformOptions , prefix )
308+ _ , existErr := terraform .InitAndApplyE (t , existingTerraformOptions )
309+ if existErr != nil {
310+ assert .True (t , existErr == nil , "Init and Apply of new resources failed" )
311+ } else {
312+
313+ // Set up a schematics test
314+ options := testschematic .TestSchematicOptionsDefault (& testschematic.TestSchematicOptions {
315+ Testing : t ,
316+ TarIncludePatterns : []string {
317+ "*.tf" ,
318+ fmt .Sprintf ("%s/*.tf" , securityEnforcedTerraformDir ),
319+ fmt .Sprintf ("%s/*.tf" , fullyConfigurableTerraformDir ),
320+ fmt .Sprintf ("%s/*.tf" , fscloudExampleTerraformDir ),
321+ fmt .Sprintf ("%s/*.tf" , "modules/secrets" ),
322+ fmt .Sprintf ("%s/*.tf" , "modules/fscloud" ),
323+ },
324+ TemplateFolder : securityEnforcedTerraformDir ,
325+ ResourceGroup : resourceGroup ,
326+ Prefix : "sm-se" ,
327+ Tags : []string {"test-schematic" },
328+ DeleteWorkspaceOnFail : false ,
329+ WaitJobCompleteMinutes : 60 ,
330+ })
331+
332+ options .TerraformVars = []testschematic.TestSchematicTerraformVar {
333+ {Name : "ibmcloud_api_key" , Value : options .RequiredEnvironmentVars ["TF_VAR_ibmcloud_api_key" ], DataType : "string" , Secure : true },
334+ {Name : "prefix" , Value : options .Prefix , DataType : "string" },
335+ {Name : "region" , Value : validRegions [rand .Intn (len (validRegions ))], DataType : "string" },
336+ {Name : "existing_resource_group_name" , Value : terraform .Output (t , existingTerraformOptions , "resource_group_name" ), DataType : "string" },
337+ {Name : "service_plan" , Value : "trial" , DataType : "string" },
338+ {Name : "existing_kms_instance_crn" , Value : permanentResources ["hpcs_south_crn" ], DataType : "string" },
339+ {Name : "secret_groups" , Value : _secret_group_config (options .Prefix ), DataType : "list(object)" },
340+ }
341+ err := options .RunSchematicTest ()
342+ assert .NoError (t , err , "Schematic Test had unexpected error" )
343+ }
344+
345+ // Check if "DO_NOT_DESTROY_ON_FAILURE" is set
346+ envVal , _ := os .LookupEnv ("DO_NOT_DESTROY_ON_FAILURE" )
347+ // Destroy the temporary existing resources if required
348+ if t .Failed () && strings .ToLower (envVal ) == "true" {
349+ fmt .Println ("Terratest failed. Debug the test and delete resources manually." )
350+ } else {
351+ logger .Log (t , "START: Destroy (existing resources)" )
352+ terraform .Destroy (t , existingTerraformOptions )
353+ terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
354+ logger .Log (t , "END: Destroy (existing resources)" )
265355 }
266- err := options .RunSchematicTest ()
267- assert .NoError (t , err , "Schematic Test had unexpected error" )
268356}
269357
270358func TestRunSecretsManagerSecurityEnforcedUpgradeSchematic (t * testing.T ) {
271359 t .Parallel ()
272360
273- // Set up a schematics test
274- options := testschematic .TestSchematicOptionsDefault (& testschematic.TestSchematicOptions {
275- Testing : t ,
276- TarIncludePatterns : []string {
277- "*.tf" ,
278- fmt .Sprintf ("%s/*.tf" , securityEnforcedTerraformDir ),
279- fmt .Sprintf ("%s/*.tf" , fullyConfigurableTerraformDir ),
280- fmt .Sprintf ("%s/*.tf" , "modules/secrets" ),
281- fmt .Sprintf ("%s/*.tf" , "modules/fscloud" ),
361+ // ------------------------------------------------------------------------------------
362+ // Provision new RG
363+ // ------------------------------------------------------------------------------------
364+ region := validRegions [rand .Intn (len (validRegions ))]
365+ prefix := fmt .Sprintf ("sm-se-ug-%s" , strings .ToLower (random .UniqueId ()))
366+ realTerraformDir := ".."
367+ tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
368+
369+ // Verify ibmcloud_api_key variable is set
370+ checkVariable := "TF_VAR_ibmcloud_api_key"
371+ val , present := os .LookupEnv (checkVariable )
372+ require .True (t , present , checkVariable + " environment variable not set" )
373+ require .NotEqual (t , "" , val , checkVariable + " environment variable is empty" )
374+ logger .Log (t , "Tempdir: " , tempTerraformDir )
375+ existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
376+ TerraformDir : tempTerraformDir + "/tests/new-resources" ,
377+ Vars : map [string ]interface {}{
378+ "prefix" : prefix ,
379+ "region" : region ,
282380 },
283- TemplateFolder : securityEnforcedTerraformDir ,
284- ResourceGroup : resourceGroup ,
285- Prefix : "sm-se-ug" ,
286- Tags : []string {"test-schematic" },
287- DeleteWorkspaceOnFail : false ,
288- WaitJobCompleteMinutes : 60 ,
381+ // Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
382+ // This is the same as setting the -upgrade=true flag with terraform.
383+ Upgrade : true ,
289384 })
290385
291- options .TerraformVars = []testschematic.TestSchematicTerraformVar {
292- {Name : "ibmcloud_api_key" , Value : options .RequiredEnvironmentVars ["TF_VAR_ibmcloud_api_key" ], DataType : "string" , Secure : true },
293- {Name : "prefix" , Value : options .Prefix , DataType : "string" },
294- {Name : "region" , Value : validRegions [rand .Intn (len (validRegions ))], DataType : "string" },
295- {Name : "existing_resource_group_name" , Value : "geretain-test-secrets-manager" , DataType : "string" },
296- {Name : "service_plan" , Value : "trial" , DataType : "string" },
297- {Name : "existing_kms_instance_crn" , Value : permanentResources ["hpcs_south_crn" ], DataType : "string" },
298- {Name : "secret_groups" , Value : _secret_group_config (options .Prefix ), DataType : "list(object)" },
386+ terraform .WorkspaceSelectOrNew (t , existingTerraformOptions , prefix )
387+ _ , existErr := terraform .InitAndApplyE (t , existingTerraformOptions )
388+ if existErr != nil {
389+ assert .True (t , existErr == nil , "Init and Apply of new resources failed" )
390+ } else {
391+ // Set up a schematics test
392+ options := testschematic .TestSchematicOptionsDefault (& testschematic.TestSchematicOptions {
393+ Testing : t ,
394+ TarIncludePatterns : []string {
395+ "*.tf" ,
396+ fmt .Sprintf ("%s/*.tf" , securityEnforcedTerraformDir ),
397+ fmt .Sprintf ("%s/*.tf" , fullyConfigurableTerraformDir ),
398+ fmt .Sprintf ("%s/*.tf" , "modules/secrets" ),
399+ fmt .Sprintf ("%s/*.tf" , "modules/fscloud" ),
400+ },
401+ TemplateFolder : securityEnforcedTerraformDir ,
402+ ResourceGroup : resourceGroup ,
403+ Prefix : "sm-se-ug" ,
404+ Tags : []string {"test-schematic" },
405+ DeleteWorkspaceOnFail : false ,
406+ WaitJobCompleteMinutes : 60 ,
407+ })
408+
409+ options .TerraformVars = []testschematic.TestSchematicTerraformVar {
410+ {Name : "ibmcloud_api_key" , Value : options .RequiredEnvironmentVars ["TF_VAR_ibmcloud_api_key" ], DataType : "string" , Secure : true },
411+ {Name : "prefix" , Value : options .Prefix , DataType : "string" },
412+ {Name : "region" , Value : validRegions [rand .Intn (len (validRegions ))], DataType : "string" },
413+ {Name : "existing_resource_group_name" , Value : terraform .Output (t , existingTerraformOptions , "resource_group_name" ), DataType : "string" },
414+ {Name : "service_plan" , Value : "trial" , DataType : "string" },
415+ {Name : "existing_kms_instance_crn" , Value : permanentResources ["hpcs_south_crn" ], DataType : "string" },
416+ {Name : "secret_groups" , Value : _secret_group_config (options .Prefix ), DataType : "list(object)" },
417+ }
418+
419+ err := options .RunSchematicUpgradeTest ()
420+ if ! options .UpgradeTestSkipped {
421+ assert .Nil (t , err , "This should not have errored" )
422+ }
299423 }
300424
301- err := options .RunSchematicUpgradeTest ()
302- if ! options .UpgradeTestSkipped {
303- assert .Nil (t , err , "This should not have errored" )
425+ // Check if "DO_NOT_DESTROY_ON_FAILURE" is set
426+ envVal , _ := os .LookupEnv ("DO_NOT_DESTROY_ON_FAILURE" )
427+ // Destroy the temporary existing resources if required
428+ if t .Failed () && strings .ToLower (envVal ) == "true" {
429+ fmt .Println ("Terratest failed. Debug the test and delete resources manually." )
430+ } else {
431+ logger .Log (t , "START: Destroy (existing resources)" )
432+ terraform .Destroy (t , existingTerraformOptions )
433+ terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
434+ logger .Log (t , "END: Destroy (existing resources)" )
304435 }
436+
305437}
0 commit comments