-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathintegration_test.go
481 lines (391 loc) · 15.4 KB
/
integration_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
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
package cmd
import (
"bytes"
"encoding/json"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/testutils/httpmultibin"
)
const (
noopDefaultFunc = `export default function() {};`
fooLogDefaultFunc = `export default function() { console.log('foo'); };`
noopHandleSummary = `
export function handleSummary(data) {
return {}; // silence the end of test summary
};
`
)
func TestVersion(t *testing.T) {
t.Parallel()
ts := newGlobalTestState(t)
ts.args = []string{"k6", "version"}
newRootCommand(ts.globalState).execute()
stdOut := ts.stdOut.String()
assert.Contains(t, stdOut, "k6 v"+consts.Version)
assert.Contains(t, stdOut, runtime.Version())
assert.Contains(t, stdOut, runtime.GOOS)
assert.Contains(t, stdOut, runtime.GOARCH)
assert.NotContains(t, stdOut[:len(stdOut)-1], "\n")
assert.Empty(t, ts.stdErr.Bytes())
assert.Empty(t, ts.loggerHook.Drain())
}
func TestSimpleTestStdin(t *testing.T) {
t.Parallel()
ts := newGlobalTestState(t)
ts.args = []string{"k6", "run", "-"}
ts.stdIn = bytes.NewBufferString(noopDefaultFunc)
newRootCommand(ts.globalState).execute()
stdOut := ts.stdOut.String()
assert.Contains(t, stdOut, "default: 1 iterations for each of 1 VUs")
assert.Contains(t, stdOut, "1 complete and 0 interrupted iterations")
assert.Empty(t, ts.stdErr.Bytes())
assert.Empty(t, ts.loggerHook.Drain())
}
func TestStdoutAndStderrAreEmptyWithQuietAndHandleSummary(t *testing.T) {
t.Parallel()
ts := newGlobalTestState(t)
ts.args = []string{"k6", "--quiet", "run", "-"}
ts.stdIn = bytes.NewBufferString(noopDefaultFunc + noopHandleSummary)
newRootCommand(ts.globalState).execute()
assert.Empty(t, ts.stdErr.Bytes())
assert.Empty(t, ts.stdOut.Bytes())
assert.Empty(t, ts.loggerHook.Drain())
}
func TestStdoutAndStderrAreEmptyWithQuietAndLogsForwarded(t *testing.T) {
t.Parallel()
ts := newGlobalTestState(t)
// TODO: add a test with relative path
logFilePath := filepath.Join(ts.cwd, "test.log")
ts.args = []string{
"k6", "--quiet", "--log-output", "file=" + logFilePath,
"--log-format", "raw", "run", "--no-summary", "-",
}
ts.stdIn = bytes.NewBufferString(fooLogDefaultFunc)
newRootCommand(ts.globalState).execute()
// The test state hook still catches this message
assert.True(t, testutils.LogContains(ts.loggerHook.Drain(), logrus.InfoLevel, `foo`))
// But it's not shown on stderr or stdout
assert.Empty(t, ts.stdErr.Bytes())
assert.Empty(t, ts.stdOut.Bytes())
// Instead it should be in the log file
logContents, err := afero.ReadFile(ts.fs, logFilePath)
require.NoError(t, err)
assert.Equal(t, "foo\n", string(logContents))
}
func TestRelativeLogPathWithSetupAndTeardown(t *testing.T) {
t.Parallel()
ts := newGlobalTestState(t)
ts.args = []string{"k6", "--log-output", "file=test.log", "--log-format", "raw", "run", "-i", "2", "-"}
ts.stdIn = bytes.NewBufferString(fooLogDefaultFunc + `
export function setup() { console.log('bar'); };
export function teardown() { console.log('baz'); };
`)
newRootCommand(ts.globalState).execute()
// The test state hook still catches these messages
logEntries := ts.loggerHook.Drain()
assert.True(t, testutils.LogContains(logEntries, logrus.InfoLevel, `foo`))
assert.True(t, testutils.LogContains(logEntries, logrus.InfoLevel, `bar`))
assert.True(t, testutils.LogContains(logEntries, logrus.InfoLevel, `baz`))
// And check that the log file also contains everything
logContents, err := afero.ReadFile(ts.fs, filepath.Join(ts.cwd, "test.log"))
require.NoError(t, err)
assert.Equal(t, "bar\nfoo\nfoo\nbaz\n", string(logContents))
}
func TestWrongCliFlagIterations(t *testing.T) {
t.Parallel()
ts := newGlobalTestState(t)
ts.args = []string{"k6", "run", "--iterations", "foo", "-"}
ts.stdIn = bytes.NewBufferString(noopDefaultFunc)
// TODO: check for exitcodes.InvalidConfig after https://github.com/loadimpact/k6/issues/883 is done...
ts.expectedExitCode = -1
newRootCommand(ts.globalState).execute()
assert.True(t, testutils.LogContains(ts.loggerHook.Drain(), logrus.ErrorLevel, `invalid argument "foo"`))
}
func TestWrongEnvVarIterations(t *testing.T) {
t.Parallel()
ts := newGlobalTestState(t)
ts.args = []string{"k6", "run", "--vus", "2", "-"}
ts.envVars = map[string]string{"K6_ITERATIONS": "4"}
ts.stdIn = bytes.NewBufferString(noopDefaultFunc)
newRootCommand(ts.globalState).execute()
stdOut := ts.stdOut.String()
t.Logf(stdOut)
assert.Contains(t, stdOut, "4 iterations shared among 2 VUs")
assert.Contains(t, stdOut, "4 complete and 0 interrupted iterations")
assert.Empty(t, ts.stdErr.Bytes())
assert.Empty(t, ts.loggerHook.Drain())
}
func TestMetricsAndThresholds(t *testing.T) {
t.Parallel()
script := `
import { Counter } from 'k6/metrics';
var setupCounter = new Counter('setup_counter');
var teardownCounter = new Counter('teardown_counter');
var defaultCounter = new Counter('default_counter');
let unusedCounter = new Counter('unused_counter');
export const options = {
scenarios: {
sc1: {
executor: 'per-vu-iterations',
vus: 1,
iterations: 1,
},
sc2: {
executor: 'shared-iterations',
vus: 1,
iterations: 1,
},
},
thresholds: {
'setup_counter': ['count == 1'],
'teardown_counter': ['count == 1'],
'default_counter': ['count == 2'],
'default_counter{scenario:sc1}': ['count == 1'],
'default_counter{scenario:sc2}': ['count == 1'],
'iterations': ['count == 2'],
'iterations{scenario:sc1}': ['count == 1'],
'iterations{scenario:sc2}': ['count == 1'],
'default_counter{nonexistent:tag}': ['count == 0'],
'unused_counter': ['count == 0'],
'http_req_duration{status:200}': [' max == 0'], // no HTTP requests
},
};
export function setup() {
console.log('setup() start');
setupCounter.add(1);
console.log('setup() end');
return { foo: 'bar' }
}
export default function (data) {
console.log('default(' + JSON.stringify(data) + ')');
defaultCounter.add(1);
}
export function teardown(data) {
console.log('teardown(' + JSON.stringify(data) + ')');
teardownCounter.add(1);
}
export function handleSummary(data) {
console.log('handleSummary()');
return { stdout: JSON.stringify(data, null, 4) }
}
`
ts := newGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.fs, filepath.Join(ts.cwd, "test.js"), []byte(script), 0o644))
ts.args = []string{"k6", "run", "--quiet", "--log-format=raw", "test.js"}
newRootCommand(ts.globalState).execute()
expLogLines := []string{
`setup() start`, `setup() end`, `default({"foo":"bar"})`,
`default({"foo":"bar"})`, `teardown({"foo":"bar"})`, `handleSummary()`,
}
logHookEntries := ts.loggerHook.Drain()
require.Len(t, logHookEntries, len(expLogLines))
for i, expLogLine := range expLogLines {
assert.Equal(t, expLogLine, logHookEntries[i].Message)
}
assert.Equal(t, strings.Join(expLogLines, "\n")+"\n", ts.stdErr.String())
var summary map[string]interface{}
require.NoError(t, json.Unmarshal(ts.stdOut.Bytes(), &summary))
metrics, ok := summary["metrics"].(map[string]interface{})
require.True(t, ok)
teardownCounter, ok := metrics["teardown_counter"].(map[string]interface{})
require.True(t, ok)
teardownThresholds, ok := teardownCounter["thresholds"].(map[string]interface{})
require.True(t, ok)
expected := map[string]interface{}{"count == 1": map[string]interface{}{"ok": true}}
require.Equal(t, expected, teardownThresholds)
}
func TestSSLKEYLOGFILEAbsolute(t *testing.T) {
t.Parallel()
ts := newGlobalTestState(t)
testSSLKEYLOGFILE(t, ts, filepath.Join(ts.cwd, "ssl.log"))
}
func TestSSLKEYLOGFILEARelative(t *testing.T) {
t.Parallel()
ts := newGlobalTestState(t)
testSSLKEYLOGFILE(t, ts, "./ssl.log")
}
func testSSLKEYLOGFILE(t *testing.T, ts *globalTestState, filePath string) {
t.Helper()
// TODO don't use insecureSkipTLSVerify when/if tlsConfig is given to the runner from outside
tb := httpmultibin.NewHTTPMultiBin(t)
ts.args = []string{"k6", "run", "-"}
ts.envVars = map[string]string{"SSLKEYLOGFILE": filePath}
ts.stdIn = bytes.NewReader([]byte(tb.Replacer.Replace(`
import http from "k6/http"
export const options = {
hosts: {
"HTTPSBIN_DOMAIN": "HTTPSBIN_IP",
},
insecureSkipTLSVerify: true,
}
export default () => {
http.get("HTTPSBIN_URL/get");
}
`)))
newRootCommand(ts.globalState).execute()
assert.True(t,
testutils.LogContains(ts.loggerHook.Drain(), logrus.WarnLevel, "SSLKEYLOGFILE was specified"))
sslloglines, err := afero.ReadFile(ts.fs, filepath.Join(ts.cwd, "ssl.log"))
require.NoError(t, err)
// TODO maybe have multiple depending on the ciphers used as that seems to change it
require.Regexp(t, "^CLIENT_[A-Z_]+ [0-9a-f]+ [0-9a-f]+\n", string(sslloglines))
}
func TestThresholdDeprecationWarnings(t *testing.T) {
t.Parallel()
// TODO: adjust this test after we actually make url, error, iter and vu non-indexable
ts := newGlobalTestState(t)
ts.args = []string{"k6", "run", "--system-tags", "url,error,vu,iter", "-"}
ts.stdIn = bytes.NewReader([]byte(`
export const options = {
thresholds: {
'http_req_duration{url:https://test.k6.io}': ['p(95)<500', 'p(99)<1000'],
'http_req_duration{error:foo}': ['p(99)<1000'],
'iterations{vu:1,iter:0}': ['count == 1'],
},
};
export default function () { }`,
))
newRootCommand(ts.globalState).execute()
logs := ts.loggerHook.Drain()
assert.True(t, testutils.LogContains(logs, logrus.WarnLevel,
"Thresholds like 'http_req_duration{url:https://test.k6.io}', based on the high-cardinality 'url' metric tag, are deprecated",
))
assert.True(t, testutils.LogContains(logs, logrus.WarnLevel,
"Thresholds like 'http_req_duration{error:foo}', based on the high-cardinality 'error' metric tag, are deprecated",
))
assert.True(t, testutils.LogContains(logs, logrus.WarnLevel,
"Thresholds like 'iterations{vu:1,iter:0}', based on the high-cardinality 'vu' metric tag, are deprecated",
))
assert.True(t, testutils.LogContains(logs, logrus.WarnLevel,
"Thresholds like 'iterations{vu:1,iter:0}', based on the high-cardinality 'iter' metric tag, are deprecated",
))
}
// TODO: add a hell of a lot more integration tests, including some that spin up
// a test HTTP server and actually check if k6 hits it
// TODO: also add a test that starts multiple k6 "instances", for example:
// - one with `k6 run --paused` and another with `k6 resume`
// - one with `k6 run` and another with `k6 stats` or `k6 status`
func TestExecutionTestOptionsDefaultValues(t *testing.T) {
t.Parallel()
script := `
import exec from 'k6/execution';
export default function () {
console.log(exec.test.options)
}
`
ts := newGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.fs, filepath.Join(ts.cwd, "test.js"), []byte(script), 0o644))
ts.args = []string{"k6", "run", "--iterations", "1", "test.js"}
newRootCommand(ts.globalState).execute()
loglines := ts.loggerHook.Drain()
require.Len(t, loglines, 1)
expected := `{"paused":null,"executionSegment":null,"executionSegmentSequence":null,"noSetup":null,"setupTimeout":null,"noTeardown":null,"teardownTimeout":null,"rps":null,"dns":{"ttl":null,"select":null,"policy":null},"maxRedirects":null,"userAgent":null,"batch":null,"batchPerHost":null,"httpDebug":null,"insecureSkipTLSVerify":null,"tlsCipherSuites":null,"tlsVersion":null,"tlsAuth":null,"throw":null,"thresholds":null,"blacklistIPs":null,"blockHostnames":null,"hosts":null,"noConnectionReuse":null,"noVUConnectionReuse":null,"minIterationDuration":null,"ext":null,"summaryTrendStats":["avg", "min", "med", "max", "p(90)", "p(95)"],"summaryTimeUnit":null,"systemTags":["check","error","error_code","expected_response","group","method","name","proto","scenario","service","status","subproto","tls_version","url"],"tags":null,"metricSamplesBufferSize":null,"noCookiesReset":null,"discardResponseBodies":null,"consoleOutput":null,"scenarios":{"default":{"vus":null,"iterations":1,"executor":"shared-iterations","maxDuration":null,"startTime":null,"env":null,"tags":null,"gracefulStop":null,"exec":null}},"localIPs":null}`
assert.JSONEq(t, expected, loglines[0].Message)
}
func TestSubMetricThresholdNoData(t *testing.T) {
t.Parallel()
script := `
import { Counter } from 'k6/metrics';
const counter1 = new Counter("one");
const counter2 = new Counter("two");
export const options = {
thresholds: {
'one{tag:xyz}': [],
},
};
export default function () {
counter2.add(42);
}
`
ts := newGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.fs, filepath.Join(ts.cwd, "test.js"), []byte(script), 0o644))
ts.args = []string{"k6", "run", "--quiet", "test.js"}
newRootCommand(ts.globalState).execute()
require.Len(t, ts.loggerHook.Drain(), 0)
require.Contains(t, ts.stdOut.String(), `
one..................: 0 0/s
{ tag:xyz }........: 0 0/s
two..................: 42`)
}
func TestSetupTeardownThresholds(t *testing.T) {
t.Parallel()
tb := httpmultibin.NewHTTPMultiBin(t)
script := []byte(tb.Replacer.Replace(`
import http from "k6/http";
import { check } from "k6";
import { Counter } from "k6/metrics";
let statusCheck = { "status is 200": (r) => r.status === 200 }
let myCounter = new Counter("setup_teardown");
export let options = {
iterations: 5,
thresholds: {
"setup_teardown": ["count == 2"],
"iterations": ["count == 5"],
"http_reqs": ["count == 7"],
},
};
export function setup() {
check(http.get("HTTPBIN_IP_URL"), statusCheck) && myCounter.add(1);
};
export default function () {
check(http.get("HTTPBIN_IP_URL"), statusCheck);
};
export function teardown() {
check(http.get("HTTPBIN_IP_URL"), statusCheck) && myCounter.add(1);
};
`))
ts := newGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.fs, filepath.Join(ts.cwd, "test.js"), script, 0o644))
ts.args = []string{"k6", "run", "test.js"}
newRootCommand(ts.globalState).execute()
require.Len(t, ts.loggerHook.Drain(), 0)
stdOut := ts.stdOut.String()
require.Contains(t, stdOut, `✓ http_reqs......................: 7`)
require.Contains(t, stdOut, `✓ iterations.....................: 5`)
require.Contains(t, stdOut, `✓ setup_teardown.................: 2`)
}
func TestThresholdsFailed(t *testing.T) {
t.Parallel()
tb := httpmultibin.NewHTTPMultiBin(t)
script := []byte(tb.Replacer.Replace(`
export let options = {
scenarios: {
sc1: {
executor: 'per-vu-iterations',
vus: 1, iterations: 1,
},
sc2: {
executor: 'shared-iterations',
vus: 1, iterations: 2,
},
},
thresholds: {
'iterations': ['count == 3'],
'iterations{scenario:sc1}': ['count == 2'],
'iterations{scenario:sc2}': ['count == 1'],
'iterations{scenario:sc3}': ['count == 0'],
},
};
export default function () {};
`))
ts := newGlobalTestState(t)
require.NoError(t, afero.WriteFile(ts.fs, filepath.Join(ts.cwd, "test.js"), script, 0o644))
ts.args = []string{"k6", "run", "test.js"}
ts.expectedExitCode = 99 // ThresholdsHaveFailed
newRootCommand(ts.globalState).execute()
assert.True(t, testutils.LogContains(ts.loggerHook.Drain(), logrus.ErrorLevel, `some thresholds have failed`))
stdOut := ts.stdOut.String()
t.Logf(stdOut)
require.Contains(t, stdOut, ` ✓ iterations...........: 3`)
require.Contains(t, stdOut, ` ✗ { scenario:sc1 }...: 1`)
require.Contains(t, stdOut, ` ✗ { scenario:sc2 }...: 2`)
require.Contains(t, stdOut, ` ✓ { scenario:sc3 }...: 0 0/s`)
}