@@ -5,11 +5,17 @@ import (
55 "fmt"
66 "log"
77 "os"
8+ "strings"
89 "testing"
910
1011 "github.com/IBM/go-sdk-core/v5/core"
1112 "github.com/IBM/secrets-manager-go-sdk/v2/secretsmanagerv2"
13+ "github.com/gruntwork-io/terratest/modules/files"
14+ "github.com/gruntwork-io/terratest/modules/logger"
15+ "github.com/gruntwork-io/terratest/modules/random"
16+ "github.com/gruntwork-io/terratest/modules/terraform"
1217 "github.com/stretchr/testify/assert"
18+ "github.com/stretchr/testify/require"
1319 "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common"
1420 "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
1521 "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testschematic"
@@ -79,8 +85,6 @@ func TestRunUpgradeExample(t *testing.T) {
7985func TestFSCloudInSchematics (t * testing.T ) {
8086 t .Parallel ()
8187
82- const region = "us-south"
83-
8488 options := testschematic .TestSchematicOptionsDefault (& testschematic.TestSchematicOptions {
8589 Testing : t ,
8690 Prefix : "sm-fscloud" ,
@@ -89,12 +93,12 @@ func TestFSCloudInSchematics(t *testing.T) {
8993 fscloudExampleTerraformDir + "/*.tf" ,
9094 "modules/fscloud/*.tf" ,
9195 },
96+ BestRegionYAMLPath : "../common-dev-assets/common-go-assets/cloudinfo-region-secmgr-prefs.yaml" ,
9297 // ResourceGroup: resourceGroup,
9398 TemplateFolder : fscloudExampleTerraformDir ,
9499 Tags : []string {"test-schematic" },
95100 DeleteWorkspaceOnFail : false ,
96101 WaitJobCompleteMinutes : 60 ,
97- Region : region ,
98102 })
99103
100104 options .TerraformVars = []testschematic.TestSchematicTerraformVar {
@@ -113,7 +117,6 @@ func TestFSCloudInSchematics(t *testing.T) {
113117func TestRunDASolutionSchematics (t * testing.T ) {
114118 t .Parallel ()
115119
116- const region = "us-south"
117120 acme_letsencrypt_private_key := GetSecretsManagerKey ( // pragma: allowlist secret
118121 permanentResources ["acme_letsencrypt_private_key_sm_id" ].(string ),
119122 permanentResources ["acme_letsencrypt_private_key_sm_region" ].(string ),
@@ -130,7 +133,7 @@ func TestRunDASolutionSchematics(t *testing.T) {
130133 Tags : []string {"test-schematic" },
131134 DeleteWorkspaceOnFail : false ,
132135 WaitJobCompleteMinutes : 60 ,
133- Region : region ,
136+ BestRegionYAMLPath : "../common-dev-assets/common-go-assets/cloudinfo- region-secmgr-prefs.yaml" ,
134137 })
135138
136139 options .TerraformVars = []testschematic.TestSchematicTerraformVar {
@@ -175,3 +178,80 @@ func GetSecretsManagerKey(sm_id string, sm_region string, sm_key_id string) *str
175178 }
176179 return secret .(* secretsmanagerv2.ArbitrarySecret ).Payload
177180}
181+
182+ // A test to pass existing resources to the SM DA
183+ func TestRunExistingResourcesInstances (t * testing.T ) {
184+ t .Parallel ()
185+
186+ // Init test options for DA to get the region, which is used for provisioning the existing resources
187+ options := testhelper .TestOptionsDefault (& testhelper.TestOptions {
188+ Testing : t ,
189+ TerraformDir : solutionsTerraformDir ,
190+ // Do not hard fail the test if the implicit destroy steps fail to allow a full destroy of resource to occur
191+ ImplicitRequired : false ,
192+ BestRegionYAMLPath : "../common-dev-assets/common-go-assets/cloudinfo-region-secmgr-prefs.yaml" ,
193+ })
194+
195+ // ------------------------------------------------------------------------------------
196+ // Provision Event Notification, KMS key and resource group first
197+ // ------------------------------------------------------------------------------------
198+
199+ prefix := fmt .Sprintf ("scc-exist-%s" , strings .ToLower (random .UniqueId ()))
200+ realTerraformDir := "./existing-resources"
201+ tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
202+ tags := common .GetTagsFromTravis ()
203+ region := "us-south"
204+
205+ // Verify ibmcloud_api_key variable is set
206+ checkVariable := "TF_VAR_ibmcloud_api_key"
207+ val , present := os .LookupEnv (checkVariable )
208+ require .True (t , present , checkVariable + " environment variable not set" )
209+ require .NotEqual (t , "" , val , checkVariable + " environment variable is empty" )
210+ logger .Log (t , "Tempdir: " , tempTerraformDir )
211+ existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
212+ TerraformDir : tempTerraformDir ,
213+ Vars : map [string ]interface {}{
214+ "prefix" : prefix ,
215+ "region" : options .Region ,
216+ "resource_tags" : tags ,
217+ },
218+ // Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
219+ // This is the same as setting the -upgrade=true flag with terraform.
220+ Upgrade : true ,
221+ })
222+
223+ terraform .WorkspaceSelectOrNew (t , existingTerraformOptions , prefix )
224+ _ , existErr := terraform .InitAndApplyE (t , existingTerraformOptions )
225+ if existErr != nil {
226+ assert .True (t , existErr == nil , "Init and Apply of temp existing resource failed" )
227+ } else {
228+ // add existing resources to previously created options
229+ options .TerraformVars = map [string ]interface {}{
230+ "ibmcloud_api_key" : os .Getenv ("TF_VAR_ibmcloud_api_key" ),
231+ "region" : region ,
232+ "resource_group_name" : terraform .Output (t , existingTerraformOptions , "resource_group_name" ),
233+ "use_existing_resource_group" : true ,
234+ "existing_event_notification_instance_crn" : terraform .Output (t , existingTerraformOptions , "event_notification_instance_crn" ),
235+ "existing_secrets_manager_kms_key_crn" : terraform .Output (t , existingTerraformOptions , "secrets_manager_kms_key_crn" ),
236+ "existing_kms_instance_crn" : terraform .Output (t , existingTerraformOptions , "secrets_manager_kms_instance_crn" ),
237+ "service_plan" : "trial" ,
238+ }
239+
240+ output , err := options .RunTestConsistency ()
241+ assert .Nil (t , err , "This should not have errored" )
242+ assert .NotNil (t , output , "Expected some output" )
243+
244+ }
245+
246+ // Check if "DO_NOT_DESTROY_ON_FAILURE" is set
247+ envVal , _ := os .LookupEnv ("DO_NOT_DESTROY_ON_FAILURE" )
248+ // Destroy the temporary existing resources if required
249+ if t .Failed () && strings .ToLower (envVal ) == "true" {
250+ fmt .Println ("Terratest failed. Debug the test and delete resources manually." )
251+ } else {
252+ logger .Log (t , "START: Destroy (existing resources)" )
253+ terraform .Destroy (t , existingTerraformOptions )
254+ terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
255+ logger .Log (t , "END: Destroy (existing resources)" )
256+ }
257+ }
0 commit comments