Sunday, November 5, 2023

Getting Started with Terraform Test

This blog explains how to use new terraform test framework to test infrastructure automation scripts.

Pre-requisites: Install Terraform v1.6.0 or later version, Azure subscription, code editor (Visual studio code use as the code editor in this blog)

What is the use of terraform test?

Terraform has introduced new test framework which helps to achieve followings.

  • Validate terraform configuration updates and get verified new changes does not introduce any breaking changes.
  • Terraform test supports running test against test environment which allows to create short lived resources without breaking existing resources.

Demo

Add following terraform configuration files which create a resource group and a storage account in Azure.

Main.tf – This file has main configuration scripts.

resource "azurerm_resource_group" "az-rg" {
  name     = "${var.resourceprefix}-test"
  location = "West Europe"
}

resource "azurerm_storage_account" "az-sacc" {
  name                     = var.accname
  resource_group_name      = azurerm_resource_group.az-rg.name
  location                 = azurerm_resource_group.az-rg.location
  account_tier             = var.tier
  account_replication_type = "GRS"
}

provider.tf – information about providers

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.78.0"
    }
  }
  backend "local" {}
}

provider "azurerm" {
  features {}
}

variable.tf – variables use in main configuration scripts.

variable "resourceprefix" {
  type = string
}

variable "accname" {
  type = string
}

variable "tier" {
  type = string
}

After adding terraform configuration files, add test file as explained below.

Create folder called tests in the same level as terraform configuration files as shown in the following image. Add test file with tftest.hcl extension. (ex: main.tftest.hcl)



test file(tftest.hcl) file consists with following sections.

  • variables - Provide input variables for configurations from the test file.
  • run – There are multiple sections such as variables, modules, commands, assertions, etc. in the run block.
  • provider - Override required providers in main configuration files using testing files.


Add following test file (main.tftest.hcl)  to tests folder to run Terraform test for above configurations.

main.tftest.hcl


variables {
  resourceprefix = "demo"
  accname = "myaccname1"
  tier  = "Standard"
}

run "TC01_verify_users" {
  command = plan

  assert { 
    condition     = azurerm_resource_group.az-rg.name == "demo-test"
    error_message = "Incorrect resource group name"
  }
}

run "TC_02_verify_storage_acc" {
  command = plan

  assert { 
    condition     = startswith(azurerm_storage_account.az-sacc.name,"myacc")
    error_message = "Incorrect storage name"
  }
}

Following are the main sections of above sample test file.

  • First set values for resourceprefixaccname and tier variables using the test file.
  • There are two run blocks in the test file as TC01_verify_users and TC_02_verify_storage_acc with following sections.
    • command - can use as plan or apply. If you put apply as a command, resources will get created in provided azure subscription and get deleted once test is completed. (If providers block provided in test file, it will override required providers in main configuration files and allows to use different subscriptions for testing)
    • assert - provide condition to check and error message to throw in case of condition failure. 

Open New Terminal in visual studio and run following commands.

  • Initialize the working directory.

                terraform init 

  • Run test.

                terraform test

You would be able to see test run results as below.        


Find a sample code in GitHub


No comments:

Post a Comment