@@ -14,11 +14,13 @@ import (
14
14
"golang.org/x/xerrors"
15
15
)
16
16
17
+ // RunContainer specifies a runtime container for performing command tests
17
18
type RunContainer struct {
18
19
name string
19
20
ctx context.Context
20
21
}
21
22
23
+ // ContainerConfig describes the RunContainer configuration schema for initializing a testing environment
22
24
type ContainerConfig struct {
23
25
Name string
24
26
Image string
@@ -40,6 +42,7 @@ func preflightChecks() error {
40
42
return nil
41
43
}
42
44
45
+ // NewRunContainer starts a new docker container for executing command tests
43
46
func NewRunContainer (ctx context.Context , config * ContainerConfig ) (* RunContainer , error ) {
44
47
if err := preflightChecks (); err != nil {
45
48
return nil , err
@@ -68,6 +71,7 @@ func NewRunContainer(ctx context.Context, config *ContainerConfig) (*RunContaine
68
71
}, nil
69
72
}
70
73
74
+ // Close kills and removes the command execution testing container
71
75
func (r * RunContainer ) Close () error {
72
76
cmd := exec .CommandContext (r .ctx ,
73
77
"sh" , "-c" , strings .Join ([]string {
@@ -84,6 +88,7 @@ func (r *RunContainer) Close() error {
84
88
return nil
85
89
}
86
90
91
+ // Assertable describes an initialized command ready to be run and asserted against
87
92
type Assertable struct {
88
93
cmd * exec.Cmd
89
94
ctx context.Context
@@ -117,6 +122,7 @@ func (r *RunContainer) RunCmd(cmd *exec.Cmd) *Assertable {
117
122
}
118
123
}
119
124
125
+ // Assert runs the Assertable and
120
126
func (a Assertable ) Assert (t * testing.T , option ... Assertion ) {
121
127
t .Run (strings .Join (a .cmd .Args [6 :], " " ), func (t * testing.T ) {
122
128
var cmdResult CommandResult
@@ -158,14 +164,19 @@ func (a Assertable) Assert(t *testing.T, option ...Assertion) {
158
164
})
159
165
}
160
166
167
+ // Assertion specifies an assertion on the given CommandResult.
168
+ // Pass custom Assertion types to cover special cases.
161
169
type Assertion interface {
162
170
Valid (r CommandResult ) error
163
171
}
164
172
173
+ // Named is an optional extension of Assertion that provides a helpful label
174
+ // to *testing.T
165
175
type Named interface {
166
176
Name () string
167
177
}
168
178
179
+ // CommandResult contains the aggregated result of a command execution
169
180
type CommandResult struct {
170
181
Stdout , Stderr []byte
171
182
ExitCode int
@@ -185,10 +196,12 @@ func (s simpleFuncAssert) Name() string {
185
196
return s .name
186
197
}
187
198
199
+ // Success asserts that the command exited with an exit code of 0
188
200
func Success () Assertion {
189
201
return ExitCodeIs (0 )
190
202
}
191
203
204
+ // ExitCodeIs asserts that the command exited with the given code
192
205
func ExitCodeIs (code int ) Assertion {
193
206
return simpleFuncAssert {
194
207
valid : func (r CommandResult ) error {
@@ -201,6 +214,7 @@ func ExitCodeIs(code int) Assertion {
201
214
}
202
215
}
203
216
217
+ // StdoutEmpty asserts that the command did not write any data to Stdout
204
218
func StdoutEmpty () Assertion {
205
219
return simpleFuncAssert {
206
220
valid : func (r CommandResult ) error {
@@ -210,6 +224,7 @@ func StdoutEmpty() Assertion {
210
224
}
211
225
}
212
226
227
+ // StderrEmpty asserts that the command did not write any data to Stderr
213
228
func StderrEmpty () Assertion {
214
229
return simpleFuncAssert {
215
230
valid : func (r CommandResult ) error {
@@ -219,6 +234,7 @@ func StderrEmpty() Assertion {
219
234
}
220
235
}
221
236
237
+ // StdoutMatches asserts that Stdout contains a substring which matches the given regexp
222
238
func StdoutMatches (pattern string ) Assertion {
223
239
return simpleFuncAssert {
224
240
valid : func (r CommandResult ) error {
@@ -228,6 +244,7 @@ func StdoutMatches(pattern string) Assertion {
228
244
}
229
245
}
230
246
247
+ // StderrMatches asserts that Stderr contains a substring which matches the given regexp
231
248
func StderrMatches (pattern string ) Assertion {
232
249
return simpleFuncAssert {
233
250
valid : func (r CommandResult ) error {
@@ -237,6 +254,7 @@ func StderrMatches(pattern string) Assertion {
237
254
}
238
255
}
239
256
257
+ // CombinedMatches asserts that either Stdout or Stderr a substring which matches the given regexp
240
258
func CombinedMatches (pattern string ) Assertion {
241
259
return simpleFuncAssert {
242
260
valid : func (r CommandResult ) error {
@@ -270,6 +288,7 @@ func empty(name string, a []byte) error {
270
288
return nil
271
289
}
272
290
291
+ // DurationLessThan asserts that the command completed in less than the given duration
273
292
func DurationLessThan (dur time.Duration ) Assertion {
274
293
return simpleFuncAssert {
275
294
valid : func (r CommandResult ) error {
@@ -282,6 +301,7 @@ func DurationLessThan(dur time.Duration) Assertion {
282
301
}
283
302
}
284
303
304
+ // DurationGreaterThan asserts that the command completed in greater than the given duration
285
305
func DurationGreaterThan (dur time.Duration ) Assertion {
286
306
return simpleFuncAssert {
287
307
valid : func (r CommandResult ) error {
0 commit comments