Easier way to unit test terraform

Overview

Unit testing terraform (WIP)

Go Go Report Card

Disclaimer

Currently, the only way to compare values is using JSON query path and all types are strings.

  want := terraformtest.TFResource{
    Filter: `planned_values.root_module.child_modules.#.resources`,
    Check: map[string]string{
      "0.0.address":              "module.nomad_job.nomad_job.test_job",
      "0.0.type":                 "nomad_job",
      "0.0.values.name":          "unit-test",
      "0.0.values.datacenters.0": "dc1",
    },
  }

If we would have a second datacenter we would add one more line.

  want := terraformtest.TFResource{
    Filter: `planned_values.root_module.child_modules.#.resources`,
    Check: map[string]string{
      "0.0.address":              "module.nomad_job.nomad_job.test_job",
      "0.0.type":                 "nomad_job",
      "0.0.values.name":          "unit-test",
      "0.0.values.datacenters.0": "dc1",
      "0.0.values.datacenters.1": "dc2",
    },
  }
You might also like...
How we can run unit tests in parallel mode with failpoint injection taking effect and without injection race

This is a simple demo to show how we can run unit tests in parallel mode with failpoint injection taking effect and without injection race. The basic

Go Unit Testing Clean Arch

Golang Unit Testing Tutorial melakukan unit testing di Golang yang sudah menerapkan clean architecture Menjalankan Service PSQL_HOST=IP Database Serv

Vault mock - Mock of Hashicorp Vault used for unit testing

vault_mock Mock of Hashicorp Vault used for unit testing Notice This is a person

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.

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,

A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.

About chromedp Package chromedp is a faster, simpler way to drive browsers supporting the Chrome DevTools Protocol in Go without external dependencies

A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.

About chromedp Package chromedp is a faster, simpler way to drive browsers supporting the Chrome DevTools Protocol in Go without external dependencies

A clipboard-based mocking framework for Go that gets out of your way.
A clipboard-based mocking framework for Go that gets out of your way.

A clipboard-based mocking framework for Go that gets out of your way. This tool has been built with inspiration lovingly taken from Moq, and fuelled b

Test your command line interfaces on windows, linux and osx and nodes viá ssh and docker

Commander Define language independent tests for your command line scripts and programs in simple yaml files. It runs on windows, osx and linux It can

Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test
Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test

embedded-postgres Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test. When testing this provides

Comments
  • feat: comparing nested structures

    feat: comparing nested structures

    Add new function to normalize strings to avoid issues when comparing nested stuff. Add tests for nested comparison. Add new terraform plan in json as test data.

    opened by thiagonache 1
  • Change test ReadPlanFile function

    Change test ReadPlanFile function

    Depends on https://github.com/thiagonache/terraformtest/issues/3.

    The test function ReadPlanFile should Fatal instead of Error since nothing can be done if it cannot read the file.

    Also, we should update from wantBiggerThan := 2000 to want := the exact number of bytes.

    opened by thiagonache 1
  • Add new function to do test paperwork

    Add new function to do test paperwork

    I want to reduce the paperwork required to run a test. Eg.:

            tfPlan, err := terraformtest.ReadPlan("./mymodule.plan.json")
    	if err != nil {
    		t.Fatalf("Cannot read plan file ./mymodule.plan.json: %v", err)
    	}
    
    	wantResource := terraformtest.Resource{
    		Address: "module.vpc.aws_vpc.this[0]",
    		Metadata: map[string]string{
    			"type": "aws_vpc",
    			"name": "this",
    		},
    		Values: map[string]string{
    			"enable_dns_hostnames":             "true",
    			"enable_dns_support":               "true",
    			"assign_generated_ipv6_cidr_block": "false",
    			"cidr_block":                       "10.0.0.0/16",
    			"instance_tenancy":                 "default",
    		},
    	}
    
    	gotRS := tfPlan.Resources
    	if !gotRS.Contains(wantResource) {
    		t.Error(gotRS.Diff())
    	}
    

    Could be simplified to

    	wantResource := terraformtest.Resource{
    		Address: "module.vpc.aws_vpc.this[0]",
    		Metadata: map[string]string{
    			"type": "aws_vpc",
    			"name": "this",
    		},
    		Values: map[string]string{
    			"enable_dns_hostnames":             "true",
    			"enable_dns_support":               "true",
    			"assign_generated_ipv6_cidr_block": "false",
    			"cidr_block":                       "10.0.0.0/16",
    			"instance_tenancy":                 "default",
    		},
    	}
    
            r := terraformtest.New(
                    terraformtest.WithPlanFile("./mymodule.plan.json")
            )
    	terraformtest.Contains(wantResource)
    
    enhancement 
    opened by thiagonache 1
  • Ability to unit test variables also

    Ability to unit test variables also

    Currently we can only test values but we may want to test some variables.

    {
      "variables": {
    
      }
    }
    

    The transform function should also store the values inside of this block into the type Resource which will need an extra field probably called Variables which is going to be a map[string]string.

    The functions Contains and Equal should be modified to consider the new data.

    Please, also add respective tests for the new function 😄

    enhancement good first issue 
    opened by thiagonache 0
Releases(v1.0.2)
Owner
Thiago Nache Carvalho
Sysadmin && Software engineer && @bitfield mentee
Thiago Nache Carvalho
A simple yet intuitive golang unit test framework.

gotest Intuitive and simple golang testing framework which helps in writing unit tests in a way which improves the readability of the test. Here is an

null 1 Jan 1, 2022
Benchmarking deferent Fibonacci functions and algorithms with running unit test

GoFibonacciBench Benchmarking deferent Fibonacci functions and algorithms with running unit test ... Introduction: Fibonacci numbers are special kinds

null 2 Feb 27, 2022
Belajar golang unit test

perintah eksekusi di root folder : go test -v ./... assertion ambil dari framewo

Dhany 0 Feb 3, 2022
go-test-trace is like go test but it also generates distributed traces.

go-test-trace go-test-trace is like go test but it also generates distributed traces. Generated traces are exported in OTLP to a OpenTelemetry collect

JBD 376 Sep 22, 2022
Flugel Test Documentation for steps to run and test the automatio

Flugel Test Documentation Documentation for steps to run and test the automation #Test-01 1 - Local Test Using Terratest (End To End) 1- By runing " t

Suc0_Unix 0 Dec 30, 2021
Test-assignment - Test assignment with golang

test-assignment We have a two steam of data and we need to save it in the map: I

null 0 Jan 19, 2022
go websocket client for unit testing of a websocket handler

wstest A websocket client for unit-testing a websocket server The gorilla organization provides full featured websocket implementation that the standa

Eyal Posener 95 Sep 27, 2022
gostub is a library to make stubbing in unit tests easy

gostub gostub is a library to make stubbing in unit tests easy. Getting started Import the following package: github.com/prashantv/gostub Click here t

Prashant Varanasi 254 Sep 23, 2022
gomonkey is a library to make monkey patching in unit tests easy

gomonkey is a library to make monkey patching in unit tests easy, and the core idea of monkey patching comes from Bouke, you can read this blogpost for an explanation on how it works.

Zhang Xiaolong 1.3k Sep 21, 2022
A mock of Go's net package for unit/integration testing

netmock: Simulate Go network connections netmock is a Go package for simulating net connections, including delays and disconnects. This is work in pro

Lucas Wolf 1 Oct 27, 2021