Skip to content

Commit 702db73

Browse files
authored
fix: fixed bug which caused validation to fail when passing existing EN instance + plug the test gap (#300)
1 parent bd098f6 commit 702db73

File tree

8 files changed

+183
-3
lines changed

8 files changed

+183
-3
lines changed

solutions/standard/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ locals {
2626
existing_kms_guid = var.existing_kms_instance_crn != null ? element(split(":", var.existing_kms_instance_crn), length(split(":", var.existing_kms_instance_crn)) - 3) : tobool("The CRN of the existing KMS is not provided.")
2727
en_key_name = var.prefix != null ? "${var.prefix}-${var.en_key_name}" : var.en_key_name
2828
en_key_ring_name = var.prefix != null ? "${var.prefix}-${var.en_key_ring_name}" : var.en_key_ring_name
29-
en_kms_key_id = local.existing_kms_root_key_id != null ? local.existing_kms_root_key_id : module.kms[0].keys[format("%s.%s", local.en_key_ring_name, local.en_key_name)].key_id
29+
en_kms_key_id = local.existing_kms_root_key_id != null ? local.existing_kms_root_key_id : var.existing_en_instance_crn == null ? module.kms[0].keys[format("%s.%s", local.en_key_ring_name, local.en_key_name)].key_id : null
3030
cos_key_name = var.prefix != null ? "${var.prefix}-${var.cos_key_name}" : var.cos_key_name
3131
cos_key_ring_name = var.prefix != null ? "${var.prefix}-${var.cos_key_ring_name}" : var.cos_key_ring_name
3232
cos_instance_guid = var.existing_cos_instance_crn != null ? element(split(":", var.existing_cos_instance_crn), length(split(":", var.existing_cos_instance_crn)) - 3) : null
33-
cos_kms_key_crn = var.existing_cos_bucket_name != null ? null : var.existing_kms_root_key_crn != null ? var.existing_kms_root_key_crn : module.kms[0].keys[format("%s.%s", local.cos_key_ring_name, local.cos_key_name)].crn
33+
cos_kms_key_crn = var.existing_cos_bucket_name != null ? null : var.existing_kms_root_key_crn != null ? var.existing_kms_root_key_crn : var.existing_en_instance_crn == null ? module.kms[0].keys[format("%s.%s", local.cos_key_ring_name, local.cos_key_name)].crn : null
3434

3535
kms_service_name = var.existing_kms_instance_crn != null ? (
3636
can(regex(".*kms.*", var.existing_kms_instance_crn)) ? "kms" : (

tests/existing-resources/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 existing resource test in tests/pr_test.go

tests/existing-resources/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module "resource_group" {
2+
source = "terraform-ibm-modules/resource-group/ibm"
3+
version = "1.1.6"
4+
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
5+
existing_resource_group_name = var.resource_group
6+
}
7+
8+
module "event_notification" {
9+
source = "../../"
10+
resource_group_id = module.resource_group.resource_group_id
11+
name = "${var.prefix}-en"
12+
tags = var.resource_tags
13+
plan = "lite"
14+
service_endpoints = "public"
15+
region = var.region
16+
cos_integration_enabled = false
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
########################################################################################################################
2+
# Outputs
3+
########################################################################################################################
4+
5+
output "resource_group_id" {
6+
description = "The id of the resource group where resources are created"
7+
value = module.resource_group.resource_group_id
8+
}
9+
10+
output "resource_group_name" {
11+
description = "The name of the resource group where resources are created"
12+
value = module.resource_group.resource_group_name
13+
}
14+
15+
output "event_notification_instance_name" {
16+
description = "Event Notification name"
17+
value = module.event_notification.event_notification_instance_name
18+
}
19+
20+
output "event_notification_instance_crn" {
21+
description = "Event notification instance crn"
22+
value = module.event_notification.crn
23+
}
24+
25+
output "event_notification_instance_guid" {
26+
description = "Event Notification guid"
27+
value = module.event_notification.guid
28+
}
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 to provision all resources created by this example"
14+
default = "us-south"
15+
}
16+
17+
variable "prefix" {
18+
type = string
19+
description = "Prefix to append to all resources created by this example"
20+
default = "en-ext-res"
21+
}
22+
23+
variable "resource_group" {
24+
type = string
25+
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"
26+
default = null
27+
}
28+
29+
variable "resource_tags" {
30+
type = list(string)
31+
description = "Optional list of tags to be added to created resources"
32+
default = []
33+
}
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"
3+
required_providers {
4+
ibm = {
5+
source = "ibm-cloud/ibm"
6+
version = ">= 1.64.1"
7+
}
8+
}
9+
}

tests/pr_test.go

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22
package test
33

44
import (
5+
"fmt"
56
"log"
67
"math/rand"
78
"os"
9+
"strings"
810
"testing"
911

12+
"github.com/gruntwork-io/terratest/modules/files"
13+
"github.com/gruntwork-io/terratest/modules/logger"
14+
"github.com/gruntwork-io/terratest/modules/random"
15+
"github.com/gruntwork-io/terratest/modules/terraform"
16+
1017
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
1119
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common"
1220
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
1321
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testschematic"
@@ -22,7 +30,7 @@ const resourceGroup = "geretain-test-event-notifications"
2230

2331
const yamlLocation = "../common-dev-assets/common-go-assets/common-permanent-resources.yaml"
2432

25-
// Current supported SCC region
33+
// Current supported EN region
2634
var validRegions = []string{
2735
"us-south",
2836
"eu-de",
@@ -183,3 +191,83 @@ func TestRunUpgradeDASolution(t *testing.T) {
183191
assert.NotNil(t, output, "Expected some output")
184192
}
185193
}
194+
195+
func TestRunExistingResourcesInstances(t *testing.T) {
196+
t.Parallel()
197+
198+
// ------------------------------------------------------------------------------------
199+
// Provision RG, EN
200+
// ------------------------------------------------------------------------------------
201+
202+
prefix := fmt.Sprintf("en-existing-%s", strings.ToLower(random.UniqueId()))
203+
realTerraformDir := ".."
204+
tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId())))
205+
existingRes := realTerraformDir + "/tests/existing-resources"
206+
207+
// Verify ibmcloud_api_key variable is set
208+
checkVariable := "TF_VAR_ibmcloud_api_key"
209+
val, present := os.LookupEnv(checkVariable)
210+
require.True(t, present, checkVariable+" environment variable not set")
211+
require.NotEqual(t, "", val, checkVariable+" environment variable is empty")
212+
logger.Log(t, "Tempdir: ", tempTerraformDir)
213+
existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
214+
TerraformDir: existingRes,
215+
Vars: map[string]interface{}{
216+
"prefix": prefix,
217+
"region": validRegions[rand.Intn(len(validRegions))],
218+
},
219+
// Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
220+
// This is the same as setting the -upgrade=true flag with terraform.
221+
Upgrade: true,
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+
229+
var region = validRegions[rand.Intn(len(validRegions))]
230+
231+
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
232+
Testing: t,
233+
Prefix: "en-exs-res",
234+
TarIncludePatterns: []string{
235+
"*.tf",
236+
solutionDADir + "/*.tf",
237+
},
238+
TemplateFolder: solutionDADir,
239+
Tags: []string{"test-schematic"},
240+
DeleteWorkspaceOnFail: false,
241+
WaitJobCompleteMinutes: 60,
242+
})
243+
244+
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
245+
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
246+
{Name: "region", Value: region, DataType: "string"},
247+
{Name: "resource_group_name", Value: terraform.Output(t, existingTerraformOptions, "resource_group_name"), DataType: "string"},
248+
{Name: "existing_kms_instance_crn", Value: permanentResources["hpcs_south_crn"], DataType: "string"},
249+
{Name: "existing_en_instance_crn", Value: terraform.Output(t, existingTerraformOptions, "event_notification_instance_crn"), DataType: "string"},
250+
{Name: "kms_endpoint_url", Value: permanentResources["hpcs_south_private_endpoint"], DataType: "string"},
251+
}
252+
253+
err := options.RunSchematicTest()
254+
255+
if assert.NoError(t, err) {
256+
t.Log("TestRunExistingResourcesInstances Passed")
257+
} else {
258+
t.Error("TestRunExistingResourcesInstances Failed")
259+
}
260+
}
261+
262+
// Check if "DO_NOT_DESTROY_ON_FAILURE" is set
263+
envVal, _ := os.LookupEnv("DO_NOT_DESTROY_ON_FAILURE")
264+
// Destroy the temporary existing resources if required
265+
if t.Failed() && strings.ToLower(envVal) == "true" {
266+
fmt.Println("Terratest failed. Debug the test and delete resources manually.")
267+
} else {
268+
logger.Log(t, "START: Destroy (existing resources)")
269+
terraform.Destroy(t, existingTerraformOptions)
270+
terraform.WorkspaceDelete(t, existingTerraformOptions, prefix)
271+
logger.Log(t, "END: Destroy (existing resources)")
272+
}
273+
}

0 commit comments

Comments
 (0)