Giter Club home page Giter Club logo

tftest's Introduction

logo

A collection of functions and common patterns to test Terraform code using Go, and Terratest.


Why ๐Ÿค”

This library wraps Terratest to provide simplified functions for common testing patterns in Terraform modules. The problem that this library aims to solve is to make it so easy to write (terraform) tests that you don't need to be a profficient Go developer to write them.

Installation ๐Ÿ› ๏ธ

Install it using Go get:

go get github.com/Excoriate/tftest

Pre-requisites ๐Ÿ“‹

  • Go >= 1.18

NOTE: For the tools used in this project, please check the Makefile, and the Taskfile files. You'll also need pre-commit installed.


Usage ๐Ÿš€

Simple 'Plan' scenario

package simple

import (
    "testing"
    "github.com/stretchr/testify/assert"
    "github.com/excoriate/tftest/pkg/scenario"
)

func TestSimpleOptionsPlanScenario(t *testing.T) {
    s, err := scenario.New(t, "../../data/tf-random")
    assert.NoErrorf(t, err, "Failed to create scenario: %s", err)

    s.Stg.PlanStage(t, s.GetTerraformOptions())
}

Expecting changes on plan time for certain resources

import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/excoriate/tftest/pkg/scenario"
)


func TestSpecificResourcesExpectedChanges(t *testing.T) {
    s, err := scenario.New(t, "../../data/tf-random")
    assert.NoErrorf(t, err, "Failed to create scenario: %s", err)

    s.Stg.PlanWithSpecificResourcesThatWillChange(t, s.GetTerraformOptions(), []string{"random_id.this"})
}

Full lifecycle (init, plan, destroy) scenario

import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/excoriate/tftest/pkg/scenario"
)

func TestLifecycle(t *testing.T) {
    s, err := scenario.New(t, "../../data/tf-random")
    assert.NoErrorf(t, err, "Failed to create scenario: %s", err)

    defer s.Stg.DestroyStage(t, s.GetTerraformOptions())

    s.Stg.PlanStageWithAnySortOfChanges(t, s.GetTerraformOptions())
    s.Stg.ApplyStage(t, s.GetTerraformOptions())
}

Expecting a variable that should have an expected value on Plan time

import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/excoriate/tftest/pkg/scenario"
)

func TestWithVarOptionsValid(t *testing.T) {
	workdir := "../../data/tf-random"
	s, err := scenario.NewWithOptions(t, workdir,
		scenario.WithVarFiles(workdir, "fixtures/override-random-password.tfvars"))

	assert.NoErrorf(t, err, "Failed to create scenario: %s", err)

	s.Stg.PlanWithSpecificVariableValueToExpect(t, s.GetTerraformOptions(), "random_length_password", "25")
}

More examples will be added in the examples folder.


APIs ๐Ÿ“š

There are more API(s) available for the following common patterns:

  • A strict validation of the terraform directory (it validates whether it's an actual terraform module, directory integrity and others).
  • A strict validation of terraform.tfvars files.
  • Cloud Provider API(s). Currently, AWS is supported.

Roadmap ๐Ÿ—“๏ธ

  • Add more tests
  • Add a set of pre-defined error messages

Note: This is still work in progress, however, I'll be happy to receive any feedback or contribution. Ensure you've read the contributing guide before doing so.

Contributing

Please read our contributing guide.

tftest's People

Contributors

excoriate avatar github-actions[bot] avatar gchiesa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.