Skip to content

Commit 73e253b

Browse files
author
Matt Calhoun
authored
allow multiple test suites (#39)
1 parent bd1fd71 commit 73e253b

File tree

5 files changed

+56
-21
lines changed

5 files changed

+56
-21
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ individually skipped as needed. By default, all phases are run. The following ph
192192
193193
| Phase | Description |Flag|
194194
| ----- | ----------- |----|
195+
| Force New Test Suite | Creates a new test suite in a new temp dir when another test suite is present | force-new-suite |
196+
| Select Test Suite | Selects a test suite from `test-suite.json`. Required when multiple test suites are present | suite-index |
197+
| Setup Test Suite | Bootstraps a temp directory and creates a new test suite or reads in a test suite from `test-suite.json` | N/A |
195198
| Setup Test Suite | Bootstraps a temp directory and creates a new test suite or reads in a test suite from `test-suite.json` | N/A |
196199
| Setup Component Under Test | Copies the component from `src/` to the temp dir `components/terraform` | `-skip-setup-cut` |
197200
| Vendor Dependencies | Runs the `atmos vendor pull` command to pull in dependency components | `-skip-vendor` |

README.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ introduction: |-
181181
182182
| Phase | Description |Flag|
183183
| ----- | ----------- |----|
184+
| Force New Test Suite | Creates a new test suite in a new temp dir when another test suite is present | force-new-suite |
185+
| Select Test Suite | Selects a test suite from `test-suite.json`. Required when multiple test suites are present | suite-index |
186+
| Setup Test Suite | Bootstraps a temp directory and creates a new test suite or reads in a test suite from `test-suite.json` | N/A |
184187
| Setup Test Suite | Bootstraps a temp directory and creates a new test suite or reads in a test suite from `test-suite.json` | N/A |
185188
| Setup Component Under Test | Copies the component from `src/` to the temp dir `components/terraform` | `-skip-setup-cut` |
186189
| Vendor Dependencies | Runs the `atmos vendor pull` command to pull in dependency components | `-skip-vendor` |

pkg/atmos/aws-component-helper/cli.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package aws_component_helper
33
import "flag"
44

55
func parseCLIArgs(ts *TestSuite) *TestSuite {
6+
forceNewSuite := flag.Bool("force-new-suite", false, "force new suite")
7+
suiteIndex := flag.Int("suite-index", -1, "suite index")
68
skipAwsNuke := flag.Bool("skip-aws-nuke", ts.SkipNukeTestAccount, "skip aws nuke")
79
skipDeployDependencies := flag.Bool("skip-deploy-deps", ts.SkipDeployDependencies, "skip deploy dependencies")
810
skipDestroyDependencies := flag.Bool("skip-destroy-deps", ts.SkipDestroyDependencies, "skip destroy dependencies")
@@ -15,6 +17,8 @@ func parseCLIArgs(ts *TestSuite) *TestSuite {
1517

1618
flag.Parse()
1719

20+
ts.ForceNewSuite = *forceNewSuite
21+
ts.Index = *suiteIndex
1822
ts.SkipNukeTestAccount = *skipAwsNuke
1923
ts.SkipDeployDependencies = *skipDeployDependencies
2024
ts.SkipDestroyDependencies = *skipDestroyDependencies

pkg/atmos/aws-component-helper/setup_test_suite.go

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,35 +80,54 @@ func getAwsAccountId() (string, error) {
8080
}
8181

8282
func readOrCreateTestSuiteFile(testSuite *TestSuite, testName string) (*TestSuite, error) {
83+
// Initialize TestSuites structure
84+
var testSuites TestSuites
85+
8386
if data, err := os.ReadFile(testSuiteFile); err == nil {
84-
if err := json.Unmarshal(data, &testSuite); err != nil {
87+
// File exists, try to unmarshal existing test suites
88+
if err := json.Unmarshal(data, &testSuites); err != nil {
8589
return &TestSuite{}, fmt.Errorf("failed to parse test_suites.json: %s", err.Error())
8690
}
8791

88-
fmt.Printf("running tests in %s\n", testSuite.TempDir)
89-
return testSuite, nil
90-
} else {
91-
randID := random.UniqueId()
92-
testSuite.RandomIdentifier = strings.ToLower(randID)
93-
94-
testSuite.TempDir, err = os.MkdirTemp("", testName)
95-
if err != nil {
96-
return &TestSuite{}, err
92+
if len(testSuites.Suites) > 1 && testSuite.Index < 0 {
93+
return &TestSuite{}, fmt.Errorf("test suite index is required when multiple test suites are present")
9794
}
98-
fmt.Printf("running tests in %s\n", testSuite.TempDir)
9995

100-
// Write new values to file
101-
data, err := json.MarshalIndent(testSuite, "", " ")
102-
103-
if err != nil {
104-
return &TestSuite{}, err
96+
if testSuite.Index == -1 && len(testSuites.Suites) == 1 {
97+
testSuite.Index = 0
10598
}
10699

107-
if err := os.WriteFile(testSuiteFile, data, 0644); err != nil {
108-
return &TestSuite{}, err
100+
if !testSuite.ForceNewSuite && len(testSuites.Suites) > 0 {
101+
return testSuites.Suites[testSuite.Index], nil
109102
}
110103
}
111104

105+
// If we get here, either the file doesn't exist or we didn't find a matching suite
106+
fmt.Println("no matching test suite found for index", testSuite.Index, "creating new test suite")
107+
randID := random.UniqueId()
108+
testSuite.RandomIdentifier = strings.ToLower(randID)
109+
testSuite.Index = len(testSuites.Suites) // Set index to current length
110+
111+
var err error
112+
testSuite.TempDir, err = os.MkdirTemp("", testName)
113+
if err != nil {
114+
return &TestSuite{}, err
115+
}
116+
fmt.Printf("running tests in %s\n", testSuite.TempDir)
117+
118+
// Add new test suite to the collection
119+
testSuites.Suites = append(testSuites.Suites, testSuite)
120+
121+
// Write updated test suites to file
122+
data, err := json.MarshalIndent(testSuites, "", " ")
123+
if err != nil {
124+
return &TestSuite{}, err
125+
}
126+
127+
if err := os.WriteFile(testSuiteFile, data, 0644); err != nil {
128+
return &TestSuite{}, err
129+
}
130+
112131
os.Setenv("ATMOS_BASE_PATH", testSuite.TempDir)
113132
os.Setenv("ATMOS_CLI_CONFIG_PATH", testSuite.TempDir)
114133
os.Setenv("TEST_ACCOUNT_ID", testSuite.AwsAccountId)

pkg/atmos/aws-component-helper/test_suite.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type TestSuite struct {
1515
ComponentSrcPath string
1616
Dependencies []*Dependency
1717
FixturesPath string
18+
ForceNewSuite bool
19+
Index int
1820
RandomIdentifier string
1921
SkipSetupComponentUnderTest bool
2022
SkipDeployDependencies bool
@@ -29,6 +31,10 @@ type TestSuite struct {
2931
TempDir string
3032
}
3133

34+
type TestSuites struct {
35+
Suites []*TestSuite
36+
}
37+
3238
// Option type represents a configuration option
3339
type TestSuiteOption func(*TestSuite)
3440

@@ -229,15 +235,15 @@ func NewTestSuite(awsRegion string, componentName string, stackName string, opts
229235
opt(suite)
230236
}
231237

238+
// Parse the CLI args
239+
suite = parseCLIArgs(suite)
240+
232241
// Read or create the test suite file
233242
suite, err = readOrCreateTestSuiteFile(suite, testName)
234243
if err != nil {
235244
panic("Failed to create test suite: " + err.Error())
236245
}
237246

238-
// Parse the CLI args
239-
suite = parseCLIArgs(suite)
240-
241247
return suite, nil
242248
}
243249

0 commit comments

Comments
 (0)