/
events.go
392 lines (346 loc) · 11.5 KB
/
events.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package jobber
import (
"fmt"
corev1 "k8s.io/api/core/v1"
)
type StringRetriever func() string
type K8sResourceInformation struct {
Kind string
Name string
NamespaceName string
}
type EventContext struct {
CaseName string
UnitName string
}
func EventContextFor(testUnit *TestUnit, testCase *TestCase) EventContext {
if testUnit == nil {
return EventContext{}
}
if testCase == nil {
return EventContext{
UnitName: testUnit.Name,
}
}
return EventContext{
UnitName: testUnit.Name,
CaseName: testCase.Name,
}
}
type EventType int
const (
ResourceCreationSuccess EventType = iota
ResourceCreationFailure
ResourceTemplateExpansionFailure
ResourceDeletionSuccess
ResourceDeletionFailure
ValuesTransformSuccess
ValuesTransformFailure
ExecutableRunSuccess
ExecutableRunFailure
TestUnitStarted
TestUnitCompletedSuccessfully
TestCaseStarted
TestCaseCompletedSuccessfully
TestingCompletedSuccesfully
PipelineDefinitionIsInvalid
AssetDirectoryCreatedSuccessfully
AssetDirectoryCreationFailed
AssetDirectoryDeletedSuccessfully
AssetDirectoryDeletionFailed
WaitingForPodToReachRunningState
WaitingForJobToComplete
JobFailedToComplete
ArchiveFileCreatedSuccessfully
ArchiveFileCreationFailed
)
type ResourceEvent struct {
// ExpandedTemplateRetriever provides a StringRetriever method that generates the template (as a string) after it has been
// expanded (i.e., the go-template transforms have run). If the Resource is built-in (e.g., the Default
// Namespace) this will be nil.
ExpandedTemplateRetriever StringRetriever
// ResourceInformation describes the Kubernetes Resource to which the event pertains.
// If there was an error and the template didn't provide enough information to determine
// all of the information for the Resource, this value will be nil.
ResourceDetails *K8sResourceInformation
// The pipeline action identifier for the template. This is set only when the event type is ResourceTemplateExpansionFailure.
TemplateName string
}
type ValuesTransformEvent struct {
TransformerName string
InputValuesRetriever StringRetriever
OutputValuesRetriever StringRetriever
StderrOutputRetriever StringRetriever
}
type ExecutableEvent struct {
ExecutableName string
StdoutOutputRetriever StringRetriever
StderrOutputRetriever StringRetriever
}
type FileEvent struct {
Path string
}
type Event struct {
Type EventType
Context EventContext
ResourceInformation *ResourceEvent
ValuesTransformInformation *ValuesTransformEvent
ExecuableInformation *ExecutableEvent
FileEvent *FileEvent
Error error
}
type eventHandler struct {
eventChannel chan<- *Event
}
func (h *eventHandler) sayThatUnitStarted(testUnit *TestUnit) {
h.eventChannel <- &Event{
Type: TestUnitStarted,
Context: EventContext{
UnitName: testUnit.Name,
},
}
}
func (h *eventHandler) sayThatUnitCompletedSuccessfully(testUnit *TestUnit) {
h.eventChannel <- &Event{
Type: TestUnitCompletedSuccessfully,
Context: EventContext{
UnitName: testUnit.Name,
},
}
}
func (h *eventHandler) sayThatCaseStarted(testUnit *TestUnit, testCase *TestCase) {
h.eventChannel <- &Event{
Type: TestCaseStarted,
Context: EventContext{
UnitName: testUnit.Name,
CaseName: testCase.Name,
},
}
}
func (h *eventHandler) sayThatCaseCompletedSuccessfully(testUnit *TestUnit, testCase *TestCase) {
h.eventChannel <- &Event{
Type: TestCaseCompletedSuccessfully,
Context: EventContext{
UnitName: testUnit.Name,
CaseName: testCase.Name,
},
}
}
func (h *eventHandler) sayThatTestingCompletedSuccessfully() {
h.eventChannel <- &Event{
Type: TestingCompletedSuccesfully,
}
}
func (h *eventHandler) sayThatResourceCreationSucceeded(resourceInformation *K8sResourceInformation, templateRetrieverMethod StringRetriever, testUnit *TestUnit, testCase *TestCase) {
h.eventChannel <- &Event{
Type: ResourceCreationSuccess,
ResourceInformation: &ResourceEvent{
ExpandedTemplateRetriever: templateRetrieverMethod,
ResourceDetails: resourceInformation,
},
Context: EventContextFor(testUnit, testCase),
}
}
func (h *eventHandler) sayThatResourceCreationFailed(resourceInformation *K8sResourceInformation, templateRetrieverMethod StringRetriever, err error, testUnit *TestUnit, testCase *TestCase) {
h.eventChannel <- &Event{
Type: ResourceCreationFailure,
ResourceInformation: &ResourceEvent{
ExpandedTemplateRetriever: templateRetrieverMethod,
ResourceDetails: resourceInformation,
},
Context: EventContextFor(testUnit, testCase),
Error: err,
}
}
func (h *eventHandler) sayThatResourceDeletionSucceeded(resourceInformation *K8sResourceInformation, testUnit *TestUnit, testCase *TestCase) {
h.eventChannel <- &Event{
Type: ResourceDeletionSuccess,
Context: EventContextFor(testUnit, testCase),
ResourceInformation: &ResourceEvent{
ResourceDetails: resourceInformation,
},
}
}
func (h *eventHandler) sayThatResourceDeletionFailed(resourceInformation *K8sResourceInformation, err error, testUnit *TestUnit, testCase *TestCase) {
h.eventChannel <- &Event{
Type: ResourceDeletionFailure,
Context: EventContextFor(testUnit, testCase),
ResourceInformation: &ResourceEvent{
ResourceDetails: resourceInformation,
},
Error: err,
}
}
func (h *eventHandler) sayThatResourceTemplateExpansionFailed(templateName string, templateRetrieverMethod StringRetriever, err error, testUnit *TestUnit, testCase *TestCase) {
h.eventChannel <- &Event{
Type: ResourceTemplateExpansionFailure,
ResourceInformation: &ResourceEvent{
ExpandedTemplateRetriever: templateRetrieverMethod,
TemplateName: templateName,
},
Context: EventContextFor(testUnit, testCase),
Error: fmt.Errorf("on template (%s) %s", templateName, err),
}
}
func (h *eventHandler) explainAttemptToCreateDefaultNamespace(createdNamespaceApiObject *corev1.Namespace, context EventContext, errorOnCreationAttempt error) {
var namespaceName string
if createdNamespaceApiObject != nil {
namespaceName = createdNamespaceApiObject.Name
} else {
namespaceName = fmt.Sprintf("%s-<generated>", createdNamespaceApiObject.GenerateName)
}
if errorOnCreationAttempt != nil {
h.eventChannel <- &Event{
Type: ResourceCreationFailure,
Context: context,
ResourceInformation: &ResourceEvent{
ResourceDetails: &K8sResourceInformation{
Kind: "namespace",
Name: namespaceName,
NamespaceName: "",
},
},
Error: errorOnCreationAttempt,
}
} else {
h.eventChannel <- &Event{
Type: ResourceCreationSuccess,
Context: context,
ResourceInformation: &ResourceEvent{
ResourceDetails: &K8sResourceInformation{
Kind: "namespace",
Name: namespaceName,
NamespaceName: "",
},
},
}
}
}
func (h *eventHandler) sayThatExecutionFailed(actionId string, err error, testUnit *TestUnit, testCase *TestCase) {
h.eventChannel <- &Event{
Type: ExecutableRunFailure,
Context: EventContextFor(testUnit, testCase),
ExecuableInformation: &ExecutableEvent{
ExecutableName: actionId,
StdoutOutputRetriever: nil,
StderrOutputRetriever: nil,
},
Error: err,
}
}
func (h *eventHandler) sayThatExecutionSucceeded(actionId string, testUnit *TestUnit, testCase *TestCase) {
h.eventChannel <- &Event{
Type: ExecutableRunSuccess,
Context: EventContextFor(testUnit, testCase),
ExecuableInformation: &ExecutableEvent{
ExecutableName: actionId,
StdoutOutputRetriever: nil,
StderrOutputRetriever: nil,
},
}
}
func (h *eventHandler) sayThatPipelineDefinitionIsInvalid(err error) {
h.eventChannel <- &Event{
Type: PipelineDefinitionIsInvalid,
Context: EventContext{},
Error: err,
}
}
// func (h *eventHandler) explainActionOutcome(action *PipelineAction, outcome *PipelineActionOutcome, testUnit *TestUnit, testCase *TestCase) {
// switch action.Type {
// case TemplatedResource:
// templateOutputFunc := func() string {
// if outcome.OutputBuffer != nil {
// return outcome.OutputBuffer.String()
// }
// return ""
// }
// if outcome.Error == nil {
// h.sayThatResourceCreationSucceeded(outcome.CreatedResource.Information(), templateOutputFunc, testUnit, testCase)
// } else {
// switch typedError := outcome.Error.(type) {
// case *TemplateError:
// h.sayThatResourceTemplateExpansionFailed(typedError.TemplateName, templateOutputFunc, typedError, testUnit, testCase)
// case *ResourceCreationError:
// h.sayThatResourceCreationFailed(typedError.ResourceInformation, templateOutputFunc, typedError, testUnit, testCase)
// case *JobCompletionFailureError:
// h.sayThatJobFailedToComplete(typedError.ResourceInformation, typedError, testUnit, testCase)
// }
// }
// case ValuesTransform:
// case Executable:
// if outcome.Error != nil {
// h.sayThatExecutionFailed(action.Descriptor, outcome, testUnit, testCase)
// } else {
// h.sayThatExecutionSucceeded(action.Descriptor, outcome, testUnit, testCase)
// }
// }
// }
func (handler *eventHandler) sayThatAssetDirectoryCreationFailed(path string, err error, testUnit *TestUnit, testCase *TestCase) {
handler.eventChannel <- &Event{
Type: AssetDirectoryCreationFailed,
FileEvent: &FileEvent{
Path: path,
},
Error: err,
Context: EventContextFor(testUnit, testCase),
}
}
// func (handler *eventHandler) sayThatJobFailedToComplete(resourceInformation *K8sResourceInformation, err error, testUnit *TestUnit, testCase *TestCase) {
// handler.eventChannel <- &Event{
// Type: JobFailedToComplete,
// Error: err,
// Context: EventContextFor(testUnit, testCase),
// }
// }
func (handler *eventHandler) sayThatAssetDirectoryCreationSucceeded(directoryPath string, testUnit *TestUnit, testCase *TestCase) {
handler.eventChannel <- &Event{
Type: AssetDirectoryCreatedSuccessfully,
FileEvent: &FileEvent{
Path: directoryPath,
},
Context: EventContextFor(testUnit, testCase),
}
}
func (handler *eventHandler) explainAssetCreationOutcome(outcome *TestCaseAssetsDirectoryCreationOutcome, testUnit *TestUnit, testCase *TestCase) {
for _, successfullyCreatedDir := range outcome.SuccessfullyCreatedDirectoryPaths {
handler.sayThatAssetDirectoryCreationSucceeded(successfullyCreatedDir, testUnit, testCase)
}
if outcome.DirectoryCreationFailureError != nil {
handler.sayThatAssetDirectoryCreationFailed(outcome.DirectoryPathOfFailedCreation, outcome.DirectoryCreationFailureError, testUnit, testCase)
}
}
func (handler *eventHandler) sayThatArchiveCreationFailed(archivePath string, assetsRootDirectoryPath string, err error) {
handler.eventChannel <- &Event{
Type: ArchiveFileCreationFailed,
FileEvent: &FileEvent{
Path: archivePath,
},
Error: fmt.Errorf("failed to create archive file (%s) from assets directory (%s): %s", archivePath, assetsRootDirectoryPath, err),
}
}
func (handler *eventHandler) sayThatArchiveCreationSucceeded(archivePath string) {
handler.eventChannel <- &Event{
Type: ArchiveFileCreatedSuccessfully,
FileEvent: &FileEvent{
Path: archivePath,
},
}
}
func (handler *eventHandler) sayThatAssetDirectoryDeletionWasSuccessful(assetDirectoryPath string) {
handler.eventChannel <- &Event{
Type: AssetDirectoryDeletedSuccessfully,
FileEvent: &FileEvent{
Path: assetDirectoryPath,
},
}
}
func (handler *eventHandler) sayThatAssetDirectoryDeletionFailed(assetDirectoryPath string, err error) {
handler.eventChannel <- &Event{
Type: AssetDirectoryDeletionFailed,
FileEvent: &FileEvent{
Path: assetDirectoryPath,
},
Error: err,
}
}