diff --git a/README.md b/README.md index f420fe5..4cc5ec1 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,9 @@ individually skipped as needed. By default, all phases are run. The following ph | Phase | Description |Flag| | ----- | ----------- |----| +| Force New Test Suite | Creates a new test suite in a new temp dir when another test suite is present | force-new-suite | +| Select Test Suite | Selects a test suite from `test-suite.json`. Required when multiple test suites are present | suite-index | +| 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 | | 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 | | Setup Component Under Test | Copies the component from `src/` to the temp dir `components/terraform` | `-skip-setup-cut` | | Vendor Dependencies | Runs the `atmos vendor pull` command to pull in dependency components | `-skip-vendor` | diff --git a/README.yaml b/README.yaml index 5365b61..ed0c3c6 100644 --- a/README.yaml +++ b/README.yaml @@ -181,6 +181,9 @@ introduction: |- | Phase | Description |Flag| | ----- | ----------- |----| + | Force New Test Suite | Creates a new test suite in a new temp dir when another test suite is present | force-new-suite | + | Select Test Suite | Selects a test suite from `test-suite.json`. Required when multiple test suites are present | suite-index | + | 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 | | 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 | | Setup Component Under Test | Copies the component from `src/` to the temp dir `components/terraform` | `-skip-setup-cut` | | Vendor Dependencies | Runs the `atmos vendor pull` command to pull in dependency components | `-skip-vendor` | diff --git a/pkg/atmos/aws-component-helper/cli.go b/pkg/atmos/aws-component-helper/cli.go index 01f92f9..1619e05 100644 --- a/pkg/atmos/aws-component-helper/cli.go +++ b/pkg/atmos/aws-component-helper/cli.go @@ -3,6 +3,8 @@ package aws_component_helper import "flag" func parseCLIArgs(ts *TestSuite) *TestSuite { + forceNewSuite := flag.Bool("force-new-suite", false, "force new suite") + suiteIndex := flag.Int("suite-index", -1, "suite index") skipAwsNuke := flag.Bool("skip-aws-nuke", ts.SkipNukeTestAccount, "skip aws nuke") skipDeployDependencies := flag.Bool("skip-deploy-deps", ts.SkipDeployDependencies, "skip deploy dependencies") skipDestroyDependencies := flag.Bool("skip-destroy-deps", ts.SkipDestroyDependencies, "skip destroy dependencies") @@ -15,6 +17,8 @@ func parseCLIArgs(ts *TestSuite) *TestSuite { flag.Parse() + ts.ForceNewSuite = *forceNewSuite + ts.Index = *suiteIndex ts.SkipNukeTestAccount = *skipAwsNuke ts.SkipDeployDependencies = *skipDeployDependencies ts.SkipDestroyDependencies = *skipDestroyDependencies diff --git a/pkg/atmos/aws-component-helper/setup_test_suite.go b/pkg/atmos/aws-component-helper/setup_test_suite.go index 1a3b7f9..694f502 100644 --- a/pkg/atmos/aws-component-helper/setup_test_suite.go +++ b/pkg/atmos/aws-component-helper/setup_test_suite.go @@ -80,35 +80,54 @@ func getAwsAccountId() (string, error) { } func readOrCreateTestSuiteFile(testSuite *TestSuite, testName string) (*TestSuite, error) { + // Initialize TestSuites structure + var testSuites TestSuites + if data, err := os.ReadFile(testSuiteFile); err == nil { - if err := json.Unmarshal(data, &testSuite); err != nil { + // File exists, try to unmarshal existing test suites + if err := json.Unmarshal(data, &testSuites); err != nil { return &TestSuite{}, fmt.Errorf("failed to parse test_suites.json: %s", err.Error()) } - fmt.Printf("running tests in %s\n", testSuite.TempDir) - return testSuite, nil - } else { - randID := random.UniqueId() - testSuite.RandomIdentifier = strings.ToLower(randID) - - testSuite.TempDir, err = os.MkdirTemp("", testName) - if err != nil { - return &TestSuite{}, err + if len(testSuites.Suites) > 1 && testSuite.Index < 0 { + return &TestSuite{}, fmt.Errorf("test suite index is required when multiple test suites are present") } - fmt.Printf("running tests in %s\n", testSuite.TempDir) - // Write new values to file - data, err := json.MarshalIndent(testSuite, "", " ") - - if err != nil { - return &TestSuite{}, err + if testSuite.Index == -1 && len(testSuites.Suites) == 1 { + testSuite.Index = 0 } - if err := os.WriteFile(testSuiteFile, data, 0644); err != nil { - return &TestSuite{}, err + if !testSuite.ForceNewSuite && len(testSuites.Suites) > 0 { + return testSuites.Suites[testSuite.Index], nil } } + // If we get here, either the file doesn't exist or we didn't find a matching suite + fmt.Println("no matching test suite found for index", testSuite.Index, "creating new test suite") + randID := random.UniqueId() + testSuite.RandomIdentifier = strings.ToLower(randID) + testSuite.Index = len(testSuites.Suites) // Set index to current length + + var err error + testSuite.TempDir, err = os.MkdirTemp("", testName) + if err != nil { + return &TestSuite{}, err + } + fmt.Printf("running tests in %s\n", testSuite.TempDir) + + // Add new test suite to the collection + testSuites.Suites = append(testSuites.Suites, testSuite) + + // Write updated test suites to file + data, err := json.MarshalIndent(testSuites, "", " ") + if err != nil { + return &TestSuite{}, err + } + + if err := os.WriteFile(testSuiteFile, data, 0644); err != nil { + return &TestSuite{}, err + } + os.Setenv("ATMOS_BASE_PATH", testSuite.TempDir) os.Setenv("ATMOS_CLI_CONFIG_PATH", testSuite.TempDir) os.Setenv("TEST_ACCOUNT_ID", testSuite.AwsAccountId) diff --git a/pkg/atmos/aws-component-helper/test_suite.go b/pkg/atmos/aws-component-helper/test_suite.go index 49e079d..e003fb4 100644 --- a/pkg/atmos/aws-component-helper/test_suite.go +++ b/pkg/atmos/aws-component-helper/test_suite.go @@ -15,6 +15,8 @@ type TestSuite struct { ComponentSrcPath string Dependencies []*Dependency FixturesPath string + ForceNewSuite bool + Index int RandomIdentifier string SkipSetupComponentUnderTest bool SkipDeployDependencies bool @@ -29,6 +31,10 @@ type TestSuite struct { TempDir string } +type TestSuites struct { + Suites []*TestSuite +} + // Option type represents a configuration option type TestSuiteOption func(*TestSuite) @@ -229,15 +235,15 @@ func NewTestSuite(awsRegion string, componentName string, stackName string, opts opt(suite) } + // Parse the CLI args + suite = parseCLIArgs(suite) + // Read or create the test suite file suite, err = readOrCreateTestSuiteFile(suite, testName) if err != nil { panic("Failed to create test suite: " + err.Error()) } - // Parse the CLI args - suite = parseCLIArgs(suite) - return suite, nil }