-
Notifications
You must be signed in to change notification settings - Fork 69
/
multifile_orchestrator.go
385 lines (314 loc) 路 11.3 KB
/
multifile_orchestrator.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
package runner
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"sync"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/metadata"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/pkg/fileutil"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
"github.com/kubeshop/tracetest/cli/runner"
"github.com/kubeshop/tracetest/cli/varset"
"github.com/kubeshop/tracetest/server/pkg/id"
"go.uber.org/zap"
)
// RunOptions defines options for running a resource
// IDs and DefinitionFiles are mutually exclusive and the only required options
type RunOptions struct {
// if IDs is used it needs to have the ResourceName defined
IDs []string
ResourceName string
// path to the file with resource definition
// the file will be applied before running
DefinitionFiles []string
// varsID or path to the file with environment definition
VarsID string
// runGroupID as string to define it for the entire run execution
RunGroupID string
// By default the runner will wait for the result of the run
// if this option is true, the wait will be skipped
SkipResultWait bool
// Optional path to the file where the result of the run will be saved
// in JUnit xml format
JUnitOuptutFile string
// Overrides the default required gates for the resource
RequiredGates []string
}
type multipleRunFormatter interface {
Format(output formatters.MultipleRunOutput[runner.RunResult], format formatters.Output) string
}
type runGroupWaiter interface {
WaitForCompletion(ctx context.Context, runGroupID string) (openapi.RunGroup, error)
}
func MultiFileOrchestrator(
logger *zap.Logger,
runGroupWaiter runGroupWaiter,
variableSets resourcemanager.Client,
runnerRegistry runner.Registry,
multipleRunFormatter multipleRunFormatter,
) orchestrator {
return orchestrator{
logger: logger,
runGroupWaiter: runGroupWaiter,
variableSets: variableSets,
runnerRegistry: runnerRegistry,
multipleRunFormatter: multipleRunFormatter,
}
}
type orchestrator struct {
logger *zap.Logger
runGroupWaiter runGroupWaiter
variableSets resourcemanager.Client
runnerRegistry runner.Registry
multipleRunFormatter multipleRunFormatter
}
const (
ExitCodeSuccess = 0
ExitCodeGeneralError = 1
ExitCodeTestNotPassed = 2
)
func (o orchestrator) Run(ctx context.Context, opts RunOptions, outputFormat string) (exitCode int, _ error) {
o.logger.Debug(
"Running tests from definition",
zap.Strings("definitionFiles", opts.DefinitionFiles),
zap.String("varSetID", opts.VarsID),
zap.Bool("skipResultsWait", opts.SkipResultWait),
zap.String("junitOutputFile", opts.JUnitOuptutFile),
zap.Strings("requiredGates", opts.RequiredGates),
)
variableSetFetcher := runner.GetVariableSetFetcher(o.logger, o.variableSets)
varsID, err := variableSetFetcher.Fetch(ctx, opts.VarsID)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot resolve variable set id: %w", err)
}
o.logger.Debug("env resolved", zap.String("ID", varsID))
vars := varset.VarSets{}
resourceFetcher := runner.GetResourceFetcher(o.logger, o.runnerRegistry)
runGroupID := opts.RunGroupID
if runGroupID == "" {
runGroupID = id.GenerateID().String()
}
var resources []any
var runsResults []runner.RunResult
if len(opts.DefinitionFiles) > 0 {
resources, runsResults, err = o.runByFiles(ctx, opts, resourceFetcher, &vars, varsID, runGroupID)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot run files: %w", err)
}
} else {
resources, runsResults, err = o.runByIDs(ctx, opts, resourceFetcher, &vars, varsID, runGroupID)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot run by id: %w", err)
}
}
runnerGetter := func(resource any) (formatters.Runner[runner.RunResult], error) {
resourceType, err := resourcemanager.GetResourceType(resource)
if err != nil {
return nil, fmt.Errorf("cannot extract type from resource: %w", err)
}
runner, err := o.runnerRegistry.Get(resourceType)
if err != nil {
return nil, fmt.Errorf("cannot find runner for resource type %s: %w", resourceType, err)
}
return runner, nil
}
// 3. if skip wait, print results and exit
if opts.SkipResultWait {
output := formatters.MultipleRunOutput[runner.RunResult]{
Runs: runsResults,
Resources: resources,
RunGroup: openapi.RunGroup{Id: runGroupID},
RunnerGetter: runnerGetter,
HasResults: false,
}
summary := o.multipleRunFormatter.Format(output, formatters.Output(outputFormat))
fmt.Println(summary)
return ExitCodeSuccess, nil
}
// 4. wait for the run group
runGroup, err := o.runGroupWaiter.WaitForCompletion(ctx, runGroupID)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot wait for test result: %w", err)
}
// 5. update runs and print results
for i, result := range runsResults {
resource := resources[i]
resourceType, err := resourcemanager.GetResourceType(resource)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot extract type from resource: %w", err)
}
runner, err := o.runnerRegistry.Get(resourceType)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot find runner for resource type %s: %w", resourceType, err)
}
// TODO: I think we can just pull the test runs from the group instead of updating each of them
updated, err := runner.UpdateResult(ctx, result)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot update test result: %w", err)
}
runsResults[i] = updated
err = o.writeJUnitReport(ctx, runner, result, opts.JUnitOuptutFile)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot write junit report: %w", err)
}
}
output := formatters.MultipleRunOutput[runner.RunResult]{
Runs: runsResults,
Resources: resources,
RunGroup: runGroup,
RunnerGetter: runnerGetter,
HasResults: true,
}
summary := o.multipleRunFormatter.Format(output, formatters.Output(outputFormat))
fmt.Println(summary)
exitCode = ExitCodeSuccess
if runGroup.GetStatus() == "failed" {
exitCode = ExitCodeTestNotPassed
}
return exitCode, nil
}
func (o orchestrator) getDefinitionFiles(file []string) ([]string, error) {
files := make([]string, 0)
for _, f := range file {
files = append(files, fileutil.ReadDirFileNames(f)...)
}
return files, nil
}
func (o orchestrator) runByFiles(ctx context.Context, opts RunOptions, resourceFetcher runner.ResourceFetcher, vars *varset.VarSets, varsID string, runGroupID string) ([]any, []runner.RunResult, error) {
resources := make([]any, 0)
runsResults := make([]runner.RunResult, 0)
var mainErr error
hasDefinitionFilesDefined := opts.DefinitionFiles != nil && len(opts.DefinitionFiles) > 0
if !hasDefinitionFilesDefined {
return resources, runsResults, fmt.Errorf("no definition files defined")
}
definitionFiles, err := o.getDefinitionFiles(opts.DefinitionFiles)
if err != nil {
return resources, runsResults, fmt.Errorf("cannot get definition files: %w", err)
}
var wg sync.WaitGroup
wg.Add(len(definitionFiles))
for _, definitionFile := range definitionFiles {
go func(def string) {
defer wg.Done()
resource, err := resourceFetcher.FetchWithDefinitionFile(ctx, def)
if err != nil {
mainErr = fmt.Errorf("cannot fetch resource from definition file: %w", err)
return
}
result, resource, err := o.createRun(ctx, resource, vars, opts.RequiredGates, varsID, runGroupID)
if err != nil {
mainErr = fmt.Errorf("cannot run test: %w", err)
return
}
runsResults = append(runsResults, result)
resources = append(resources, resource)
}(definitionFile)
}
wg.Wait()
return resources, runsResults, mainErr
}
func (o orchestrator) runByIDs(ctx context.Context, opts RunOptions, resourceFetcher runner.ResourceFetcher, vars *varset.VarSets, varsID string, runGroupID string) ([]any, []runner.RunResult, error) {
resources := make([]any, 0)
runsResults := make([]runner.RunResult, 0)
for _, id := range opts.IDs {
resource, err := resourceFetcher.FetchWithID(ctx, opts.ResourceName, id)
if err != nil {
return resources, runsResults, err
}
result, resource, err := o.createRun(ctx, resource, vars, opts.RequiredGates, varsID, runGroupID)
if err != nil {
return resources, runsResults, fmt.Errorf("cannot run test: %w", err)
}
runsResults = append(runsResults, result)
resources = append(resources, resource)
}
return resources, runsResults, nil
}
func (o orchestrator) createRun(ctx context.Context, resource any, vars *varset.VarSets, requiredGates []string, varsID, runGroupID string) (runner.RunResult, any, error) {
resourceType, err := resourcemanager.GetResourceType(resource)
if err != nil {
return runner.RunResult{}, nil, fmt.Errorf("cannot extract type from resource: %w", err)
}
r, err := o.runnerRegistry.Get(resourceType)
if err != nil {
return runner.RunResult{}, nil, fmt.Errorf("cannot find runner for resource type %s: %w", resourceType, err)
}
runInfo := openapi.RunInformation{
VariableSetId: &varsID,
Variables: vars.ToOpenapi(),
Metadata: metadata.GetMetadata(),
RequiredGates: getRequiredGates(requiredGates),
RunGroupId: &runGroupID,
}
// 2. validate missing vars
for {
result, err := r.StartRun(ctx, resource, runInfo)
if err == nil {
return result, resource, nil
}
if !errors.Is(err, varset.MissingVarsError{}) {
// actual error, return
return result, resource, fmt.Errorf("cannot run test: %w", err)
}
// missing vars error
newVars := varset.AskForMissingVars([]varset.VarSet(err.(varset.MissingVarsError)))
vars = &newVars
o.logger.Debug("filled variables", zap.Any("variables", vars))
}
}
var ErrJUnitNotSupported = errors.New("junit report is not supported for this resource type")
func (a orchestrator) writeJUnitReport(ctx context.Context, r runner.Runner, result runner.RunResult, outputFile string) error {
if outputFile == "" {
a.logger.Debug("no junit output file specified")
return nil
}
a.logger.Debug("saving junit report", zap.String("outputFile", outputFile))
report, err := r.JUnitResult(ctx, result)
if err != nil {
return err
}
a.logger.Debug("junit report", zap.String("report", report))
f, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("could not create junit output file: %w", err)
}
_, err = f.WriteString(report)
return err
}
func getRequiredGates(gates []string) []openapi.SupportedGates {
if len(gates) == 0 {
return nil
}
requiredGates := make([]openapi.SupportedGates, 0, len(gates))
for _, g := range gates {
requiredGates = append(requiredGates, openapi.SupportedGates(g))
}
return requiredGates
}
// HandleRunError handles errors returned by the server when running a test.
// It normalizes the handling of general errors, like 404,
// but more importantly, it processes the missing environment variables error
// so the orchestrator can request them from the user.
func HandleRunError(resp *http.Response, reqErr error) error {
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("could not read response body: %w", err)
}
resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("resource not found in server")
}
if resp.StatusCode == http.StatusUnprocessableEntity {
return varset.BuildMissingVarsError(body)
}
if reqErr != nil {
return fmt.Errorf("could not run test suite: %w", reqErr)
}
return nil
}