Skip to content

Commit 95a2cec

Browse files
authored
test: use schematics for admin pass (#575)
1 parent b2adff6 commit 95a2cec

File tree

2 files changed

+119
-66
lines changed

2 files changed

+119
-66
lines changed

tests/other_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8+
"github.com/gruntwork-io/terratest/modules/terraform"
89
"github.com/stretchr/testify/assert"
910
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
1011
)
@@ -137,3 +138,38 @@ func TestRunBasicExampleWithFlavorMultitenant(t *testing.T) {
137138
assert.Nil(t, err, "This should not have errored")
138139
assert.NotNil(t, output, "Expected some output")
139140
}
141+
142+
func TestRunFSCloudExample(t *testing.T) {
143+
t.Parallel()
144+
145+
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
146+
Testing: t,
147+
TerraformDir: "examples/fscloud",
148+
Prefix: "postgres-fscloud",
149+
Region: "us-south", // For FSCloud locking into us-south since that is where the HPCS permanent instance is
150+
/*
151+
Comment out the 'ResourceGroup' input to force this tests to create a unique resource group to ensure tests do
152+
not clash. This is due to the fact that an auth policy may already exist in this resource group since we are
153+
re-using a permanent HPCS instance. By using a new resource group, the auth policy will not already exist
154+
since this module scopes auth policies by resource group.
155+
*/
156+
//ResourceGroup: resourceGroup,
157+
TerraformVars: map[string]interface{}{
158+
"access_tags": permanentResources["accessTags"],
159+
"kms_key_crn": permanentResources["hpcs_south_root_key_crn"],
160+
"pg_version": "16", // Always lock this test into the latest supported Postgres version
161+
},
162+
CloudInfoService: sharedInfoSvc,
163+
})
164+
options.SkipTestTearDown = true
165+
output, err := options.RunTestConsistency()
166+
assert.Nil(t, err, "This should not have errored")
167+
assert.NotNil(t, output, "Expected some output")
168+
169+
// check if outputs exist
170+
outputs := terraform.OutputAll(options.Testing, options.TerraformOptions)
171+
expectedOutputs := []string{"port", "hostname"}
172+
_, outputErr := testhelper.ValidateTerraformOutputs(outputs, expectedOutputs...)
173+
assert.NoErrorf(t, outputErr, "Some outputs not found or nil")
174+
options.TestTearDown()
175+
}

tests/pr_test.go

Lines changed: 83 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ package test
44
import (
55
"crypto/rand"
66
"encoding/base64"
7+
"fmt"
8+
"io/fs"
79
"log"
810
"os"
11+
"path/filepath"
12+
"strings"
913
"testing"
1014

1115
"github.com/gruntwork-io/terratest/modules/logger"
@@ -15,6 +19,7 @@ import (
1519
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/cloudinfo"
1620
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common"
1721
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
22+
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testschematic"
1823
)
1924

2025
// Use existing resource group
@@ -45,84 +50,97 @@ func TestMain(m *testing.M) {
4550
os.Exit(m.Run())
4651
}
4752

48-
func TestRunFSCloudExample(t *testing.T) {
49-
t.Parallel()
53+
type tarIncludePatterns struct {
54+
excludeDirs []string
5055

51-
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
52-
Testing: t,
53-
TerraformDir: "examples/fscloud",
54-
Prefix: "postgres-fscloud",
55-
Region: "us-south", // For FSCloud locking into us-south since that is where the HPCS permanent instance is
56-
/*
57-
Comment out the 'ResourceGroup' input to force this tests to create a unique resource group to ensure tests do
58-
not clash. This is due to the fact that an auth policy may already exist in this resource group since we are
59-
re-using a permanent HPCS instance. By using a new resource group, the auth policy will not already exist
60-
since this module scopes auth policies by resource group.
61-
*/
62-
//ResourceGroup: resourceGroup,
63-
TerraformVars: map[string]interface{}{
64-
"access_tags": permanentResources["accessTags"],
65-
"kms_key_crn": permanentResources["hpcs_south_root_key_crn"],
66-
"pg_version": "16", // Always lock this test into the latest supported Postgres version
67-
},
68-
CloudInfoService: sharedInfoSvc,
69-
})
70-
options.SkipTestTearDown = true
71-
output, err := options.RunTestConsistency()
72-
assert.Nil(t, err, "This should not have errored")
73-
assert.NotNil(t, output, "Expected some output")
74-
75-
// check if outputs exist
76-
outputs := terraform.OutputAll(options.Testing, options.TerraformOptions)
77-
expectedOutputs := []string{"port", "hostname"}
78-
_, outputErr := testhelper.ValidateTerraformOutputs(outputs, expectedOutputs...)
79-
assert.NoErrorf(t, outputErr, "Some outputs not found or nil")
80-
options.TestTearDown()
81-
}
56+
includeFiletypes []string
8257

83-
func TestRunBasicExampleWithFlavor(t *testing.T) {
84-
t.Parallel()
58+
includeDirs []string
59+
}
8560

