Skip to content

Commit

Permalink
allow multiple test suites (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalhoun authored Nov 25, 2024
1 parent bd1fd71 commit 73e253b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
3 changes: 3 additions & 0 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
4 changes: 4 additions & 0 deletions pkg/atmos/aws-component-helper/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
55 changes: 37 additions & 18 deletions pkg/atmos/aws-component-helper/setup_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions pkg/atmos/aws-component-helper/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type TestSuite struct {
ComponentSrcPath string
Dependencies []*Dependency
FixturesPath string
ForceNewSuite bool
Index int
RandomIdentifier string
SkipSetupComponentUnderTest bool
SkipDeployDependencies bool
Expand All @@ -29,6 +31,10 @@ type TestSuite struct {
TempDir string
}

type TestSuites struct {
Suites []*TestSuite
}

// Option type represents a configuration option
type TestSuiteOption func(*TestSuite)

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 73e253b

Please sign in to comment.