@@ -152,11 +152,12 @@ func TestWithExistingKP(t *testing.T) {
152
152
},
153
153
},
154
154
TerraformVars : map [string ]interface {}{
155
- "location" : validRegions [rand .Intn (len (validRegions ))],
156
- "resource_group_name" : prefix ,
157
- "provider_visibility" : "public" ,
158
- "cos_kms_crn" : terraform .Output (t , existingTerraformOptions , "key_protect_crn" ),
159
- "cos_kms_key_crn" : terraform .Output (t , existingTerraformOptions , "kms_key_crn" ),
155
+ "location" : validRegions [rand .Intn (len (validRegions ))],
156
+ "resource_group_name" : prefix ,
157
+ "provider_visibility" : "public" ,
158
+ "enable_cos_kms_encryption" : true ,
159
+ "cos_kms_crn" : terraform .Output (t , existingTerraformOptions , "key_protect_crn" ),
160
+ "cos_kms_key_crn" : terraform .Output (t , existingTerraformOptions , "kms_key_crn" ),
160
161
},
161
162
})
162
163
@@ -178,3 +179,89 @@ func TestWithExistingKP(t *testing.T) {
178
179
}
179
180
180
181
}
182
+
183
+ func TestRunUpgradeExistingKP (t * testing.T ) {
184
+ t .Parallel ()
185
+
186
+ // ------------------------------------------------------------------------------------
187
+ // Provision KP first
188
+ // ------------------------------------------------------------------------------------
189
+
190
+ prefix := fmt .Sprintf ("kp-ut-%s" , strings .ToLower (random .UniqueId ()))
191
+ realTerraformDir := "./resources/kp-instance"
192
+ tempTerraformDir , _ := files .CopyTerraformFolderToTemp (realTerraformDir , fmt .Sprintf (prefix + "-%s" , strings .ToLower (random .UniqueId ())))
193
+ region := "us-south"
194
+
195
+ // Verify ibmcloud_api_key variable is set
196
+ checkVariable := "TF_VAR_ibmcloud_api_key"
197
+ val , present := os .LookupEnv (checkVariable )
198
+ require .True (t , present , checkVariable + " environment variable not set" )
199
+ require .NotEqual (t , "" , val , checkVariable + " environment variable is empty" )
200
+
201
+ logger .Log (t , "Tempdir: " , tempTerraformDir )
202
+ existingTerraformOptions := terraform .WithDefaultRetryableErrors (t , & terraform.Options {
203
+ TerraformDir : tempTerraformDir ,
204
+ Vars : map [string ]interface {}{
205
+ "prefix" : prefix ,
206
+ "region" : region ,
207
+ },
208
+ // Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
209
+ // This is the same as setting the -upgrade=true flag with terraform.
210
+ Upgrade : true ,
211
+ })
212
+
213
+ terraform .WorkspaceSelectOrNew (t , existingTerraformOptions , prefix )
214
+ _ , existErr := terraform .InitAndApplyE (t , existingTerraformOptions )
215
+ if existErr != nil {
216
+ assert .True (t , existErr == nil , "Init and Apply of temp existing resource failed" )
217
+ } else {
218
+
219
+ // ------------------------------------------------------------------------------------
220
+ // Upgrade test for watsonx DA passing in existing KP details
221
+ // ------------------------------------------------------------------------------------
222
+
223
+ options := testhelper .TestOptionsDefault (& testhelper.TestOptions {
224
+ Testing : t ,
225
+ TerraformDir : rootDaDir ,
226
+ Prefix : "existing-kp-upg" ,
227
+ IgnoreDestroys : testhelper.Exemptions { // Ignore for consistency check
228
+ List : []string {
229
+ "module.configure_user.null_resource.configure_user" ,
230
+ "module.configure_user.null_resource.restrict_access" ,
231
+ },
232
+ },
233
+ IgnoreUpdates : testhelper.Exemptions { // Ignore for consistency check
234
+ List : []string {
235
+ "module.configure_user.null_resource.configure_user" ,
236
+ "module.configure_user.null_resource.restrict_access" ,
237
+ },
238
+ },
239
+ TerraformVars : map [string ]interface {}{
240
+ "location" : validRegions [rand .Intn (len (validRegions ))],
241
+ "resource_group_name" : prefix ,
242
+ "provider_visibility" : "public" ,
243
+ "enable_cos_kms_encryption" : true ,
244
+ "cos_kms_crn" : terraform .Output (t , existingTerraformOptions , "key_protect_crn" ),
245
+ },
246
+ })
247
+
248
+ output , err := options .RunTestUpgrade ()
249
+ if ! options .UpgradeTestSkipped {
250
+ assert .Nil (t , err , "This should not have errored" )
251
+ assert .NotNil (t , output , "Expected some output" )
252
+ }
253
+ }
254
+
255
+ // Check if "DO_NOT_DESTROY_ON_FAILURE" is set
256
+ envVal , _ := os .LookupEnv ("DO_NOT_DESTROY_ON_FAILURE" )
257
+ // Destroy the temporary existing resources if required
258
+ if t .Failed () && strings .ToLower (envVal ) == "true" {
259
+ fmt .Println ("Terratest failed. Debug the test and delete resources manually." )
260
+ } else {
261
+ logger .Log (t , "START: Destroy (existing resources)" )
262
+ terraform .Destroy (t , existingTerraformOptions )
263
+ terraform .WorkspaceDelete (t , existingTerraformOptions , prefix )
264
+ logger .Log (t , "END: Destroy (existing resources)" )
265
+ }
266
+
267
+ }
0 commit comments