Skip to content

Add component tests #16

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

Merged
merged 4 commits into from
Apr 2, 2025
Merged

Add component tests #16

merged 4 commits into from
Apr 2, 2025

Conversation

goruha
Copy link
Contributor

@goruha goruha commented Apr 1, 2025

What

  • Add basic component test
  • Add disabled component test
  • Test component drifting
  • Add any additional use case tests

Why

  • Test basic component features
  • Verify that the component does not create any resources when input enabled: false set
  • Verify that the component does not drift on a second run with the same inputs
  • Add test for any additional than basic use cases for the component

References

Summary by CodeRabbit

  • New Features

    • Repository and team settings now reflect a configurable enabled flag, ensuring outputs are conditionally populated.
    • Introduced new YAML configurations for ArgoCD and GitHub integration, specifying components and settings for deployment.
  • Tests

    • Expanded test coverage with new test suites and fixtures to validate component behavior and configuration.
  • Chores

    • Improved repository hygiene with updated ignore rules and removed redundant test scripts.

@goruha goruha requested review from a team as code owners April 1, 2025 17:56
@goruha goruha linked an issue Apr 1, 2025 that may be closed by this pull request
4 tasks
Copy link

coderabbitai bot commented Apr 1, 2025

Walkthrough

This pull request introduces conditional logic into Terraform configurations, affecting how values are assigned in both resource definitions and outputs based on the local.enabled flag. In addition to these logic changes, new test files, configuration fixtures, and a Go module have been added while a legacy test script has been removed. The new configurations cover Atmos settings, Terraform backends, and component definitions, with corresponding tests to verify functionality.

Changes

File(s) Summary of Changes
src/main.tf, src/outputs.tf Modified Terraform logic to use conditional expressions based on local.enabled. In main.tf, team_slugs is assigned either the computed value or an empty list. In outputs.tf, repository outputs now return either their respective values from local.github_repository or null.
test/.gitignore New .gitignore file added to exclude directories and files such as state/, .cache, test/test-suite.json, .atmos, and test_suite.yaml.
test/component_test.go Introduced a new test suite for the "argocd-github-repo" component, including tests for basic functionality and verification of the enabled flag.
test/fixtures/** Added several YAML configuration files (e.g., atmos.yaml, files under stacks/catalog/, stacks/orgs/default/test/, and vendor.yaml) to define settings for components, stacks, workflows, and Atmos vendoring.
test/go.mod New Go module file created for tests, specifying the Go version, required dependencies, and a replace directive for the aws-nuke module.
test/run.sh Removed the legacy Bash test script that previously printed a simple message.

Sequence Diagram(s)

sequenceDiagram
    participant TF as "Terraform Engine"
    participant Main as "main.tf"
    participant Outputs as "outputs.tf"

    TF->>Main: Evaluate local.enabled
    alt local.enabled == true
        Main->>Main: Process var.permissions via toset(compact([...]))
        Main->>Outputs: Output repository details from local.github_repository
    else local.enabled == false
        Main->>Main: Set team_slugs to empty list
        Main->>Outputs: Output null values in repository outputs
    end
Loading

Poem

I'm a hopping coder, a rabbit in flight,
With conditional logic, my code feels just right.
Team slugs and outputs now dance in the sun,
Each YAML and test proving our work is fun.
In fields of config, I nibble with delight 🐰!
Cheers to changes that keep our code light.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@goruha goruha changed the title Added tests Add component tests Apr 1, 2025
Copy link

mergify bot commented Apr 1, 2025

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Require terratest

Wonderful, this rule succeeded.

This rule require terratest status

  • check-success = test/terratest

@mergify mergify bot added the needs-test Needs testing label Apr 1, 2025
@goruha
Copy link
Contributor Author

goruha commented Apr 1, 2025

/terratest

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (6)
test/go.mod (1)

17-18: Consider uncommenting the replace directive if needed

The replace directive is commented out. If it's necessary for the tests to function correctly, consider uncommenting it.

-// replace github.com/rebuy-de/aws-nuke/v2/mocks => github.com/ekristen/aws-nuke/v3/ v3.29.0
+replace github.com/rebuy-de/aws-nuke/v2/mocks => github.com/ekristen/aws-nuke/v3/ v3.29.0
test/component_test.go (2)

6-8: Remove or use commented imports

There are commented-out imports that should be removed if they're not needed to keep the code clean.

import (
	"fmt"
	"os"
-	// "strconv"
	"testing"
	"strings"

	helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper"
	awsTerratest "github.com/gruntwork-io/terratest/modules/aws"
	"github.com/gruntwork-io/terratest/modules/random"
-	// "github.com/jferrl/go-githubauth"
	"github.com/stretchr/testify/assert"
)

Also applies to: 13-14


48-54: Remove unused constant

The awsRegion constant is defined but never used in this test method.

func (s *ComponentSuite) TestEnabledFlag() {
	const component = "argocd-github-repo/disabled"
	const stack = "default-test"
-	const awsRegion = "us-east-2"

	s.VerifyEnabledFlag(component, stack, nil)
}
🧰 Tools
🪛 golangci-lint (1.64.8)

51-51: const awsRegion is unused

(unused)

test/fixtures/atmos.yaml (3)

11-18: Clear Base Path Configuration.
The base_path setting is well-documented with fallback options via environment variables and command-line arguments. Verify that the default empty string ("") behaves as expected across dependent configurations.


34-50: Comprehensive Stacks Configuration.
The stacks block clearly defines the base path, included/excluded paths, and naming patterns. Consider reviewing whether additional validations or defaults are needed for the path arrays to keep stack selection robust.


56-61: Effective Log Settings.
Defining the log file as /dev/stdout and specifying the log level (Info) are practical defaults. If dynamic log level configuration is later needed, consider exposing an override via environment variables.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9100bfa and 406c652.

⛔ Files ignored due to path filters (1)
  • test/go.sum is excluded by !**/*.sum
