Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0ee2540

Browse files
authored
test: update test to use unique kms key (#291)
1 parent 8bc8e2e commit 0ee2540

File tree

11 files changed

+136
-117
lines changed

11 files changed

+136
-117
lines changed

tests/existing-resources/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/existing-resources/main.tf

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/existing-resources/outputs.tf

Lines changed: 0 additions & 58 deletions
This file was deleted.

tests/pr_test.go

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

tests/resources/kms-key/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The terraform code in this directory is used by the DA tests in tests/pr_test.go

tests/resources/kms-key/main.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
##############################################################################
2+
# Resource Group
3+
##############################################################################
4+
5+
module "resource_group" {
6+
source = "terraform-ibm-modules/resource-group/ibm"
7+
version = "1.2.0"
8+
# if an existing resource group is not set (null) create a new one using prefix
9+
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
10+
existing_resource_group_name = var.resource_group
11+
}
12+
13+
##############################################################################
14+
# Key Protect (instance and key)
15+
##############################################################################
16+
17+
module "key_protect" {
18+
source = "terraform-ibm-modules/kms-all-inclusive/ibm"
19+
version = "5.0.2"
20+
key_protect_instance_name = "${var.prefix}-key-protect"
21+
resource_group_id = module.resource_group.resource_group_id
22+
region = var.region
23+
resource_tags = var.resource_tags
24+
keys = [
25+
{
26+
key_ring_name = "${var.prefix}-scc"
27+
keys = [
28+
{
29+
key_name = "${var.prefix}-scc-key"
30+
force_delete = true
31+
}
32+
]
33+
}
34+
]
35+
}

tests/resources/kms-key/outputs.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
##############################################################################
2+
# Outputs
3+
##############################################################################
4+
5+
output "resource_group_name" {
6+
description = "Resource group name"
7+
value = module.resource_group.resource_group_name
8+
}
9+
10+
output "prefix" {
11+
value = var.prefix
12+
description = "Prefix"
13+
}
14+
15+
output "region" {
16+
value = var.region
17+
description = "region"
18+
}
19+
20+
output "kms_key_crn" {
21+
value = module.key_protect.keys["${var.prefix}-scc.${var.prefix}-scc-key"].crn
22+
description = "CRN of KMS key"
23+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ variable "ibmcloud_api_key" {
1010

1111
variable "region" {
1212
type = string
13-
description = "Region to provision all resources created by this example"
14-
default = "us-south"
13+
description = "Region"
1514
}
1615

1716
variable "prefix" {
1817
type = string
19-
description = "Prefix to append to all resources created by this example"
20-
default = "scc"
18+
description = "Prefix to append to all resources"
2119
}
2220

2321
variable "resource_group" {

0 commit comments

Comments
 (0)