github.com/mponton/terratest@v0.44.0/modules/terraform/plan_struct_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  
     6  	http_helper "github.com/mponton/terratest/modules/http-helper"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  const (
    12  	// NOTE: We pull down the json files from github during test runtime as opposed to checking it in as these source
    13  	// files are licensed under MPL and we want to avoid a dual license scenario where some source files in terratest
    14  	// are licensed under a different license.
    15  	basicJsonUrl      = "https://raw.githubusercontent.com/hashicorp/terraform-json/v0.8.0/testdata/basic/plan.json"
    16  	deepModuleJsonUrl = "https://raw.githubusercontent.com/hashicorp/terraform-json/v0.8.0/testdata/deep_module/plan.json"
    17  
    18  	changesJsonUrl = "https://raw.githubusercontent.com/hashicorp/terraform-json/v0.8.0/testdata/has_changes/plan.json"
    19  )
    20  
    21  func TestPlannedValuesMapWithBasicJson(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	// Retrieve test data from the terraform-json project.
    25  	_, jsonData := http_helper.HttpGet(t, basicJsonUrl, nil)
    26  	plan, err := parsePlanJson(jsonData)
    27  	require.NoError(t, err)
    28  
    29  	query := []string{
    30  		"data.null_data_source.baz",
    31  		"null_resource.bar",
    32  		"null_resource.baz[0]",
    33  		"null_resource.baz[1]",
    34  		"null_resource.baz[2]",
    35  		"null_resource.foo",
    36  		"module.foo.null_resource.aliased",
    37  		"module.foo.null_resource.foo",
    38  	}
    39  	for _, key := range query {
    40  		RequirePlannedValuesMapKeyExists(t, plan, key)
    41  		resource := plan.ResourcePlannedValuesMap[key]
    42  		assert.Equal(t, resource.Address, key)
    43  	}
    44  }
    45  
    46  func TestPlannedValuesMapWithDeepModuleJson(t *testing.T) {
    47  	t.Parallel()
    48  
    49  	// Retrieve test data from the terraform-json project.
    50  	_, jsonData := http_helper.HttpGet(t, deepModuleJsonUrl, nil)
    51  	plan, err := parsePlanJson(jsonData)
    52  	require.NoError(t, err)
    53  
    54  	query := []string{
    55  		"module.foo.module.bar.null_resource.baz",
    56  	}
    57  	for _, key := range query {
    58  		AssertPlannedValuesMapKeyExists(t, plan, key)
    59  	}
    60  }
    61  
    62  func TestResourceChangesJson(t *testing.T) {
    63  	t.Parallel()
    64  
    65  	// Retrieve test data from the terraform-json project.
    66  	_, jsonData := http_helper.HttpGet(t, changesJsonUrl, nil)
    67  	plan, err := parsePlanJson(jsonData)
    68  	require.NoError(t, err)
    69  
    70  	// Spot check a few changes to make sure the right address was registered
    71  	RequireResourceChangesMapKeyExists(t, plan, "module.foo.null_resource.foo")
    72  	fooChanges := plan.ResourceChangesMap["module.foo.null_resource.foo"]
    73  	require.NotNil(t, fooChanges.Change)
    74  	assert.Equal(t, fooChanges.Change.After.(map[string]interface{})["triggers"].(map[string]interface{})["foo"].(string), "bar")
    75  
    76  	RequireResourceChangesMapKeyExists(t, plan, "null_resource.bar")
    77  	barChanges := plan.ResourceChangesMap["null_resource.bar"]
    78  	require.NotNil(t, barChanges.Change)
    79  	assert.Equal(t, barChanges.Change.After.(map[string]interface{})["triggers"].(map[string]interface{})["foo_id"].(string), "424881806176056736")
    80  
    81  }