platformtest: logging-related refactorings

This commit is contained in:
Christian Schwarz 2019-10-14 17:32:58 +02:00
parent b422e6f12e
commit f8f9fd11cd
5 changed files with 12 additions and 10 deletions

View File

@ -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()
}
}

View File

@ -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() {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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 {