📒 Files selected for processing (13)
  • src/main.tf (1 hunks)
  • src/outputs.tf (1 hunks)
  • test/.gitignore (1 hunks)
  • test/component_test.go (1 hunks)
  • test/fixtures/atmos.yaml (1 hunks)
  • test/fixtures/stacks/catalog/account-map.yaml (1 hunks)
  • test/fixtures/stacks/catalog/usecase/basic.yaml (1 hunks)
  • test/fixtures/stacks/catalog/usecase/disabled.yaml (1 hunks)
  • test/fixtures/stacks/orgs/default/test/_defaults.yaml (1 hunks)
  • test/fixtures/stacks/orgs/default/test/tests.yaml (1 hunks)
  • test/fixtures/vendor.yaml (1 hunks)
  • test/go.mod (1 hunks)
  • test/run.sh (0 hunks)
💤 Files with no reviewable changes (1)
  • test/run.sh
🧰 Additional context used
🪛 golangci-lint (1.64.8)
test/component_test.go

51-51: const awsRegion is unused

(unused)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Mergify Merge Protections
  • GitHub Check: Summary
🔇 Additional comments (22)
test/.gitignore (1)

1-6: .gitignore entries are well-defined.
The new .gitignore file correctly ignores common temporary and state files (such as the state/ directory, .cache, and test output files) that should not be tracked in version control. Please double-check that these patterns do not conflict with any global or repository-level .gitignore settings.

test/fixtures/stacks/orgs/default/test/tests.yaml (1)

1-5: Import configuration is clear and structured.
The YAML file cleanly imports the defaults and test use cases for both the basic and disabled scenarios. Verify that the imported paths (orgs/default/test/_defaults, catalog/usecase/basic, and catalog/usecase/disabled) exist and match the intended structure in your repository.

test/fixtures/stacks/catalog/account-map.yaml (1)

1-47: Account-map configuration is comprehensive.
This new YAML configuration clearly outlines the component definition for the Terraform account-map, including metadata, variables, and a remote state backend setup. The detailed inline comments provide valuable context regarding the purpose and limitations of the static backend. If future enhancements are needed, consider adding a small example or further documentation for end users.

test/fixtures/vendor.yaml (1)

1-19: Atmos Vendor Configuration is clear and appropriately versioned.
The manifest defines the Atmos vendor configuration with a clear source, version, and inclusion patterns for relevant files. Make sure that the component path and version (1.520.0) are up-to-date and continue to align with the expectations in your testing framework.

test/fixtures/stacks/catalog/usecase/disabled.yaml (1)

1-23: LGTM: Configuration for disabled test case is well-structured

The configuration properly sets enabled: false which is crucial for testing that no resources are created when the component is disabled, aligning perfectly with one of the PR objectives.

test/fixtures/stacks/catalog/usecase/basic.yaml (1)

1-23: LGTM: Basic test configuration looks good

The configuration correctly sets enabled: true and provides all necessary parameters for testing the basic functionality of the component, which aligns with the PR objective to test basic features.

test/fixtures/stacks/orgs/default/test/_defaults.yaml (1)

1-66: LGTM: Test environment setup looks comprehensive

The configuration provides all necessary components for a proper test environment, including backend configuration, variables, and account mappings. The use of environment variables for dynamic values is a good practice.

test/component_test.go (2)

21-46: LGTM: Basic test implementation looks good

