forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_test.go
324 lines (282 loc) · 9.55 KB
/
run_test.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
package cmd
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"runtime"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/lib/testutils"
)
type mockWriter struct {
err error
errAfter int
}
func (fw mockWriter) Write(p []byte) (n int, err error) {
if fw.err != nil {
return fw.errAfter, fw.err
}
return len(p), nil
}
var _ io.Writer = mockWriter{}
func getFiles(t *testing.T, fileSystem fsext.Fs) map[string]*bytes.Buffer {
result := map[string]*bytes.Buffer{}
walkFn := func(filePath string, _ fs.FileInfo, err error) error {
if filePath == "/" || filePath == "\\" {
return nil
}
require.NoError(t, err)
contents, err := fsext.ReadFile(fileSystem, filePath)
require.NoError(t, err)
result[filePath] = bytes.NewBuffer(contents)
return nil
}
err := fsext.Walk(fileSystem, fsext.FilePathSeparator, filepath.WalkFunc(walkFn))
require.NoError(t, err)
return result
}
func assertEqual(t *testing.T, exp string, actual io.Reader) {
act, err := io.ReadAll(actual)
require.NoError(t, err)
assert.Equal(t, []byte(exp), act)
}
func initVars() (
content map[string]io.Reader, stdout *bytes.Buffer, stderr *bytes.Buffer, fs fsext.Fs,
) {
return map[string]io.Reader{}, bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{}), fsext.NewMemMapFs()
}
func TestHandleSummaryResultSimple(t *testing.T) {
t.Parallel()
content, stdout, stderr, fs := initVars()
// Test noop
assert.NoError(t, handleSummaryResult(fs, stdout, stderr, content))
require.Empty(t, getFiles(t, fs))
require.Empty(t, stdout.Bytes())
require.Empty(t, stderr.Bytes())
// Test stdout only
content["stdout"] = bytes.NewBufferString("some stdout summary")
assert.NoError(t, handleSummaryResult(fs, stdout, stderr, content))
require.Empty(t, getFiles(t, fs))
assertEqual(t, "some stdout summary", stdout)
require.Empty(t, stderr.Bytes())
}
func TestHandleSummaryResultError(t *testing.T) {
t.Parallel()
content, _, stderr, fs := initVars()
expErr := errors.New("test error")
stdout := mockWriter{err: expErr, errAfter: 10}
filePath1 := "/path/file1"
filePath2 := "/path/file2"
if runtime.GOOS == "windows" {
filePath1 = "\\path\\file1"
filePath2 = "\\path\\file2"
}
content["stdout"] = bytes.NewBufferString("some stdout summary")
content["stderr"] = bytes.NewBufferString("some stderr summary")
content[filePath1] = bytes.NewBufferString("file summary 1")
content[filePath2] = bytes.NewBufferString("file summary 2")
err := handleSummaryResult(fs, stdout, stderr, content)
assert.Error(t, err)
assert.Contains(t, err.Error(), expErr.Error())
files := getFiles(t, fs)
assertEqual(t, "file summary 1", files[filePath1])
assertEqual(t, "file summary 2", files[filePath2])
}
func TestRunScriptErrorsAndAbort(t *testing.T) {
t.Parallel()
testCases := []struct {
testFilename, name string
expErr, expLogOutput string
expExitCode exitcodes.ExitCode
extraArgs []string
}{
{
testFilename: "abort.js",
expErr: errext.AbortTest,
expExitCode: exitcodes.ScriptAborted,
},
{
testFilename: "abort_initerr.js",
expErr: errext.AbortTest,
expExitCode: exitcodes.ScriptAborted,
},
{
testFilename: "abort_initvu.js",
expErr: errext.AbortTest,
expExitCode: exitcodes.ScriptAborted,
},
{
testFilename: "abort_teardown.js",
expErr: errext.AbortTest,
expExitCode: exitcodes.ScriptAborted,
expLogOutput: "Calling teardown function after test.abort()",
},
{
testFilename: "initerr.js",
expErr: "ReferenceError: someUndefinedVar is not defined",
expExitCode: exitcodes.ScriptException,
},
{
testFilename: "thresholds/non_existing_metric.js",
name: "run should fail with exit status 104 on a threshold applied to a non existing metric",
expErr: "invalid threshold",
expExitCode: exitcodes.InvalidConfig,
},
{
testFilename: "thresholds/non_existing_metric.js",
name: "run should succeed on a threshold applied to a non existing metric with the --no-thresholds flag set",
extraArgs: []string{"--no-thresholds"},
},
{
testFilename: "thresholds/non_existing_metric.js",
name: "run should succeed on a threshold applied to a non existing submetric with the --no-thresholds flag set",
extraArgs: []string{"--no-thresholds"},
},
{
testFilename: "thresholds/malformed_expression.js",
name: "run should fail with exit status 104 on a malformed threshold expression",
expErr: "malformed threshold expression",
expExitCode: exitcodes.InvalidConfig,
},
{
testFilename: "thresholds/malformed_expression.js",
name: "run should on a malformed threshold expression but --no-thresholds flag set",
extraArgs: []string{"--no-thresholds"},
// we don't expect an error
},
{
testFilename: "thresholds/unsupported_aggregation_method.js",
name: "run should fail with exit status 104 on a threshold applying an unsupported aggregation method to a metric",
expErr: "invalid threshold",
expExitCode: exitcodes.InvalidConfig,
},
{
testFilename: "thresholds/unsupported_aggregation_method.js",
name: "run should succeed on a threshold applying an unsupported aggregation method to a metric with the --no-thresholds flag set",
extraArgs: []string{"--no-thresholds"},
},
}
for _, tc := range testCases {
tc := tc
name := tc.testFilename
if tc.name != "" {
name = fmt.Sprintf("%s (%s)", tc.testFilename, tc.name)
}
t.Run(name, func(t *testing.T) {
t.Parallel()
testScript, err := os.ReadFile(path.Join("testdata", tc.testFilename)) //nolint:forbidigo
require.NoError(t, err)
ts := tests.NewGlobalTestState(t)
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
ts.CmdArgs = append([]string{"k6", "run", tc.testFilename}, tc.extraArgs...)
ts.ExpectedExitCode = int(tc.expExitCode)
newRootCommand(ts.GlobalState).execute()
logs := ts.LoggerHook.Drain()
if tc.expErr != "" {
assert.True(t, testutils.LogContains(logs, logrus.ErrorLevel, tc.expErr))
}
if tc.expLogOutput != "" {
assert.True(t, testutils.LogContains(logs, logrus.InfoLevel, tc.expLogOutput))
}
})
}
}
func TestInvalidOptionsThresholdErrExitCode(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
testFilename string
expExitCode exitcodes.ExitCode
extraArgs []string
}{
{
name: "run should fail with exit status 104 on a malformed threshold expression",
testFilename: "thresholds/malformed_expression.js",
expExitCode: exitcodes.InvalidConfig,
},
{
name: "run should fail with exit status 104 on a threshold applied to a non existing metric",
testFilename: "thresholds/non_existing_metric.js",
expExitCode: exitcodes.InvalidConfig,
},
{
name: "run should fail with exit status 104 on a threshold method being unsupported by the metric",
testFilename: "thresholds/unsupported_aggregation_method.js",
expExitCode: exitcodes.InvalidConfig,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
testScript, err := os.ReadFile(path.Join("testdata", tc.testFilename)) //nolint:forbidigo
require.NoError(t, err)
ts := tests.NewGlobalTestState(t)
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
ts.CmdArgs = append([]string{"k6", "run", tc.testFilename}, tc.extraArgs...)
ts.ExpectedExitCode = int(tc.expExitCode)
newRootCommand(ts.GlobalState).execute()
})
}
}
func TestThresholdsRuntimeBehavior(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
testFilename string
expExitCode exitcodes.ExitCode
expStdoutContains string
expStdoutNotContains string
extraArgs []string
}{
{
name: "#2518: submetrics without values should be rendered under their parent metric #2518",
testFilename: "thresholds/thresholds_on_submetric_without_samples.js",
expExitCode: 0,
expStdoutContains: " one..................: 0 0/s\n { tag:xyz }........: 0 0/s\n",
},
{
name: "#2512: parsing threshold names containing parsable tokens should be valid",
testFilename: "thresholds/name_contains_tokens.js",
expExitCode: 0,
},
{
name: "#2520: thresholds over metrics without values should avoid division by zero and displaying NaN values",
testFilename: "thresholds/empty_sink_no_nan.js",
expExitCode: 0,
expStdoutContains: "rate.................: 0.00%",
expStdoutNotContains: "NaN",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
testScript, err := os.ReadFile(path.Join("testdata", tc.testFilename)) //nolint:forbidigo
require.NoError(t, err)
ts := tests.NewGlobalTestState(t)
require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, tc.testFilename), testScript, 0o644))
ts.CmdArgs = []string{"k6", "run", tc.testFilename}
ts.ExpectedExitCode = int(tc.expExitCode)
newRootCommand(ts.GlobalState).execute()
if tc.expStdoutContains != "" {
assert.Contains(t, ts.Stdout.String(), tc.expStdoutContains)
}
if tc.expStdoutNotContains != "" {
t.Log(ts.Stdout.String())
assert.NotContains(t, ts.Stdout.String(), tc.expStdoutNotContains)
}
})
}
}