Like any code, IaC changes can introduce errors, failures, or breaking changes. Automated tests are an essential component of IaC to ensure code quality and reduce the risk of deployment failures, downtime, and associated costs. By detecting errors, failures, or breaking changes early in the process, automated tests can improve the readiness of deployed resources and ensure better service stability.
Symphony offers samples that help to write and execute both module and end-to-end tests for IaC module code and demonstrate how the tests can be integrated into the symphony workflows.
Module tests ensure that module code/configuration will create the resources successfully.
- Module tests deploy the module resources, then validate the deployed resources & configurations, and finally tear down any deployed resources.
- Slow to execute, but can be executed in parallel.
- Have no dependency on any resource other than the module under test resources.
End to End tests ensure that all resources deployed by one or more modules are working as expected.
- End to End tests validate already deployed resources for a long-lived environment e.g. development or production
- Fast to execute, and can be executed in parallel.
- Depend on multiple modules, and sometimes the entire system resources.
All below test examples have an assumption for the working directory - should be IAC/Bicep/test
The tests will check a template or set of templates for coding best practices using arm-ttk.
-
Generate ARM template based on Bicep
az bicep build --file ../bicep/01_sql/02_deployment/main.bicep
-
Install ARM TTK module
Import-Module .\arm-ttk\arm-ttk.psd1
-
Run the test
Test-AzTemplate -TemplatePath ../bicep/01_sql/02_deployment/main.json
-
Cleanup
Remove-Item -Force -Path ../bicep/01_sql/02_deployment/main.json
Pester is a testing and mocking framework for PowerShell.
-
Install Pester module
Install-Module -Name Pester -AllowClobber -Force -Confirm:$False -SkipPublisherCheck
-
(option 1) Run the test
Invoke-Pester -Path ./pester/SqlIntegration.Tests.ps1
-
(option 2) Run the test with the JUnit report
Invoke-Pester -Path ./pester/SqlIntegration.Tests.ps1 -OutputFile Test.xml -OutputFormat JUnitXml
ShellSpec is a full-featured BDD unit testing framework for the dash, bash, ksh, zsh and all POSIX shells that provide first-class features such as code coverage, mocking, parameterized test, parallel execution and more.
-
Install ShellSpec
curl -fsSL https://git.io/shellspec | sh -s -- --yes
-
(option 1) Run the test
shellspec -f d
-
(option 2) Run the test with the JUnit report
shellspec -f j > tests.xml
All below test examples have an assumption for the working directory - should be IAC/Terraform/test/terraform
Terratest is a Go library that makes it easier to write automated tests for your infrastructure code. It provides a variety of helper functions and patterns for common infrastructure testing tasks, and offers good support for the most commonly used Azure resources.
-
Ensure Go 1.16 is installed, and the Terratest Go environment is properly configured.
-
Run the tests.
go test -v -timeout 1000s --tags=e2e_test