From a3e264d57e981c1ef161985ecfa2c558535548d7 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 9 Jul 2024 09:44:55 +0900 Subject: [PATCH 1/5] bonsai --- operator.go | 74 +++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/operator.go b/operator.go index 236120b6..fc200fb7 100644 --- a/operator.go +++ b/operator.go @@ -32,45 +32,41 @@ var errStepSkiped = errors.New("step skipped") var _ otchkiss.Requester = (*operators)(nil) type operator struct { - id string - httpRunners map[string]*httpRunner - dbRunners map[string]*dbRunner - grpcRunners map[string]*grpcRunner - cdpRunners map[string]*cdpRunner - sshRunners map[string]*sshRunner - includeRunners map[string]*includeRunner - steps []*step - store store - desc string - labels []string - useMap bool // Use map syntax in `steps:`. - debug bool - profile bool - interval time.Duration - loop *Loop - // loopIndex - Index of the loop is dynamically recorded at runtime - loopIndex *int - concurrency []string - // root - Root directory of runbook ( rubbook path or working directory ) - root string - t *testing.T - thisT *testing.T - parent *step - force bool - trace bool - waitTimeout time.Duration - failFast bool - included bool - ifCond string - skipTest bool - skipped bool - stdout io.Writer - stderr io.Writer - // Skip some errors for `runn list` - newOnly bool - bookPath string - // Number of steps for `runn list` - numberOfSteps int + id string + httpRunners map[string]*httpRunner + dbRunners map[string]*dbRunner + grpcRunners map[string]*grpcRunner + cdpRunners map[string]*cdpRunner + sshRunners map[string]*sshRunner + includeRunners map[string]*includeRunner + steps []*step + store store + desc string + labels []string + useMap bool // Use map syntax in `steps:`. + debug bool + profile bool + interval time.Duration + loop *Loop + loopIndex *int // Index of the loop is dynamically recorded at runtime + concurrency []string + root string // Root directory of runbook ( rubbook path or working directory ) + t *testing.T + thisT *testing.T + parent *step + force bool + trace bool + waitTimeout time.Duration + failFast bool + included bool + ifCond string + skipTest bool + skipped bool + stdout io.Writer + stderr io.Writer + newOnly bool // Skip some errors for `runn list` + bookPath string + numberOfSteps int // Number of steps for `runn list` beforeFuncs []func(*RunResult) error afterFuncs []func(*RunResult) error sw *stopw.Span From 6211f114cef5529d9a0f31b393a2a871bc34712b Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 9 Jul 2024 09:45:06 +0900 Subject: [PATCH 2/5] runN has no responsibility for setting up profiles. --- operator.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/operator.go b/operator.go index fc200fb7..c54ef412 100644 --- a/operator.go +++ b/operator.go @@ -1453,6 +1453,9 @@ func (ops *operators) RunN(ctx context.Context) (err error) { if ops.t != nil { ops.t.Helper() } + if !ops.profile { + ops.sw.Disable() + } result, err := ops.runN(cctx) ops.mu.Lock() ops.results = append(ops.results, result) @@ -1490,6 +1493,9 @@ func (ops *operators) Init() error { } func (ops *operators) RequestOne(ctx context.Context) error { + if !ops.profile { + ops.sw.Disable() + } result, err := ops.runN(ctx) if err != nil { return err @@ -1598,9 +1604,6 @@ func (ops *operators) runN(ctx context.Context) (*runNResult, error) { if ops.t != nil { ops.t.Helper() } - if !ops.profile { - ops.sw.Disable() - } defer ops.sw.Start().Stop() defer ops.Close() cg, cctx := concgroup.WithContext(ctx) From f49d2d82e93475f1bbfdd7a27ddbbb00ceb06180 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 9 Jul 2024 12:00:10 +0900 Subject: [PATCH 3/5] Use runN in Run as well as RunN. Assuming future implementation of the `needs:` section, even Run cannot guarantee that only one runbook is executed. --- operator.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/operator.go b/operator.go index c54ef412..6815e8f4 100644 --- a/operator.go +++ b/operator.go @@ -884,14 +884,12 @@ func (o *operator) Run(ctx context.Context) (err error) { if !o.profile { o.sw.Disable() } - defer o.sw.Start().Stop() - defer func() { - o.capturers.captureResult(o.trails(), o.Result()) - o.capturers.captureEnd(o.trails(), o.bookPath, o.desc) - o.Close(true) - }() - o.capturers.captureStart(o.trails(), o.bookPath, o.desc) - if err := o.run(cctx); err != nil { + ops := o.toOperators() + result, err := ops.runN(cctx) + ops.mu.Lock() + ops.results = append(ops.results, result) + ops.mu.Unlock() + if err != nil { return err } return nil @@ -1277,6 +1275,21 @@ func (o *operator) skip() error { return nil } +func (o *operator) toOperators() *operators { + sw := stopw.New() + ops := &operators{ + ops: []*operator{o}, + t: o.t, + sw: sw, + profile: o.profile, + kv: newKV(), + concmax: 1, + dbg: o.dbg, + } + ops.dbg.ops = ops // link back to ops + return ops +} + func (o *operator) StepResults() []*StepResult { var results []*StepResult for _, s := range o.steps { From 3f44fce105fa102f6fb623f01d6f133fdbb93680 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 9 Jul 2024 12:25:43 +0900 Subject: [PATCH 4/5] Fix --- operator.go | 3 ++- operator_test.go | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/operator.go b/operator.go index 6815e8f4..aeeb634c 100644 --- a/operator.go +++ b/operator.go @@ -892,7 +892,7 @@ func (o *operator) Run(ctx context.Context) (err error) { if err != nil { return err } - return nil + return result.RunResults[len(result.RunResults)-1].Err } // DumpProfile write run time profile. @@ -1275,6 +1275,7 @@ func (o *operator) skip() error { return nil } +// toOperators convert *operator to *operators. func (o *operator) toOperators() *operators { sw := stopw.New() ops := &operators{ diff --git a/operator_test.go b/operator_test.go index d3d7db62..8780757a 100644 --- a/operator_test.go +++ b/operator_test.go @@ -1094,7 +1094,10 @@ func TestFailWithStepDesc(t *testing.T) { t.Fatal(err) } err = o.Run(ctx) - + if err == nil { + t.Error("expected error but got nil") + return + } if !strings.Contains(err.Error(), tt.expectedSubString) { t.Errorf("expected: %q is contained in result but not.\ngot string: %s", tt.expectedSubString, err.Error()) } From 776930542e8bbb7ccbcf858599860227f9bd01fe Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 9 Jul 2024 12:40:45 +0900 Subject: [PATCH 5/5] bonsai --- operator.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/operator.go b/operator.go index aeeb634c..179f9b9c 100644 --- a/operator.go +++ b/operator.go @@ -44,7 +44,7 @@ type operator struct { desc string labels []string useMap bool // Use map syntax in `steps:`. - debug bool + debug bool // Enable debug mode profile bool interval time.Duration loop *Loop @@ -55,7 +55,7 @@ type operator struct { thisT *testing.T parent *step force bool - trace bool + trace bool // Enable tracing ( e.g. add trace header to HTTP request ) waitTimeout time.Duration failFast bool included bool