-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat(tests): Added tests for the tfe block #96
Merged
Merged
Changes from 15 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
1976031
Fixed ref to workspace_env_vars variable
SanderBlom bbc1349
Added the possibility to set env_vars as sensitive
SanderBlom 9c1baa3
Added setp for individual module testing
SanderBlom 83a1a6c
Added test for the tfe block
SanderBlom 8be8e7d
Automatic linting and formatting [skip ci]
invalid-email-address 21b9c66
Renamed all tests to make the them more distinguable
SanderBlom 2b47607
Added outputs for the variables to be able to test the workspace vars…
SanderBlom bc86a6a
Added new test type for the tfe block
SanderBlom 0b34b9d
Automatic linting and formatting [skip ci]
invalid-email-address dd15045
Added current subscription as default value for scope
SanderBlom 72ada6f
Made scope variable optional to work with new default value
SanderBlom 589f772
Replaced old tfe test with new tests written in offical testing frame…
SanderBlom 4591c17
Changed variable names and added readme for new test types
SanderBlom 4967d3e
Merge branch 'trunk' into 90-add-tests-tfe
SanderBlom 4c61449
Formatting
SanderBlom 2f023b2
Updated default scope for role definition from subscription to resour…
SanderBlom bd73e0e
Uncommented assertions for the applications test
SanderBlom 582d4b4
Added missing environment variable
SanderBlom 60f5c2f
Merge branch 'trunk' into 90-add-tests-tfe
SanderBlom 823018a
Merge branch 'trunk' into 90-add-tests-tfe
SanderBlom 579160d
Automatic linting and formatting [skip ci]
invalid-email-address 6050eff
ci(terraform): Added new action yaml for new terraform test framework
SanderBlom 4fea80e
ci(terraform_test): Changed validation for terraform tests and remove…
SanderBlom 8732785
ci(terraform_test): Changed name of steps and fixed bug with init in …
SanderBlom 77f9a16
ci(terraform_test): Removed separate validate for test folder as this…
SanderBlom 3aa80f7
ci(tests): Added different icon to new tests to visualise difference …
SanderBlom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# Terraform Station Tests | ||
|
||
## Overview | ||
|
||
This folder contains all tests for the Terraform Station module. The aim is to simplify the development and testing process for contributors. | ||
|
||
## How It Works | ||
|
||
1. **Terraform Cloud (TFC) Project Creation**: The tests are dependent on an existing TFC project so this should be created using the `setup-fte-project` module. This project's name should match the value set in the `TF_VARtfc_tfc_project_name` environment variable. It's recommended to use a name like `station-tests`. | ||
|
||
2. **Workspace and Resource Creation**: For each test (files ending with `tftest.hcl`), a new workspace is created, and the defined Azure and/or Entra ID resources are provisioned. | ||
|
||
## Prerequisites | ||
|
||
- Azure CLI installed and configured | ||
- Terraform CLI installed | ||
- Access to a Terraform Cloud account and an Azure subscription | ||
|
||
## How to Run Tests | ||
|
||
1. **Login to Azure and Terraform**: | ||
```bash | ||
az login --tenant YourTenantIdHere | ||
terraform login | ||
``` | ||
|
||
2. **Initialize Terraform**: | ||
```bash | ||
terraform init | ||
``` | ||
|
||
3. **Set Environment Variables**: | ||
Replace empty strings with your TFC organization name. | ||
```bash | ||
export TFE_ORGANIZATION="" | ||
export TF_VAR_tfc_organization_name="" | ||
``` | ||
|
||
4. **Starting the tests**: | ||
```bash | ||
terraform test #This will run all the tests | ||
terraform test -filter=tests/tfe.tftest.hcl #This will only run the tests for the tfe block | ||
``` | ||
|
||
## Testing Approach for New Features in the Station Module | ||
|
||
When adding new features to the Station module, it's crucial to validate their functionality. Contributors should create tests that encompass at least two fundamental configurations: | ||
|
||
1. **Minimal Configuration**: Focus on the core functionality of the feature by using only the essential parameters. This configuration aims to verify that the feature works with the bare minimum setup. | ||
|
||
2. **Maximum Configuration**: Expand the test to include all possible parameters. This approach is designed to showcase the feature's full capabilities and ensures compatibility with a wide range of options and settings. | ||
|
||
### Additional Configurations | ||
|
||
- **Scenario-Specific Tests**: If the feature includes multiple optional values that cannot all be used simultaneously or has different modes of operation, create additional test configurations to cover these scenarios. | ||
- **Combination Tests**: For features with parameters that interact in complex ways, design tests that combine these parameters in various forms. This helps in understanding the interactions and dependencies between different options. | ||
|
||
### Example Test Case Structure | ||
|
||
Add your required providers: | ||
```hcl | ||
provider "tfe" {} | ||
provider "azurerm" { | ||
features {} | ||
} | ||
provider "azuread" { | ||
|
||
} | ||
``` | ||
Call the module like how you would use it but inside a variables block | ||
|
||
```hcl | ||
variables { | ||
tfe = { | ||
project_name = "name_matching_the_project_in_the_setup_block" | ||
organization_name = "Your_tfc_org_name" | ||
workspace_name = "tfe_test" | ||
workspace_description = "Workspace description" | ||
} | ||
|
||
# Call you new_feature module | ||
new_feature = { | ||
# Minimal Configuration Example | ||
minimal = { | ||
required_param = "basic_value" | ||
}, | ||
|
||
# Maximum Configuration Example | ||
maximum = { | ||
required_param = "value" | ||
optional_param1 = "advanced_value1" | ||
optional_param2 = "advanced_value2" | ||
... | ||
}, | ||
|
||
# Additional Scenario-Specific Configurations | ||
scenario_specific = { | ||
... | ||
} | ||
} | ||
} | ||
``` | ||
Call the setup module that will create a test project for us in TFC. Ensure the project name is unique to the test your creating | ||
|
||
```hcl | ||
run "setup_create_tfc_test_project" { | ||
variables { | ||
project_name = "tests_new_feature" | ||
organization_name = "Your_tfc_org_name" | ||
} | ||
module { | ||
source = "./tests/setup-tfe-project" | ||
} | ||
} | ||
`````` | ||
Create multiple `assert` or `expect_failures` blocks to validate that the required resources where created with the values we provided in the variable block. | ||
|
||
```hcl | ||
run "test_new_feature" { | ||
|
||
module { | ||
source = "./" | ||
} | ||
|
||
assert { | ||
condition = module.new_feature.name == "Name you set in the variables block" | ||
error_message = "Some error message explaining why it failed" | ||
} | ||
|
||
assert { | ||
condition = module.new_feature.someValue == true | ||
error_message = "Some error message explaining why it failed" | ||
} | ||
|
||
} | ||
|
||
``` | ||
Use this structure as a template when adding tests for new functionalities to ensure both basic and advanced use cases are covered. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
data "tfe_organization" "test" { | ||
name = var.tfc_organization_name | ||
} | ||
|
||
resource "tfe_project" "test" { | ||
name = var.tfc_project_name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
variable "tfc_organization_name" { | ||
type = string | ||
description = "The name of the Terraform Cloud organization to use" | ||
} | ||
|
||
variable "tfc_project_name" { | ||
type = string | ||
description = "The name of the Terraform Cloud project to create" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to discuss if we can set the default scope to the workload's resource group id. Are there any downsides to doing that @SanderBlom?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably be a better to reduce the scope to the workload resource group. I'll update the PR :)