add command test for errored plan

This commit is contained in:
James Bardin 2022-12-14 16:23:31 -05:00
parent bb5f360747
commit 76d5e4a9cb
2 changed files with 64 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform/internal/addrs"
backendinit "github.com/hashicorp/terraform/internal/backend/init"
"github.com/hashicorp/terraform/internal/checks"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/providers"
@ -275,6 +276,54 @@ func TestPlan_outPathNoChange(t *testing.T) {
}
}
func TestPlan_outPathWithError(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-fail-condition"), td)
defer testChdir(t, td)()
outPath := filepath.Join(td, "test.plan")
p := planFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.NullVal(cty.EmptyObject),
}
args := []string{
"-out", outPath,
}
code := c.Run(args)
output := done(t)
if code == 0 {
t.Fatal("expected non-zero exit status", output)
}
plan := testReadPlan(t, outPath) // will call t.Fatal itself if the file cannot be read
if !plan.Errored {
t.Fatal("plan should be marked with Errored")
}
if plan.Checks == nil {
t.Fatal("plan contains no checks")
}
// the checks should only contain one failure
results := plan.Checks.ConfigResults.Elements()
if len(results) != 1 {
t.Fatal("incorrect number of check results", len(results))
}
if results[0].Value.Status != checks.StatusFail {
t.Errorf("incorrect status, got %s", results[0].Value.Status)
}
}
// When using "-out" with a backend, the plan should encode the backend config
func TestPlan_outBackend(t *testing.T) {
// Create a temporary working directory that is empty

View File

@ -0,0 +1,15 @@
locals {
ami = "bar"
}
resource "test_instance" "foo" {
ami = local.ami
lifecycle {
precondition {
// failing condition
condition = local.ami != "bar"
error_message = "ami is bar"
}
}
}