Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Commit

Permalink
approximate previous behaviour of wd.Setenv
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed Sep 7, 2020
1 parent 00df159 commit a0ac7b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
18 changes: 18 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tftest
import (
"os"
"path/filepath"
"strings"
)

func symlinkFile(src string, dest string) (err error) {
Expand Down Expand Up @@ -93,3 +94,20 @@ func symlinkDirectoriesOnly(srcDir string, destDir string) (err error) {
}
return
}

func envMap(environ []string) map[string]string {
env := map[string]string{}
for _, ev := range environ {
parts := strings.SplitN(ev, "=", 2)
if len(parts) == 0 {
continue
}
k := parts[0]
v := ""
if len(parts) == 2 {
v = parts[1]
}
env[k] = v
}
return env
}
26 changes: 26 additions & 0 deletions working_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ func (wd *WorkingDir) relativeConfigDir() (string, error) {
return relPath, nil
}

func (wd *WorkingDir) buildEnv() map[string]string {
var env []string
for _, e := range os.Environ() {
env = append(env, e)
}

for k, v := range wd.env {
env = append(env, k+"="+v)
}

return envMap(env)
}

// SetConfig sets a new configuration for the working directory.
//
// This must be called at least once before any call to Init, Plan, Apply, or
Expand Down Expand Up @@ -113,6 +126,8 @@ func (wd *WorkingDir) SetConfig(cfg string) error {
tf.SetLogPath(p)
}

tf.SetEnv(wd.buildEnv())

wd.configDir = configDir
wd.tf = tf

Expand Down Expand Up @@ -182,6 +197,7 @@ func (wd *WorkingDir) Init() error {
return fmt.Errorf("must call SetConfig before Init")
}

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

Expand All @@ -202,6 +218,7 @@ func (wd *WorkingDir) planFilename() string {
// 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 {
wd.tf.SetEnv(wd.buildEnv())
_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out("tfplan"), tfexec.Dir(wd.configDir))
return err
}
Expand All @@ -219,6 +236,7 @@ func (wd *WorkingDir) RequireCreatePlan(t TestControl) {
// 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 {
wd.tf.SetEnv(wd.buildEnv())
_, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out("tfplan"), tfexec.Destroy(true), tfexec.Dir(wd.configDir))
return err
}
Expand All @@ -242,6 +260,7 @@ func (wd *WorkingDir) Apply() error {
}
args = append(args, tfexec.DirOrPlan(configDir))
}
wd.tf.SetEnv(wd.buildEnv())
return wd.tf.Apply(context.Background(), args...)
}

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

Expand Down Expand Up @@ -294,6 +314,7 @@ func (wd *WorkingDir) SavedPlan() (*tfjson.Plan, error) {
return nil, fmt.Errorf("there is no current saved plan")
}

wd.tf.SetEnv(wd.buildEnv())
return wd.tf.ShowPlanFile(context.Background(), wd.planFilename(), tfexec.Reattach(wd.reattachInfo))
}

Expand All @@ -320,6 +341,7 @@ func (wd *WorkingDir) SavedPlanStdout() (string, error) {

var ret bytes.Buffer

wd.tf.SetEnv(wd.buildEnv())
wd.tf.SetStdout(&ret)
defer wd.tf.SetStdout(ioutil.Discard)
_, err := wd.tf.ShowPlanFile(context.Background(), wd.planFilename(), tfexec.Reattach(wd.reattachInfo))
Expand All @@ -346,6 +368,7 @@ func (wd *WorkingDir) RequireSavedPlanStdout(t TestControl) string {
//
// If the state cannot be read, State returns an error.
func (wd *WorkingDir) State() (*tfjson.State, error) {
wd.tf.SetEnv(wd.buildEnv())
return wd.tf.Show(context.Background(), tfexec.Reattach(wd.reattachInfo))
}

Expand All @@ -363,6 +386,7 @@ func (wd *WorkingDir) RequireState(t TestControl) *tfjson.State {

// Import runs terraform import
func (wd *WorkingDir) Import(resource, id string) error {
wd.tf.SetEnv(wd.buildEnv())
return wd.tf.Import(context.Background(), resource, id, tfexec.Config(wd.configDir), tfexec.Reattach(wd.reattachInfo))
}

Expand All @@ -378,6 +402,7 @@ func (wd *WorkingDir) RequireImport(t TestControl, resource, id string) {

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

Expand All @@ -395,6 +420,7 @@ func (wd *WorkingDir) RequireRefresh(t TestControl) {
//
// If the schemas cannot be read, Schemas returns an error.
func (wd *WorkingDir) Schemas() (*tfjson.ProviderSchemas, error) {
wd.tf.SetEnv(wd.buildEnv())
return wd.tf.ProvidersSchema(context.Background())
}

Expand Down

0 comments on commit a0ac7b3

Please sign in to comment.