Skip to content

Commit

Permalink
Merge 7769305 into 2f0c5fb
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Jul 9, 2024
2 parents 2f0c5fb + 7769305 commit 958a98c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 52 deletions.
115 changes: 64 additions & 51 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 // Enable debug mode
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 // Enable tracing ( e.g. add trace header to HTTP request )
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
Expand Down Expand Up @@ -888,17 +884,15 @@ 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
return result.RunResults[len(result.RunResults)-1].Err
}

// DumpProfile write run time profile.
Expand Down Expand Up @@ -1281,6 +1275,22 @@ func (o *operator) skip() error {
return nil
}

// toOperators convert *operator to *operators.
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 {
Expand Down Expand Up @@ -1457,6 +1467,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)
Expand Down Expand Up @@ -1494,6 +1507,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
Expand Down Expand Up @@ -1602,9 +1618,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)
Expand Down
5 changes: 4 additions & 1 deletion operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit 958a98c

Please sign in to comment.