-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
cmd.go
607 lines (530 loc) · 19.3 KB
/
cmd.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package helpers
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/onsi/gomega"
"github.com/onsi/gomega/types"
"k8s.io/client-go/util/jsonpath"
"github.com/cilium/cilium/test/logger"
)
type JSONParseError struct {
error
}
func (e *JSONParseError) Error() string {
return e.error.Error()
}
// CmdStreamBuffer is a buffer that buffers the stream output of a command.
type CmdStreamBuffer struct {
*Buffer
cmd string
}
// Cmd returns the command that generated the stream.
func (b CmdStreamBuffer) Cmd() string {
return b.cmd
}
// ByLines returns res's stdout split by the newline character and, if the
// stdout contains `\r\n`, it will be split by carriage return and new line
// characters.
func (b *CmdStreamBuffer) ByLines() []string {
out := b.String()
sep := "\n"
if strings.Contains(out, "\r\n") {
sep = "\r\n"
}
out = strings.TrimRight(out, sep)
return strings.Split(out, sep)
}
// KVOutput returns a map of the stdout of res split based on
// the separator '='.
// For example, the following strings would be split as follows:
//
// a=1
// b=2
// c=3
// a=1
// b=2
// c=3
func (b *CmdStreamBuffer) KVOutput() map[string]string {
result := make(map[string]string)
for _, line := range b.ByLines() {
vals := strings.Split(line, "=")
if len(vals) == 2 {
result[vals[0]] = vals[1]
}
}
return result
}
// Filter returns the contents of res's stdout filtered using the provided
// JSONPath filter in a buffer. Returns an error if the unmarshalling of the
// contents of res's stdout fails.
func (b *CmdStreamBuffer) Filter(filter string) (*FilterBuffer, error) {
var data interface{}
result := new(bytes.Buffer)
err := json.Unmarshal(b.Bytes(), &data)
if err != nil {
return nil, &JSONParseError{
error: fmt.Errorf("could not parse JSON from command %q\n%w\n%s", b.Cmd(), err, b.Bytes()),
}
}
parser := jsonpath.New("").AllowMissingKeys(true)
parser.Parse(filter)
err = parser.Execute(result, data)
if err != nil {
return nil, err
}
return &FilterBuffer{result}, nil
}
// FilterLinesJSONPath decodes each line as JSON and applies the JSONPath
// filter to each line. Returns an array with the result for each line.
func (b *CmdStreamBuffer) FilterLinesJSONPath(filter *jsonpath.JSONPath) ([]FilterBuffer, error) {
lines := b.ByLines()
results := make([]FilterBuffer, 0, len(lines))
for i, line := range lines {
if len(line) == 0 {
continue
}
var data interface{}
result := new(bytes.Buffer)
err := json.Unmarshal([]byte(line), &data)
if err != nil {
return nil, &JSONParseError{
error: fmt.Errorf("could not parse %q as JSON (line %d of %q)", line, i, b.Cmd()),
}
}
err = filter.Execute(result, data)
if err != nil {
return nil, err
}
results = append(results, FilterBuffer{result})
}
return results, nil
}
// FilterLines works like Filter, but applies the JSONPath filter to each line
// separately and returns returns a Buffer for each line. An error is
// returned only for the first line which cannot be unmarshalled.
func (b *CmdStreamBuffer) FilterLines(filter string) ([]FilterBuffer, error) {
parsedFilter := jsonpath.New("").AllowMissingKeys(true)
err := parsedFilter.Parse(filter)
if err != nil {
return nil, err
}
return b.FilterLinesJSONPath(parsedFilter)
}
// CmdRes contains a variety of data which results from running a command.
type CmdRes struct {
cmd string // Command to run
stdout *Buffer // Stdout from running cmd
stderr *Buffer // Stderr from running cmd
success bool // Whether command successfully executed
exitcode int // The exit code of cmd
duration time.Duration // Is the representation of the the time that command took to execute.
wg *sync.WaitGroup // Used to wait until the command has finished running when used in conjunction with a Context
err error // If the command had any error being executed, the error will be written here.
}
// GetCmd returns res's cmd.
func (res *CmdRes) GetCmd() string {
return res.cmd
}
// GetExitCode returns res's exitcode.
func (res *CmdRes) GetExitCode() int {
return res.exitcode
}
// GetStdOut returns res's stdout.
func (res *CmdRes) GetStdOut() *CmdStreamBuffer {
return &CmdStreamBuffer{
res.stdout,
res.cmd,
}
}
// GetStdErr returns res's stderr.
func (res *CmdRes) GetStdErr() *CmdStreamBuffer {
return &CmdStreamBuffer{
res.stderr,
res.cmd,
}
}
// Stdout returns the contents of the stdout buffer of res as a string.
func (res *CmdRes) Stdout() string {
return res.GetStdOut().String()
}
// Stderr returns the contents of the stderr buffer of res as a string.
func (res *CmdRes) Stderr() string {
return res.GetStdErr().String()
}
// SendToLog writes to `TestLogWriter` the debug message for the running
// command, if the quietMode argument is true will print only the command and
// the exitcode.
func (res *CmdRes) SendToLog(quietMode bool) {
if quietMode {
logformat := "cmd: %q exitCode: %d duration: %s\n"
fmt.Fprintf(&logger.TestLogWriter, logformat, res.cmd, res.GetExitCode(), res.duration)
return
}
logformat := "cmd: %q exitCode: %d duration: %s stdout:\n%s\n"
log := fmt.Sprintf(logformat, res.cmd, res.GetExitCode(), res.duration, res.stdout.String())
if res.err != nil {
log = fmt.Sprintf("%serr:\n%s\n", log, res.err)
}
if res.stderr.Len() > 0 {
log = fmt.Sprintf("%sstderr:\n%s\n", log, res.stderr.String())
}
fmt.Fprint(&logger.TestLogWriter, log)
}
// WasSuccessful returns true if cmd completed successfully.
func (res *CmdRes) WasSuccessful() bool {
return res.err == nil && res.success
}
// ExpectFail asserts whether res failed to execute. It accepts an optional
// parameter that can be used to annotate failure messages.
func (res *CmdRes) ExpectFail(optionalDescription ...interface{}) bool {
return gomega.ExpectWithOffset(1, res).ShouldNot(
CMDSuccess(), optionalDescription...)
}
// ExpectFailWithError asserts whether res failed to execute with the
// error output containing the given data. It accepts an optional
// parameter that can be used to annotate failure messages.
func (res *CmdRes) ExpectFailWithError(data string, optionalDescription ...interface{}) bool {
return gomega.ExpectWithOffset(1, res).ShouldNot(
CMDSuccess(), optionalDescription...) &&
gomega.ExpectWithOffset(1, res.Stderr()).To(
gomega.ContainSubstring(data), optionalDescription...)
}
// ExpectSuccess asserts whether res executed successfully. It accepts an optional
// parameter that can be used to annotate failure messages.
func (res *CmdRes) ExpectSuccess(optionalDescription ...interface{}) bool {
return gomega.ExpectWithOffset(1, res).Should(
CMDSuccess(), optionalDescription...)
}
// ExpectContains asserts a string into the stdout of the response of executed
// command. It accepts an optional parameter that can be used to annotate
// failure messages.
func (res *CmdRes) ExpectContains(data string, optionalDescription ...interface{}) bool {
return gomega.ExpectWithOffset(1, res.Stdout()).To(
gomega.ContainSubstring(data), optionalDescription...)
}
// ExpectMatchesRegexp asserts that the stdout of the executed command
// matches the regexp. It accepts an optional parameter that can be
// used to annotate failure messages.
func (res *CmdRes) ExpectMatchesRegexp(regexp string, optionalDescription ...interface{}) bool {
return gomega.ExpectWithOffset(1, res.Stdout()).To(
gomega.MatchRegexp(regexp), optionalDescription...)
}
// ExpectContainsFilterLine applies the provided JSONPath filter to each line
// of stdout of the executed command and asserts that the expected string
// matches at least one of the lines.
// It accepts an optional parameter that can be used to annotate failure
// messages.
func (res *CmdRes) ExpectContainsFilterLine(filter, expected string, optionalDescription ...interface{}) bool {
lines, err := res.FilterLines(filter)
gomega.ExpectWithOffset(1, err).To(gomega.BeNil(), optionalDescription...)
sLines := []string{}
for _, fLine := range lines {
sLines = append(sLines, fLine.ByLines()...)
}
return gomega.ExpectWithOffset(1, sLines).To(
gomega.ContainElement(expected), optionalDescription...)
}
// ExpectDoesNotContain asserts that a string is not contained in the stdout of
// the executed command. It accepts an optional parameter that can be used to
// annotate failure messages.
func (res *CmdRes) ExpectDoesNotContain(data string, optionalDescription ...interface{}) bool {
return gomega.ExpectWithOffset(1, res.Stdout()).ToNot(
gomega.ContainSubstring(data), optionalDescription...)
}
// ExpectDoesNotMatchRegexp asserts that the stdout of the executed command
// doesn't match the regexp. It accepts an optional parameter that can be used
// to annotate failure messages.
func (res *CmdRes) ExpectDoesNotMatchRegexp(regexp string, optionalDescription ...interface{}) bool {
return gomega.ExpectWithOffset(1, res.Stdout()).ToNot(
gomega.MatchRegexp(regexp), optionalDescription...)
}
// ExpectDoesNotContainFilterLine applies the provided JSONPath filter to each
// line of stdout of the executed command and asserts that the expected string
// does not matches any of the lines.
// It accepts an optional parameter that can be used to annotate failure
// messages.
func (res *CmdRes) ExpectDoesNotContainFilterLine(filter, expected string, optionalDescription ...interface{}) bool {
lines, err := res.FilterLines(filter)
gomega.ExpectWithOffset(1, err).To(gomega.BeNil(), optionalDescription...)
sLines := []string{}
for _, fLine := range lines {
sLines = append(sLines, fLine.ByLines()...)
}
return gomega.ExpectWithOffset(1, sLines).ToNot(
gomega.ContainElement(expected), optionalDescription...)
}
// CountLines return the number of lines in the stdout of res.
func (res *CmdRes) CountLines() int {
return strings.Count(res.stdout.String(), "\n")
}
// CombineOutput returns the combined output of stdout and stderr for res.
func (res *CmdRes) CombineOutput() *bytes.Buffer {
result := new(bytes.Buffer)
result.WriteString(res.stdout.String())
result.WriteString(res.stderr.String())
return result
}
// IntOutput returns the stdout of res as an integer
func (res *CmdRes) IntOutput() (int, error) {
return strconv.Atoi(strings.TrimSpace(res.stdout.String()))
}
// FloatOutput returns the stdout of res as a float
func (res *CmdRes) FloatOutput() (float64, error) {
return strconv.ParseFloat(strings.TrimSpace(res.stdout.String()), 64)
}
// InRange returns nil if res matches the expected value range or error otherwise
func (res *CmdRes) InRange(min, max int) error {
raw, err := res.FloatOutput()
if err != nil {
return err
}
val := int(raw)
if val >= min && val <= max {
return nil
} else {
return fmt.Errorf(
"Expected result %d (%s) is not in the range of [%d, %d]",
val, strings.TrimSpace(res.stdout.String()), min, max)
}
}
// FindResults filters res's stdout using the provided JSONPath filter. It
// returns an array of the values that match the filter, and an error if
// the unmarshalling of the stdout of res fails.
// TODO - what exactly is the need for this vs. Filter function below?
func (res *CmdRes) FindResults(filter string) ([]reflect.Value, error) {
var data interface{}
var result []reflect.Value
err := json.Unmarshal(res.stdout.Bytes(), &data)
if err != nil {
return nil, err
}
parser := jsonpath.New("").AllowMissingKeys(true)
parser.Parse(filter)
fullResults, _ := parser.FindResults(data)
for _, res := range fullResults {
result = append(result, res...)
}
return result, nil
}
// Filter returns the contents of res's stdout filtered using the provided
// JSONPath filter in a buffer. Returns an error if the unmarshalling of the
// contents of res's stdout fails.
func (res *CmdRes) Filter(filter string) (*FilterBuffer, error) {
return res.GetStdOut().Filter(filter)
}
// FilterLinesJSONPath decodes each line as JSON and applies the JSONPath
// filter to each line. Returns an array with the result for each line.
func (res *CmdRes) FilterLinesJSONPath(filter *jsonpath.JSONPath) ([]FilterBuffer, error) {
return res.GetStdOut().FilterLinesJSONPath(filter)
}
// FilterLines works like Filter, but applies the JSONPath filter to each line
// separately and returns returns a buffer for each line. An error is
// returned only for the first line which cannot be unmarshalled.
func (res *CmdRes) FilterLines(filter string) ([]FilterBuffer, error) {
return res.GetStdOut().FilterLines(filter)
}
// ByLines returns res's stdout split by the newline character and, if the
// stdout contains `\r\n`, it will be split by carriage return and new line
// characters.
func (res *CmdRes) ByLines() []string {
return res.GetStdOut().ByLines()
}
// KVOutput returns a map of the stdout of res split based on
// the separator '='.
// For example, the following strings would be split as follows:
//
// a=1
// b=2
// c=3
func (res *CmdRes) KVOutput() map[string]string {
return res.GetStdOut().KVOutput()
}
// OutputPrettyPrint returns a string with the ExitCode, stdout and stderr in a
// pretty format.
func (res *CmdRes) OutputPrettyPrint() string {
format := func(message string) string {
result := []string{}
for _, line := range strings.Split(message, "\n") {
result = append(result, fmt.Sprintf("\t %s", line))
}
return strings.Join(result, "\n")
}
errStr := ""
if res.err != nil {
errStr = fmt.Sprintf("Err: %s\n", res.err)
}
return fmt.Sprintf(
"Exitcode: %d \n%sStdout:\n %s\nStderr:\n %s\n",
res.GetExitCode(),
errStr,
format(res.Stdout()),
format(res.Stderr()))
}
// ExpectEqual asserts whether cmdRes.Output().String() and expected are equal.
// It accepts an optional parameter that can be used to annotate failure
// messages.
func (res *CmdRes) ExpectEqual(expected string, optionalDescription ...interface{}) bool {
return gomega.ExpectWithOffset(1, res.Stdout()).Should(
gomega.Equal(expected), optionalDescription...)
}
// Reset resets res's stdout buffer to be empty.
func (res *CmdRes) Reset() {
res.stdout.Reset()
}
// SingleOut returns res's stdout as a string without any newline characters
func (res *CmdRes) SingleOut() string {
strstdout := res.stdout.String()
strstdoutSingle := strings.Replace(strstdout, "\n", "", -1)
return strings.Replace(strstdoutSingle, "\r", "", -1)
}
// Unmarshal unmarshalls res's stdout into data. It assumes that the stdout of
// res is in JSON format. Returns an error if the unmarshalling fails.
func (res *CmdRes) Unmarshal(data interface{}) error {
return json.Unmarshal(res.stdout.Bytes(), data)
}
// GetDebugMessage returns executed command and its output
func (res *CmdRes) GetDebugMessage() string {
return fmt.Sprintf("cmd: %s\n%s", res.GetCmd(), res.OutputPrettyPrint())
}
// WaitUntilMatch waits until the given substring is present in the `CmdRes.stdout`
// If the timeout is reached it will return an error.
func (res *CmdRes) WaitUntilMatch(substr string) error {
return res.WaitUntilMatchTimeout(substr, HelperTimeout)
}
// WaitUntilMatchTimeout is the same as WaitUntilMatch but with a user-provided
// timeout value.
func (res *CmdRes) WaitUntilMatchTimeout(substr string, timeout time.Duration) error {
body := func() bool {
return strings.Contains(res.OutputPrettyPrint(), substr)
}
return WithTimeout(
body,
fmt.Sprintf("%s is not in the output after timeout", substr),
&TimeoutConfig{Timeout: timeout})
}
// WaitUntilMatchRegexp waits until the `CmdRes.stdout` matches the given regexp.
// If the timeout is reached it will return an error.
func (res *CmdRes) WaitUntilMatchRegexp(expr string, timeout time.Duration) error {
r := regexp.MustCompile(expr)
body := func() bool {
return r.Match(res.GetStdOut().Bytes())
}
return WithTimeout(
body,
fmt.Sprintf("The output doesn't match regexp %q after timeout", expr),
&TimeoutConfig{Timeout: timeout})
}
// WaitUntilMatchFilterLineTimeout applies the JSONPath 'filter' to each line of
// `CmdRes.stdout` and waits until a line matches the 'expected' output.
// If the 'timeout' is reached it will return an error.
func (res *CmdRes) WaitUntilMatchFilterLineTimeout(filter, expected string, timeout time.Duration) error {
parsedFilter := jsonpath.New("").AllowMissingKeys(true)
err := parsedFilter.Parse(filter)
if err != nil {
return err
}
errChan := make(chan error, 1)
body := func() bool {
lines, err := res.FilterLinesJSONPath(parsedFilter)
if err != nil {
if errors.Is(err, &JSONParseError{}) {
// We might have read a partial line; continue
return false
}
errChan <- err
return true
}
for _, line := range lines {
if line.String() == expected {
return true
}
}
return false
}
err = RepeatUntilTrue(body, &TimeoutConfig{Timeout: timeout})
if err != nil {
return fmt.Errorf(
"Expected string %q is not in the filter output of %q: %w",
expected, filter, err)
}
select {
case err := <-errChan:
return err
default:
}
return nil
}
// WaitUntilMatchFilterLine applies the JSONPath 'filter' to each line of
// `CmdRes.stdout` and waits until a line matches the 'expected' output.
// If helpers.HelperTimout is reached it will return an error.
func (res *CmdRes) WaitUntilMatchFilterLine(filter, expected string) error {
return res.WaitUntilMatchFilterLineTimeout(filter, expected, HelperTimeout)
}
// WaitUntilFinish waits until the command context completes correctly
func (res *CmdRes) WaitUntilFinish() {
if res.wg == nil {
return
}
res.wg.Wait()
}
// GetErr returns error created from program output if command is not successful
func (res *CmdRes) GetErr(context string) error {
if res.WasSuccessful() {
return nil
}
return &cmdError{fmt.Sprintf("%s (%s) output: %s", context, res.err, res.GetDebugMessage())}
}
// GetError returns the error for this CmdRes.
func (res *CmdRes) GetError() error {
return res.err
}
// BeSuccesfulMatcher a new Ginkgo matcher for CmdRes struct
type BeSuccesfulMatcher struct{}
// Match validates that the given interface will be a `*CmdRes` struct and it
// was successful. In case of not a valid CmdRes will return an error. If the
// command was not successful it returns false.
func (matcher *BeSuccesfulMatcher) Match(actual interface{}) (success bool, err error) {
res, ok := actual.(*CmdRes)
if !ok {
return false, fmt.Errorf("%q is not a valid *CmdRes type", actual)
}
return res.WasSuccessful(), nil
}
// FailureMessage it returns a pretty printed error message in the case of the
// command was not successful.
func (matcher *BeSuccesfulMatcher) FailureMessage(actual interface{}) (message string) {
res, _ := actual.(*CmdRes)
return fmt.Sprintf("Expected command: %s \nTo succeed, but it failed:\n%s",
res.GetCmd(), res.OutputPrettyPrint())
}
// NegatedFailureMessage returns a pretty printed error message in case of the
// command is tested with a negative
func (matcher *BeSuccesfulMatcher) NegatedFailureMessage(actual interface{}) (message string) {
res, _ := actual.(*CmdRes)
return fmt.Sprintf("Expected command: %s\nTo have failed, but it was successful:\n%s",
res.GetCmd(), res.OutputPrettyPrint())
}
// CMDSuccess return a new Matcher that expects a CmdRes is a successful run command.
func CMDSuccess() types.GomegaMatcher {
return &BeSuccesfulMatcher{}
}
// cmdError is a implementation of error with String method to improve the debugging.
type cmdError struct {
s string
}
func (e *cmdError) Error() string {
return e.s
}
func (e *cmdError) String() string {
return e.s
}