Skip to content

Commit

Permalink
added tests and test fixes for empty stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Cerebrovinny committed Nov 5, 2024
1 parent 80bfa6e commit f76f914
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pkg/describe/describe_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func ExecuteDescribeStacks(
componentTypes []string,
sections []string,
ignoreMissingFiles bool,
includeEmptyStacks bool,
) (map[string]any, error) {

return e.ExecuteDescribeStacks(cliConfig, filterByStack, components, componentTypes, sections, ignoreMissingFiles, true)
return e.ExecuteDescribeStacks(cliConfig, filterByStack, components, componentTypes, sections, ignoreMissingFiles, false, includeEmptyStacks)
}
89 changes: 81 additions & 8 deletions pkg/describe/describe_stacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestDescribeStacks(t *testing.T) {
cliConfig, err := cfg.InitCliConfig(configAndStacksInfo, true)
assert.Nil(t, err)

stacks, err := ExecuteDescribeStacks(cliConfig, "", nil, nil, nil, false)
stacks, err := ExecuteDescribeStacks(cliConfig, "", nil, nil, nil, false, false)
assert.Nil(t, err)

dependentsYaml, err := u.ConvertToYAML(stacks)
Expand All @@ -32,7 +32,7 @@ func TestDescribeStacksWithFilter1(t *testing.T) {

stack := "tenant1-ue2-dev"

stacks, err := ExecuteDescribeStacks(cliConfig, stack, nil, nil, nil, false)
stacks, err := ExecuteDescribeStacks(cliConfig, stack, nil, nil, nil, false, false)
assert.Nil(t, err)

dependentsYaml, err := u.ConvertToYAML(stacks)
Expand All @@ -49,7 +49,7 @@ func TestDescribeStacksWithFilter2(t *testing.T) {
stack := "tenant1-ue2-dev"
components := []string{"infra/vpc"}

stacks, err := ExecuteDescribeStacks(cliConfig, stack, components, nil, nil, false)
stacks, err := ExecuteDescribeStacks(cliConfig, stack, components, nil, nil, false, false)
assert.Nil(t, err)

dependentsYaml, err := u.ConvertToYAML(stacks)
Expand All @@ -66,7 +66,7 @@ func TestDescribeStacksWithFilter3(t *testing.T) {
stack := "tenant1-ue2-dev"
sections := []string{"vars"}

stacks, err := ExecuteDescribeStacks(cliConfig, stack, nil, nil, sections, false)
stacks, err := ExecuteDescribeStacks(cliConfig, stack, nil, nil, sections, false, false)
assert.Nil(t, err)

dependentsYaml, err := u.ConvertToYAML(stacks)
Expand All @@ -83,7 +83,7 @@ func TestDescribeStacksWithFilter4(t *testing.T) {
componentTypes := []string{"terraform"}
sections := []string{"none"}

stacks, err := ExecuteDescribeStacks(cliConfig, "", nil, componentTypes, sections, false)
stacks, err := ExecuteDescribeStacks(cliConfig, "", nil, componentTypes, sections, false, false)
assert.Nil(t, err)

dependentsYaml, err := u.ConvertToYAML(stacks)
Expand All @@ -101,7 +101,7 @@ func TestDescribeStacksWithFilter5(t *testing.T) {
components := []string{"top-level-component1"}
sections := []string{"vars"}

stacks, err := ExecuteDescribeStacks(cliConfig, "", components, componentTypes, sections, false)
stacks, err := ExecuteDescribeStacks(cliConfig, "", components, componentTypes, sections, false, false)
assert.Nil(t, err)
assert.Equal(t, 8, len(stacks))

Expand Down Expand Up @@ -133,7 +133,7 @@ func TestDescribeStacksWithFilter6(t *testing.T) {
components := []string{"top-level-component1"}
sections := []string{"workspace"}

stacks, err := ExecuteDescribeStacks(cliConfig, "tenant1-ue2-dev", components, componentTypes, sections, false)
stacks, err := ExecuteDescribeStacks(cliConfig, "tenant1-ue2-dev", components, componentTypes, sections, false, false)
assert.Nil(t, err)
assert.Equal(t, 1, len(stacks))

Expand All @@ -160,7 +160,7 @@ func TestDescribeStacksWithFilter7(t *testing.T) {
components := []string{"test/test-component-override-3"}
sections := []string{"workspace"}

stacks, err := ExecuteDescribeStacks(cliConfig, stack, components, componentTypes, sections, false)
stacks, err := ExecuteDescribeStacks(cliConfig, stack, components, componentTypes, sections, false, false)
assert.Nil(t, err)
assert.Equal(t, 1, len(stacks))

Expand All @@ -175,3 +175,76 @@ func TestDescribeStacksWithFilter7(t *testing.T) {
assert.Nil(t, err)
t.Log(stacksYaml)
}

func TestDescribeStacksWithEmptyStacks(t *testing.T) {
configAndStacksInfo := schema.ConfigAndStacksInfo{}

cliConfig, err := cfg.InitCliConfig(configAndStacksInfo, true)
assert.Nil(t, err)

stacks, err := ExecuteDescribeStacks(cliConfig, "", nil, nil, nil, false, false)
assert.Nil(t, err)

initialStackCount := len(stacks)

stacksWithEmpty, err := ExecuteDescribeStacks(cliConfig, "", nil, nil, nil, false, true)
assert.Nil(t, err)

assert.Greater(t, len(stacksWithEmpty), initialStackCount, "Should include more stacks when empty stacks are included")

foundEmptyStack := false
for _, stackContent := range stacksWithEmpty {
if components, ok := stackContent.(map[string]any)["components"].(map[string]any); ok {
if len(components) == 0 || (len(components) == 1 && len(components["terraform"].(map[string]any)) == 0) {
foundEmptyStack = true
break
}
}
}
assert.True(t, foundEmptyStack, "Should find at least one empty stack")
}

func TestDescribeStacksWithVariousEmptyStacks(t *testing.T) {
configAndStacksInfo := schema.ConfigAndStacksInfo{}

cliConfig, err := cfg.InitCliConfig(configAndStacksInfo, true)
assert.Nil(t, err)

stacksWithoutEmpty, err := ExecuteDescribeStacks(cliConfig, "", nil, nil, nil, false, false)
assert.Nil(t, err)
initialCount := len(stacksWithoutEmpty)

stacksWithEmpty, err := ExecuteDescribeStacks(cliConfig, "", nil, nil, nil, false, true)
assert.Nil(t, err)

assert.Greater(t, len(stacksWithEmpty), initialCount, "Should have more stacks when including empty ones")

var (
emptyStacks []string
nonEmptyStacks []string
)

for stackName, stackContent := range stacksWithEmpty {
if stack, ok := stackContent.(map[string]any); ok {
if components, hasComponents := stack["components"].(map[string]any); hasComponents {
// Check for completely empty components
if len(components) == 0 || (len(components) == 1 && len(components["terraform"].(map[string]any)) == 0) {
emptyStacks = append(emptyStacks, stackName)
continue
}

// If we have any components at all, consider it non-empty
for _, compType := range components {
if compMap, ok := compType.(map[string]any); ok && len(compMap) > 0 {
nonEmptyStacks = append(nonEmptyStacks, stackName)
break
}
}
}
}
}

// Verify we found both types of stacks
assert.NotEmpty(t, emptyStacks, "Should find at least one empty stack")
assert.NotEmpty(t, nonEmptyStacks, "Should find at least one non-empty stack")
}

0 comments on commit f76f914

Please sign in to comment.