86-
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
87-
Testing: t,
88-
TerraformDir: "examples/basic",
89-
Prefix: "postgres-flvr",
90-
BestRegionYAMLPath: regionSelectionPath,
91-
ResourceGroup: resourceGroup,
92-
TerraformVars: map[string]interface{}{
93-
"member_host_flavor": "b3c.4x16.encrypted",
94-
},
95-
CloudInfoService: sharedInfoSvc,
61+
func getTarIncludePatternsRecursively(dir string, dirsToExclude []string, fileTypesToInclude []string) ([]string, error) {
62+
r := tarIncludePatterns{dirsToExclude, fileTypesToInclude, nil}
63+
err := filepath.WalkDir(dir, func(path string, entry fs.DirEntry, err error) error {
64+
return walk(&r, path, entry, err)
9665
})
66+
if err != nil {
67+
fmt.Println("error")
68+
return r.includeDirs, err
69+
}
70+
return r.includeDirs, nil
71+
}
9772

98-
output, err := options.RunTestConsistency()
99-
assert.Nil(t, err, "This should not have errored")
100-
assert.NotNil(t, output, "Expected some output")
73+
func walk(r *tarIncludePatterns, s string, d fs.DirEntry, err error) error {
74+
if err != nil {
75+
return err
76+
}
77+
if d.IsDir() {
78+
for _, excludeDir := range r.excludeDirs {
79+
if strings.Contains(s, excludeDir) {
80+
return nil
81+
}
82+
}
83+
if s == ".." {
84+
r.includeDirs = append(r.includeDirs, "*.tf")
85+
return nil
86+
}
87+
for _, includeFiletype := range r.includeFiletypes {
88+
r.includeDirs = append(r.includeDirs, strings.ReplaceAll(s+"/*"+includeFiletype, "../", ""))
89+
}
90+
}
91+
return nil
10192
}
10293

103-
func TestRunStandardSolution(t *testing.T) {
94+
func TestRunStandardSolutionSchematics(t *testing.T) {
10495
t.Parallel()
10596

106-
options := testhelper.TestOptionsDefault(&testhelper.TestOptions{
107-
Testing: t,
108-
TerraformDir: standardSolutionTerraformDir,
109-
Region: "us-south",
110-
Prefix: "postgres-st-da",
111-
ResourceGroup: resourceGroup,
97+
excludeDirs := []string{
98+
".terraform",
99+
".docs",
100+
".github",
101+
".git",
102+
".idea",
103+
"common-dev-assets",
104+
"examples",
105+
"tests",
106+
"reference-architectures",
107+
}
108+
includeFiletypes := []string{
109+
".tf",
110+
".yaml",
111+
".py",
112+
".tpl",
113+
".sh",
114+
}
115+
116+
tarIncludePatterns, recurseErr := getTarIncludePatternsRecursively("..", excludeDirs, includeFiletypes)
117+
118+
// if error producing tar patterns (very unexpected) fail test immediately
119+
require.NoError(t, recurseErr, "Schematic Test had unexpected error traversing directory tree")
120+
prefix := "postgres-st-da"
121+
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
122+
Testing: t,
123+
TarIncludePatterns: tarIncludePatterns,
124+
TemplateFolder: standardSolutionTerraformDir,
125+
BestRegionYAMLPath: regionSelectionPath,
126+
Prefix: prefix,
127+
ResourceGroup: resourceGroup,
128+
DeleteWorkspaceOnFail: false,
129+
WaitJobCompleteMinutes: 60,
112130
})
113131

114-
options.TerraformVars = map[string]interface{}{
115-
"pg_version": "16", // Always lock this test into the latest supported PostgreSQL version
116-
"existing_kms_instance_crn": permanentResources["hpcs_south_crn"],
117-
"kms_endpoint_type": "public",
118-
"provider_visibility": "public",
119-
"existing_backup_kms_key_crn": permanentResources["hpcs_south_root_key_crn"],
120-
"resource_group_name": options.Prefix,
132+
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
133+
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
134+
{Name: "access_tags", Value: permanentResources["accessTags"], DataType: "list(string)"},
135+
{Name: "existing_kms_instance_crn", Value: permanentResources["hpcs_south_crn"], DataType: "string"},
136+
{Name: "existing_backup_kms_key_crn", Value: permanentResources["hpcs_south_root_key_crn"], DataType: "string"},
137+
{Name: "kms_endpoint_type", Value: "private", DataType: "string"},
138+
{Name: "pg_version", Value: "16", DataType: "string"}, // Always lock this test into the latest supported PostgresSQL version
139+
{Name: "resource_group_name", Value: options.Prefix, DataType: "string"},
140+
{Name: "admin_pass", Value: GetRandomAdminPassword(t), DataType: "string"},
121141
}
122-
123-
output, err := options.RunTestConsistency()
142+
err := options.RunSchematicTest()
124143
assert.Nil(t, err, "This should not have errored")
125-
assert.NotNil(t, output, "Expected some output")
126144
}
127145

128146
func TestRunStandardUpgradeSolution(t *testing.T) {
@@ -141,7 +159,6 @@ func TestRunStandardUpgradeSolution(t *testing.T) {
141159
"kms_endpoint_type": "public",
142160
"provider_visibility": "public",
143161
"resource_group_name": options.Prefix,
144-
"admin_pass": GetRandomAdminPassword(t),
145162
}
146163

147164
output, err := options.RunTestUpgrade()

0 commit comments

Comments
 (0)