Skip to content

Commit 1bdf4be

Browse files
authored
updates to tests to ensure auth policy clashes don't occur (#327)
1 parent 58d92eb commit 1bdf4be

12 files changed

+118
-9
lines changed

.catalog-onboard-pipeline.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ offerings:
99
- name: security-enforced
1010
mark_ready: true
1111
install_type: fullstack
12+
pre_validation: "tests/scripts/pre-validate.sh solutions/security-enforced"
13+
post_validation: "tests/scripts/post-validate.sh"
1214
scc:
1315
instance_id: 1c7d5f78-9262-44c3-b779-b28fe4d88c37
1416
region: us-south
1517
scope_resource_group_var_name: existing_resource_group_name
1618
- name: fully-configurable
1719
mark_ready: true
1820
install_type: fullstack
21+
pre_validation: "tests/scripts/pre-validate.sh solutions/fully-configurable"
22+
post_validation: "tests/scripts/post-validate.sh"
1923
scc:
2024
instance_id: 1c7d5f78-9262-44c3-b779-b28fe4d88c37
2125
region: us-south

solutions/fully-configurable/catalogValidationValues.json.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"ibmcloud_api_key": $VALIDATION_APIKEY,
3-
"existing_resource_group_name": "geretain-test-secrets-manager",
43
"prefix": $PREFIX,
54
"service_plan": "trial",
65
"kms_encryption_enabled": true,

solutions/security-enforced/catalogValidationValues.json.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"ibmcloud_api_key": $VALIDATION_APIKEY,
3-
"existing_resource_group_name": "geretain-test-secrets-manager",
43
"prefix": $PREFIX,
54
"service_plan": "trial",
65
"existing_kms_instance_crn": $HPCS_US_SOUTH_CRN,

tests/new-rg/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 for by catalog pipeline

tests/new-rg/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

tests/new-rg/outputs.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
##############################################################################
2+
# Outputs
3+
##############################################################################
4+
5+
output "resource_group_id" {
6+
value = module.resource_group.resource_group_id
7+
description = "Resource group ID."
8+
}
9+
10+
output "resource_group_name" {
11+
value = module.resource_group.resource_group_name
12+
description = "Resource group name."
13+
}

tests/new-rg/provider.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
}

tests/new-rg/variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
variable "ibmcloud_api_key" {
2+
type = string
3+
description = "The IBM Cloud API Key."
4+
sensitive = true
5+
}
6+
7+
variable "prefix" {
8+
type = string
9+
description = "Prefix to append to all resources created by this example."
10+
default = "sm"
11+
}
12+
13+
variable "resource_group" {
14+
type = string
15+
description = "The name of an existing resource group to provision resources in. If not specified, a new resource group is created with the `prefix` variable."
16+
default = null
17+
}

tests/new-rg/version.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.9.0"
3+
required_providers {
4+
ibm = {
5+
source = "ibm-cloud/ibm"
6+
version = ">= 1.76.1"
7+
}
8+
}
9+
}

