Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Output in Atmos Tests #923

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
744855f
added verbose option tests
Cerebrovinny Jan 11, 2025
f0b3c24
output fixes wip
Cerebrovinny Jan 11, 2025
908e3ae
test workflow improvements
Cerebrovinny Jan 11, 2025
260c55c
test workflow improvements
Cerebrovinny Jan 11, 2025
c6c3f94
test verbosity
Cerebrovinny Jan 13, 2025
2690f6a
test verbosity
Cerebrovinny Jan 13, 2025
7d14d35
Add test verbosity control and validate logs-level flag
Cerebrovinny Jan 14, 2025
d063f45
Remove test command from acceptance tests workflow
Cerebrovinny Jan 14, 2025
7a7ad5a
Replace go-test-action with make testacc for acceptance tests
Cerebrovinny Jan 14, 2025
b755565
Add verbose test option and set quiet mode for CI tests
Cerebrovinny Jan 14, 2025
2a4b207
Add go-test-action and simplify test workflow
Cerebrovinny Jan 14, 2025
b7a2f09
refactor: update acceptance test workflow to use test-command parameter
Cerebrovinny Jan 14, 2025
7af82ce
test verbosity
Cerebrovinny Jan 14, 2025
f3348c6
add test action for testing
Cerebrovinny Jan 14, 2025
1593cd6
add test action for testing
Cerebrovinny Jan 14, 2025
bd8839a
add back testacc
Cerebrovinny Jan 14, 2025
fd4602e
revert log changes in cli test
Cerebrovinny Jan 14, 2025
22f5523
Rename TEST_VERBOSITY to ATMOS_TEST_VERBOSITY for consistency
Cerebrovinny Jan 16, 2025
9419720
Move test verbosity documentation from README.md to README.yaml and u…
Cerebrovinny Jan 16, 2025
c687249
Merge branch 'main' into DEV-2909
aknysh Jan 17, 2025
6750c43
fix: suppress config error when help is requested
Cerebrovinny Jan 17, 2025
6be3195
resolve conflicts
Cerebrovinny Jan 20, 2025
4a3dd88
Merge branch 'main' into DEV-2909
aknysh Jan 21, 2025
2e65cee
Merge branch 'main' into DEV-2909
Cerebrovinny Jan 24, 2025
debc554
refactor: improve test output handling and buffering
Cerebrovinny Jan 24, 2025
243160f
Merge branch 'main' into DEV-2909
Cerebrovinny Jan 26, 2025
7f19125
refactor: simplify test output handling and add config-aware test runner
Cerebrovinny Jan 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
aknysh marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ jobs:
timeout-minutes: 10
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: make testacc
run: make testacc ATMOS_TEST_VERBOSITY=quiet

docker:
name: "Docker Lint"
Expand Down Expand Up @@ -503,4 +503,4 @@ jobs:
with:
publish: false
format: binary
secrets: inherit
secrets: inherit
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,17 @@ version-windows: build-windows
deps:
go mod download

# Run acceptance tests
testacc: get
go test $(TEST) -v $(TESTARGS) -timeout 2m
# Test verbosity levels: quiet, normal, verbose
ATMOS_TEST_VERBOSITY ?= normal

.PHONY: testacc
testacc: ## Run all tests
@if [ "$(ATMOS_TEST_VERBOSITY)" = "quiet" ]; then \
go test ./... -timeout 20m; \
elif [ "$(ATMOS_TEST_VERBOSITY)" = "verbose" ]; then \
go test -v -count=1 ./... -timeout 20m; \
else \
go test -v ./... -timeout 20m; \
fi

