-
Notifications
You must be signed in to change notification settings - Fork 70
/
orchestrator.go
396 lines (320 loc) · 10.9 KB
/
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
386
387
388
389
390
391
392
393
394
395
396
package runner
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"sync"
"time"
cienvironment "github.com/cucumber/ci-environment/go"
"github.com/davecgh/go-spew/spew"
"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/variable"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)
// RunOptions defines options for running a resource
// ID and DefinitionFile are mutually exclusive and the only required options
type RunOptions struct {
// ID of the resource to run
ID string
// path to the file with resource definition
// the file will be applied before running
DefinitionFile string
// varsID or path to the file with environment definition
VarsID 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
}
// RunResult holds the result of the run
// Resources
type RunResult struct {
// The resource being run. If has been preprocessed, this needs to be the updated version
Resource any
// The result of the run. It can be anything the resource needs for validating and formatting the result
Run any
// If true, it means that the current run is ready to be presented to the user
Finished bool
// Whether the run has passed or not. Used to determine exit code
Passed bool
}
// Runner defines interface for running a resource
type Runner interface {
// Name of the runner. must match the resource name it handles
Name() string
// Apply the given file and return a resource. The resource can be of any type.
// It will then be used by Run method
Apply(context.Context, fileutil.File) (resource any, _ error)
// GetByID gets the resource by ID. This method is used to get the resource when running from id
GetByID(_ context.Context, id string) (resource any, _ error)
// StartRun starts running the resource and return the result. This method should not wait for the test to finish
StartRun(_ context.Context, resource any, _ openapi.RunInformation) (RunResult, error)
// UpdateResult is regularly called by the orchestrator to check the status of the run
UpdateResult(context.Context, RunResult) (RunResult, error)
// JUnitResult returns the result of the run in JUnit format
JUnitResult(context.Context, RunResult) (string, error)
// Format the result of the run into a string
FormatResult(_ RunResult, format string) string
}
func Orchestrator(
logger *zap.Logger,
openapiClient *openapi.APIClient,
variableSets resourcemanager.Client,
) orchestrator {
return orchestrator{
logger: logger,
openapiClient: openapiClient,
variableSets: variableSets,
}
}
type orchestrator struct {
logger *zap.Logger
openapiClient *openapi.APIClient
variableSets resourcemanager.Client
}
var (
yamlFormat = resourcemanager.Formats.Get(resourcemanager.FormatYAML)
jsonFormat = resourcemanager.Formats.Get(resourcemanager.FormatJSON)
)
const (
ExitCodeSuccess = 0
ExitCodeGeneralError = 1
ExitCodeTestNotPassed = 2
)
func (o orchestrator) Run(ctx context.Context, r Runner, opts RunOptions, outputFormat string) (exitCode int, _ error) {
o.logger.Debug(
"Running test from definition",
zap.String("definitionFile", opts.DefinitionFile),
zap.String("ID", opts.ID),
zap.String("varSetID", opts.VarsID),
zap.Bool("skipResultsWait", opts.SkipResultWait),
zap.String("junitOutputFile", opts.JUnitOuptutFile),
zap.Strings("requiredGates", opts.RequiredGates),
)
varsID, err := o.resolveVarsID(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))
var resource any
if opts.DefinitionFile != "" {
f, err := fileutil.Read(opts.DefinitionFile)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot read definition file %s: %w", opts.DefinitionFile, err)
}
df := f
o.logger.Debug("Definition file read", zap.String("absolutePath", df.AbsPath()))
df, err = o.injectLocalEnvVars(ctx, df)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot inject local env vars: %w", err)
}
resource, err = r.Apply(ctx, df)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot apply definition file: %w", err)
}
o.logger.Debug("Definition file applied", zap.String("updated", string(df.Contents())))
} else {
o.logger.Debug("Definition file not provided, fetching resource by ID", zap.String("ID", opts.ID))
resource, err = r.GetByID(ctx, opts.ID)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot get resource by ID: %w", err)
}
o.logger.Debug("Resource fetched by ID", zap.String("ID", opts.ID), zap.Any("resource", resource))
}
var result RunResult
var ev varSets
// iterate until we have all env vars,
// or the server returns an actual error
for {
runInfo := openapi.RunInformation{
VariableSetId: &varsID,
Variables: ev.toOpenapi(),
Metadata: getMetadata(),
RequiredGates: getRequiredGates(opts.RequiredGates),
}
result, err = r.StartRun(ctx, resource, runInfo)
if err == nil {
break
}
if !errors.Is(err, missingVarsError{}) {
// actual error, return
return ExitCodeGeneralError, fmt.Errorf("cannot run test: %w", err)
}
// missing vars error
ev = askForMissingVars([]varSet(err.(missingVarsError)))
o.logger.Debug("filled variables", zap.Any("variables", ev))
}
if opts.SkipResultWait {
fmt.Println(r.FormatResult(result, outputFormat))
return ExitCodeSuccess, nil
}
result, err = o.waitForResult(ctx, r, result)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot wait for test result: %w", err)
}
fmt.Println(r.FormatResult(result, outputFormat))
err = o.writeJUnitReport(ctx, r, result, opts.JUnitOuptutFile)
if err != nil {
return ExitCodeGeneralError, fmt.Errorf("cannot write junit report: %w", err)
}
exitCode = ExitCodeSuccess
if !result.Passed {
exitCode = ExitCodeTestNotPassed
}
return exitCode, nil
}
func (o orchestrator) resolveVarsID(ctx context.Context, varsID string) (string, error) {
if varsID == "" {
return "", nil // user have not defined variables, skipping it
}
if !fileutil.IsFilePath(varsID) {
o.logger.Debug("varsID is not a file path", zap.String("vars", varsID))
// validate that env exists
_, err := o.variableSets.Get(ctx, varsID, resourcemanager.Formats.Get(resourcemanager.FormatYAML))
if errors.Is(err, resourcemanager.ErrNotFound) {
return "", fmt.Errorf("variable set '%s' not found", varsID)
}
if err != nil {
return "", fmt.Errorf("cannot get variable set '%s': %w", varsID, err)
}
o.logger.Debug("envID is valid")
return varsID, nil
}
f, err := fileutil.Read(varsID)
if err != nil {
return "", fmt.Errorf("cannot read environment set file %s: %w", varsID, err)
}
o.logger.Debug("envID is a file path", zap.String("filePath", varsID), zap.Any("file", f))
updatedEnv, err := o.variableSets.Apply(ctx, f, yamlFormat)
if err != nil {
return "", fmt.Errorf("could not read environment set file: %w", err)
}
var vars openapi.VariableSetResource
err = yaml.Unmarshal([]byte(updatedEnv), &vars)
if err != nil {
o.logger.Error("error parsing json", zap.String("content", updatedEnv), zap.Error(err))
return "", fmt.Errorf("could not unmarshal variable set json: %w", err)
}
return vars.Spec.GetId(), nil
}
func (o orchestrator) injectLocalEnvVars(ctx context.Context, df fileutil.File) (fileutil.File, error) {
variableInjector := variable.NewInjector(variable.WithVariableProvider(
variable.EnvironmentVariableProvider{},
))
injected, err := variableInjector.ReplaceInString(string(df.Contents()))
if err != nil {
return df, fmt.Errorf("cannot inject local variable set: %w", err)
}
df = fileutil.New(df.AbsPath(), []byte(injected))
return df, nil
}
func (o orchestrator) waitForResult(ctx context.Context, r Runner, result RunResult) (RunResult, error) {
var (
updatedResult RunResult
lastError error
wg sync.WaitGroup
)
wg.Add(1)
ticker := time.NewTicker(1 * time.Second) // TODO: change to websockets
go func() {
for range ticker.C {
updated, err := r.UpdateResult(ctx, result)
o.logger.Debug("updated result", zap.String("result", spew.Sdump(updated)))
if err != nil {
o.logger.Debug("UpdateResult failed", zap.Error(err))
lastError = err
wg.Done()
return
}
if updated.Finished {
o.logger.Debug("result is finished")
updatedResult = updated
wg.Done()
return
}
o.logger.Debug("still waiting")
}
}()
wg.Wait()
if lastError != nil {
return RunResult{}, lastError
}
return updatedResult, nil
}
var ErrJUnitNotSupported = errors.New("junit report is not supported for this resource type")
func (a orchestrator) writeJUnitReport(ctx context.Context, r Runner, result 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 getMetadata() map[string]string {
ci := cienvironment.DetectCIEnvironment()
if ci == nil {
return map[string]string{}
}
metadata := map[string]string{
"name": ci.Name,
"url": ci.URL,
"buildNumber": ci.BuildNumber,
}
if ci.Git != nil {
metadata["branch"] = ci.Git.Branch
metadata["tag"] = ci.Git.Tag
metadata["revision"] = ci.Git.Revision
}
return metadata
}
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 buildMissingVarsError(body)
}
if reqErr != nil {
return fmt.Errorf("could not run test suite: %w", reqErr)
}
return nil
}