tests/pr_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestRunExistingResourcesInstancesFullyConfigurable(t *testing.T) {
176176
{Name: "region", Value: region, DataType: "string"},
177177
{Name: "existing_resource_group_name", Value: terraform.Output(t, existingTerraformOptions, "resource_group_name"), DataType: "string"},
178178
{Name: "existing_event_notification_instance_crn", Value: terraform.Output(t, existingTerraformOptions, "event_notification_instance_crn"), DataType: "string"},
179-
{Name: "existing_secrets_manager_kms_key_crn", Value: permanentResources["hpcs_south_root_key_crn"], DataType: "string"},
179+
{Name: "existing_secrets_manager_kms_key_crn", Value: terraform.Output(t, existingTerraformOptions, "secrets_manager_kms_key_crn"), DataType: "string"},
180180
{Name: "kms_encryption_enabled", Value: true, DataType: "bool"},
181181
{Name: "service_plan", Value: "trial", DataType: "string"},
182182
{Name: "secret_groups", Value: _secret_group_config(options.Prefix), DataType: "list(object)"},
@@ -282,7 +282,6 @@ func TestRunSecurityEnforcedSchematics(t *testing.T) {
282282
// ------------------------------------------------------------------------------------
283283
// Provision new RG
284284
// ------------------------------------------------------------------------------------
285-
region := validRegions[rand.Intn(len(validRegions))]
286285
prefix := fmt.Sprintf("sm-se-%s", strings.ToLower(random.UniqueId()))
287286
realTerraformDir := ".."
288287
tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId())))
@@ -294,10 +293,9 @@ func TestRunSecurityEnforcedSchematics(t *testing.T) {
294293
require.NotEqual(t, "", val, checkVariable+" environment variable is empty")
295294
logger.Log(t, "Tempdir: ", tempTerraformDir)
296295
existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
297-
TerraformDir: tempTerraformDir + "/tests/new-resources",
296+
TerraformDir: tempTerraformDir + "/tests/new-rg",
298297
Vars: map[string]interface{}{
299298
"prefix": prefix,
300-
"region": region,
301299
},
302300
// Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
303301
// This is the same as setting the -upgrade=true flag with terraform.
@@ -361,7 +359,6 @@ func TestRunSecretsManagerSecurityEnforcedUpgradeSchematic(t *testing.T) {
361359
// ------------------------------------------------------------------------------------
362360
// Provision new RG
363361
// ------------------------------------------------------------------------------------
364-
region := validRegions[rand.Intn(len(validRegions))]
365362
prefix := fmt.Sprintf("sm-se-ug-%s", strings.ToLower(random.UniqueId()))
366363
realTerraformDir := ".."
367364
tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId())))
@@ -373,10 +370,9 @@ func TestRunSecretsManagerSecurityEnforcedUpgradeSchematic(t *testing.T) {
373370
require.NotEqual(t, "", val, checkVariable+" environment variable is empty")
374371
logger.Log(t, "Tempdir: ", tempTerraformDir)
375372
existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
376-
TerraformDir: tempTerraformDir + "/tests/new-resources",
373+
TerraformDir: tempTerraformDir + "/tests/new-rg",
377374
Vars: map[string]interface{}{
378375
"prefix": prefix,
379-
"region": region,
380376
},
381377
// Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
382378
// This is the same as setting the -upgrade=true flag with terraform.

tests/scripts/post-validate.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /bin/bash
2+
3+
########################################################################################################################
4+
## This script is used by the catalog pipeline to destroy prerequisite resource required for catalog validation ##
5+
########################################################################################################################
6+
7+
set -e
8+
9+
TERRAFORM_SOURCE_DIR="tests/new-rg"
10+
TF_VARS_FILE="terraform.tfvars"
11+
12+
(
13+
cd ${TERRAFORM_SOURCE_DIR}
14+
echo "Destroying resource group .."
15+
terraform destroy -input=false -auto-approve -var-file=${TF_VARS_FILE} || exit 1
16+
rm -f "${TF_VARS_FILE}"
17+
18+
echo "Post-validation completed successfully"
19+
)

tests/scripts/pre-validate.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! /bin/bash
2+
3+
############################################################################################################
4+
## This script is used by the catalog pipeline to provision a new resource group
5+
## (required to ensure auth policies don't clash in account)
6+
############################################################################################################
7+
8+
set -e
9+
10+
DA_DIR="${1}"
11+
TERRAFORM_SOURCE_DIR="tests/new-rg"
12+
JSON_FILE="${DA_DIR}/catalogValidationValues.json"
13+
TF_VARS_FILE="terraform.tfvars"
14+
15+
(
16+
cwd=$(pwd)
17+
cd ${TERRAFORM_SOURCE_DIR}
18+
echo "Provisioning new resource group .."
19+
terraform init || exit 1
20+
# $VALIDATION_APIKEY is available in the catalog runtime
21+
{
22+
echo "ibmcloud_api_key=\"${VALIDATION_APIKEY}\""
23+
echo "prefix=\"ocp-$(openssl rand -hex 2)\""
24+
} >> ${TF_VARS_FILE}
25+
terraform apply -input=false -auto-approve -var-file=${TF_VARS_FILE} || exit 1
26+
27+
rg_var_name="existing_resource_group_name"
28+
rg_value=$(terraform output -state=terraform.tfstate -raw resource_group_name)
29+
30+
echo "Appending '${rg_var_name}', input variable value to ${JSON_FILE}.."
31+
32+
cd "${cwd}"
33+
jq -r --arg rg_var_name "${rg_var_name}" \
34+
--arg rg_value "${rg_value}" \
35+
'. + {($rg_var_name): $rg_value}' "${JSON_FILE}" > tmpfile && mv tmpfile "${JSON_FILE}" || exit 1
36+
37+
echo "Pre-validation complete successfully"
38+
)

0 commit comments

Comments
 (0)