forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
191 lines (175 loc) · 5.63 KB
/
config_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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2019 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package cmd
import (
"fmt"
"testing"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v3"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/executor"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/types"
)
type testCmdData struct {
Name string
Tests []testCmdTest
}
type testCmdTest struct {
Args []string
Expected []string
Name string
}
func TestConfigCmd(t *testing.T) {
testdata := []testCmdData{
{
Name: "Out",
Tests: []testCmdTest{
{
Name: "NoArgs",
Args: []string{""},
Expected: []string{},
},
{
Name: "SingleArg",
Args: []string{"--out", "influxdb=http://localhost:8086/k6"},
Expected: []string{"influxdb=http://localhost:8086/k6"},
},
{
Name: "MultiArg",
Args: []string{"--out", "influxdb=http://localhost:8086/k6", "--out", "json=test.json"},
Expected: []string{"influxdb=http://localhost:8086/k6", "json=test.json"},
},
},
},
}
for _, data := range testdata {
t.Run(data.Name, func(t *testing.T) {
for _, test := range data.Tests {
t.Run(`"`+test.Name+`"`, func(t *testing.T) {
fs := configFlagSet()
fs.AddFlagSet(optionFlagSet())
assert.NoError(t, fs.Parse(test.Args))
config, err := getConfig(fs)
assert.NoError(t, err)
assert.Equal(t, test.Expected, config.Out)
})
}
})
}
}
func TestConfigEnv(t *testing.T) {
testdata := map[struct{ Name, Key string }]map[string]func(Config){
{"Linger", "K6_LINGER"}: {
"": func(c Config) { assert.Equal(t, null.Bool{}, c.Linger) },
"true": func(c Config) { assert.Equal(t, null.BoolFrom(true), c.Linger) },
"false": func(c Config) { assert.Equal(t, null.BoolFrom(false), c.Linger) },
},
{"NoUsageReport", "K6_NO_USAGE_REPORT"}: {
"": func(c Config) { assert.Equal(t, null.Bool{}, c.NoUsageReport) },
"true": func(c Config) { assert.Equal(t, null.BoolFrom(true), c.NoUsageReport) },
"false": func(c Config) { assert.Equal(t, null.BoolFrom(false), c.NoUsageReport) },
},
{"Out", "K6_OUT"}: {
"": func(c Config) { assert.Equal(t, []string{}, c.Out) },
"influxdb": func(c Config) { assert.Equal(t, []string{"influxdb"}, c.Out) },
},
}
for field, data := range testdata {
field, data := field, data
t.Run(field.Name, func(t *testing.T) {
for value, fn := range data {
value, fn := value, fn
t.Run(`"`+value+`"`, func(t *testing.T) {
restore := testutils.SetEnv(t, []string{fmt.Sprintf("%s=%s", field.Key, value)})
defer restore()
var config Config
assert.NoError(t, envconfig.Process("", &config))
fn(config)
})
}
})
}
}
func TestConfigApply(t *testing.T) {
t.Run("Linger", func(t *testing.T) {
conf := Config{}.Apply(Config{Linger: null.BoolFrom(true)})
assert.Equal(t, null.BoolFrom(true), conf.Linger)
})
t.Run("NoUsageReport", func(t *testing.T) {
conf := Config{}.Apply(Config{NoUsageReport: null.BoolFrom(true)})
assert.Equal(t, null.BoolFrom(true), conf.NoUsageReport)
})
t.Run("Out", func(t *testing.T) {
conf := Config{}.Apply(Config{Out: []string{"influxdb"}})
assert.Equal(t, []string{"influxdb"}, conf.Out)
conf = Config{}.Apply(Config{Out: []string{"influxdb", "json"}})
assert.Equal(t, []string{"influxdb", "json"}, conf.Out)
})
}
func TestDeriveAndValidateConfig(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
conf Config
isExec bool
err string
}{
{"defaultOK", Config{}, true, ""},
{"defaultErr", Config{}, false,
"executor default: function 'default' not found in exports"},
{"nonDefaultOK", Config{Options: lib.Options{Scenarios: lib.ScenarioConfigs{
"per_vu_iters": executor.PerVUIterationsConfig{BaseConfig: executor.BaseConfig{
Name: "per_vu_iters", Type: "per-vu-iterations", Exec: null.StringFrom("nonDefault")},
VUs: null.IntFrom(1),
Iterations: null.IntFrom(1),
MaxDuration: types.NullDurationFrom(time.Second),
}}}}, true, "",
},
{"nonDefaultErr", Config{Options: lib.Options{Scenarios: lib.ScenarioConfigs{
"per_vu_iters": executor.PerVUIterationsConfig{BaseConfig: executor.BaseConfig{
Name: "per_vu_iters", Type: "per-vu-iterations", Exec: null.StringFrom("nonDefaultErr")},
VUs: null.IntFrom(1),
Iterations: null.IntFrom(1),
MaxDuration: types.NullDurationFrom(time.Second),
}}}}, false,
"executor per_vu_iters: function 'nonDefaultErr' not found in exports",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
_, err := deriveAndValidateConfig(tc.conf,
func(_ string) bool { return tc.isExec })
if tc.err != "" {
var ecerr errext.HasExitCode
assert.ErrorAs(t, err, &ecerr)
assert.Equal(t, exitcodes.InvalidConfig, ecerr.ExitCode())
assert.Contains(t, err.Error(), tc.err)
} else {
assert.NoError(t, err)
}
})
}
}