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

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/mponton/terratest/modules/files"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestShowWithInlinePlan(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-basic-configuration", t.Name())
    16  	require.NoError(t, err)
    17  	planFilePath := filepath.Join(testFolder, "plan.out")
    18  
    19  	options := &Options{
    20  		TerraformDir: testFolder,
    21  		PlanFilePath: planFilePath,
    22  		Vars: map[string]interface{}{
    23  			"cnt": 1,
    24  		},
    25  	}
    26  
    27  	out := InitAndPlan(t, options)
    28  	require.Contains(t, out, fmt.Sprintf("Saved the plan to: %s", planFilePath))
    29  	require.FileExists(t, planFilePath, "Plan file was not saved to expected location:", planFilePath)
    30  
    31  	// show command does not accept Vars
    32  	showOptions := &Options{
    33  		TerraformDir: testFolder,
    34  		PlanFilePath: planFilePath,
    35  	}
    36  
    37  	// Test the JSON string
    38  	planJSON := Show(t, showOptions)
    39  	require.Contains(t, planJSON, "null_resource.test[0]")
    40  }
    41  
    42  func TestShowWithStructInlinePlan(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-basic-configuration", t.Name())
    46  	require.NoError(t, err)
    47  	planFilePath := filepath.Join(testFolder, "plan.out")
    48  
    49  	options := &Options{
    50  		TerraformDir: testFolder,
    51  		PlanFilePath: planFilePath,
    52  		Vars: map[string]interface{}{
    53  			"cnt": 1,
    54  		},
    55  	}
    56  
    57  	out := InitAndPlan(t, options)
    58  	require.Contains(t, out, fmt.Sprintf("Saved the plan to: %s", planFilePath))
    59  	require.FileExists(t, planFilePath, "Plan file was not saved to expected location:", planFilePath)
    60  
    61  	// show command does not accept Vars
    62  	showOptions := &Options{
    63  		TerraformDir: testFolder,
    64  		PlanFilePath: planFilePath,
    65  	}
    66  
    67  	// Test the JSON string
    68  	plan := ShowWithStruct(t, showOptions)
    69  	require.Contains(t, plan.ResourcePlannedValuesMap, "null_resource.test[0]")
    70  }