-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathintegration_test.go
115 lines (91 loc) · 3.48 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
package cmd
import (
"bytes"
"path/filepath"
"testing"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/lib/testutils"
)
const (
noopDefaultFunc = `export default function() {};`
noopHandleSummary = `
export function handleSummary(data) {
return {}; // silence the end of test summary
};
`
)
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(`export default function() { console.log('foo'); };`)
newRootCommand(ts.globalState).execute()
// The our 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 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())
}
// 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`