11package test
22
33import (
4+ "encoding/json"
45 "fmt"
56 "io/fs"
67 "log"
@@ -9,7 +10,9 @@ import (
910 "strings"
1011 "testing"
1112
13+ "github.com/gruntwork-io/terratest/modules/files"
1214 "github.com/gruntwork-io/terratest/modules/logger"
15+ "github.com/gruntwork-io/terratest/modules/random"
1316 "github.com/gruntwork-io/terratest/modules/terraform"
1417 "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/cloudinfo"
1518 "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common"
@@ -425,6 +428,63 @@ func setupOptionsSchematics(t *testing.T, prefix string, dir string) *testschema
425428 return options
426429}
427430
431+ func setupOptionsVsiExtention (t * testing.T , prefix string , region string , existingTerraformOptions * terraform.Options ) * testhelper.TestOptions {
432+
433+ sshPublicKey := sshPublicKey (t )
434+ outputVpcJson := terraform .OutputJson (t , existingTerraformOptions , "vpc_data" )
435+
436+ var managementVpcID string
437+ var vpcs []struct {
438+ VpcID string `json:"vpc_id"`
439+ VpcName string `json:"vpc_name"`
440+ }
441+ // Unmarshal the JSON data into the struct
442+ if err := json .Unmarshal ([]byte (outputVpcJson ), & vpcs ); err != nil {
443+ fmt .Println (err )
444+ return nil
445+ }
446+ // Loop through the vpcs and find the vpc_id when vpc_name is "<prefix>-management"
447+ for _ , vpc := range vpcs {
448+ if vpc .VpcName == fmt .Sprintf ("%s-management" , prefix ) {
449+ managementVpcID = vpc .VpcID
450+ }
451+ }
452+
453+ outputKeysJson := terraform .OutputJson (t , existingTerraformOptions , "key_map" )
454+ var keyID string
455+ var keys map [string ]map [string ]string
456+ // Unmarshal the JSON data into the map
457+ if err := json .Unmarshal ([]byte (outputKeysJson ), & keys ); err != nil {
458+ fmt .Println (err )
459+ return nil
460+ }
461+
462+ // Extract the key_id for the name "test-vsi-volume-key."
463+ if keyData , ok := keys [fmt .Sprintf ("%s-vsi-volume-key" , prefix )]; ok {
464+ keyID = keyData ["crn" ]
465+ } else {
466+ fmt .Println ("Name 'test-vsi-volume-key' not found in the JSON data." )
467+ }
468+ // ------------------------------------------------------------------------------------
469+ // Deploy landing-zone extension
470+ // ------------------------------------------------------------------------------------
471+ options := testhelper .TestOptionsDefault (& testhelper.TestOptions {
472+ Testing : t ,
473+ TerraformDir : "patterns/vsi-extension" ,
474+ // Do not hard fail the test if the implicit destroy steps fail to allow a full destroy of resource to occur
475+ ImplicitRequired : false ,
476+ TerraformVars : map [string ]interface {}{
477+ "prefix" : prefix ,
478+ "region" : region ,
479+ "boot_volume_encryption_key" : keyID ,
480+ "vpc_id" : managementVpcID ,
481+ "ssh_public_key" : sshPublicKey ,
482+ },
483+ })
484+
485+ return options
486+ }
487+
428488/***************************************************************************
429489SCHEMATICS TESTS
430490These schematics tests will only be run if the "RUN_SCHEMATICS_TESTS"
@@ -538,6 +598,137 @@ func TestRunVPCPatternSchematics(t *testing.T) {
538598 assert .NoError (t , err , "Schematic Test had unexpected error" )
539599}
540600
601+ func TestRunVsiExtention (t * testing.T ) {
602+ t .Parallel ()
603+
604+ // ------------------------------------------------------------------------------------
605+ // Deploy SLZ VPC first since it is needed for the landing-zone extension input
606+ // ------------------------------------------------------------------------------------
607+
608+ prefix := fmt .Sprintf ("vsi-slz-%s" , strings .ToLower (random .UniqueId ()))
609+ realTerraformDir := ".."
610+ tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
611+ vpcTerraformDir := realTerraformDir + "/patterns/vpc"
612+ tags := common .GetTagsFromTravis ()
613+
614+ // Verify ibmcloud_api_key variable is set
615+ checkVariable := "TF_VAR_ibmcloud_api_key"
616+ val , present := os .LookupEnv (checkVariable )
617+ require .True (t , present , checkVariable + " environment variable not set" )
618+ require .NotEqual (t , "" , val , checkVariable + " environment variable is empty" )
619+
620+ // Programmatically determine region to use based on availability
621+ region , _ := testhelper .GetBestVpcRegion (val , "../common-dev-assets/common-go-assets/cloudinfo-region-vpc-gen2-prefs.yaml" , "eu-de" )
622+
623+ // Exclude br-sao due to direct endpoint connectivity issues with CI/CD infrastructure
624+ if region == "br-sao" {
625+ region = "us-south" // Fallback to us-south if br-sao is selected
626+ }
627+
628+ logger .Log (t , "Tempdir: " , tempTerraformDir )
629+ existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
630+ TerraformDir : vpcTerraformDir ,
631+ Vars : map [string ]interface {}{
632+ "prefix" : prefix ,
633+ "region" : region ,
634+ "tags" : tags ,
635+ "enable_transit_gateway" : false ,
636+ },
637+ // Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
638+ // This is the same as setting the -upgrade=true flag with terraform.
639+ Upgrade : true ,
640+ })
641+
642+ terraform .WorkspaceSelectOrNew (t , existingTerraformOptions , prefix )
643+ _ , existErr := terraform .InitAndApplyE (t , existingTerraformOptions )
644+ if existErr != nil {
645+ assert .True (t , existErr == nil , "Init and Apply of temp existing resource failed" )
646+ } else {
647+ options := setupOptionsVsiExtention (t , prefix , region , existingTerraformOptions )
648+ output , err := options .RunTestConsistency ()
649+ assert .Nil (t , err , "This should not have errored" )
650+ assert .NotNil (t , output , "Expected some output" )
651+ }
652+
653+ // Check if "DO_NOT_DESTROY_ON_FAILURE" is set
654+ envVal , _ := os .LookupEnv ("DO_NOT_DESTROY_ON_FAILURE" )
655+ // Destroy the temporary existing resources if required
656+ if t .Failed () && strings .ToLower (envVal ) == "true" {
657+ fmt .Println ("Terratest failed. Debug the test and delete resources manually." )
658+ } else {
659+ logger .Log (t , "START: Destroy (existing resources)" )
660+ // ignore resource groups when destroying
661+ terraform .RunTerraformCommand (t , existingTerraformOptions , "state" , "rm" , "module.vpc_landing_zone.module.landing_zone.ibm_resource_group.resource_groups" )
662+ terraform .Destroy (t , existingTerraformOptions )
663+ terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
664+ logger .Log (t , "END: Destroy (existing resources)" )
665+ }
666+ }
667+
668+ func TestRunUpgradeVsiExtention (t * testing.T ) {
669+ // ------------------------------------------------------------------------------------
670+ // Deploy SLZ VPC first since it is needed for the landing-zone extension input
671+ // ------------------------------------------------------------------------------------
672+
673+ prefix := fmt .Sprintf ("vsi-upg-%s" , strings .ToLower (random .UniqueId ()))
674+ realTerraformDir := ".."
675+ tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
676+ vpcTerraformDir := realTerraformDir + "/patterns/vpc"
677+ tags := common .GetTagsFromTravis ()
678+
679+ // Verify ibmcloud_api_key variable is set
680+ checkVariable := "TF_VAR_ibmcloud_api_key"
681+ val , present := os .LookupEnv (checkVariable )
682+ require .True (t , present , checkVariable + " environment variable not set" )
683+ require .NotEqual (t , "" , val , checkVariable + " environment variable is empty" )
684+
685+ // Programmatically determine region to use based on availability
686+ region , _ := testhelper .GetBestVpcRegion (val , "../common-dev-assets/common-go-assets/cloudinfo-region-vpc-gen2-prefs.yaml" , "eu-de" )
687+
688+ // Exclude br-sao due to direct endpoint connectivity issues with CI/CD infrastructure
689+ if region == "br-sao" {
690+ region = "us-south" // Fallback to us-south if br-sao is selected
691+ }
692+
693+ logger .Log (t , "Tempdir: " , tempTerraformDir )
694+ existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
695+ TerraformDir : vpcTerraformDir ,
696+ Vars : map [string ]interface {}{
697+ "prefix" : prefix ,
698+ "region" : region ,
699+ "tags" : tags ,
700+ },
701+ // Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
702+ // This is the same as setting the -upgrade=true flag with terraform.
703+ Upgrade : true ,
704+ })
705+
706+ terraform .WorkspaceSelectOrNew (t , existingTerraformOptions , prefix )
707+ _ , existErr := terraform .InitAndApplyE (t , existingTerraformOptions )
708+ if existErr != nil {
709+ assert .True (t , existErr == nil , "Init and Apply of temp existing resource failed" )
710+ } else {
711+ options := setupOptionsVsiExtention (t , prefix , region , existingTerraformOptions )
712+ output , err := options .RunTestUpgrade ()
713+ if ! options .UpgradeTestSkipped {
714+ assert .Nil (t , err , "This should not have errored" )
715+ assert .NotNil (t , output , "Expected some output" )
716+ }
717+ }
718+
719+ // Check if "DO_NOT_DESTROY_ON_FAILURE" is set
720+ envVal , _ := os .LookupEnv ("DO_NOT_DESTROY_ON_FAILURE" )
721+ // Destroy the temporary existing resources if required
722+ if t .Failed () && strings .ToLower (envVal ) == "true" {
723+ fmt .Println ("Terratest failed. Debug the test and delete resources manually." )
724+ } else {
725+ logger .Log (t , "START: Destroy (existing resources)" )
726+ terraform .Destroy (t , existingTerraformOptions )
727+ terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
728+ logger .Log (t , "END: Destroy (existing resources)" )
729+ }
730+ }
731+
541732func TestRunOverrideExample (t * testing.T ) {
542733 t .Parallel ()
543734
0 commit comments