terraform test - outputs from your module

To test terraform module, you can place your module into folder, let's call module.

In your test that you place in the root directory, you can have the following:

run "resource-group-name-created-correctly" {
 
  variables {
    prefix = "test"
  }

 module {
    source = "./modules"
  }
   
  command = plan
  assert {
    condition     = output.resource_group_name == "myrg"
    error_message = "Resource group created is not expected"
  }
}

Then run terraform init and terraform test to execute your tests. As shown in the diagram above, you need to declare output from your module - and in this case we have our output called resource_group_name. All we need to do is, use the output keyword and append whatever variable we have just output. 

The output full code.

# Outputs
output "storage_account_name" {
  description = "The name of the storage account"
  value       = azurerm_storage_account.example.name
}

output "primary_blob_endpoint" {
  description = "The primary blob service endpoint for the storage account"
  value       = azurerm_storage_account.example.primary_blob_endpoint
}

output "resource_group_name" {
  description = "The name of the resource group"
  value       = azurerm_resource_group.example.name
}

You should see the final test results as shown here. A thing to note about running terraform test is that it will run test on the root folder and then look for a directory call tests.

You can still test it to use a different folder by specifying --test-directory options. Now terraform will go to the specified directory say for example, abc folder, then root folder and finally tests folder.





Code to the repo 

https://github.com/mitzenjeremywoo?tab=repositories







Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm