Stack-Level Testing

Note: Premium video content requires a subscription.

Stack-level testing refers to testing specific stacks. Their tests live within the stack’s folder itself and are meant to be ran independently.


Let’s say you have terraform stack named demo:


We can use terraspace new test run from the root of the terraspace project to create a app/stacks/demo/test/spec/main_spec.rb test.

terraspace new test demo --type stack

The structure will look something like this:

└── spec
    ├── fixtures
    │   └── stack
    │       ├──
    │       └──
    └── main_spec.rb

Test Code

Here’s an example of the generate spec.


describe "main" do
  before(:all) do
    stack_path = File.expand_path("../..", __dir__) # the source of the stack to test is 2 levels up
    ts_root = File.expand_path("../../..", stack_path) # original Terraspace.root
    # Build terraspace project to use as a test harness
    # Will be located at: /tmp/terraspace/test-harnesses/demo-harness
      name:    "demo-harness",
      modules: "#{ts_root}/app/modules", # include all modules in folder
      stacks:  {demo: stack_path},
      # override demo stack tfvars for testing
      tfvars:  {demo: "#{stack_path}/spec/fixtures/tfvars/demo.tfvars"},
      # create config if needed. The folder will be copied over
      # config:  "#{stack_path}/spec/fixtures/config",
  after(:all) do

  it "successful deploy" do
    # Replace with your own test
    expect(true).to be true
    # Example
    # pp terraspace.outputs
    # output_value = terraspace.output("demo", "some-output")
    # expect(output_value).to include("some-value")
    # More useful helpers:
    # pp terraspace.state['resources']
    # pp terraspace.state_resource('random_pet.this')
    # pp terraspace.state_resource('module.bucket')

Run Tests

To run the test, you should be within in the module folder itself.

cd app/stacks/demo
terraspace test

The test process will:

  1. Generate a test harness, which is a terraspace project
  2. Run terraspace up to create actual resources
  3. Run your test logic
  4. Run terraspace down to destroy the resources

More tools: