mirror of
https://github.com/zrepl/zrepl.git
synced 2025-06-19 17:27:46 +02:00
platformtest: logging-related refactorings
This commit is contained in:
parent
b422e6f12e
commit
f8f9fd11cd
@ -48,6 +48,8 @@ func main() {
|
|||||||
ex := platformtest.NewEx(logger)
|
ex := platformtest.NewEx(logger)
|
||||||
|
|
||||||
bold := color.New(color.Bold)
|
bold := color.New(color.Bold)
|
||||||
|
boldRed := color.New(color.Bold, color.FgHiRed)
|
||||||
|
boldGreen := color.New(color.Bold, color.FgHiGreen)
|
||||||
for _, c := range tests.Cases {
|
for _, c := range tests.Cases {
|
||||||
// ATTENTION future parallelism must pass c by value into closure!
|
// ATTENTION future parallelism must pass c by value into closure!
|
||||||
err := func() error {
|
err := func() error {
|
||||||
@ -66,14 +68,14 @@ func main() {
|
|||||||
RootDataset: filepath.Join(pool.Name(), "rootds"),
|
RootDataset: filepath.Join(pool.Name(), "rootds"),
|
||||||
}
|
}
|
||||||
c(ctx)
|
c(ctx)
|
||||||
bold.Printf("DONE TEST CASE %s\n", c)
|
|
||||||
fmt.Println()
|
|
||||||
return nil
|
return nil
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bold.Printf("TEST CASE FAILED WITH ERROR:\n")
|
boldRed.Printf("TEST CASE FAILED WITH ERROR:\n")
|
||||||
fmt.Println(err)
|
} else {
|
||||||
|
boldGreen.Printf("DONE TEST CASE %s\n", c)
|
||||||
}
|
}
|
||||||
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ var _ assert.TestingT = (*Context)(nil)
|
|||||||
var _ require.TestingT = (*Context)(nil)
|
var _ require.TestingT = (*Context)(nil)
|
||||||
|
|
||||||
func (c *Context) Errorf(format string, args ...interface{}) {
|
func (c *Context) Errorf(format string, args ...interface{}) {
|
||||||
getLog(c).Printf(format, args...)
|
GetLog(c).Printf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) FailNow() {
|
func (c *Context) FailNow() {
|
||||||
|
@ -32,7 +32,7 @@ func (e *ex) runNoOutput(expectSuccess bool, ctx context.Context, cmd string, ar
|
|||||||
buf, _ := circlog.NewCircularLog(32 << 10)
|
buf, _ := circlog.NewCircularLog(32 << 10)
|
||||||
ecmd.Stdout, ecmd.Stderr = buf, buf
|
ecmd.Stdout, ecmd.Stderr = buf, buf
|
||||||
err := ecmd.Run()
|
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 {
|
if _, ok := err.(*exec.ExitError); err != nil && !ok {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,6 @@ func WithLogger(ctx context.Context, logger Logger) context.Context {
|
|||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLog(ctx context.Context) Logger {
|
func GetLog(ctx context.Context) Logger {
|
||||||
return ctx.Value(contextKeyLogger).(Logger)
|
return ctx.Value(contextKeyLogger).(Logger)
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ type DestroyRootOp struct {
|
|||||||
func (o *DestroyRootOp) Run(ctx context.Context, e Execer) error {
|
func (o *DestroyRootOp) Run(ctx context.Context, e Execer) error {
|
||||||
// early-exit if it doesn't exist
|
// early-exit if it doesn't exist
|
||||||
if err := e.RunExpectSuccessNoOutput(ctx, "zfs", "get", "-H", "name", o.Path); err != nil {
|
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 nil
|
||||||
}
|
}
|
||||||
return e.RunExpectSuccessNoOutput(ctx, "zfs", "destroy", "-r", o.Path)
|
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 := exec.CommandContext(ctx, "/usr/bin/env", "bash", "-c", o.Script)
|
||||||
cmd.Env = os.Environ()
|
cmd.Env = os.Environ()
|
||||||
cmd.Env = append(cmd.Env, fmt.Sprintf("ROOTDS=%s", o.RootDS))
|
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")
|
log.Info("start script")
|
||||||
defer log.Info("script done")
|
defer log.Info("script done")
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
@ -126,7 +126,7 @@ func Run(ctx context.Context, rk RunKind, rootds string, stmtsStr string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
execer := NewEx(getLog(ctx))
|
execer := NewEx(GetLog(ctx))
|
||||||
for _, s := range stmt {
|
for _, s := range stmt {
|
||||||
err := s.Run(ctx, execer)
|
err := s.Run(ctx, execer)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user