-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
204 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
bad_block { | ||
} | ||
|
||
terraform { | ||
bad_attribute = "string" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package e2etest | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/hashicorp/terraform-exec/tfexec" | ||
) | ||
|
||
var ( | ||
validateMinVersion = version.Must(version.NewVersion("0.12.0")) | ||
) | ||
|
||
func TestValidate(t *testing.T) { | ||
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { | ||
if tfv.LessThan(validateMinVersion) { | ||
t.Skip("terraform validate -json was added in Terraform 0.12, so test is not valid") | ||
} | ||
|
||
err := tf.Init(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
validation, err := tf.Validate(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !validation.Valid { | ||
t.Fatalf("expected valid, got %#v", validation) | ||
} | ||
}) | ||
|
||
runTest(t, "invalid", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { | ||
if tfv.LessThan(validateMinVersion) { | ||
t.Skip("terraform validate -json was added in Terraform 0.12, so test is not valid") | ||
} | ||
|
||
err := tf.Init(context.Background()) | ||
if err != nil { | ||
t.Logf("error initializing: %s", err) | ||
|
||
// allow for invalid config errors only here | ||
// 0.13 will return this, 0.12 will not | ||
// unsure why 0.12 terraform init does not have a non-zero exit code for syntax problems | ||
var confErr *tfexec.ErrConfigInvalid | ||
if !errors.As(err, &confErr) { | ||
t.Fatalf("expected err ErrConfigInvalid, got %T: %s", err, err) | ||
} | ||
} | ||
|
||
actual, err := tf.Validate(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// reset byte locations in actual as CRLF issues render them off between operating systems | ||
cleanActual := []tfexec.Diagnostic{} | ||
for _, diag := range actual.Diagnostics { | ||
diag.Range.Start.Byte = 0 | ||
diag.Range.End.Byte = 0 | ||
cleanActual = append(cleanActual, diag) | ||
} | ||
|
||
assert.Equal(t, []tfexec.Diagnostic{ | ||
{ | ||
Severity: "error", | ||
Summary: "Unsupported block type", | ||
Detail: "Blocks of type \"bad_block\" are not expected here.", | ||
Range: tfexec.Range{ | ||
Filename: "main.tf", | ||
Start: tfexec.Pos{ | ||
Line: 1, | ||
Column: 1, | ||
}, | ||
End: tfexec.Pos{ | ||
Line: 1, | ||
Column: 10, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Severity: "error", | ||
Summary: "Unsupported argument", | ||
Detail: "An argument named \"bad_attribute\" is not expected here.", | ||
Range: tfexec.Range{ | ||
Filename: "main.tf", | ||
Start: tfexec.Pos{ | ||
Line: 5, | ||
Column: 5, | ||
}, | ||
End: tfexec.Pos{ | ||
Line: 5, | ||
Column: 18, | ||
}, | ||
}, | ||
}, | ||
}, cleanActual) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package tfexec | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Validate represents the validate subcommand to the Terraform CLI. The -json | ||
// flag support was added in 0.12.0, so this will not work on earlier versions. | ||
func (tf *Terraform) Validate(ctx context.Context) (*Validation, error) { | ||
err := tf.compatible(ctx, tf0_12_0, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("terraform validate -json was added in 0.12.0: %w", err) | ||
} | ||
|
||
cmd := tf.buildTerraformCmd(ctx, "validate", "-no-color", "-json") | ||
|
||
var outbuf = bytes.Buffer{} | ||
cmd.Stdout = &outbuf | ||
|
||
err = tf.runTerraformCmd(cmd) | ||
// TODO: this command should not exit 1 if you pass -json as its hard to differentiate other errors | ||
if err != nil && cmd.ProcessState.ExitCode() != 1 { | ||
return nil, err | ||
} | ||
|
||
var out Validation | ||
jsonErr := json.Unmarshal(outbuf.Bytes(), &out) | ||
if jsonErr != nil { | ||
// the original call was possibly bad, if it has an error, actually just return that | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil, jsonErr | ||
} | ||
|
||
return &out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tfexec | ||
|
||
// TODO: move these types to terraform-json | ||
|
||
type Validation struct { | ||
Valid bool `json:"valid"` | ||
ErrorCount int `json:"error_count"` | ||
WarningCount int `json:"warning_count"` | ||
|
||
Diagnostics []Diagnostic `json:"diagnostics"` | ||
} | ||
|
||
type Diagnostic struct { | ||
Severity string `json:"severity"` | ||
Summary string `json:"summary"` | ||
Detail string `json:"detail"` | ||
Range Range `json:"range"` | ||
} | ||
|
||
type Range struct { | ||
Filename string `json:"filename"` | ||
Start Pos `json:"start"` | ||
End Pos `json:"end"` | ||
} | ||
|
||
type Pos struct { | ||
Line int `json:"line"` | ||
Column int `json:"column"` | ||
Byte int `json:"byte"` | ||
} |