From dec3708e96b499c4fc3f563953611c0ed1de0a68 Mon Sep 17 00:00:00 2001 From: Katy Moe Date: Wed, 9 Sep 2020 16:38:19 +0100 Subject: [PATCH] check SetEnv errs throughout --- working_dir.go | 62 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/working_dir.go b/working_dir.go index 69ff2fe..650ff62 100644 --- a/working_dir.go +++ b/working_dir.go @@ -134,7 +134,10 @@ func (wd *WorkingDir) SetConfig(cfg string) error { tf.SetLogPath(p) } - tf.SetEnv(wd.buildEnv()) + err = tf.SetEnv(wd.buildEnv()) + if err != nil { + return err + } wd.configDir = configDir wd.tf = tf @@ -205,7 +208,10 @@ func (wd *WorkingDir) Init() error { return fmt.Errorf("must call SetConfig before Init") } - wd.tf.SetEnv(wd.buildEnv()) + err := wd.tf.SetEnv(wd.buildEnv()) + if err != nil { + return err + } return wd.tf.Init(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Dir(wd.configDir)) } @@ -226,7 +232,10 @@ 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.SetEnv(wd.buildEnv()) + if err != nil { + return err + } _, err := wd.tf.Plan(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Out("tfplan"), tfexec.Dir(wd.configDir)) return err } @@ -244,7 +253,10 @@ 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.SetEnv(wd.buildEnv()) + if err != nil { + return err + } _, 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 } @@ -268,7 +280,10 @@ func (wd *WorkingDir) Apply() error { } args = append(args, tfexec.DirOrPlan(configDir)) } - wd.tf.SetEnv(wd.buildEnv()) + err := wd.tf.SetEnv(wd.buildEnv()) + if err != nil { + return err + } return wd.tf.Apply(context.Background(), args...) } @@ -288,7 +303,10 @@ 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()) + err := wd.tf.SetEnv(wd.buildEnv()) + if err != nil { + return err + } return wd.tf.Destroy(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.Refresh(false), tfexec.Dir(wd.configDir)) } @@ -322,7 +340,10 @@ func (wd *WorkingDir) SavedPlan() (*tfjson.Plan, error) { return nil, fmt.Errorf("there is no current saved plan") } - wd.tf.SetEnv(wd.buildEnv()) + err := wd.tf.SetEnv(wd.buildEnv()) + if err != nil { + return nil, err + } return wd.tf.ShowPlanFile(context.Background(), wd.planFilename(), tfexec.Reattach(wd.reattachInfo)) } @@ -349,10 +370,13 @@ func (wd *WorkingDir) SavedPlanStdout() (string, error) { var ret bytes.Buffer - wd.tf.SetEnv(wd.buildEnv()) + err := wd.tf.SetEnv(wd.buildEnv()) + if err != nil { + return "", err + } wd.tf.SetStdout(&ret) defer wd.tf.SetStdout(ioutil.Discard) - _, err := wd.tf.ShowPlanFile(context.Background(), wd.planFilename(), tfexec.Reattach(wd.reattachInfo)) + _, err = wd.tf.ShowPlanFile(context.Background(), wd.planFilename(), tfexec.Reattach(wd.reattachInfo)) if err != nil { return "", err } @@ -376,7 +400,10 @@ 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()) + err := wd.tf.SetEnv(wd.buildEnv()) + if err != nil { + return nil, err + } return wd.tf.Show(context.Background(), tfexec.Reattach(wd.reattachInfo)) } @@ -394,7 +421,10 @@ 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()) + err := wd.tf.SetEnv(wd.buildEnv()) + if err != nil { + return err + } return wd.tf.Import(context.Background(), resource, id, tfexec.Config(wd.configDir), tfexec.Reattach(wd.reattachInfo)) } @@ -410,7 +440,10 @@ func (wd *WorkingDir) RequireImport(t TestControl, resource, id string) { // Refresh runs terraform refresh func (wd *WorkingDir) Refresh() error { - wd.tf.SetEnv(wd.buildEnv()) + err := wd.tf.SetEnv(wd.buildEnv()) + if err != nil { + return err + } return wd.tf.Refresh(context.Background(), tfexec.Reattach(wd.reattachInfo), tfexec.State(filepath.Join(wd.baseDir, "terraform.tfstate")), tfexec.Dir(wd.configDir)) } @@ -428,7 +461,10 @@ 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()) + err := wd.tf.SetEnv(wd.buildEnv()) + if err != nil { + return err + } return wd.tf.ProvidersSchema(context.Background()) }