Terraform is an awesome way of provisioning your infrastructure. However, like with any new tool, the ecosystem surrounding it is very immature. This can cause problems when sharing code or coming up against the rough edges of a tool in active development.
The creation of rspec-terraform
was initially intended to smooth the creation and sharing of common Terraform
modules. Some sort of basic testing would ensure a stable and clearly defined interface for each module.
Eventually, a two-tiered approach to testing would be ideal.
Provisioning non-trivial infrastructure should involve the use of many Terraform modules (rather than defining
everything yourself). Taking AWS as an example, this might include VPCs, ASGs, SGs, public/private subnets etc. Each of
these modules should be unit tested using rspec-terraform
so that the interfaces they expose are well-defined.
Assembling many individual modules into a cohesive platform should also - ideally - be tested. It's unclear at this
stage how this might look and is yet to be implemented. Something similar to
serverspec
may work.
As outlined above, only simple unit-test type operations are currently supported.
You will need Ruby and bundler installed.
Create a Gemfile
at the root of your Terraform module and add the following:
source 'https://rubygems.org'
gem 'rspec-terraform'
Then install the gem:
bundle
N.B. You must set the provider for each test. This is best done in the opening describe
block:
describe 'tf-aws-vpc', provider: :aws do
...
end
At present, only the AWS
provider is available.
The matchers currently implemented are:
require_variables
it 'expects the correct variables to be provided' do
expected_variables = %w(vpccidr ec2nameserver region account envname domain)
expect('terraform plan').to require_variables expected_variables
end
create_a_plan
it 'creates a plan' do
expect('terraform plan -var-file example_variables/test_values.tfvars').to create_a_plan
end
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)