Skip to content

Latest commit

 

History

History
113 lines (70 loc) · 3.96 KB

TESTING.md

File metadata and controls

113 lines (70 loc) · 3.96 KB

IaC Testing

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

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

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.

Bicep

All below test examples have an assumption for the working directory - should be IAC/Bicep/test

Azure Resource Manager Template Toolkit (arm-ttk)

The tests will check a template or set of templates for coding best practices using arm-ttk.

  1. Generate ARM template based on Bicep

    az bicep build --file ../bicep/01_sql/02_deployment/main.bicep
  2. Install ARM TTK module

    Import-Module .\arm-ttk\arm-ttk.psd1
  3. Run the test

    Test-AzTemplate -TemplatePath ../bicep/01_sql/02_deployment/main.json
  4. Cleanup

    Remove-Item -Force -Path ../bicep/01_sql/02_deployment/main.json

End to End Tests with Pester

Pester is a testing and mocking framework for PowerShell.

  1. Install Pester module

    Install-Module -Name Pester -AllowClobber -Force -Confirm:$False -SkipPublisherCheck
  2. (option 1) Run the test

    Invoke-Pester -Path ./pester/SqlIntegration.Tests.ps1
  3. (option 2) Run the test with the JUnit report

    Invoke-Pester -Path ./pester/SqlIntegration.Tests.ps1 -OutputFile Test.xml -OutputFormat JUnitXml

End to End test with ShellSpec

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.

  1. Install ShellSpec

    curl -fsSL https://git.io/shellspec | sh -s -- --yes
  2. (option 1) Run the test

    shellspec -f d
  3. (option 2) Run the test with the JUnit report

    shellspec -f j > tests.xml

Terraform

All below test examples have an assumption for the working directory - should be IAC/Terraform/test/terraform

End to End tests with Terratest

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.

  1. Ensure Go 1.16 is installed, and the Terratest Go environment is properly configured.

  2. Run the tests.

    go test -v -timeout 1000s --tags=e2e_test