.PHONY: lint get build version build-linux build-windows build-macos deps version-linux version-windows version-macos testacc
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,4 @@ Copyright © 2017-2024 [Cloud Posse, LLC](https://cpco.io/copyright)

<a href="https://cloudposse.com/readme/footer/link?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=readme_footer_link"><img alt="README footer" src="https://cloudposse.com/readme/footer/img"/></a>

<img alt="Beacon" width="0" src="https://ga-beacon.cloudposse.com/UA-76589703-4/cloudposse/atmos?pixel&cs=github&cm=readme&an=atmos"/>
<img alt="Beacon" width="0" src="https://ga-beacon.cloudposse.com/UA-76589703-4/cloudposse/atmos?pixel&cs=github&cm=readme&an=atmos"/>
24 changes: 24 additions & 0 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,27 @@ introduction: |-
Find all documentation at: [atmos.tools](https://atmos.tools)

contributors: []

testing:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is testing a supported section in the README.md.tmpl?

content: |-
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a misunderstanding how the README.yaml works. We run this though gomplate and apply this template: https://github.com/cloudposse/.github/blob/main/README.md.gotmpl

There's no section we look for called content or testing.

Also, after updating the README.yaml, run make readme to rebuild it.

Note, we're going to do this with atmos soon, see:

And the confusion here, shows we should have a JSON schema.

## Testing

### Test Verbosity Control

The test suite supports three verbosity levels to control output:

- `quiet`: Minimal output, only shows failures
- `normal`: Standard test output (default)
- `verbose`: Detailed test output with additional debugging information

You can control the verbosity level using the `ATMOS_TEST_VERBOSITY` environment variable:

```bash
# Run tests with minimal output
make test ATMOS_TEST_VERBOSITY=quiet

# Run acceptance tests with verbose output
make testacc ATMOS_TEST_VERBOSITY=verbose
```

This is particularly useful when running tests in CI environments or when debugging specific test failures.
16 changes: 15 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cloudposse/atmos/internal/tui/templates"
tuiUtils "github.com/cloudposse/atmos/internal/tui/utils"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)
Expand Down Expand Up @@ -44,6 +45,14 @@ var RootCmd = &cobra.Command{
cmd.SilenceErrors = true
}

// Validate logs-level flag if provided
if cmd.Flags().Changed("logs-level") {
logsLevel, _ := cmd.Flags().GetString("logs-level")
if _, err := logger.ParseLogLevel(logsLevel); err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
}
}

logsLevel, _ := cmd.Flags().GetString("logs-level")
logsFile, _ := cmd.Flags().GetString("logs-file")

