From f8f9fd11cd70f5f0ad8b0b0623c61fcae0b86b77 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Mon, 14 Oct 2019 17:32:58 +0200 Subject: [PATCH] platformtest: logging-related refactorings --- platformtest/harness/harness.go | 10 ++++++---- platformtest/platformtest.go | 2 +- platformtest/platformtest_exec.go | 2 +- platformtest/platformtest_logging.go | 2 +- platformtest/platformtest_ops.go | 6 +++--- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/platformtest/harness/harness.go b/platformtest/harness/harness.go index 3a87359..505476d 100644 --- a/platformtest/harness/harness.go +++ b/platformtest/harness/harness.go @@ -48,6 +48,8 @@ func main() { ex := platformtest.NewEx(logger) bold := color.New(color.Bold) + boldRed := color.New(color.Bold, color.FgHiRed) + boldGreen := color.New(color.Bold, color.FgHiGreen) for _, c := range tests.Cases { // ATTENTION future parallelism must pass c by value into closure! err := func() error { @@ -66,14 +68,14 @@ func main() { RootDataset: filepath.Join(pool.Name(), "rootds"), } c(ctx) - bold.Printf("DONE TEST CASE %s\n", c) - fmt.Println() return nil }() if err != nil { - bold.Printf("TEST CASE FAILED WITH ERROR:\n") - fmt.Println(err) + boldRed.Printf("TEST CASE FAILED WITH ERROR:\n") + } else { + boldGreen.Printf("DONE TEST CASE %s\n", c) } + fmt.Println() } } diff --git a/platformtest/platformtest.go b/platformtest/platformtest.go index e31ee97..7b7a1ed 100644 --- a/platformtest/platformtest.go +++ b/platformtest/platformtest.go @@ -16,7 +16,7 @@ var _ assert.TestingT = (*Context)(nil) var _ require.TestingT = (*Context)(nil) func (c *Context) Errorf(format string, args ...interface{}) { - getLog(c).Printf(format, args...) + GetLog(c).Printf(format, args...) } func (c *Context) FailNow() { diff --git a/platformtest/platformtest_exec.go b/platformtest/platformtest_exec.go index 70b9aea..abc3105 100644 --- a/platformtest/platformtest_exec.go +++ b/platformtest/platformtest_exec.go @@ -32,7 +32,7 @@ func (e *ex) runNoOutput(expectSuccess bool, ctx context.Context, cmd string, ar buf, _ := circlog.NewCircularLog(32 << 10) ecmd.Stdout, ecmd.Stderr = buf, buf err := ecmd.Run() - log.Printf("command output: %s", buf.String()) + log.WithField("output", buf.String()).Debug("command output") if _, ok := err.(*exec.ExitError); err != nil && !ok { panic(err) } diff --git a/platformtest/platformtest_logging.go b/platformtest/platformtest_logging.go index 1f797b5..31bf6e8 100644 --- a/platformtest/platformtest_logging.go +++ b/platformtest/platformtest_logging.go @@ -19,6 +19,6 @@ func WithLogger(ctx context.Context, logger Logger) context.Context { return ctx } -func getLog(ctx context.Context) Logger { +func GetLog(ctx context.Context) Logger { return ctx.Value(contextKeyLogger).(Logger) } diff --git a/platformtest/platformtest_ops.go b/platformtest/platformtest_ops.go index 0c2c6c0..15ae0ef 100644 --- a/platformtest/platformtest_ops.go +++ b/platformtest/platformtest_ops.go @@ -39,7 +39,7 @@ type DestroyRootOp struct { func (o *DestroyRootOp) Run(ctx context.Context, e Execer) error { // early-exit if it doesn't exist if err := e.RunExpectSuccessNoOutput(ctx, "zfs", "get", "-H", "name", o.Path); err != nil { - getLog(ctx).WithField("root_ds", o.Path).Info("assume root ds doesn't exist") + GetLog(ctx).WithField("root_ds", o.Path).Info("assume root ds doesn't exist") return nil } return e.RunExpectSuccessNoOutput(ctx, "zfs", "destroy", "-r", o.Path) @@ -94,7 +94,7 @@ func (o *RunOp) Run(ctx context.Context, e Execer) error { cmd := exec.CommandContext(ctx, "/usr/bin/env", "bash", "-c", o.Script) cmd.Env = os.Environ() cmd.Env = append(cmd.Env, fmt.Sprintf("ROOTDS=%s", o.RootDS)) - log := getLog(ctx).WithField("script", o.Script) + log := GetLog(ctx).WithField("script", o.Script) log.Info("start script") defer log.Info("script done") output, err := cmd.CombinedOutput() @@ -126,7 +126,7 @@ func Run(ctx context.Context, rk RunKind, rootds string, stmtsStr string) { if err != nil { panic(err) } - execer := NewEx(getLog(ctx)) + execer := NewEx(GetLog(ctx)) for _, s := range stmt { err := s.Run(ctx, execer) if err == nil {