-
Notifications
You must be signed in to change notification settings - Fork 457
/
run.go
245 lines (199 loc) · 6.14 KB
/
run.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//go:build integration_cli
package base
import (
"bytes"
"os"
"os/exec"
"regexp"
"strings"
"github.com/siderolabs/go-retry/retry"
"github.com/stretchr/testify/suite"
)
// RunOption configures options for Run.
type RunOption func(*runOptions)
// MatchFunc runs against output (stdout or stderr).
type MatchFunc func(output string) error
type runOptions struct {
retryer retry.Retryer
shouldFail bool
stdoutEmpty bool
stderrNotEmpty bool
stdoutRegexps []*regexp.Regexp
stdoutNegativeRegexps []*regexp.Regexp
stderrRegexps []*regexp.Regexp
stderrNegativeRegexps []*regexp.Regexp
stdoutMatchers []MatchFunc
stderrMatchers []MatchFunc
}
// WithRetry retries failing command runs.
func WithRetry(retryer retry.Retryer) RunOption {
return func(opts *runOptions) {
opts.retryer = retryer
}
}
// ShouldFail tells run command should fail (with non-empty stderr).
//
// ShouldFail also sets StdErrNotEmpty.
func ShouldFail() RunOption {
return func(opts *runOptions) {
opts.shouldFail = true
opts.stderrNotEmpty = true
}
}
// StdoutEmpty tells run that stdout of the command should be empty.
func StdoutEmpty() RunOption {
return func(opts *runOptions) {
opts.stdoutEmpty = true
}
}
// StderrNotEmpty tells run that stderr of the command should not be empty.
func StderrNotEmpty() RunOption {
return func(opts *runOptions) {
opts.stderrNotEmpty = true
}
}
// StdoutShouldMatch appends to the set of regexps stdout contents should match.
func StdoutShouldMatch(r *regexp.Regexp) RunOption {
return func(opts *runOptions) {
opts.stdoutRegexps = append(opts.stdoutRegexps, r)
}
}
// StdoutShouldNotMatch appends to the set of regexps stdout contents should not match.
func StdoutShouldNotMatch(r *regexp.Regexp) RunOption {
return func(opts *runOptions) {
opts.stdoutNegativeRegexps = append(opts.stdoutNegativeRegexps, r)
}
}
// StderrShouldMatch appends to the set of regexps stderr contents should match.
//
// StderrShouldMatch also sets StdErrNotEmpty.
func StderrShouldMatch(r *regexp.Regexp) RunOption {
return func(opts *runOptions) {
opts.stderrRegexps = append(opts.stderrRegexps, r)
opts.stderrNotEmpty = true
}
}
// StderrShouldNotMatch appends to the set of regexps stderr contents should not match.
func StderrShouldNotMatch(r *regexp.Regexp) RunOption {
return func(opts *runOptions) {
opts.stderrNegativeRegexps = append(opts.stderrNegativeRegexps, r)
}
}
// StdoutMatchFunc appends to the list of MatchFuncs to run against stdout.
func StdoutMatchFunc(f MatchFunc) RunOption {
return func(opts *runOptions) {
opts.stdoutMatchers = append(opts.stdoutMatchers, f)
}
}
// StderrMatchFunc appends to the list of MatchFuncs to run against stderr.
func StderrMatchFunc(f MatchFunc) RunOption {
return func(opts *runOptions) {
opts.stderrMatchers = append(opts.stderrMatchers, f)
}
}
// runAndWait launches the command and waits for completion.
//
// runAndWait doesn't do any assertions on result.
func runAndWait(suite *suite.Suite, cmd *exec.Cmd) (stdoutBuf, stderrBuf *bytes.Buffer, err error) {
var stdout, stderr bytes.Buffer
cmd.Stdin = nil
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = []string{}
// filter environment variables
for _, keyvalue := range os.Environ() {
index := strings.Index(keyvalue, "=")
if index < 0 {
continue
}
switch strings.ToUpper(keyvalue[:index]) {
case "PATH":
fallthrough
case "HOME":
fallthrough
case "USERNAME":
cmd.Env = append(cmd.Env, keyvalue)
}
}
suite.T().Logf("Running %q", strings.Join(cmd.Args, " "))
suite.Require().NoError(cmd.Start())
err = cmd.Wait()
return &stdout, &stderr, err
}
// retryRunAndWait retries runAndWait if the command fails to run.
func retryRunAndWait(suite *suite.Suite, cmdFunc func() *exec.Cmd, retryer retry.Retryer) (stdoutBuf, stderrBuf *bytes.Buffer, err error) {
err = retryer.Retry(func() error {
stdoutBuf, stderrBuf, err = runAndWait(suite, cmdFunc())
if _, ok := err.(*exec.ExitError); ok {
return retry.ExpectedErrorf("command failed, stderr %v: %w", stderrBuf.String(), err)
}
return err
})
return
}
// run executes command, asserts on its exit status/output, and returns stdout.
//
//nolint:gocyclo,nakedret
func run(suite *suite.Suite, cmdFunc func() *exec.Cmd, options ...RunOption) (stdout, stderr string) {
var opts runOptions
for _, o := range options {
o(&opts)
}
var (
stdoutBuf, stderrBuf *bytes.Buffer
err error
)
if opts.retryer != nil {
stdoutBuf, stderrBuf, err = retryRunAndWait(suite, cmdFunc, opts.retryer)
} else {
stdoutBuf, stderrBuf, err = runAndWait(suite, cmdFunc())
}
if err != nil {
// check that command failed, not something else happened
_, ok := err.(*exec.ExitError)
suite.Require().True(ok, "%s", err)
}
if stdoutBuf != nil {
stdout = stdoutBuf.String()
}
if stderrBuf != nil {
stderr = stderrBuf.String()
}
if opts.shouldFail {
suite.Assert().Error(err, "command expected to fail, but did not")
} else {
suite.Assert().NoError(err, "command failed, stdout: %q, stderr: %q", stdout, stderr)
}
if opts.stdoutEmpty {
suite.Assert().Empty(stdout, "stdout should be empty")
} else {
suite.Assert().NotEmpty(stdout, "stdout should be not empty")
}
if opts.stderrNotEmpty {
suite.Assert().NotEmpty(stderr, "stderr should be not empty")
} else {
suite.Assert().Empty(stderr, "stderr should be empty")
}
for _, rx := range opts.stdoutRegexps {
suite.Assert().Regexp(rx, stdout)
}
for _, rx := range opts.stderrRegexps {
suite.Assert().Regexp(rx, stderr)
}
for _, rx := range opts.stdoutNegativeRegexps {
suite.Assert().NotRegexp(rx, stdout)
}
for _, rx := range opts.stderrNegativeRegexps {
suite.Assert().NotRegexp(rx, stderr)
}
for _, f := range opts.stdoutMatchers {
suite.Assert().NoError(f(stdout), "stdout match: %q", stdout)
}
for _, f := range opts.stderrMatchers {
suite.Assert().NoError(f(stderr), "stderr match: %q", stderr)
}
return
}