Expand All @@ -62,7 +71,12 @@ var RootCmd = &cobra.Command{
// Only validate the config, don't store it yet since commands may need to add more info
_, err := cfg.InitCliConfig(configAndStacksInfo, false)
if err != nil {
if !errors.Is(err, cfg.NotFound) {
if errors.Is(err, cfg.NotFound) {
// For help commands or when help flag is set, we don't want to show the error
if !isHelpRequested {
u.LogWarning(errorConfig, err.Error())
}
} else {
u.LogErrorAndExit(errorConfig, err)
}
}
Expand Down
75 changes: 51 additions & 24 deletions pkg/describe/describe_stacks_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package describe

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -10,6 +11,11 @@ import (
u "github.com/cloudposse/atmos/pkg/utils"
)

// shouldLogVerbose returns true if test verbosity is set to verbose
func shouldLogVerbose() bool {
return os.Getenv("ATMOS_TEST_VERBOSITY") == "verbose"
}

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

Expand All @@ -18,10 +24,13 @@ func TestDescribeStacks(t *testing.T) {

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

dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
if shouldLogVerbose() {
dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
}
}

func TestDescribeStacksWithFilter1(t *testing.T) {
Expand All @@ -34,10 +43,13 @@ func TestDescribeStacksWithFilter1(t *testing.T) {

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

dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
if shouldLogVerbose() {
dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
}
}

func TestDescribeStacksWithFilter2(t *testing.T) {
Expand All @@ -51,10 +63,13 @@ func TestDescribeStacksWithFilter2(t *testing.T) {

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

dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
if shouldLogVerbose() {
dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
}
}

func TestDescribeStacksWithFilter3(t *testing.T) {
Expand All @@ -68,10 +83,13 @@ func TestDescribeStacksWithFilter3(t *testing.T) {

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

dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
if shouldLogVerbose() {
dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
}
}

func TestDescribeStacksWithFilter4(t *testing.T) {
Expand All @@ -85,10 +103,13 @@ func TestDescribeStacksWithFilter4(t *testing.T) {

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

dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
if shouldLogVerbose() {
dependentsYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(dependentsYaml)
}
}

func TestDescribeStacksWithFilter5(t *testing.T) {
Expand Down Expand Up @@ -117,9 +138,11 @@ func TestDescribeStacksWithFilter5(t *testing.T) {
assert.Equal(t, "ue2", tenant1Ue2DevStackComponentsTerraformComponentVarsEnvironment)
assert.Equal(t, "dev", tenant1Ue2DevStackComponentsTerraformComponentVarsStage)

stacksYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(stacksYaml)
if shouldLogVerbose() {
stacksYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(stacksYaml)
}
}

func TestDescribeStacksWithFilter6(t *testing.T) {
Expand All @@ -144,9 +167,11 @@ func TestDescribeStacksWithFilter6(t *testing.T) {
tenant1Ue2DevStackComponentsTerraformWorkspace := tenant1Ue2DevStackComponentsTerraformComponent["workspace"].(string)
assert.Equal(t, "tenant1-ue2-dev", tenant1Ue2DevStackComponentsTerraformWorkspace)

stacksYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(stacksYaml)
if shouldLogVerbose() {
stacksYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(stacksYaml)
}
}

func TestDescribeStacksWithFilter7(t *testing.T) {
Expand All @@ -171,9 +196,11 @@ func TestDescribeStacksWithFilter7(t *testing.T) {
tenant1Ue2DevStackComponentsTerraformWorkspace := tenant1Ue2DevStackComponentsTerraformComponent["workspace"].(string)
assert.Equal(t, "test-component-override-3-workspace", tenant1Ue2DevStackComponentsTerraformWorkspace)

stacksYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(stacksYaml)
if shouldLogVerbose() {
stacksYaml, err := u.ConvertToYAML(stacks)
assert.Nil(t, err)
t.Log(stacksYaml)
}
}

func TestDescribeStacksWithEmptyStacks(t *testing.T) {
Expand Down
103 changes: 103 additions & 0 deletions pkg/utils/test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package utils

import (
"fmt"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

// TestVerbosity controls the verbosity level of test output
type TestVerbosity int

const (
// TestVerbosityQuiet minimal output, only failures
TestVerbosityQuiet TestVerbosity = iota
// TestVerbosityNormal standard test output
TestVerbosityNormal
// TestVerbosityVerbose detailed test output
TestVerbosityVerbose
)

var (
// DefaultTestVerbosity is the default verbosity level for tests
DefaultTestVerbosity = TestVerbosityNormal
// Store descriptions to avoid repetition
printedDescriptions = make(map[string]bool)
)

// GetTestVerbosity returns the current test verbosity level
func GetTestVerbosity() TestVerbosity {
verbStr := os.Getenv("ATMOS_TEST_VERBOSITY")
switch verbStr {
case "quiet":
return TestVerbosityQuiet
case "verbose":
return TestVerbosityVerbose
default:
return DefaultTestVerbosity
}
}

// TestLogf logs a message during test execution based on verbosity level
func TestLogf(t *testing.T, minVerbosity TestVerbosity, format string, args ...interface{}) {
if GetTestVerbosity() >= minVerbosity {
t.Logf(format, args...)
}
}

// TestLog logs a message during test execution based on verbosity level
func TestLog(t *testing.T, minVerbosity TestVerbosity, args ...interface{}) {
if GetTestVerbosity() >= minVerbosity {
t.Log(args...)
}
}

// LogTestDescription logs a test description only once to avoid repetition
func LogTestDescription(t *testing.T, description string) {
if description == "" {
return
}

// Create a unique key for the description to avoid repetition within the same test
key := fmt.Sprintf("%s-%s", t.Name(), description)
if !printedDescriptions[key] {
if GetTestVerbosity() == TestVerbosityQuiet {
t.Logf("Test Description: %s", description)
} else {
t.Logf("\nTest Description: %s", description)
}
printedDescriptions[key] = true
}
}

// LogTestFailure logs detailed failure information based on verbosity
func LogTestFailure(t *testing.T, description string, expected, actual interface{}, extraInfo ...string) {
LogTestDescription(t, description)

verbosity := GetTestVerbosity()
if verbosity == TestVerbosityQuiet {
t.Errorf("Expected: %v\nGot: %v", expected, actual)
} else {
t.Errorf("\nExpected: %v\nGot: %v", expected, actual)
if len(extraInfo) > 0 && verbosity >= TestVerbosityNormal {
t.Logf("Additional Info:\n%s", strings.Join(extraInfo, "\n"))
}
}
}

// AssertTestResult is a wrapper for testify/assert that respects verbosity
func AssertTestResult(t *testing.T, assertion func() bool, description string, msgAndArgs ...interface{}) bool {
result := assertion()
if !result {
LogTestDescription(t, description)
return assert.True(t, result, msgAndArgs...)
}
if GetTestVerbosity() >= TestVerbosityVerbose {
LogTestDescription(t, description)
return assert.True(t, result, msgAndArgs...)
}
return result
}
Loading