Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/plugintest pkg (formerly terraform-plugin-test) #578

Merged
merged 4 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02 // indirect
github.com/hashicorp/go-getter v1.5.0 // indirect
github.com/hashicorp/go-hclog v0.9.2
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/go-plugin v1.3.0
Expand All @@ -28,7 +28,6 @@ require (
github.com/hashicorp/logutils v1.0.0
github.com/hashicorp/terraform-exec v0.10.0
github.com/hashicorp/terraform-json v0.5.0
github.com/hashicorp/terraform-plugin-test/v2 v2.1.2
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba
github.com/kylelemons/godebug v1.1.0 // indirect
Expand All @@ -38,7 +37,6 @@ require (
github.com/mitchellh/mapstructure v1.1.2
github.com/mitchellh/reflectwalk v1.0.1
github.com/pierrec/lz4 v2.0.5+incompatible
github.com/ulikunitz/xz v0.5.7 // indirect
github.com/vmihailenco/msgpack v4.0.1+incompatible // indirect
github.com/zclconf/go-cty v1.2.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUK
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBMaudVLy8fmjf9Npq1dq9RalhveqZG5w/yz3mHWs=
github.com/hashicorp/go-getter v1.4.0 h1:ENHNi8494porjD0ZhIrjlAHnveSFhY7hvOJrV/fsKkw=
github.com/hashicorp/go-getter v1.4.0/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY=
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02 h1:l1KB3bHVdvegcIf5upQ5mjcHjs2qsWnKh4Yr9xgIuu8=
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY=
github.com/hashicorp/go-getter v1.5.0 h1:ciWJaeZWSMbc5OiLMpKp40MKFPqO44i0h3uyfXPBkkk=
github.com/hashicorp/go-getter v1.5.0/go.mod h1:a7z7NPPfNQpJWcn4rSWFtdrSldqLdLPEF3d8nFMsSLM=
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
Expand All @@ -195,8 +195,6 @@ github.com/hashicorp/terraform-exec v0.10.0 h1:3nh/1e3u9gYRUQGOKWp/8wPR7ABlL2F14
github.com/hashicorp/terraform-exec v0.10.0/go.mod h1:tOT8j1J8rP05bZBGWXfMyU3HkLi1LWyqL3Bzsc3CJjo=
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.1.2 h1:p96IIn+XpvVjw7AtN8y9MKxn0x69S7wtbGf7JgDJoIk=
github.com/hashicorp/terraform-plugin-test/v2 v2.1.2/go.mod h1:jerO5mrd+jVNALy8aiq+VZOg/CR8T2T1QR3jd6JKGOI=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
Expand Down Expand Up @@ -278,8 +276,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.7 h1:YvTNdFzX6+W5m9msiYg/zpkSURPPtOlzbqYjrFn7Yt4=
github.com/ulikunitz/xz v0.5.7/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU=
github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
4 changes: 2 additions & 2 deletions helper/resource/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
grpcplugin "github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/plugin"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
proto "github.com/hashicorp/terraform-plugin-sdk/v2/internal/tfplugin5"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
tftest "github.com/hashicorp/terraform-plugin-test/v2"
testing "github.com/mitchellh/go-testing-interface"
)

func runProviderCommand(t testing.T, f func() error, wd *tftest.WorkingDir, factories map[string]func() (*schema.Provider, error)) error {
func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir, factories map[string]func() (*schema.Provider, error)) error {
// don't point to this as a test failure location
// point to whatever called it
t.Helper()
Expand Down
6 changes: 3 additions & 3 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"strings"

"github.com/hashicorp/go-multierror"
tftest "github.com/hashicorp/terraform-plugin-test/v2"
testing "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/addrs"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

Expand Down Expand Up @@ -534,8 +534,8 @@ func Test(t testing.T, c TestCase) {
if err != nil {
t.Fatalf("Error getting working dir: %s", err)
}
helper := tftest.AutoInitProviderHelper(sourceDir)
defer func(helper *tftest.Helper) {
helper := plugintest.AutoInitProviderHelper(sourceDir)
defer func(helper *plugintest.Helper) {
err := helper.Close()
if err != nil {
log.Printf("Error cleaning up temporary test files: %s", err)
Expand Down
58 changes: 40 additions & 18 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ import (

"github.com/davecgh/go-spew/spew"
tfjson "github.com/hashicorp/terraform-json"
tftest "github.com/hashicorp/terraform-plugin-test/v2"
testing "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func runPostTestDestroy(t testing.T, c TestCase, wd *tftest.WorkingDir, factories map[string]func() (*schema.Provider, error), statePreDestroy *terraform.State) error {
func runPostTestDestroy(t testing.T, c TestCase, wd *plugintest.WorkingDir, factories map[string]func() (*schema.Provider, error), statePreDestroy *terraform.State) error {
t.Helper()

err := runProviderCommand(t, func() error {
wd.RequireDestroy(t)
return nil
return wd.Destroy()
}, wd, factories)
if err != nil {
return err
Expand All @@ -35,7 +34,7 @@ func runPostTestDestroy(t testing.T, c TestCase, wd *tftest.WorkingDir, factorie
return nil
}

func runNewTest(t testing.T, c TestCase, helper *tftest.Helper) {
func runNewTest(t testing.T, c TestCase, helper *plugintest.Helper) {
t.Helper()

spewConf := spew.NewDefaultConfig()
Expand All @@ -44,8 +43,12 @@ func runNewTest(t testing.T, c TestCase, helper *tftest.Helper) {

defer func() {
var statePreDestroy *terraform.State
err := runProviderCommand(t, func() error {
statePreDestroy = getState(t, wd)
var err error
err = runProviderCommand(t, func() error {
statePreDestroy, err = getState(t, wd)
if err != nil {
return err
}
return nil
}, wd, c.ProviderFactories)
if err != nil {
Expand All @@ -68,10 +71,12 @@ func runNewTest(t testing.T, c TestCase, helper *tftest.Helper) {
t.Fatal(err)
}

wd.RequireSetConfig(t, providerCfg)
err = wd.SetConfig(providerCfg)
if err != nil {
t.Fatalf("Error setting test config: %s", err)
}
err = runProviderCommand(t, func() error {
wd.RequireInit(t)
return nil
return wd.Init()
}, wd, c.ProviderFactories)
if err != nil {
t.Fatalf("Error running init: %s", err.Error())
Expand Down Expand Up @@ -137,15 +142,18 @@ func runNewTest(t testing.T, c TestCase, helper *tftest.Helper) {
}
}

func getState(t testing.T, wd *tftest.WorkingDir) *terraform.State {
func getState(t testing.T, wd *plugintest.WorkingDir) (*terraform.State, error) {
t.Helper()

jsonState := wd.RequireState(t)
jsonState, err := wd.State()
if err != nil {
return nil, err
}
state, err := shimStateFromJson(jsonState)
if err != nil {
t.Fatal(err)
}
return state
return state, nil
}

func stateIsEmpty(state *terraform.State) bool {
Expand All @@ -169,7 +177,7 @@ func planIsEmpty(plan *tfjson.Plan) bool {
return true
}

func testIDRefresh(c TestCase, t testing.T, wd *tftest.WorkingDir, step TestStep, r *terraform.ResourceState) error {
func testIDRefresh(c TestCase, t testing.T, wd *plugintest.WorkingDir, step TestStep, r *terraform.ResourceState) error {
t.Helper()

spewConf := spew.NewDefaultConfig()
Expand All @@ -187,13 +195,27 @@ func testIDRefresh(c TestCase, t testing.T, wd *tftest.WorkingDir, step TestStep
if err != nil {
return err
}
wd.RequireSetConfig(t, cfg)
defer wd.RequireSetConfig(t, step.Config)
err = wd.SetConfig(cfg)
if err != nil {
t.Fatalf("Error setting import test config: %s", err)
}
defer func() {
err = wd.SetConfig(step.Config)
if err != nil {
t.Fatalf("Error resetting test config: %s", err)
}
}()

// Refresh!
err = runProviderCommand(t, func() error {
wd.RequireRefresh(t)
state = getState(t, wd)
err = wd.Refresh()
if err != nil {
t.Fatalf("Error running terraform refresh: %s", err)
}
state, err = getState(t, wd)
if err != nil {
return err
}
return nil
}, wd, c.ProviderFactories)
if err != nil {
Expand Down
27 changes: 20 additions & 7 deletions helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import (
"fmt"

tfjson "github.com/hashicorp/terraform-json"
tftest "github.com/hashicorp/terraform-plugin-test/v2"
testing "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func testStepNewConfig(t testing.T, c TestCase, wd *tftest.WorkingDir, step TestStep) error {
func testStepNewConfig(t testing.T, c TestCase, wd *plugintest.WorkingDir, step TestStep) error {
t.Helper()

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

if !step.Destroy {
var state *terraform.State
err := runProviderCommand(t, func() error {
state = getState(t, wd)
var err error
err = runProviderCommand(t, func() error {
state, err = getState(t, wd)
if err != nil {
return err
}
return nil
}, wd, c.ProviderFactories)
if err != nil {
Expand Down Expand Up @@ -64,7 +68,10 @@ func testStepNewConfig(t testing.T, c TestCase, wd *tftest.WorkingDir, step Test
// check function
var stateBeforeApplication *terraform.State
err = runProviderCommand(t, func() error {
stateBeforeApplication = getState(t, wd)
stateBeforeApplication, err = getState(t, wd)
if err != nil {
return err
}
return nil
}, wd, c.ProviderFactories)
if err != nil {
Expand All @@ -85,7 +92,10 @@ func testStepNewConfig(t testing.T, c TestCase, wd *tftest.WorkingDir, step Test
// Get the new state
var state *terraform.State
err = runProviderCommand(t, func() error {
state = getState(t, wd)
state, err = getState(t, wd)
if err != nil {
return err
}
return nil
}, wd, c.ProviderFactories)
if err != nil {
Expand Down Expand Up @@ -194,7 +204,10 @@ func testStepNewConfig(t testing.T, c TestCase, wd *tftest.WorkingDir, step Test
// empty, find the first resource and test it.
var state *terraform.State
err = runProviderCommand(t, func() error {
state = getState(t, wd)
state, err = getState(t, wd)
if err != nil {
return err
}
return nil
}, wd, c.ProviderFactories)
if err != nil {
Expand Down
25 changes: 17 additions & 8 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"strings"

"github.com/davecgh/go-spew/spew"
tftest "github.com/hashicorp/terraform-plugin-test/v2"
testing "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func testStepNewImportState(t testing.T, c TestCase, helper *tftest.Helper, wd *tftest.WorkingDir, step TestStep, cfg string) error {
func testStepNewImportState(t testing.T, c TestCase, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfg string) error {
t.Helper()

spewConf := spew.NewDefaultConfig()
Expand All @@ -23,8 +23,12 @@ func testStepNewImportState(t testing.T, c TestCase, helper *tftest.Helper, wd *

// get state from check sequence
var state *terraform.State
err := runProviderCommand(t, func() error {
state = getState(t, wd)
var err error
err = runProviderCommand(t, func() error {
state, err = getState(t, wd)
if err != nil {
return err
}
return nil
}, wd, c.ProviderFactories)
if err != nil {
Expand Down Expand Up @@ -60,11 +64,13 @@ func testStepNewImportState(t testing.T, c TestCase, helper *tftest.Helper, wd *
}
importWd := helper.RequireNewWorkingDir(t)
defer importWd.Close()
importWd.RequireSetConfig(t, step.Config)
err = importWd.SetConfig(step.Config)
if err != nil {
t.Fatalf("Error setting test config: %s", err)
}

err = runProviderCommand(t, func() error {
importWd.RequireInit(t)
return nil
return importWd.Init()
}, importWd, c.ProviderFactories)
if err != nil {
t.Fatalf("Error running init: %s", err)
Expand All @@ -79,7 +85,10 @@ func testStepNewImportState(t testing.T, c TestCase, helper *tftest.Helper, wd *

var importState *terraform.State
err = runProviderCommand(t, func() error {
importState = getState(t, importWd)
importState, err = getState(t, importWd)
if err != nil {
return err
}
return nil
}, importWd, c.ProviderFactories)
if err != nil {
Expand Down
Loading