Terraform Integration/Unit Testing with Tests

terraform integration and unit testing with test blocks

In this quick start demo we are going to cover Terraform Integration/Unit Testing with Tests

Terraform has introduced testing framework in 1.6.0 version release.

Terraform test files should have .tftest.hcl or .tftest.json file extensions. Terraform automatically discover code in these files and execute them. We can have multiple .tftest.hcl or .tftest.json files.

Each test file should have:

Minimum one run block is required.
Maximum one variables block is allowed.

In this demo, we are going to write tests for a GCS bucket location and name.

Following code creates a GCS bucket in us-east1 region with prefix “devops-counsel”

variable "prefix" {
        default = "devops-cousel"
}

variable "region" {
        default = "us-east1"
}

resource "google_storage_bucket" "bucket" {
  name          = "${var.prefix}-bucket"
  location      = var.region
  project       = "devops-counsel-demo"
}

Now we are going to use below code to test bucket name prefix concatenation and bucket location by adding it to a file called “gcs-test.tftest.hcl” and running “terraform test” command

variables {
 prefix = "devops-counsel-tftest"
 region = "us-east1"
}

run "prefix_check" {
  command = plan
  assert {
    condition     = google_storage_bucket.bucket.name == "devops-counsel-tftest-bucket"
    error_message = "GCS bucket name is not matching"
  }
}

run "location_check" {
  command = plan
  assert {
    condition     = google_storage_bucket.bucket.location == "US-EAST1"
    error_message = "GCS bucket location is other than us-east1"
  }
}

The above file has a variables block and 2 run blocks to check bucket name after prefix concatenation and bucket location.

To run test for above code run terraform test like below after running terraform init command.

sudheerv@cloudshell:~/terraform-test-demo$ terraform test
gcs-test.tftest.hcl... in progress
  run "prefix_check"... pass
  run "location_check"... pass
gcs-test.tftest.hcl... tearing down
gcs-test.tftest.hcl... pass

Success! 2 passed, 0 failed.

In the above example, we used command = plan option, plan instructs terraform not to create actual resource, it just uses terraform plan output to validate conditions, which is kind of unit test for Terraform code.

If we change command value to apply it will create a resources and then validate conditions and delete resources once validation is done, which is kind of integration test for Terraform code.

Test output looks same for plan and apply test commands.

Testing Module Blocks with the help of Terraform Outputs

Following code uses a remote module to create a GCS bucket in us-east1 region with prefix “devops-counsel”. This module has name and location output values.

variable "prefix" {
        default = "devops-cousel"
}

variable "region" {
        default = "us-east1"
}

module "gcs_bucket" {
        source = "github.com/devops-counsel/gcs-demo-module.git"
        prefix = var.prefix
        project_id = "devops-counsel-demo"
        region = var.region
}

Now we can use below code to test bucket name prefix concatenation and bucket location by adding it to a file called “gcs-module-test.tftest.hcl” and running terraform test command.

In run blocks we used module output values in assertion block.

variables {
 prefix = "devops-counsel-tftest"
 region = "us-east1"
}

run "prefix_check" {
  command = apply
  assert {
    condition     = module.gcs_bucket.name == "devops-counsel-tftest-bucket"
    error_message = "GCS bucket name is not matching"
  }
}

run "location_check" {
  command = apply
  assert {
    condition     = module.gcs_bucket.region == "US-EAST1"
    error_message = "GCS bucket location is other than us-east1"
  }
}

following is the “terraform test” output.

sudheerv@cloudshell:~/terraform-test-module-demo$ terraform test
gcs-test.tftest.hcl... in progress
  run "prefix_check"... pass
  run "location_check"... pass
gcs-test.tftest.hcl... tearing down
gcs-test.tftest.hcl... pass

Success! 2 passed, 0 failed.

Above basic and simple examples let you understand how to do Terraform Integration/Unit Testing with Tests. You can find more information on Terraform tests here.

More on Terraform:

Terraform: for_each and count meta-arguments

Leave a Reply

%d