@@ -153,34 +153,83 @@ func TestExistingKeyFullyConfigurable(t *testing.T) {
153153
154154 var region = validRegions [rand .Intn (len (validRegions ))]
155155
156- options := testschematic .TestSchematicOptionsDefault (& testschematic.TestSchematicOptions {
157- Testing : t ,
158- Region : region ,
159- Prefix : "scc-key" ,
160- TarIncludePatterns : []string {
161- "*.tf" ,
162- "modules/*/*.tf" ,
163- fullyConfigFlavorDir + "/*.tf" ,
156+ // ------------------------------------------------------------------------------------
157+ // Provision Key Protect + key first
158+ // ------------------------------------------------------------------------------------
159+
160+ prefix := fmt .Sprintf ("scc-%s" , strings .ToLower (random .UniqueId ()))
161+ realTerraformDir := "./resources/kms-key"
162+ tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
163+ tags := common .GetTagsFromTravis ()
164+
165+ // Verify ibmcloud_api_key variable is set
166+ checkVariable := "TF_VAR_ibmcloud_api_key"
167+ val , present := os .LookupEnv (checkVariable )
168+ require .True (t , present , checkVariable + " environment variable not set" )
169+ require .NotEqual (t , "" , val , checkVariable + " environment variable is empty" )
170+
171+ logger .Log (t , "Tempdir: " , tempTerraformDir )
172+ existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
173+ TerraformDir : tempTerraformDir ,
174+ Vars : map [string ]interface {}{
175+ "prefix" : prefix ,
176+ "region" : region ,
177+ "resource_tags" : tags ,
164178 },
165- TemplateFolder : fullyConfigFlavorDir ,
166- Tags : []string {"scc-da-test" },
167- DeleteWorkspaceOnFail : false ,
168- WaitJobCompleteMinutes : 60 ,
179+ // Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
180+ // This is the same as setting the -upgrade=true flag with terraform.
181+ Upgrade : true ,
169182 })
170183
171- options .TerraformVars = []testschematic.TestSchematicTerraformVar {
172- {Name : "ibmcloud_api_key" , Value : options .RequiredEnvironmentVars ["TF_VAR_ibmcloud_api_key" ], DataType : "string" , Secure : true },
173- {Name : "existing_resource_group_name" , Value : resourceGroup , DataType : "string" },
174- {Name : "scc_region" , Value : options .Region , DataType : "string" },
175- {Name : "scc_instance_resource_tags" , Value : options .Tags , DataType : "list(string)" },
176- {Name : "prefix" , Value : options .Prefix , DataType : "string" },
177- {Name : "existing_cos_instance_crn" , Value : permanentResources ["general_test_storage_cos_instance_crn" ], DataType : "string" },
178- {Name : "existing_kms_key_crn" , Value : permanentResources ["hpcs_south_root_key_crn" ], DataType : "string" },
179- {Name : "kms_encryption_enabled_bucket" , Value : true , DataType : "bool" },
184+ terraform .WorkspaceSelectOrNew (t , existingTerraformOptions , prefix )
185+ _ , existErr := terraform .InitAndApplyE (t , existingTerraformOptions )
186+ if existErr != nil {
187+ assert .True (t , existErr == nil , "Init and Apply of pre-req resources failed in TestFullyConfigurable test" )
188+ } else {
189+ // ------------------------------------------------------------------------------------
190+ // Deploy DA
191+ // ------------------------------------------------------------------------------------
192+ options := testschematic .TestSchematicOptionsDefault (& testschematic.TestSchematicOptions {
193+ Testing : t ,
194+ Region : region ,
195+ Prefix : prefix ,
196+ TarIncludePatterns : []string {
197+ "*.tf" ,
198+ "modules/*/*.tf" ,
199+ fullyConfigFlavorDir + "/*.tf" ,
200+ },
201+ TemplateFolder : fullyConfigFlavorDir ,
202+ Tags : []string {"scc-da-test" },
203+ DeleteWorkspaceOnFail : false ,
204+ WaitJobCompleteMinutes : 60 ,
205+ })
206+
207+ options .TerraformVars = []testschematic.TestSchematicTerraformVar {
208+ {Name : "ibmcloud_api_key" , Value : options .RequiredEnvironmentVars ["TF_VAR_ibmcloud_api_key" ], DataType : "string" , Secure : true },
209+ {Name : "existing_resource_group_name" , Value : terraform .Output (t , existingTerraformOptions , "resource_group_name" ), DataType : "string" },
210+ {Name : "scc_region" , Value : options .Region , DataType : "string" },
211+ {Name : "scc_instance_resource_tags" , Value : options .Tags , DataType : "list(string)" },
212+ {Name : "prefix" , Value : terraform .Output (t , existingTerraformOptions , "prefix" ), DataType : "string" },
213+ {Name : "existing_cos_instance_crn" , Value : permanentResources ["general_test_storage_cos_instance_crn" ], DataType : "string" },
214+ {Name : "existing_kms_key_crn" , Value : terraform .Output (t , existingTerraformOptions , "kms_key_crn" ), DataType : "string" },
215+ {Name : "kms_encryption_enabled_bucket" , Value : true , DataType : "bool" },
216+ }
217+
218+ err := options .RunSchematicTest ()
219+ assert .Nil (t , err , "This should not have errored" )
180220 }
181221
182- err := options .RunSchematicTest ()
183- assert .Nil (t , err , "This should not have errored" )
222+ // Check if "DO_NOT_DESTROY_ON_FAILURE" is set
223+ envVal , _ := os .LookupEnv ("DO_NOT_DESTROY_ON_FAILURE" )
224+ // Destroy the temporary existing resources if required
225+ if t .Failed () && strings .ToLower (envVal ) == "true" {
226+ fmt .Println ("Terratest failed. Debug the test and delete resources manually." )
227+ } else {
228+ logger .Log (t , "START: Destroy (prereq resources)" )
229+ terraform .Destroy (t , existingTerraformOptions )
230+ terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
231+ logger .Log (t , "END: Destroy (prereq resources)" )
232+ }
184233}
185234
186235// Test the security-enforced DA with defaults (pass KMS instance details and create new key)
0 commit comments