This blog explains how to use the terraform test framework to verify that new changes do not break existing resources.
Pre-requisites: Follow this blog and configure a terraform project with a test file.
The sample terraform project configure a resource group and a storage account in Azure.
Open a new terminal in VS Code and change the directory to the folder containing the terraform configuration files.Execute the terraform test command to verify that the configuration script has been written and is working correctly.
Replace the content of main.tftest.hcl file with following script.
variables {
resourceprefix = "demo"
accname = "myaccname1"
tier = "Premium"
}
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"
}
}
run "TC_03_verify_storage_acc_tier" {
command = plan
assert {
condition = azurerm_storage_account.az-sacc.account_tier == "Standard"
error_message = "Incorrect storage tier"
}
}
Following are the test cases executed in the above test file.
TC01_verify_users - check name of the resource group is demo-test
TC_02_verify_storage_acc - verify azure storage account name starts with myacc
TC_03_verify_storage_acc_tier - verify storage account tier is Standard
We can use the 'variables' section of the test file to assign values to variables used in the main configuration file. Values for resourceprefix, accname and tier variables are added by the test file.
Run terrafom test command to verify terraform configurations. You would be able to see two passing tests and one failing test. According to TC_03_verify_storage_acc_tier, main configuration file expects Standard as the Azure Storage Account tier value, but the test file passes Premium as the tier value, which is incorrect value in this case.
No comments:
Post a Comment