The test properly sets up the environment, handles GitHub token securely, ensures proper cleanup using defer, and performs drift testing. This implementation satisfies the PR objective for basic component testing.


48-54: LGTM: Enabled flag test implementation is appropriate

This test verifies that no resources are created when the component is disabled (enabled: false), which satisfies one of the PR objectives.

🧰 Tools
🪛 golangci-lint (1.64.8)

51-51: const awsRegion is unused

(unused)

test/fixtures/atmos.yaml (5)

1-10: Great File Header & Documentation!
The initial comments concisely explain the configuration loading order and support for POSIX-style globs. The documentation here is clear and sets a solid context for users.


20-33: Well-Structured Components Block.
The Terraform components configuration is detailed and includes settings for auto-approval, initialization, and backend file generation. This block is thorough and follows best practices.


51-55: Concise Workflows Configuration.
The workflows block is short and consistent with the structure used in other sections. No issues found.


62-65: Straightforward Settings Block.
The merge strategy for lists is clearly set here. This simplicity aids maintainability.


66-78: Robust Templates Configuration.
The templates section is comprehensive, enabling both Sprig and Gomplate utilities. Links to documentation embedded in the comments enhance the readability and utility for users updating or auditing these options.

src/outputs.tf (8)

1-4: Deploy Keys SSM Paths Output.
The output for deploy_keys_ssm_paths correctly references module.store_write.names. The configuration appears sound and maintains consistency with expected outputs.


6-10: Deploy Keys SSM Path Format Output.
Using the ternary operator to conditionally output var.ssm_github_deploy_key_format when enabled (and null otherwise) is clear and appropriate for managing the state.


11-15: Repository Name Output with Dual Conditionals.
The output for repository combines local.enabled and var.create_repo effectively—falling back to var.name when necessary. This layered condition ensures that repository naming remains consistent based on the creation flag.


16-19: Improved Repository Description Output.
The change to conditionally return local.github_repository.description only when local.enabled is true is a smart move for preventing misconfiguration.


21-25: Consistent Repository Default Branch Output.
The updated logic for repository_default_branch mirrors the pattern seen elsewhere, ensuring consistency. It cleanly prevents exposing branch details when the repository is disabled.


26-30: Conditional Repository URL Output.
Applying the condition local.enabled ? local.github_repository.html_url : null ensures that the repository URL is only output when applicable, which helps avoid leaking stale or irrelevant data.


31-35: Refined Git Clone URL Output.
The output for repository_git_clone_url conditionally outputs the URL based on local.enabled, maintaining coherence with other conditional outputs.


36-40: Secure SSH Clone URL Output.
The conditional assignment for repository_ssh_clone_url helps ensure that sensitive SSH details are only provided when the repository is enabled, safeguarding against unintended exposure.

@goruha
Copy link
Contributor Author

goruha commented Apr 2, 2025

/terratest

@goruha
Copy link
Contributor Author

goruha commented Apr 2, 2025

/terratest

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
test/component_test.go (3)

6-7: Remove commented-out imports.

There are unused commented imports that should be removed to improve code cleanliness and maintainability.

import (
	"fmt"
	"os"
-	// "strconv"
	"testing"
	"strings"

	helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper"
	awsTerratest "github.com/gruntwork-io/terratest/modules/aws"
	"github.com/gruntwork-io/terratest/modules/random"
-	// "github.com/jferrl/go-githubauth"
	"github.com/stretchr/testify/assert"
)

Also applies to: 13-13


26-26: Add GitHub token validation.

The test retrieves the GitHub token from environment variables but doesn't validate if it exists. Without this check, the test might fail in unexpected ways if the token is missing.

token := os.Getenv("GITHUB_TOKEN")
+if token == "" {
+	s.T().Fatal("GITHUB_TOKEN environment variable must be set")
+}

50-54: Remove unused awsRegion constant.

The awsRegion constant is defined but never used in this method, as confirmed by static analysis.

func (s *ComponentSuite) TestEnabledFlag() {
	const component = "argocd-github-repo/disabled"
	const stack = "default-test"
-	const awsRegion = "us-east-2"

	s.VerifyEnabledFlag(component, stack, nil)
}
🧰 Tools
🪛 golangci-lint (1.64.8)

53-53: const awsRegion is unused

(unused)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 406c652 and d2429e0.

📒 Files selected for processing (1)
  • test/component_test.go (1 hunks)
🧰 Additional context used
🪛 golangci-lint (1.64.8)
test/component_test.go

53-53: const awsRegion is unused

(unused)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Mergify Merge Protections
  • GitHub Check: Mergify Merge Protections
  • GitHub Check: Summary
