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

Make binary testing compatible with Terraform 0.15 #694

Merged
merged 3 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/hcl/v2 v2.3.0
github.com/hashicorp/logutils v1.0.0
github.com/hashicorp/terraform-exec v0.12.0
github.com/hashicorp/terraform-exec v0.13.0
github.com/hashicorp/terraform-json v0.8.0
github.com/hashicorp/terraform-plugin-go v0.2.1
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ github.com/hashicorp/hcl/v2 v2.3.0 h1:iRly8YaMwTBAKhn1Ybk7VSdzbnopghktCD031P8ggU
github.com/hashicorp/hcl/v2 v2.3.0/go.mod h1:d+FwDBbOLvpAM3Z6J7gPj/VoAGkNe/gm352ZhjJ/Zv8=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.12.0 h1:Tb1VC2gqArl9EJziJjoazep2MyxMk00tnNKV/rgMba0=
github.com/hashicorp/terraform-exec v0.12.0/go.mod h1:SGhto91bVRlgXQWcJ5znSz+29UZIa8kpBbkGwQ+g9E8=
github.com/hashicorp/terraform-exec v0.13.0 h1:1Pth+pdWJAufJuWWjaVOVNEkoRTOjGn3hQpAqj4aPdg=
github.com/hashicorp/terraform-exec v0.13.0/go.mod h1:SGhto91bVRlgXQWcJ5znSz+29UZIa8kpBbkGwQ+g9E8=
github.com/hashicorp/terraform-json v0.8.0 h1:XObQ3PgqU52YLQKEaJ08QtUshAfN3yu4u8ebSW0vztc=
github.com/hashicorp/terraform-json v0.8.0/go.mod h1:3defM4kkMfttwiE7VakJDwCd4R+umhSQnvJwORXbprE=
github.com/hashicorp/terraform-plugin-go v0.2.1 h1:EW/R8bB2Zbkjmugzsy1d27yS8/0454b3MtYHkzOknqA=
Expand Down
6 changes: 0 additions & 6 deletions internal/plugintest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ func (h *Helper) NewWorkingDir() (*WorkingDir, error) {
return nil, err
}

// symlink the provider source files into the base directory
err = symlinkDirectoriesOnly(h.sourceDir, dir)
if err != nil {
return nil, err
}

return &WorkingDir{
h: h,
baseDir: dir,
Expand Down
34 changes: 16 additions & 18 deletions internal/plugintest/working_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ func (wd *WorkingDir) SetConfig(cfg string) error {
return err
}

tf, err := tfexec.NewTerraform(wd.baseDir, wd.terraformExec)
// symlink the provider source files into the config directory
// e.g. testdata
err = symlinkDirectoriesOnly(wd.h.sourceDir, configDir)
if err != nil {
return err
}

tf, err := tfexec.NewTerraform(configDir, wd.terraformExec)
if err != nil {
return err
}
Expand Down Expand Up @@ -137,7 +144,7 @@ func (wd *WorkingDir) SetConfig(cfg string) error {
// Any remote objects tracked by the state are not destroyed first, so this
// will leave them dangling in the remote system.
func (wd *WorkingDir) ClearState() error {
err := os.Remove(filepath.Join(wd.baseDir, "terraform.tfstate"))
err := os.Remove(filepath.Join(wd.configDir, "terraform.tfstate"))
if os.IsNotExist(err) {
return nil
}
Expand All @@ -160,24 +167,24 @@ func (wd *WorkingDir) Init() error {
return fmt.Errorf("must call SetConfig before Init")
}

return wd.tf.Init(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Dir(wd.configDir))
return wd.tf.Init(context.Background(), tfexec.Reattach(wd.reattachInfo))
}

func (wd *WorkingDir) planFilename() string {
return filepath.Join(wd.baseDir, "tfplan")
return filepath.Join(wd.configDir, "tfplan")
}

// CreatePlan runs "terraform plan" to create a saved plan file, which if successful
// will then be used for the next call to Apply.
func (wd *WorkingDir) CreatePlan() error {
_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out("tfplan"), tfexec.Dir(wd.configDir))
_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out("tfplan"))
return err
}

// CreateDestroyPlan runs "terraform plan -destroy" to create a saved plan
// file, which if successful will then be used for the next call to Apply.
func (wd *WorkingDir) CreateDestroyPlan() error {
_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out("tfplan"), tfexec.Destroy(true), tfexec.Dir(wd.configDir))
_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out("tfplan"), tfexec.Destroy(true))
return err
}

Expand All @@ -189,17 +196,8 @@ func (wd *WorkingDir) Apply() error {
args := []tfexec.ApplyOption{tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false)}
if wd.HasSavedPlan() {
args = append(args, tfexec.DirOrPlan("tfplan"))
} else {
// we need to use a relative config dir here or we get an
// error about Terraform not having any configuration. See
// https://github.com/hashicorp/terraform-plugin-sdk/issues/495
// for more info.
configDir, err := wd.relativeConfigDir()
if err != nil {
return err
}
args = append(args, tfexec.DirOrPlan(configDir))
}

return wd.tf.Apply(context.Background(), args...)
}

Expand All @@ -209,7 +207,7 @@ func (wd *WorkingDir) Apply() error {
// If destroy fails then remote objects might still exist, and continue to
// exist after a particular test is concluded.
func (wd *WorkingDir) Destroy() error {
return wd.tf.Destroy(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Dir(wd.configDir))
return wd.tf.Destroy(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false))
}

// HasSavedPlan returns true if there is a saved plan in the working directory. If
Expand Down Expand Up @@ -267,7 +265,7 @@ func (wd *WorkingDir) Import(resource, id string) error {

// Refresh runs terraform refresh
func (wd *WorkingDir) Refresh() error {
return wd.tf.Refresh(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.State(filepath.Join(wd.baseDir, "terraform.tfstate")), tfexec.Dir(wd.configDir))
return wd.tf.Refresh(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.State(filepath.Join(wd.configDir, "terraform.tfstate")))
}

// Schemas returns an object describing the provider schemas.
Expand Down