GithubHelp home page GithubHelp logo

azure / terratest-terraform-fluent Goto Github PK

View Code? Open in Web Editor NEW
12.0 4.0 2.0 15.24 MB

Go module for Terraform module testing with fluent assertions

Home Page: https://pkg.go.dev/github.com/Azure/terratest-terraform-fluent

License: MIT License

Makefile 1.94% Go 98.06%
go golang terraform terraform-module terratest testing

terratest-terraform-fluent's Introduction

terratest-terraform-fluent

codecov

Terratest extension package for testing Terraform code with fluent assertions.

Usage

package test

import (
  "testing"

  "github.com/Azure/terratest-terraform-fluent/check"
  "github.com/Azure/terratest-terraform-fluent/setuptest"
  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/require"
)

const (
  moduleDir = "../"
)

func TestSomeTerraform(t *testing.T) {
  // Set up the Terraform test and run terraform init, plan and show,
  // saving the plan output to a struct.
  // The returned struct in tftest contains the temp dir, the plan struct,
  // the terraform options, and the clean up func.
  //
  // The Dirs inputs are the test root directory and the relative path to the test code.
  // (this must be a subdirectory of the test root directory).
  // To test the module in the current directory, use "" for the second input.
  //
  // The WithVars inputs are the Terraform variables to pass to the test.
  // The InitPlanShow input is the testing.T pointer.
  tftest, err := setuptest.Dirs(moduleDir, "").WithVars(nil).InitPlanShow(t)
  require.NoError(t, err)

  // Defer the cleanup, which will delete the temporary directory and provide coherent logging.
  // THIS IS VERY IMPORTANT :)
  defer tftest.Cleanup()

  // Check that the plan contains the expected number of resources.
  check.InPlan(tftest.Plan).NumberOfResourcesEquals(1).ErrorIsNil(t)

  // Check that the plan contains the expected resource, with an attribute called `my_attribute` and
  // a corresponding value of `my_value`.
  check.InPlan(tftest.Plan).That("my_terraform_resource.name").Key("my_attribute").HasValue("my_value").ErrorIsNil(t)

  // Check that the plan contains the expected resource, with an attribute called `my_complex_attribute` and
  // a gjson query in a list called `mylist`, taking the first element, which is an object with a property
  // called `property`, with a value of `my_value`. See: https://github.com/tidwall/gjson/blob/master/SYNTAX.md
  check.InPlan(tftest.Plan).That("my_terraform_resource.name").Key("my_complex_attribute").Query("mylist.0.property").HasValue("my_value").ErrorIsNil(t)

  // Ensure that the terraform apply is idempotent.
  defer tftest.Destroy()
  tftest.ApplyIdempotent().ErrorIsNil(t)

  // Retrieve the value from the plan and check it using an external func.
  val, err := check.InPlan(tftest.Plan).That("my_terraform_resource.name").Key("my_other_attribute").GetValue()
  assert.NoError(t, err)
  assert.NoError(t, myValidationFunc(val))

  // Check that the output contains the expected value.
  tftest.Output("my_output").HasValue("my_output_value").ErrorIsNil(t)
}

terratest-terraform-fluent's People

Contributors

dependabot[bot] avatar matt-ffffff avatar microsoftopensource avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

mikesecurity

terratest-terraform-fluent's Issues

feat(setuptest): init only

Add method to perform init only, e.g.

resp, err := Dirs("testdata/depth1", "").WithVars(nil).Init(t)
resp.Apply()

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.