Skip to content

Commit 620e54f

Browse files
committed
[teststep] Use a Load function for plugin registration
1 parent 252b9c8 commit 620e54f

9 files changed

Lines changed: 49 additions & 26 deletions

File tree

cmds/contest/main.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ package main
88
import (
99
"errors"
1010
"flag"
11-
"fmt"
1211
"os"
1312
"os/signal"
1413
"syscall"
1514

16-
"github.com/facebookincubator/contest/pkg/event"
1715
"github.com/facebookincubator/contest/pkg/job"
1816
"github.com/facebookincubator/contest/pkg/jobmanager"
1917
"github.com/facebookincubator/contest/pkg/logging"
@@ -54,24 +52,14 @@ var testFetchers = map[string]test.TestFetcherFactory{
5452
literal.Name: literal.New,
5553
}
5654

57-
var testSteps = map[string]test.TestStepFactory{
58-
echo.Name: echo.New,
59-
slowecho.Name: slowecho.New,
60-
example.Name: example.New,
61-
cmd.Name: cmd.New,
62-
sshcmd.Name: sshcmd.New,
63-
randecho.Name: randecho.New,
64-
terminalexpect.Name: terminalexpect.New,
65-
}
66-
67-
var testStepsEvents = map[string][]event.Name{
68-
echo.Name: echo.Events,
69-
slowecho.Name: slowecho.Events,
70-
example.Name: example.Events,
71-
cmd.Name: cmd.Events,
72-
sshcmd.Name: sshcmd.Events,
73-
randecho.Name: randecho.Events,
74-
terminalexpect.Name: terminalexpect.Events,
55+
var testSteps = []test.TestStepLoader{
56+
echo.Load,
57+
slowecho.Load,
58+
example.Load,
59+
cmd.Load,
60+
sshcmd.Load,
61+
randecho.Load,
62+
terminalexpect.Load,
7563
}
7664

7765
var reporters = map[string]job.ReporterFactory{
@@ -112,12 +100,8 @@ func main() {
112100
}
113101

114102
// Register TestStep plugins
115-
for name, tsfactory := range testSteps {
116-
if _, ok := testStepsEvents[name]; !ok {
117-
err := fmt.Errorf("TestStep %s not associated to any list of events", name)
118-
log.Fatal(err)
119-
}
120-
if err := pluginRegistry.RegisterTestStep(name, tsfactory, testStepsEvents[name]); err != nil {
103+
for _, tsloader := range testSteps {
104+
if err := pluginRegistry.RegisterTestStep(tsloader()); err != nil {
121105
log.Fatal(err)
122106

123107
}

pkg/test/step.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ type TestStepStatus string
6363
// TestStep factories are registered in the plugin registry.
6464
type TestStepFactory func() TestStep
6565

66+
// TestStepLoader is a type representing a function which returns all the
67+
// needed things to be able to load a TestStep.
68+
type TestStepLoader func() (string, TestStepFactory, []event.Name)
69+
6670
// TestStepDescriptor is the definition of a test step matching a test step
6771
// configuration.
6872
type TestStepDescriptor struct {

plugins/teststeps/cmd/cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,8 @@ func (ts *Cmd) CanResume() bool {
121121
func New() test.TestStep {
122122
return &Cmd{}
123123
}
124+
125+
// Load returns the name, factory and events which are needed to register the step.
126+
func Load() (string, test.TestStepFactory, []event.Name) {
127+
return Name, New, Events
128+
}

plugins/teststeps/echo/echo.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func New() test.TestStep {
3333
return &Step{}
3434
}
3535

36+
// Load returns the name, factory and events which are needed to register the step.
37+
func Load() (string, test.TestStepFactory, []event.Name) {
38+
return Name, New, Events
39+
}
40+
3641
// ValidateParameters validates the parameters that will be passed to the Run
3742
// and Resume methods of the test step.
3843
func (e Step) ValidateParameters(params test.TestStepParameters) error {

plugins/teststeps/example/example.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,8 @@ func (ts *Step) CanResume() bool {
113113
func New() test.TestStep {
114114
return &Step{}
115115
}
116+
117+
// Load returns the name, factory and events which are needed to register the step.
118+
func Load() (string, test.TestStepFactory, []event.Name) {
119+
return Name, New, Events
120+
}

plugins/teststeps/randecho/randecho.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ func New() test.TestStep {
3535
return &Step{}
3636
}
3737

38+
// Load returns the name, factory and events which are needed to register the step.
39+
func Load() (string, test.TestStepFactory, []event.Name) {
40+
return Name, New, Events
41+
}
42+
3843
// ValidateParameters validates the parameters that will be passed to the Run
3944
// and Resume methods of the test step.
4045
func (e Step) ValidateParameters(params test.TestStepParameters) error {

plugins/teststeps/slowecho/slowecho.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func New() test.TestStep {
3939
return &Step{}
4040
}
4141

42+
// Load returns the name, factory and events which are needed to register the step.
43+
func Load() (string, test.TestStepFactory, []event.Name) {
44+
return Name, New, Events
45+
}
46+
4247
// Name returns the name of the Step
4348
func (e *Step) Name() string {
4449
return Name

plugins/teststeps/sshcmd/sshcmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,8 @@ func (ts *SSHCmd) CanResume() bool {
259259
func New() test.TestStep {
260260
return &SSHCmd{}
261261
}
262+
263+
// Load returns the name, factory and events which are needed to register the step.
264+
func Load() (string, test.TestStepFactory, []event.Name) {
265+
return Name, New, Events
266+
}

plugins/teststeps/terminalexpect/terminalexpect.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,8 @@ func (ts *TerminalExpect) CanResume() bool {
135135
func New() test.TestStep {
136136
return &TerminalExpect{}
137137
}
138+
139+
// Load returns the name, factory and events which are needed to register the step.
140+
func Load() (string, test.TestStepFactory, []event.Name) {
141+
return Name, New, Events
142+
}

0 commit comments

Comments
 (0)