Skip to content

Commit

Permalink
Get plan output with terraform 0.12 (#41)
Browse files Browse the repository at this point in the history
This fixes issue #38 and adds 0.12.6 to the list of versions we test against.

Context: `terraform show` is broken since 0.12 (see hashicorp/terraform#21446), so I'm using regex on top of `terraform plan` to produce the plan output.

Tests were fixed to support changes in output in 0.12.
  • Loading branch information
btromanova committed Sep 24, 2019
1 parent be238b2 commit 8e4a1f6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
24 changes: 20 additions & 4 deletions astro/terraform/terraform_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package terraform

import (
"fmt"
"regexp"
)

// Plan runs a `terraform plan`
Expand Down Expand Up @@ -53,12 +54,27 @@ func (s *Session) Plan() (Result, error) {
// are changes (so there's no error).
if process.ExitCode() == 2 {
// Fetch changes
result, err := s.Show(fmt.Sprintf("%s.plan", s.id))
terraformVersion, err := s.versionCached()
if err != nil {
return result, err
return nil, err
}
if VersionMatches(terraformVersion, "<0.12") {
result, err := s.Show(fmt.Sprintf("%s.plan", s.id))
if err != nil {
return result, err
}
changes = result.Stdout()
} else {
rawPlanOutput := process.Stdout().String()
var re = regexp.MustCompile(`(?s)Terraform will perform the following actions:(.*)-{72}`)
if match := re.FindStringSubmatch(rawPlanOutput); len(match) == 2 {
changes = match[1]
} else {
return &terraformResult{
process: process,
}, fmt.Errorf("unable to parse terraform plan output")
}
}

changes = result.Stdout()
}

return &PlanResult{
Expand Down
1 change: 1 addition & 0 deletions astro/tests/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var (
"0.9.11",
"0.10.8",
"0.11.5",
"0.12.6",
}
)

Expand Down
21 changes: 19 additions & 2 deletions astro/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber/astro/astro/terraform"

version "github.com/burl/go-version"
)

// getSessionDirs returns a list of the sessions inside a session repository.
Expand Down Expand Up @@ -52,6 +55,12 @@ func getSessionDirs(sessionBaseDir string) ([]string, error) {
return sessionDirs, nil
}

// stringVersionMatches returns whether or not the version passed as string matches the constraint.
// See terraform.VersionMatches for more info.
func stringVersionMatches(v string, versionConstraint string) bool {
return terraform.VersionMatches(version.Must(version.NewVersion(v)), versionConstraint)
}

func TestProjectApplyChangesSuccess(t *testing.T) {
for _, version := range terraformVersionsToTest {
t.Run(version, func(t *testing.T) {
Expand Down Expand Up @@ -84,7 +93,11 @@ func TestProjectPlanSuccessChanges(t *testing.T) {
t.Run(version, func(t *testing.T) {
result := RunTest(t, []string{"plan"}, "fixtures/plan-success-changes", version)
assert.Contains(t, result.Stdout.String(), "foo: OK Changes")
assert.Regexp(t, `\+.*null_resource.foo`, result.Stdout.String())
addedResourceRe := `\+.*null_resource.foo`
if stringVersionMatches(version, ">=0.12") {
addedResourceRe = `null_resource.foo.*will be created`
}
assert.Regexp(t, addedResourceRe, result.Stdout.String())
assert.Equal(t, 0, result.ExitCode)
})
}
Expand All @@ -95,7 +108,11 @@ func TestProjectPlanError(t *testing.T) {
t.Run(version, func(t *testing.T) {
result := RunTest(t, []string{"plan"}, "fixtures/plan-error", version)
assert.Contains(t, result.Stderr.String(), "foo: ERROR")
assert.Contains(t, result.Stderr.String(), "Error parsing")
errorMessage := "Error parsing"
if stringVersionMatches(version, ">=0.12") {
errorMessage = "Argument or block definition required"
}
assert.Contains(t, result.Stderr.String(), errorMessage)
assert.Equal(t, 1, result.ExitCode)
})
}
Expand Down

0 comments on commit 8e4a1f6

Please sign in to comment.