Skip to content

Commit

Permalink
Get rid of asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
goruha committed Dec 26, 2024
1 parent c124a53 commit 0392b9f
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 357 deletions.
12 changes: 6 additions & 6 deletions pkg/atmos/aws-component-helper/atmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var (
//
// atmosOptions := atmos.WithDefaultRetryableErrors(t, &atmos.Options{
// AtmosBasePath: suite.TempDir,
// Component: componentName,
// Stack: stackName,
// ComponentName: componentName,
// StackName: stackName,
// NoColor: true,
// BackendConfig: map[string]interface{}{
// "workspace_key_prefix": strings.Join([]string{suite.RandomIdentifier, stackName}, "-"),
Expand All @@ -48,7 +48,7 @@ var (
//
//func deployDependencies(t *testing.T, suite *TestSuite) error {
// for _, dependency := range suite.Dependencies {
// _, _, err := deployComponent(t, suite, dependency.ComponentName, dependency.StackName, map[string]interface{}{})
// _, _, err := DeployComponent(t, suite, dependency.ComponentName, dependency.StackName, map[string]interface{}{})
// if err != nil {
// return err
// }
Expand All @@ -60,15 +60,15 @@ var (
//func destroyDependencies(t *testing.T, suite *TestSuite) error {
// // iterate over dependencies in reverse order and destroy them
// for i := len(suite.Dependencies) - 1; i >= 0; i-- {
// _, _, err := destroyComponent(t, suite, suite.Dependencies[i].ComponentName, suite.Dependencies[i].StackName, map[string]interface{}{})
// _, _, err := DestroyComponent(t, suite, suite.Dependencies[i].ComponentName, suite.Dependencies[i].StackName, map[string]interface{}{})
// if err != nil {
// return err
// }
// }
// return nil
//}
//
//func deployComponent(t *testing.T, suite *TestSuite, componentName string, stackName string, vars map[string]interface{}) (*atmos.Options, string, error) {
//func DeployComponent(t *testing.T, suite *TestSuite, componentName string, stackName string, vars map[string]interface{}) (*atmos.Options, string, error) {
// options := GetAtmosOptions(t, suite, componentName, stackName, vars)
// out, err := atmos.ApplyE(t, options)
//
Expand All @@ -94,7 +94,7 @@ var (
// return options, nil
//}

//func destroyComponent(t *testing.T, suite *TestSuite, componentName string, stackName string, vars map[string]interface{}) (*atmos.Options, string, error) {
//func DestroyComponent(t *testing.T, suite *TestSuite, componentName string, stackName string, vars map[string]interface{}) (*atmos.Options, string, error) {
// options := GetAtmosOptions(t, suite, componentName, stackName, vars)
// out, err := atmos.DestroyE(t, options)
//
Expand Down
37 changes: 30 additions & 7 deletions pkg/atmos/aws-component-helper/atmos_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ package aws_component_helper

import (
"dario.cat/mergo"
"fmt"
"github.com/cloudposse/test-helpers/pkg/atmos"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/stretchr/testify/require"
"path/filepath"
"strings"
"testing"
)

type AtmosComponent struct {
Component string
Stack string
RandomIdentifier string
ComponentName string
StackName string
}

func NewAtmosComponent(component string, stack string) *AtmosComponent {
randID := random.UniqueId()
randomId := strings.ToLower(randID)
return &AtmosComponent{
Component: component,
Stack: stack,
RandomIdentifier: randomId,
ComponentName: component,
StackName: stack,
}
}

Expand All @@ -25,12 +33,27 @@ func (ac *AtmosComponent) getAtmosOptions(t *testing.T, options *atmos.Options,
result, _ = options.Clone()
}

currentTFDataDir := ".terraform"
if value, ok := options.EnvVars["TF_DATA_DIR"]; ok {
currentTFDataDir = value
}
stack := strings.Replace(ac.StackName, "/", "-", -1)
name := strings.Replace(ac.ComponentName, "/", "-", -1)
envvars := map[string]string{
// We need to split the TF_DATA_DIR for parallel suites mode
"TF_DATA_DIR": filepath.Join(currentTFDataDir, fmt.Sprintf("component-%s", ac.RandomIdentifier)),
"TEST_WORKSPACE_TEMPLATE": fmt.Sprintf("%s-%s-%s", stack, name, ac.RandomIdentifier),
}

err := mergo.Merge(&result.EnvVars, envvars)
require.NoError(t, err)

// Merge in any additional vars passed in
err := mergo.Merge(&result.Vars, vars)
err = mergo.Merge(&result.Vars, vars)
require.NoError(t, err)

result.Component = ac.Component
result.Stack = ac.Stack
result.Component = ac.ComponentName
result.Stack = ac.StackName

atmosOptions := atmos.WithDefaultRetryableErrors(t, result)
return atmosOptions
Expand Down
4 changes: 2 additions & 2 deletions pkg/atmos/aws-component-helper/atmos_component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func TestAtmosComponent(t *testing.T) {
// Create a temporary atmos component
component := NewAtmosComponent("vpc", "default-test")

assert.Equal(t, component.Component, "vpc")
assert.Equal(t, component.Stack, "default-test")
assert.Equal(t, component.ComponentName, "vpc")
assert.Equal(t, component.StackName, "default-test")
}
14 changes: 0 additions & 14 deletions pkg/atmos/aws-component-helper/dependency.go

This file was deleted.

57 changes: 4 additions & 53 deletions pkg/atmos/aws-component-helper/test_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,13 @@ import (

var (
skipVerifyEnabledFlag = flag.Bool("cth.skip-verify-enabled-flag", true, "skip verify enabled flag")

skipDeployTestDependencies = flag.Bool("cth.skip-deploy-test-deps", false, "skip deploy test deps")
skipDestroyTestDependencies = flag.Bool("cth.skip-destroy-test-deps-teardown", false, "skip destroy test deps")

skipDeployComponentUnderTest = flag.Bool("cth.skip-deploy-cut", false, "skip deploy component under test")
skipDestroyComponentUnderTest = flag.Bool("cth.skip-destroy-cut", false, "skip destroy component under test")

skipDeployAsserts = flag.Bool("cth.skip-deploy-asserts", false, "skip deploy component under test")
skipDestroyAsserts = flag.Bool("cth.skip-destroy-asserts", false, "skip destroy component under test")
)

type ComponentTest struct {
RandomIdentifier string
setup []*AtmosComponent
Subject *AtmosComponent
assert []*AtmosComponent
assert map[string]func(t *testing.T, ct *ComponentTest)
}

func NewComponentTest() *ComponentTest {
Expand All @@ -39,46 +30,7 @@ func NewComponentTest() *ComponentTest {
RandomIdentifier: randomId,
setup: make([]*AtmosComponent, 0),
Subject: nil,
assert: make([]*AtmosComponent, 0),
}
}

func (ct *ComponentTest) Run(t *testing.T, options *atmos.Options) {
testOptions := ct.getAtmosOptions(t, options, map[string]interface{}{})
for _, component := range ct.setup {
componentOptions := component.getAtmosOptions(t, testOptions, map[string]interface{}{})
if !*skipDeployTestDependencies && !*skipDeployDependencies {
atmosApply(t, componentOptions)
}
if !*skipDeployTestDependencies && !*skipDestroyTestDependencies && !*skipDeployDependencies && !*skipDestroyDependencies {
defer atmosDestroy(t, componentOptions)
}
}

if !*skipVerifyEnabledFlag && !*skipTests {
fmt.Println("VerifyEnabledFlag")
ct.verifyEnabledFlag(t, ct.Subject, options)
} else {
fmt.Println("Skipping VerifyEnabledFlag")
}

subjectOptions := ct.Subject.getAtmosOptions(t, testOptions, map[string]interface{}{})
if !*skipDeployComponentUnderTest && !*skipTests {
atmosApply(t, subjectOptions)
}
if !*skipDeployComponentUnderTest && !*skipDestroyComponentUnderTest && !*skipTests {
defer atmosDestroy(t, subjectOptions)
}

for _, component := range ct.assert {
componentOptions := component.getAtmosOptions(t, testOptions, map[string]interface{}{})

if !*skipDeployAsserts && !*skipTests {
atmosApply(t, componentOptions)
}
if !*skipDeployAsserts && !*skipDestroyAsserts && !*skipTests {
defer atmosDestroy(t, componentOptions)
}
assert: map[string]func(t *testing.T, ct *ComponentTest){},
}
}

Expand Down Expand Up @@ -144,7 +96,6 @@ func (ct *ComponentTest) SetSubject(component string, stack string) {
ct.Subject = NewAtmosComponent(component, stack)
}

func (ct *ComponentTest) AddSAssert(component string, stack string) {
item := NewAtmosComponent(component, stack)
ct.assert = append(ct.assert, item)
func (ct *ComponentTest) AddSAssert(name string, callback func(t *testing.T, ct *ComponentTest)) {
ct.assert[name] = callback
}
4 changes: 2 additions & 2 deletions pkg/atmos/aws-component-helper/test_component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ func TestComponentTestMinimum(t *testing.T) {
componentTest := NewComponentTest()
componentTest.SetSubject("vpc", "default-test")

assert.Equal(t, componentTest.Subject.Component, "vpc")
assert.Equal(t, componentTest.Subject.Stack, "default-test")
assert.Equal(t, componentTest.Subject.ComponentName, "vpc")
assert.Equal(t, componentTest.Subject.StackName, "default-test")
}
Loading

0 comments on commit 0392b9f

Please sign in to comment.