Repository navigation
Expand file tree
/
Copy pathorchestrator.go
More file actions
276 lines (235 loc) · 8.26 KB
/
Copy pathorchestrator.go
File metadata and controls
276 lines (235 loc) · 8.26 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package stack
import (
"github.com/ethereum-optimism/optimism/op-devstack/compat"
"github.com/ethereum-optimism/optimism/op-devstack/devtest"
)
// Lifecycle represents a controllable component by ControlPlane
type Lifecycle interface {
Start()
Stop()
}
type ControlAction int
const (
Start ControlAction = iota
Stop
)
// ControlPlane is the interface for the orchestrators to control components of the system.
type ControlPlane interface {
SupervisorState(id SupervisorID, action ControlAction)
L2CLNodeState(id L2CLNodeID, action ControlAction)
L2ELNodeState(id L2ELNodeID, action ControlAction)
FakePoSState(id L1CLNodeID, action ControlAction)
RollupBoostNodeState(id RollupBoostNodeID, action ControlAction)
OPRBuilderNodeState(id OPRBuilderNodeID, action ControlAction)
}
// Orchestrator is the base interface for all system orchestrators.
// It imposes some common things across all orchestrators, but may also have optional extensions, that not every type of backend might support.
type Orchestrator interface {
// P is the test-handle of the orchestrator.
// This may not be a Go-test handle.
// Orchestrators may be instantiated by dev-tools or test-package TestMain functions.
P() devtest.P
// Hydrate adds all services that the orchestrator is aware of to the given system.
// An orchestrator may be asked to hydrate different systems, one for each test.
Hydrate(sys ExtensibleSystem)
ControlPlane() ControlPlane
Type() compat.Type
}
type TimeTravelOrchestrator interface {
EnableTimeTravel()
}
// GateWithRemediation is an example of a test-gate that checks a system and may use an orchestrator to remediate any shortcomings.
// func GateWithRemediation(sys System, orchestrator Orchestrator) {
// step 1: check if system already does the right thing
// step 2: if not, check if orchestrator can help us
// step 3: maybe try different things, if none work, test-skip
// }
type SystemHook interface {
// PreHydrate runs before a system is hydrated,
// to prepare settings on the system like logging, or inspect test-scope
PreHydrate(sys System)
// PostHydrate runs after a system is hydrated, to run any checks.
// This may register validation that runs at the end of the test, using the sys.T().Cleanup function.
PostHydrate(sys System)
}
// ApplyOptionLifecycle applies all option lifecycle stages to the given orchestrator
func ApplyOptionLifecycle[O Orchestrator](opt Option[O], orch O) {
opt.BeforeDeploy(orch)
opt.Deploy(orch)
opt.AfterDeploy(orch)
opt.Finally(orch)
}
// Option is used to define a change that inspects and/or changes a System during the lifecycle.
type Option[O Orchestrator] interface {
// BeforeDeploy runs before any chain is created/deployed
BeforeDeploy(orch O)
// Deploy runs the deployment
Deploy(orch O)
// AfterDeploy runs after chains are created/deployed
AfterDeploy(orch O)
// Finally runs at the very end of orchestrator setup,
// but before any test-scope is created.
Finally(orch O)
// SystemHook is embedded: Options may expose system hooks, to run in test-scope.
SystemHook
}
type CommonOption = Option[Orchestrator]
// CombinedOption is a list of options.
// For each option lifecycle stage, all options are applied, left to right.
type CombinedOption[O Orchestrator] []Option[O]
var _ CommonOption = (CombinedOption[Orchestrator])(nil)
// Combine is a method to define a CombinedOption, more readable than a slice definition.
func Combine[O Orchestrator](opts ...Option[O]) CombinedOption[O] {
return CombinedOption[O](opts)
}
// Add changes the option into a new Option that that first applies the receiver, and then the other options.
// This is a convenience for bundling options together.
func (c *CombinedOption[O]) Add(other ...Option[O]) {
*c = append(*c, other...)
}
func (c CombinedOption[O]) BeforeDeploy(orch O) {
for _, opt := range c {
opt.BeforeDeploy(orch)
}
}
func (c CombinedOption[O]) Deploy(orch O) {
for _, opt := range c {
opt.Deploy(orch)
}
}
func (c CombinedOption[O]) AfterDeploy(orch O) {
for _, opt := range c {
opt.AfterDeploy(orch)
}
}
func (c CombinedOption[O]) Finally(orch O) {
for _, opt := range c {
opt.Finally(orch)
}
}
func (c CombinedOption[O]) PreHydrate(sys System) {
for _, opt := range c {
opt.PreHydrate(sys)
}
}
func (c CombinedOption[O]) PostHydrate(sys System) {
for _, opt := range c {
opt.PostHydrate(sys)
}
}
// FnOption defines an option with more flexible function instances per option lifecycle stage.
// Each nil attribute is simply a no-op when not set.
type FnOption[O Orchestrator] struct {
BeforeDeployFn func(orch O)
DeployFn func(orch O)
AfterDeployFn func(orch O)
FinallyFn func(orch O)
PreHydrateFn func(sys System)
PostHydrateFn func(sys System)
}
var _ CommonOption = (*FnOption[Orchestrator])(nil)
func (f FnOption[O]) BeforeDeploy(orch O) {
if f.BeforeDeployFn != nil {
f.BeforeDeployFn(orch)
}
}
func (f FnOption[O]) Deploy(orch O) {
if f.DeployFn != nil {
f.DeployFn(orch)
}
}
func (f FnOption[O]) AfterDeploy(orch O) {
if f.AfterDeployFn != nil {
f.AfterDeployFn(orch)
}
}
func (f FnOption[O]) Finally(orch O) {
if f.FinallyFn != nil {
f.FinallyFn(orch)
}
}
func (f FnOption[O]) PreHydrate(sys System) {
if f.PreHydrateFn != nil {
f.PreHydrateFn(sys)
}
}
func (f FnOption[O]) PostHydrate(sys System) {
if f.PostHydrateFn != nil {
f.PostHydrateFn(sys)
}
}
// BeforeDeploy registers a function to run before the deployment stage of the orchestrator.
// This may be used to customize deployment settings.
func BeforeDeploy[O Orchestrator](fn func(orch O)) Option[O] {
return FnOption[O]{BeforeDeployFn: fn}
}
// Deploy registers a function to run during the deployment stage of the orchestrator.
// This may be used to perform deployments.
func Deploy[O Orchestrator](fn func(orch O)) Option[O] {
return FnOption[O]{DeployFn: fn}
}
// AfterDeploy registers a function to run after the deployment stage of the orchestrator.
// This may be used to customize the orchestrator, after having deployment configuration in place.
func AfterDeploy[O Orchestrator](fn func(orch O)) Option[O] {
return FnOption[O]{AfterDeployFn: fn}
}
// Finally registers a function to run at the end of orchestrator setup.
// This may be used for any orchestrator post-validation,
// or to export any of the now ready orchestrator resources.
func Finally[O Orchestrator](fn func(orch O)) Option[O] {
return FnOption[O]{FinallyFn: fn}
}
// PreHydrate hooks up an option callback to run before a new System has been hydrated by the Orchestrator.
func PreHydrate[O Orchestrator](fn func(sys System)) Option[O] {
return FnOption[O]{PostHydrateFn: fn}
}
// PostHydrate hooks up an option callback to run when a new System has been hydrated by the Orchestrator.
// This is essentially a test-case preamble,
// to globally configure checks or gates that should run on the test-scope level.
// Test post-checks can be configured with sys.T().Cleanup(...).
func PostHydrate[O Orchestrator](fn func(sys System)) Option[O] {
return FnOption[O]{PostHydrateFn: fn}
}
// MakeCommon makes the type-specific option a common option.
// If the result runs with a different orchestrator type than expected
// the actual typed option will not run.
// This can be used to mix in customizations.
// Later common options should verify the orchestrator has the properties it needs to have.
func MakeCommon[O Orchestrator](opt Option[O]) CommonOption {
return FnOption[Orchestrator]{
BeforeDeployFn: func(orch Orchestrator) {
if o, ok := orch.(O); ok {
opt.BeforeDeploy(o)
} else {
orch.P().Logger().Debug("BeforeDeploy option does not apply to this orchestrator type")
}
},
DeployFn: func(orch Orchestrator) {
if o, ok := orch.(O); ok {
opt.Deploy(o)
} else {
orch.P().Logger().Debug("Deploy option does not apply to this orchestrator type")
}
},
AfterDeployFn: func(orch Orchestrator) {
if o, ok := orch.(O); ok {
opt.AfterDeploy(o)
} else {
orch.P().Logger().Debug("AfterDeploy option does not apply to this orchestrator type")
}
},
FinallyFn: func(orch Orchestrator) {
if o, ok := orch.(O); ok {
opt.Finally(o)
} else {
orch.P().Logger().Debug("Finally option does not apply to this orchestrator type")
}
},
PreHydrateFn: func(sys System) {
opt.PreHydrate(sys)
},
PostHydrateFn: func(sys System) {
opt.PostHydrate(sys)
},
}
}