Skip to content

Commit

Permalink
Merge pull request #1127 from hown3d/showWithStruct
Browse files Browse the repository at this point in the history
feat(terraform): add ShowWithStruct function
  • Loading branch information
denis256 authored Jun 10, 2022
2 parents 94dd1e7 + 3f27165 commit 99d3665
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions modules/terraform/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,21 @@ func ShowE(t testing.TestingT, options *Options) (string, error) {
}
return RunTerraformCommandAndGetStdoutE(t, options, args...)
}

func ShowWithStruct(t testing.TestingT, options *Options) *PlanStruct {
out, err := ShowWithStructE(t, options)
require.NoError(t, err)
return out
}

func ShowWithStructE(t testing.TestingT, options *Options) (*PlanStruct, error) {
json, err := ShowE(t, options)
if err != nil {
return nil, err
}
planStruct, err := parsePlanJson(json)
if err != nil {
return nil, err
}
return planStruct, nil
}
30 changes: 30 additions & 0 deletions modules/terraform/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,33 @@ func TestShowWithInlinePlan(t *testing.T) {
planJSON := Show(t, showOptions)
require.Contains(t, planJSON, "null_resource.test[0]")
}

func TestShowWithStructInlinePlan(t *testing.T) {
t.Parallel()

testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-basic-configuration", t.Name())
require.NoError(t, err)
planFilePath := filepath.Join(testFolder, "plan.out")

options := &Options{
TerraformDir: testFolder,
PlanFilePath: planFilePath,
Vars: map[string]interface{}{
"cnt": 1,
},
}

out := InitAndPlan(t, options)
require.Contains(t, out, fmt.Sprintf("Saved the plan to: %s", planFilePath))
require.FileExists(t, planFilePath, "Plan file was not saved to expected location:", planFilePath)

// show command does not accept Vars
showOptions := &Options{
TerraformDir: testFolder,
PlanFilePath: planFilePath,
}

// Test the JSON string
plan := ShowWithStruct(t, showOptions)
require.Contains(t, plan.ResourcePlannedValuesMap, "null_resource.test[0]")
}

0 comments on commit 99d3665

Please sign in to comment.