Skip to content

Commit 4526808

Browse files
author
Andreas Heumaier
committed
Sync README from azure-dev-docs-pr on best-practice-compliance-testing
1 parent 708a45e commit 4526808

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

samples/compliance-testing/README.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# Terraform Compliance Testing
1+
# Tutorial: Terraform Compliance Testing
22

3-
“Compliance testing” also known as Conformance testing is a nonfunctional testing technique which is done to validate, whether the system developed meets the organization’s prescribed standards or not. Most software teams do an analysis to check that the standards are properly enforced and implemented. Often working simultaneously to improve the standards, which will, in turn, lead to better quality.
3+
Compliance testing, also known as Conformance testing, is a nonfunctional testing technique which is done to validate whether the system developed meets the organization’s prescribed standards or not. Most software teams do an analysis to check that the standards are properly enforced and implemented. Often working simultaneously to improve the standards, which will, in turn, lead to better quality.
4+
5+
## Prerequisites
6+
7+
- **Terraform:** [install and run](configure-vs-code-extension-for-terraform.md) your first Terraform command from your machine.
8+
- **Docker:** [install](https://docs.docker.com/get-docker/) the docker command on your machine.
9+
- **terraform-compliance:** [install](https://terraform-compliance.com/pages/installation/docker) the terraform-compliance tool on your machine.
10+
- **Fork testing samples:** to get started quickly, we recommend that you fork [this repository](https://github.com/Azure/terraform) into your own GitHub organization.
411

512
## When to use Compliance Testing
613

@@ -17,7 +24,7 @@ One of the problems you might have in your team is environments getting hosed wh
1724

1825
An obvious response could be to call out a policy to require tags on resources where applicable and add a `role` and `creator` tag to the resource that is deployed. [Terraform-compliance](https://terraform-compliance.com) is a tool that helps you with that. It mainly focuses on negative testing instead of having fully-fledged functional tests that are mostly used for proving a component of code is performing properly.
1926

20-
Fortunately, `terraform` is a marvellous abstraction layer for any API that creates/updates/destroys entities. `Terraform` also provides the capability to ensure everything is up-to-date between the local configuration and the remote API(s) responses. Since `terraform` is mostly used against Cloud APIs we still miss a way to ensure the code deployed against the infrastructure must follow specific policies - like HashiCorp currently provides with `Sentinel` for Enterprise Products. `Terraform-compliance` is providing a similar functionality only for terraform while it is free-to-use and it is Open Source.
27+
Fortunately, `terraform` is a marvellous abstraction layer for any API that creates/updates/destroys entities. `Terraform` also provides the capability to ensure everything is up-to-date between the local configuration and the remote API(s) responses. Since `terraform` is mostly used against Cloud APIs we still miss a way to ensure the code deployed against the infrastructure must follow specific policies - like HashiCorp currently provides with [Sentinel](https://docs.hashicorp.com/sentinel/intro/what/) for Enterprise Products. `Terraform-compliance` is providing a similar functionality only for terraform while it is free-to-use and it is Open Source.
2128

2229
A sample compliance policy for the issue mentioned could be like this: `if you are working with Azure, you should not create a resource, without having any tags`.`Terraform-compliance` provides a test framework to create these policies that will be executed against your terraform plan file in a context where both developers and security teams can understand easily while reading it, by applying [Behaviour Driven Development](https://en.wikipedia.org/wiki/Behavior-driven_development) principles.
2330

@@ -76,14 +83,14 @@ Scenario Outline: Ensure that specific tags are defined
7683
Examples:
7784
| tags | value |
7885
| Creator | .+ |
79-
| application | .+ |
80-
| role | .+ |
81-
| environment | ^(prod\|uat\|dev)$ |
86+
| Application | .+ |
87+
| Role | .+ |
88+
| Environment | ^(prod\|uat\|dev)$ |
8289
```
8390

8491
## How-to run this example
8592

86-
The example above is taken from the [github.com/terrraform-testing](https://github.com/LeagueOfExtraordinaryHackers/terraform-testing/tree/compliance-testing/examples/compliance-testing) repository.
93+
The example above is taken from the [github.com/Azure/terrraform](https://github.com/Azure/terrraform/tree/compliance-testing/examples/master) repository.
8794

8895
After checkout the repo ...
8996

@@ -109,9 +116,9 @@ docker run --rm -v $PWD:/target -it eerkunt/terraform-compliance -f features -p
109116

110117
### From Red to Green
111118

112-
This should result in a failing test run:
119+
This should result in a failing test run. We see our first rule of requiring existence of tags suceed but we don't comply with the full spec of tags: `Role` and `Creator` tags are missing:
113120

114-
![tf-compliance-run-tagging-fail](assets/tf-compliance-run-tagging-fail.png)
121+
![tf-compliance-run-tagging-fail](media/best-practice-compliance-testing/tf-compliance-run-tagging-fail.png)
115122

116123
Make the test green again by adding all required tags to `main.tf`:
117124

@@ -120,7 +127,7 @@ Make the test green again by adding all required tags to `main.tf`:
120127
Environment = "dev"
121128
Application = "Azure Compliance"
122129
Creator = "Azure Compliance"
123-
Version = "Azure Compliance"
130+
Role = "Azure Compliance"
124131
}
125132
126133
```
@@ -132,6 +139,11 @@ terraform validate
132139
terraform plan -out tf.out
133140
```
134141

135-
Now, we should be green when running the tests suite again:
142+
Now, we should be green when running the tests suite again. We see our first rule of requiring existence of tags suceed and now we also provide the full spec of tags too:
143+
144+
![tf-compliance-run-tagging-succeed](media/best-practice-compliance-testing/tf-compliance-run-tagging-succeed.png)
145+
146+
## Next steps
136147

137-
![tf-compliance-run-tagging-succeed](assets/tf-compliance-run-tagging-succeed.png)
148+
> [!div class="nextstepaction"]
149+
> [Create and run end-to-end tests in Terraform projects](best-practices-end-to-end-testing.md)

0 commit comments

Comments
 (0)