Skip to content

Commit

Permalink
Backport fixes to reattach testing.
Browse files Browse the repository at this point in the history
Backport including step number in error messages, and backport fix to
use correct state when conducting import tests.
  • Loading branch information
paddycarver committed Sep 4, 2020
1 parent 83ea1db commit 1dcf900
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/hashicorp/logutils v1.0.0
github.com/hashicorp/terraform-config-inspect v0.0.0-20191115094559-17f92b0546e8
github.com/hashicorp/terraform-json v0.5.0
github.com/hashicorp/terraform-plugin-test/v2 v2.0.0-20200808000650-7f258174604f
github.com/hashicorp/terraform-plugin-test/v2 v2.0.0
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ github.com/hashicorp/terraform-exec v0.3.0 h1:5WLBsnv9BoEUGlHJZETROZZxw+qO3/TFQE
github.com/hashicorp/terraform-exec v0.3.0/go.mod h1:yKWvMPtkTaHpeAmllw+1qdHZ7E5u+pAZ+x8e2jQF6gM=
github.com/hashicorp/terraform-json v0.5.0 h1:7TV3/F3y7QVSuN4r9BEXqnWqrAyeOtON8f0wvREtyzs=
github.com/hashicorp/terraform-json v0.5.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-plugin-test/v2 v2.0.0-20200808000650-7f258174604f h1:SCzHbwpL0WVmshDLFigKyd/gwoxX8WF493bwx9EhRlc=
github.com/hashicorp/terraform-plugin-test/v2 v2.0.0-20200808000650-7f258174604f/go.mod h1:C6VALgUlvaif+PnHyRGKWPTdQkMJK4NQ20VJolxZLI0=
github.com/hashicorp/terraform-plugin-test/v2 v2.0.0 h1:fYGV3nZvs8KFGKuY2NPAJDMNfVSDHo+U2FGFl3bPv1s=
github.com/hashicorp/terraform-plugin-test/v2 v2.0.0/go.mod h1:C6VALgUlvaif+PnHyRGKWPTdQkMJK4NQ20VJolxZLI0=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596 h1:hjyO2JsNZUKT1ym+FAdlBEkGPevazYsmVgIMw7dVELg=
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
Expand Down
8 changes: 4 additions & 4 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func RunNewTest(t *testing.T, c TestCase, providers map[string]terraform.Resourc
t.Fatal(err)
}
if skip {
log.Printf("[WARN] Skipping step %d", i)
log.Printf("[WARN] Skipping step %d/%d", i+1, len(c.Steps))
continue
}
}
Expand All @@ -117,14 +117,14 @@ func RunNewTest(t *testing.T, c TestCase, providers map[string]terraform.Resourc
err := testStepNewConfig(t, c, wd, step)
if step.ExpectError != nil {
if err == nil {
t.Fatal("Expected an error but got none")
t.Fatalf("Step %d/%d, expected an error but got none", i+1, len(c.Steps))
}
if !step.ExpectError.MatchString(err.Error()) {
t.Fatalf("Expected an error with pattern, no match on: %s", err)
t.Fatalf("Step %d/%d, expected an error with pattern, no match on: %s", i+1, len(c.Steps), err)
}
} else {
if err != nil {
t.Fatal(err)
t.Fatalf("Step %d/%d error: %s", i+1, len(c.Steps), err)
}
}
appliedCfg = step.Config
Expand Down
26 changes: 18 additions & 8 deletions helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"testing"

"github.com/davecgh/go-spew/spew"
tfjson "github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
tftest "github.com/hashicorp/terraform-plugin-test/v2"
Expand All @@ -13,9 +12,6 @@ import (
func testStepNewConfig(t *testing.T, c TestCase, wd *tftest.WorkingDir, step TestStep) error {
t.Helper()

spewConf := spew.NewDefaultConfig()
spewConf.SortKeys = true

var idRefreshCheck *terraform.ResourceState
idRefresh := c.IDRefreshName != ""

Expand Down Expand Up @@ -90,8 +86,15 @@ func testStepNewConfig(t *testing.T, c TestCase, wd *tftest.WorkingDir, step Tes
if step.ExpectNonEmptyPlan {
t.Log("[INFO] Got non-empty plan, as expected")
} else {

t.Fatalf("After applying this test step, the plan was not empty. %s", spewConf.Sdump(plan))
var stdout string
err = runProviderCommand(t, func() error {
stdout = wd.RequireSavedPlanStdout(t)
return nil
}, wd, c.ProviderFactories)
if err != nil {
return err
}
t.Fatalf("After applying this test step, the plan was not empty.\nstdout:\n\n%s", stdout)
}
}

Expand Down Expand Up @@ -126,8 +129,15 @@ func testStepNewConfig(t *testing.T, c TestCase, wd *tftest.WorkingDir, step Tes
if step.ExpectNonEmptyPlan {
t.Log("[INFO] Got non-empty plan, as expected")
} else {

t.Fatalf("After applying this test step and performing a `terraform refresh`, the plan was not empty. %s", spewConf.Sdump(plan))
var stdout string
err = runProviderCommand(t, func() error {
stdout = wd.RequireSavedPlanStdout(t)
return nil
}, wd, c.ProviderFactories)
if err != nil {
return err
}
t.Fatalf("After applying this test step and performing a `terraform refresh`, the plan was not empty.\nstdout\n\n%s", stdout)
}
}

Expand Down
4 changes: 2 additions & 2 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func testStepNewImportState(t *testing.T, c TestCase, wd *tftest.WorkingDir, ste

var importState *terraform.State
err = runProviderCommand(t, func() error {
importState = getState(t, wd)
importState = getState(t, importWd)
return nil
}, wd, c.ProviderFactories)
}, importWd, c.ProviderFactories)
if err != nil {
return fmt.Errorf("Error getting state after import: %v", err)
}
Expand Down

0 comments on commit 1dcf900

Please sign in to comment.