# Add functional tests using Terraform outputs to get instance IP address

Pipeline step6

To improve the current pipeline, let's add a dummy functional test that tries 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 (opens new window). Then make sure the website responds correctly with an HTTP 200 response code

Follow those steps to apply all changes described in this step

To get the IP address of the instance we forward the output of the Terraform module to the pipeline in the output.tf file.

stack-sample/terraform/outputs.tf

output "ip_address" {
  value       = module.instance.ip_address
  description = "IP of the server"
}
1
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
1
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 to 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 an HTTP request until it gets an HTTP code 200. You would expect a more complex script to fully test your application in a real production pipeline.

Add and commit those changes in Git:

git add .
git commit -m "Step 6"
git push origin stacks
1
2
3

Get back to Cycloid's dashboard, and click on the Refresh pipeline button Refresh pipeline.

Then have a look at the functional-tests log to see the awesome result of our functional test:

functional-tests


# Key points to remember

  • Pipeline resources allow to share information between jobs
  • It's always good to read resources documentation to know what they can expose to jobs
  • In our case the Terraform resource expose Terraform outputs to jobs