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

feat(tests): Added tests for the tfe block #96

Merged
merged 26 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
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 Nov 14, 2023
bbc1349
Added the possibility to set env_vars as sensitive
SanderBlom Nov 14, 2023
9c1baa3
Added setp for individual module testing
SanderBlom Nov 14, 2023
83a1a6c
Added test for the tfe block
SanderBlom Nov 14, 2023
8be8e7d
Automatic linting and formatting [skip ci]
invalid-email-address Nov 14, 2023
21b9c66
Renamed all tests to make the them more distinguable
SanderBlom Nov 14, 2023
2b47607
Added outputs for the variables to be able to test the workspace vars…
SanderBlom Nov 15, 2023
bc86a6a
Added new test type for the tfe block
SanderBlom Nov 15, 2023
0b34b9d
Automatic linting and formatting [skip ci]
invalid-email-address Nov 15, 2023
dd15045
Added current subscription as default value for scope
SanderBlom Nov 16, 2023
72ada6f
Made scope variable optional to work with new default value
SanderBlom Nov 16, 2023
589f772
Replaced old tfe test with new tests written in offical testing frame…
SanderBlom Nov 16, 2023
4591c17
Changed variable names and added readme for new test types
SanderBlom Nov 16, 2023
4967d3e
Merge branch 'trunk' into 90-add-tests-tfe
SanderBlom Nov 16, 2023
4c61449
Formatting
SanderBlom Nov 16, 2023
2f023b2
Updated default scope for role definition from subscription to resour…
SanderBlom Jan 8, 2024
bd73e0e
Uncommented assertions for the applications test
SanderBlom Jan 8, 2024
582d4b4
Added missing environment variable
SanderBlom Jan 8, 2024
60f5c2f
Merge branch 'trunk' into 90-add-tests-tfe
SanderBlom Feb 6, 2024
823018a
Merge branch 'trunk' into 90-add-tests-tfe
SanderBlom Feb 6, 2024
579160d
Automatic linting and formatting [skip ci]
invalid-email-address Feb 6, 2024
6050eff
ci(terraform): Added new action yaml for new terraform test framework
SanderBlom Feb 6, 2024
4fea80e
ci(terraform_test): Changed validation for terraform tests and remove…
SanderBlom Feb 6, 2024
8732785
ci(terraform_test): Changed name of steps and fixed bug with init in …
SanderBlom Feb 6, 2024
77f9a16
ci(terraform_test): Removed separate validate for test folder as this…
SanderBlom Feb 6, 2024
3aa80f7
ci(tests): Added different icon to new tests to visualise difference …
SanderBlom Feb 6, 2024
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
3 changes: 3 additions & 0 deletions hashicorp/tfe/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ output "workspace" {
value = tfe_workspace.workload
}

output "workspace_variables" {
value = local.tfc_variables
}
17 changes: 17 additions & 0 deletions hashicorp/tfe/tfe_variable.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,20 @@ resource "tfe_variable" "workload" {
hcl = try(each.value.hcl, false)
sensitive = try(each.value.sensitive, false)
}

data "tfe_variables" "workload" {
workspace_id = tfe_workspace.workload.id
depends_on = [tfe_variable.workload]
}


locals {
#Restructure the output so it's possible to create terraform tests
tfc_variables = { for v in data.tfe_variables.workload.variables : v.name => {
category = v.category
hcl = v.hcl
id = v.id
sensitive = v.sensitive
value = v.value
} }
}
1 change: 1 addition & 0 deletions hashicorp/tfe/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ variable "workspace_env_vars" {
value = string
category = string
description = string
sensitive = optional(bool)
}))
default = null
}
Expand Down
2 changes: 1 addition & 1 deletion role_definitions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resource "azurerm_role_definition" "user_created" {
role_definition_id = each.value.role_definition_id
name = each.value.name
description = each.value.description
scope = each.value.scope
scope = each.value.scope == null ? "/subscriptions/${data.azurerm_client_config.current.subscription_id}" : each.value.scope
Copy link
Collaborator

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?

Copy link
Contributor

@SanderBlom SanderBlom Jan 8, 2024

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 :)

assignable_scopes = each.value.assignable_scopes
dynamic "permissions" {
for_each = each.value.permissions == null ? [] : [1]
Expand Down
2 changes: 2 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ This folder contains all tests for the Terraform Station module. The aim is to s
terraform destroy
```

6. **Tips**
You can also test each module separately by running `terraform plan -target module.module_you_want_to_test`

## Testing Approach for New Features in the Station Module

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
138 changes: 138 additions & 0 deletions tests/README.md
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.
7 changes: 7 additions & 0 deletions tests/setup-tfe-project/main.tf
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
}
9 changes: 9 additions & 0 deletions tests/setup-tfe-project/variables.tf
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"
}
Loading