Skip to content

Commit 24455a9

Browse files
authored
Samples: end-to-end testing (microsoft#47)
Add a new sample for end-to-end testing in Terraform projects.
1 parent 74170d8 commit 24455a9

File tree

11 files changed

+400
-1
lines changed

11 files changed

+400
-1
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Ignore terraform provider and state files
22
*.terraform
3-
*.tfstate
3+
*.tfstate*
44

55
## Ignore Visual Studio temporary files, build results, and
66
## files generated by popular Visual Studio add-ons.
@@ -290,3 +290,7 @@ __pycache__/
290290
*.btm.cs
291291
*.odx.cs
292292
*.xsd.cs
293+
294+
# Golang
295+
go.sum
296+
.test-data/

samples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ This repository contains real-world examples that will walk you through differen
55
## Testing Best Practices
66

77
- [Integration Testing](integration-testing/README.md)
8+
- [Compliance Testing](compliance-testing/README.md)
9+
- [End-to-end Testing](end-to-end-testing/README.md)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Terraform end-to-end testing
2+
3+
This is an example of how to use [Terratest](https://github.com/gruntwork-io/terratest) to perform end-to-end testing on a Terraform project.
4+
5+
## What about end-to-end testing
6+
7+
End-to-end tests validate that a program actually works in real conditions, the closest as possible to production environment. Let's imagine that we are writing a Terraform module to deploy two virtual machines into a virtual network but we don't want those machines to be able to ping each other. End-to-end tests are exactly what we need to make sure that the deployment of this module create the expected resources, but also that the virtual machines cannot ping each other.
8+
9+
In order to achieve that, the end-to-end test will apply the Terraform configuration, run all required tests, and finally tear down the infrastructure. They are much longer than integration or unit tests so they are not executed part of the continuous integration process.
10+
11+
## What about Terratest
12+
13+
[Terratest](https://github.com/gruntwork-io/terratest) is an open-source framework, written in [Go](http://golang.org/dl) and relying on the Go test framework, that helps to write end-to-end tests for Terraform projects. It provides helper and tools to:
14+
15+
1. Deploy a Terraform configuration
16+
2. Goes back to the Go tests to validate what has been actually deployed
17+
3. Tear down the deployed infrastructure
18+
19+
## Scenario of this sample
20+
21+
In this sample, we are going to use a Terraform configuration that deploys two Linux virtual machines into the same virtual network. `vm-linux-1` has a public IP address. Only port 22 is opened to allow SSH connection. `vm-linux-2` has no public IP address. The scenario we want to validate with the end-to-end test is to make sure that:
22+
23+
- infrastructure is deployed correctly
24+
- it's possible to open an SSH session to `vm-linux-1` using port 22
25+
- it's possible to ping `vm-linux-2` from `vm-linux-1` SSH session
26+
27+
![End-to-end scenario](assets/scenario.png)
28+
29+
> NOTE: this is a simple scenario to illustrate how to write a basic end-to-end test. We don't recommend having production virtual machines that exposes SSH port over a public IP address.
30+
31+
## Terraform configuration
32+
33+
The Terraform configuration for this scenario can be found in the [src/main.tf](src/main.tf) file. It contains everything to deploy the Azure infrastructure represented on the figure above.
34+
35+
If you are not familiar with creating a Linux virtual machine using Terraform we recommend that you read [this page of the documentation](https://docs.microsoft.com/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure) before.
36+
37+
## End-to-end test
38+
39+
As mentioned in the introduction, the end-to-end test is written in Go language and uses the Terratest framework. It is defined in the [src/test/end2end_test.go](src/test/end2end_test.go) file.
40+
41+
This is the common structure of a Golang test using Terratest:
42+
43+
```Go
44+
package test
45+
46+
import (
47+
"testing"
48+
49+
"github.com/gruntwork-io/terratest/modules/terraform"
50+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
51+
)
52+
53+
func TestEndToEndDeploymentScenario(t *testing.T) {
54+
t.Parallel()
55+
56+
fixtureFolder := "../"
57+
58+
// User Terratest to deploy the infrastructure
59+
test_structure.RunTestStage(t, "setup", func() {
60+
terraformOptions := &terraform.Options{
61+
// Indicate the directory that contains the Terraform configuration to deploy
62+
TerraformDir: fixtureFolder,
63+
}
64+
65+
// Save options for later test stages
66+
test_structure.SaveTerraformOptions(t, fixtureFolder, terraformOptions)
67+
68+
// Triggers the terraform init and terraform apply command
69+
terraform.InitAndApply(t, terraformOptions)
70+
})
71+
72+
test_structure.RunTestStage(t, "validate", func() {
73+
// run validation checks here
74+
terraformOptions := test_structure.LoadTerraformOptions(t, fixtureFolder)
75+
publicIpAddress := terraform.Output(t, terraformOptions, "public_ip_address")
76+
})
77+
78+
// When the test is completed, teardown the infrastructure by calling terraform destroy
79+
test_structure.RunTestStage(t, "teardown", func() {
80+
terraformOptions := test_structure.LoadTerraformOptions(t, fixtureFolder)
81+
terraform.Destroy(t, terraformOptions)
82+
})
83+
}
84+
```
85+
86+
As you can see in the snippet above, the test is composed by three stages:
87+
88+
1. `setup`: this stage is responsible for running Terraform to deploy the configuration.
89+
2. `validate`: this stage is responsible for doing the validation checks / assertions.
90+
3. `teardown`: this stage is responsible for cleaning up the infrastructure.
91+
92+
Some relevant functions provided by Terratest framework are:
93+
94+
- `terraform.InitAndApply` allows to run the `terraform init` and `terraform apply` commands from Go code.
95+
- `terraform.Output` allows to retrieve the value of a deployment output variable.
96+
- `terraform.Destroy` allows to run the `terraform destroy` command from Go code.
97+
- `test_structure.LoadTerraformOptions` allows to load Terraform options (config, variables etc.) from the state.
98+
- `test_structure.SaveTerraformOptions` allows to save Terraform options (config, variables etc.) to the state.
99+
100+
## Run the end-to-end test
101+
102+
Running the test requires that Terraform is installed and configured on your machine and that you are connected to your Azure subscription with the Azure CLI command `az login`.
103+
104+
Once ready, since the end-to-end test is just a Go test, it can be run like the following:
105+
106+
```console
107+
# Set the path of the SSH private key to use to connect the virtual machine
108+
export TEST_SSH_KEY_PATH="/home/bob/.ssh/id_rsa"
109+
cd test
110+
go test -v ./ -timeout 30m
111+
```
112+
113+
Once the test is ended, it displays the results:
114+
115+
```console
116+
--- PASS: TestEndToEndDeploymentScenario (390.99s)
117+
PASS
118+
ok test 391.052s
119+
```
66.2 KB
Loading
35.4 KB
Binary file not shown.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
resource "random_integer" "rand" {
2+
min = 1000
3+
max = 9999
4+
}
5+
6+
resource "azurerm_resource_group" "rg" {
7+
name = "rg-terratest-sample-${random_integer.rand.result}"
8+
location = var.location
9+
}
10+
11+
resource "azurerm_virtual_network" "vnet" {
12+
name = "vnet-terratest-sample"
13+
address_space = ["10.0.0.0/16"]
14+
location = azurerm_resource_group.rg.location
15+
resource_group_name = azurerm_resource_group.rg.name
16+
}
17+
18+
resource "azurerm_subnet" "subnet" {
19+
name = "default"
20+
resource_group_name = azurerm_resource_group.rg.name
21+
virtual_network_name = azurerm_virtual_network.vnet.name
22+
address_prefixes = ["10.0.2.0/24"]
23+
}
24+
25+
## Linux VM 1
26+
27+
resource "azurerm_public_ip" "pip" {
28+
name = "pip-vm-linux-1"
29+
location = azurerm_resource_group.rg.location
30+
resource_group_name = azurerm_resource_group.rg.name
31+
allocation_method = "Dynamic"
32+
idle_timeout_in_minutes = 30
33+
}
34+
35+
resource "azurerm_network_interface" "nic1" {
36+
name = "nic-vm-linux-1"
37+
location = azurerm_resource_group.rg.location
38+
resource_group_name = azurerm_resource_group.rg.name
39+
40+
ip_configuration {
41+
name = "internal"
42+
subnet_id = azurerm_subnet.subnet.id
43+
private_ip_address_allocation = "Dynamic"
44+
public_ip_address_id = azurerm_public_ip.pip.id
45+
}
46+
}
47+
48+
resource "azurerm_network_security_group" "nsg" {
49+
name = "nsg-terraform-sample"
50+
location = var.location
51+
resource_group_name = azurerm_resource_group.rg.name
52+
53+
security_rule {
54+
name = "SSH"
55+
priority = 1001
56+
direction = "Inbound"
57+
access = "Allow"
58+
protocol = "Tcp"
59+
source_port_range = "*"
60+
destination_port_range = "22"
61+
source_address_prefix = "*"
62+
destination_address_prefix = "*"
63+
}
64+
}
65+
66+
resource "azurerm_network_interface_security_group_association" "nic1-nsg" {
67+
network_interface_id = azurerm_network_interface.nic1.id
68+
network_security_group_id = azurerm_network_security_group.nsg.id
69+
}
70+
71+
resource "azurerm_linux_virtual_machine" "vm1" {
72+
name = "vm-linux-1"
73+
resource_group_name = azurerm_resource_group.rg.name
74+
location = azurerm_resource_group.rg.location
75+
size = "Standard_B2s"
76+
admin_username = "azureuser"
77+
network_interface_ids = [
78+
azurerm_network_interface.nic1.id,
79+
]
80+
81+
admin_ssh_key {
82+
username = "azureuser"
83+
public_key = file("~/.ssh/id_rsa.pub")
84+
}
85+
86+
os_disk {
87+
caching = "ReadWrite"
88+
storage_account_type = "Standard_LRS"
89+
}
90+
91+
source_image_reference {
92+
publisher = "Canonical"
93+
offer = "UbuntuServer"
94+
sku = "18.04-LTS"
95+
version = "latest"
96+
}
97+
}
98+
99+
## Linux VM 2
100+
101+
resource "azurerm_network_interface" "nic2" {
102+
name = "nic-vm-linux-2"
103+
location = azurerm_resource_group.rg.location
104+
resource_group_name = azurerm_resource_group.rg.name
105+
106+
ip_configuration {
107+
name = "internal"
108+
subnet_id = azurerm_subnet.subnet.id
109+
private_ip_address_allocation = "Dynamic"
110+
}
111+
}
112+
113+
resource "azurerm_linux_virtual_machine" "vm2" {
114+
name = "vm-linux-2"
115+
resource_group_name = azurerm_resource_group.rg.name
116+
location = azurerm_resource_group.rg.location
117+
size = "Standard_B2s"
118+
admin_username = "azureuser"
119+
120+
network_interface_ids = [
121+
azurerm_network_interface.nic2.id,
122+
]
123+
124+
admin_ssh_key {
125+
username = "azureuser"
126+
public_key = file(var.ssh_public_key_file)
127+
}
128+
129+
os_disk {
130+
caching = "ReadWrite"
131+
storage_account_type = "Standard_LRS"
132+
}
133+
134+
source_image_reference {
135+
publisher = "Canonical"
136+
offer = "UbuntuServer"
137+
sku = "18.04-LTS"
138+
version = "latest"
139+
}
140+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "resource_group_name" {
2+
value = azurerm_resource_group.rg.name
3+
}
4+
5+
output "vm_linux_1_public_ip_address" {
6+
value = azurerm_public_ip.pip.ip_address
7+
}
8+
9+
output "vm_linux_2_private_ip_address" {
10+
value = azurerm_network_interface.nic2.ip_configuration[0].private_ip_address
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "azurerm" {
2+
features {}
3+
}

0 commit comments

Comments
 (0)