🔇 Additional comments (3)
test/component_test.go (3)

21-48: Well-implemented basic component test.

The TestBasic method properly tests the core functionality with appropriate setup and cleanup:

  1. Creates a randomized resource name for isolation
  2. Sets up and tears down AWS SSM parameters
  3. Deploys the component with the required inputs
  4. Performs a drift test to verify idempotency

This successfully implements the "basic component test" and "drift test" requirements from the PR objectives.


50-56: Properly implemented disabled component test.

The TestEnabledFlag method correctly uses the helper's VerifyEnabledFlag method to test that no resources are created when the component is disabled, satisfying the "disabled component test" requirement from the PR objectives.

🧰 Tools
🪛 golangci-lint (1.64.8)

53-53: const awsRegion is unused

(unused)


58-61: Correct test suite setup.

The TestRunSuite function properly initializes and runs the test suite using the helper library's Run function, following Go testing best practices.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (7)
test/component_test.go (7)

7-7: Remove the commented-out import.

The import for "strconv" is commented out but not used anywhere in the code. Clean up by removing this unused import.

-	// "strconv"

28-29: Add validation for required environment variables.

The code retrieves the GitHub token from environment variables but doesn't check if it exists. This could lead to cryptic errors later if the token is missing.

 	token := os.Getenv("GITHUB_TOKEN")
+	if token == "" {
+		s.T().Fatal("GITHUB_TOKEN environment variable is required but not set")
+	}

52-53: Use a custom HTTP client with timeout for GitHub API calls.

The default HTTP client used by the GitHub client doesn't have a timeout set, which could lead to hanging tests if the GitHub API is unresponsive.

-	client := github.NewClient(nil).WithAuthToken(token)
+	import "net/http"
+	import "time"
+
+	httpClient := &http.Client{
+		Timeout: time.Second * 30,
+	}
+	client := github.NewClient(httpClient).WithAuthToken(token)

69-69: Remove unused constant.

The awsRegion constant is declared but never used in the TestEnabledFlag method.

-	const awsRegion = "us-east-2"
🧰 Tools
🪛 golangci-lint (1.64.8)

69-69: const awsRegion is unused

(unused)


62-64: Move drift test before GitHub API calls for better test organization.

The drift test is performed after the GitHub API tests, which doesn't follow a logical test progression. Consider moving the drift test to be executed immediately after the deployment to maintain a clearer test workflow.

-	assert.Nil(s.T(), err)
-	s.DriftTest(component, stack, &inputs)
+	assert.Nil(s.T(), err)
+
+	// Verify that there's no drift after deployment
+	s.DriftTest(component, stack, &inputs)

22-64: Add test for the repository visibility setting.

The test creates a GitHub repository but doesn't verify if it's created with the correct visibility setting (public/private). Consider adding an assertion to verify this important property.

 	assert.NotNil(s.T(), repo)
+	
+	// Verify repository visibility (assuming the default is private)
+	assert.Equal(s.T(), "private", *repo.Visibility, "Repository should be private by default")

41-47: Consider parameterizing test inputs for better testability.

Current test inputs are hardcoded in the test function. Consider extracting these into a separate function or struct that can be reused or modified for different test scenarios.

This would make it easier to create variations of the test with different inputs, such as testing with different repository settings or organizations. Consider creating a helper function that returns the test inputs with optional overrides.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d2429e0 and 6f12940.

⛔ Files ignored due to path filters (1)
  • test/go.sum is excluded by !**/*.sum
📒 Files selected for processing (2)
  • test/component_test.go (1 hunks)
  • test/go.mod (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/go.mod
🧰 Additional context used
🪛 golangci-lint (1.64.8)
test/component_test.go

69-69: const awsRegion is unused

(unused)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Mergify Merge Protections
  • GitHub Check: Mergify Merge Protections
  • GitHub Check: Summary
🔇 Additional comments (1)
test/component_test.go (1)

49-50: Check for deployment errors.

The DeployAtmosComponent call ignores the error return value. This could mask deployment failures and make debugging difficult.

-options, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
+options, err := s.DeployAtmosComponent(s.T(), component, stack, &inputs)
+if err != nil {
+	s.T().Fatalf("Failed to deploy component: %v", err)
+}
assert.NotNil(s.T(), options)

@goruha
Copy link
Contributor Author

goruha commented Apr 2, 2025

/terratest

@goruha goruha merged commit 3d16f37 into main Apr 2, 2025
19 checks passed
@goruha goruha deleted the add-tests branch April 2, 2025 09:28
Copy link

github-actions bot commented Apr 2, 2025

These changes were released in v1.536.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-test Needs testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add component tests
1 participant