# Add functional tests using terraform outputs to get instance IP address
To improve a bit the current pipeline, let's add a dummy functional test which try to access to the website deployed on the instance.
We will get the IP address of the instance using the output defined in the Terraform module Terraform outputs. Then ensure the website answer correctly with a HTTP 200 response code
Follow those steps to apply all changes described in this step
To be able to get the IP address of the instance we forward the output of the Terraform module to the pipeline in output.tf
file.
stack-sample/terraform/outputs.tf
output "ip_address" {
value = module.instance.ip_address
description = "IP of the server"
}
2
3
4
All Terraform outputs are available in the pipeline Terraform resource.
Create a new functional-tests
job into our pipeline:
stack-sample/pipeline/pipeline.yml
groups:
- name: all
jobs:
...
- functional-tests
jobs:
...
- name: functional-tests
max_in_flight: 1
build_logs_to_retain: 10
plan:
- do:
- get: tfstate
passed: [terraform-apply]
trigger: true
- task: test
config:
platform: linux
image_resource:
type: docker-image
source:
repository: cycloid/cycloid-toolkit
tag: latest
run:
path: /bin/bash
args:
- -c
- |
echo "Dummy functional tests"
# Wait 5 min ec2 up
for i in {1..20}
do
STATUS=$(curl --connect-timeout 2 -s -o /dev/null -w '%{http_code}' $(jq -r .ip_address tfstate/metadata))
if [ $STATUS -eq 200 ]; then
echo "Got 200! All done!"
exit 0
else
echo "Got $STATUS :( Not done yet... (retry ${i}/20)"
fi
sleep 15
done
echo "functional tests [Failed]"
exit 1
inputs:
- name: tfstate
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
First, don't forget to add the new job into the groups
section. Then Terraform resource tfstate
expose Terraform outputs in a json file called metadata
. Getting this resource in our job, and using it as inputs
allows us to read tfstate/metadata
to extract our previously declared Terraform output ip_address
.
The job above is a bash script using the IP address from tfstate/metadata
file to execute a HTTP request until it got a HTTP code 200 as answer.
In a real prod pipeline you woud expect a more complexe script to fully test your application.
Add and commit those changes in Git:
git add .
git commit -m "Step 6"
git push origin stacks
2
3
Get back to Cycloid's dashboard, click on the Refresh pipeline
button .
Then have a look on the functional-tests
log to see the awesome result of our functional test: