-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathexample.go
117 lines (102 loc) · 3.58 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package example
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"github.com/linuxboot/contest/pkg/event"
"github.com/linuxboot/contest/pkg/event/testevent"
"github.com/linuxboot/contest/pkg/logging"
"github.com/linuxboot/contest/pkg/target"
"github.com/linuxboot/contest/pkg/test"
"github.com/linuxboot/contest/plugins/teststeps"
)
// Name is the name used to look this plugin up.
var Name = "Example"
// Params this step accepts.
const (
// Fail this percentage of targets at random.
FailPctParam = "FailPct"
)
// events that we may emit during the plugin's lifecycle. This is used in Events below.
// Note that you don't normally need to emit start/finish/cancellation events as
// these are emitted automatically by the framework.
const (
StartedEvent = event.Name("ExampleStartedEvent")
FinishedEvent = event.Name("ExampleFinishedEvent")
FailedEvent = event.Name("ExampleFailedEvent")
)
// Events defines the events that a TestStep is allow to emit. Emitting an event
// that is not registered here will cause the plugin to terminate with an error.
var Events = []event.Name{StartedEvent, FinishedEvent, FailedEvent}
// Step is an example implementation of a TestStep which simply
// consumes Targets in input and pipes them to the output or error channel
// with intermediate buffering.
type Step struct {
failPct int64
}
// Name returns the name of the Step
func (ts Step) Name() string {
return Name
}
func (ts *Step) shouldFail(t *target.Target) bool {
if ts.failPct > 0 {
roll := rand.Int63n(101)
return (roll <= ts.failPct)
}
return false
}
// Run executes the example step.
func (ts *Step) Run(
ctx context.Context,
ch test.TestStepChannels,
ev testevent.Emitter,
stepsVars test.StepsVariables,
params test.TestStepParameters,
resumeState json.RawMessage,
) (json.RawMessage, error) {
f := func(ctx context.Context, target *target.Target) error {
logging.Infof(ctx, "Executing on target %s", target)
// NOTE: you may want more robust error handling here, possibly just
// logging the error, or a retry mechanism. Returning an error
// here means failing the entire job.
if err := ev.Emit(ctx, testevent.Data{EventName: StartedEvent, Target: target, Payload: nil}); err != nil {
return fmt.Errorf("failed to emit start event: %v", err)
}
if ts.shouldFail(target) {
if err := ev.Emit(ctx, testevent.Data{EventName: FailedEvent, Target: target, Payload: nil}); err != nil {
return fmt.Errorf("failed to emit finished event: %v", err)
}
return fmt.Errorf("target failed")
} else {
if err := ev.Emit(ctx, testevent.Data{EventName: FinishedEvent, Target: target, Payload: nil}); err != nil {
return fmt.Errorf("failed to emit failed event: %v", err)
}
}
return nil
}
return teststeps.ForEachTarget(Name, ctx, ch, f)
}
// ValidateParameters validates the parameters associated to the TestStep
func (ts *Step) ValidateParameters(_ context.Context, params test.TestStepParameters) error {
if params.GetOne(FailPctParam).String() != "" {
if pct, err := params.GetInt(FailPctParam); err == nil {
ts.failPct = pct
} else {
return fmt.Errorf("invalid FailPct: %w", err)
}
}
return nil
}
// New initializes and returns a new ExampleTestStep.
func New() test.TestStep {
return &Step{}
}
// Load returns the name, factory and events which are needed to register the step.
func Load() (string, test.TestStepFactory, []event.Name) {
return Name, New, Events
}