Skip to content

Commit

Permalink
tfcheck.execCmd: only log an error for a non-nil error (#2565)
Browse files Browse the repository at this point in the history
Currently, we always log the full output of any executed program, which
results in hyper-verbose logging. Since CI is run with `-v`, GitHub
struggles to load our CI logs. We can get by only logging the full
result of `execCmd` when it actually errors.
  • Loading branch information
iwahbe authored Nov 1, 2024
1 parent d6f4fb7 commit 023e334
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
24 changes: 17 additions & 7 deletions pkg/tests/tfcheck/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,32 @@ import (
"bytes"
"os"
"os/exec"
"strings"

"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/pulcheck"
"github.com/stretchr/testify/require"
)

func (d *TfDriver) execTf(t pulcheck.T, args ...string) ([]byte, error) {
cmd, err := execCmd(t, d.cwd, []string{d.formatReattachEnvVar()}, getTFCommand(), args...)
if stderr := cmd.Stderr.(*bytes.Buffer).String(); len(stderr) > 0 {
t.Logf("%q stderr:\n%s\n", cmd.String(), stderr)
}
return cmd.Stdout.(*bytes.Buffer).Bytes(), err
}

func execCmd(t pulcheck.T, wdir string, environ []string, program string, args ...string) (*exec.Cmd, error) {
t.Logf("%s %s", program, strings.Join(args, " "))
cmd := exec.Command(program, args...)
require.NoError(t, cmd.Err)
var stdout, stderr bytes.Buffer
cmd.Dir = wdir
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, environ...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = wdir
cmd.Env = append(os.Environ(), environ...)
t.Logf("%s", cmd.String())
err := cmd.Run()
t.Logf("error from `%s %s`\n\nStdout:\n%s\n\nStderr:\n%s\n\n",
program, strings.Join(args, " "), stdout.String(), stderr.String())
if err != nil {
t.Logf("error from %q\n\nStdout:\n%s\n\nStderr:\n%s\n\n",
cmd.String(), stdout.String(), stderr.String())
}
return cmd, err
}
31 changes: 11 additions & 20 deletions pkg/tests/tfcheck/tfcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,38 +122,31 @@ func (d *TfDriver) Write(t pulcheck.T, program string) {

func (d *TfDriver) Plan(t pulcheck.T) (*TfPlan, error) {
planFile := filepath.Join(d.cwd, "test.tfplan")
env := []string{d.formatReattachEnvVar()}
tfCmd := getTFCommand()
cm, err := execCmd(t, d.cwd, env, tfCmd, "plan", "-refresh=false", "-out", planFile, "-no-color")
planStdoutBytes, err := d.execTf(t, "plan", "-refresh=false", "-out", planFile, "-no-color")
if err != nil {
return nil, err
}
planStdout := cm.Stdout.(*bytes.Buffer).String()
planStdout = strings.Split(planStdout, "───")[0] // trim unstable output about the plan file
cmd, err := execCmd(t, d.cwd, env, tfCmd, "show", "-json", planFile)
planStdout := strings.Split(string(planStdoutBytes), "───")[0] // trim unstable output about the plan file
stdout, err := d.execTf(t, "show", "-json", planFile)
require.NoError(t, err)
tp := TfPlan{PlanFile: planFile, StdOut: planStdout}
err = json.Unmarshal(cmd.Stdout.(*bytes.Buffer).Bytes(), &tp.RawPlan)
err = json.Unmarshal(stdout, &tp.RawPlan)
require.NoErrorf(t, err, "failed to unmarshal terraform plan")
return &tp, nil
}

func (d *TfDriver) Apply(t pulcheck.T, plan *TfPlan) error {
tfCmd := getTFCommand()
_, err := execCmd(t, d.cwd, []string{d.formatReattachEnvVar()},
tfCmd, "apply", "-auto-approve", "-refresh=false", plan.PlanFile)
_, err := d.execTf(t, "apply", "-auto-approve", "-refresh=false", plan.PlanFile)
return err
}

func (d *TfDriver) Show(t pulcheck.T, planFile string) string {
tfCmd := getTFCommand()
cmd, err := execCmd(t, d.cwd, []string{d.formatReattachEnvVar()}, tfCmd, "show", "-json", planFile)
res, err := d.execTf(t, "show", "-json", planFile)
require.NoError(t, err)
res := cmd.Stdout.(*bytes.Buffer)
buf := bytes.NewBuffer(nil)
err = json.Indent(buf, res.Bytes(), "", " ")
var dst bytes.Buffer
err = json.Indent(&dst, res, "", " ")
require.NoError(t, err)
return buf.String()
return dst.String()
}

func (d *TfDriver) GetState(t pulcheck.T) string {
Expand All @@ -166,11 +159,9 @@ func (d *TfDriver) GetState(t pulcheck.T) string {
}

func (d *TfDriver) GetOutput(t pulcheck.T, outputName string) string {
tfCmd := getTFCommand()
cmd, err := execCmd(t, d.cwd, []string{d.formatReattachEnvVar()}, tfCmd, "output", outputName)
resB, err := d.execTf(t, "output", outputName)
require.NoError(t, err)
res := cmd.Stdout.(*bytes.Buffer).String()
res = strings.TrimSuffix(res, "\n")
res := strings.TrimSuffix(string(resB), "\n")
res = strings.Trim(res, "\"")
return res
}
Expand Down

0 comments on commit 023e334

Please sign in to comment.