Skip to content

Commit 506c47e

Browse files
authored
test: add a new test for DA using existing resources (#108)
1 parent c7a53c8 commit 506c47e

File tree

7 files changed

+202
-5
lines changed

7 files changed

+202
-5
lines changed

tests/existing-resources/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Existing Resources For use in tests

tests/existing-resources/main.tf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
##############################################################################
2+
# Resource Group
3+
##############################################################################
4+
5+
module "resource_group" {
6+
source = "terraform-ibm-modules/resource-group/ibm"
7+
version = "1.1.5"
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+
# Event Notification
15+
##############################################################################
16+
17+
module "event_notifications" {
18+
source = "terraform-ibm-modules/event-notifications/ibm"
19+
version = "1.3.1"
20+
resource_group_id = module.resource_group.resource_group_id
21+
name = "${var.prefix}-en"
22+
tags = var.resource_tags
23+
plan = "lite"
24+
service_endpoints = "public"
25+
region = var.region
26+
}
27+
28+
##############################################################################
29+
# Key Protect
30+
##############################################################################
31+
32+
module "key_protect" {
33+
source = "terraform-ibm-modules/kms-all-inclusive/ibm"
34+
version = "4.8.5"
35+
key_protect_instance_name = "${var.prefix}-key-protect"
36+
resource_group_id = module.resource_group.resource_group_id
37+
region = var.region
38+
keys = [
39+
{
40+
key_ring_name = "${var.prefix}-sm"
41+
keys = [
42+
{
43+
key_name = "${var.prefix}-sm-key"
44+
}
45+
]
46+
}
47+
]
48+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
output "resource_group_name" {
2+
value = module.resource_group.resource_group_name
3+
description = "Resource group name"
4+
}
5+
6+
output "resource_group_id" {
7+
value = module.resource_group.resource_group_id
8+
description = "Resource group ID"
9+
}
10+
11+
output "secrets_manager_kms_key_crn" {
12+
value = module.key_protect.keys["${var.prefix}-sm.${var.prefix}-sm-key"].crn
13+
description = "CRN of created secret manager KMS key"
14+
}
15+
16+
output "secrets_manager_kms_instance_crn" {
17+
value = module.key_protect.key_protect_id
18+
description = "CRN of created secret manager KMS instance"
19+
}
20+
21+
output "event_notification_instance_crn" {
22+
value = module.event_notifications.crn
23+
description = "CRN of created event notification"
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
region = var.region
4+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
##############################################################################
2+
# Input variables
3+
##############################################################################
4+
5+
variable "ibmcloud_api_key" {
6+
type = string
7+
description = "The IBM Cloud API Key"
8+
sensitive = true
9+
}
10+
11+
variable "region" {
12+
type = string
13+
description = "Region"
14+
}
15+
16+
variable "prefix" {
17+
type = string
18+
description = "Prefix to append to all resources"
19+
}
20+
21+
variable "resource_group" {
22+
type = string
23+
description = "The name of an existing resource group to provision resources in to. If not set a new resource group will be created using the prefix variable"
24+
default = null
25+
}
26+
27+
variable "resource_tags" {
28+
type = list(string)
29+
description = "Optional list of tags to be added to created resources"
30+
default = []
31+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.3.0, <1.7.0"
3+
required_providers {
4+
ibm = {
5+
source = "ibm-cloud/ibm"
6+
version = ">= 1.51.0"
7+
}
8+
}
9+
}

tests/pr_test.go

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
7985
func 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) {
113117
func 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

Comments
 (0)