-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
config.go
269 lines (234 loc) · 8.2 KB
/
config.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
package cmd
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"path/filepath"
"strings"
"time"
"github.com/mstoykov/envconfig"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"gopkg.in/guregu/null.v3"
"go.k6.io/k6/cmd/state"
"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/fsext"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/metrics"
)
// configFlagSet returns a FlagSet with the default run configuration flags.
func configFlagSet() *pflag.FlagSet {
flags := pflag.NewFlagSet("", 0)
flags.SortFlags = false
flags.StringArrayP("out", "o", []string{}, "`uri` for an external metrics database")
flags.BoolP("linger", "l", false, "keep the API server alive past test end")
flags.Bool("no-usage-report", false, "don't send anonymous stats to the developers")
return flags
}
// Config ...
type Config struct {
lib.Options
Out []string `json:"out" envconfig:"K6_OUT"`
Linger null.Bool `json:"linger" envconfig:"K6_LINGER"`
NoUsageReport null.Bool `json:"noUsageReport" envconfig:"K6_NO_USAGE_REPORT"`
// TODO: deprecate
Collectors map[string]json.RawMessage `json:"collectors"`
}
// Validate checks if all of the specified options make sense
func (c Config) Validate() []error {
errors := c.Options.Validate()
// TODO: validate all of the other options... that we should have already been validating...
// TODO: maybe integrate an external validation lib: https://github.com/avelino/awesome-go#validation
return errors
}
// Apply the provided config on top of the current one, returning a new one. The provided config has priority.
func (c Config) Apply(cfg Config) Config {
c.Options = c.Options.Apply(cfg.Options)
if len(cfg.Out) > 0 {
c.Out = cfg.Out
}
if cfg.Linger.Valid {
c.Linger = cfg.Linger
}
if cfg.NoUsageReport.Valid {
c.NoUsageReport = cfg.NoUsageReport
}
if len(cfg.Collectors) > 0 {
c.Collectors = cfg.Collectors
}
return c
}
// Returns a Config but only parses the Options inside.
func getPartialConfig(flags *pflag.FlagSet) (Config, error) {
opts, err := getOptions(flags)
if err != nil {
return Config{}, err
}
return Config{Options: opts}, nil
}
// Gets configuration from CLI flags.
func getConfig(flags *pflag.FlagSet) (Config, error) {
opts, err := getOptions(flags)
if err != nil {
return Config{}, err
}
out, err := flags.GetStringArray("out")
if err != nil {
return Config{}, err
}
return Config{
Options: opts,
Out: out,
Linger: getNullBool(flags, "linger"),
NoUsageReport: getNullBool(flags, "no-usage-report"),
}, nil
}
// Reads the configuration file from the supplied filesystem and returns it or
// an error. The only situation in which an error won't be returned is if the
// user didn't explicitly specify a config file path and the default config file
// doesn't exist.
func readDiskConfig(gs *state.GlobalState) (Config, error) {
// Try to see if the file exists in the supplied filesystem
if _, err := gs.FS.Stat(gs.Flags.ConfigFilePath); err != nil {
if errors.Is(err, fs.ErrNotExist) && gs.Flags.ConfigFilePath == gs.DefaultFlags.ConfigFilePath {
// If the file doesn't exist, but it was the default config file (i.e. the user
// didn't specify anything), silence the error
err = nil
}
return Config{}, err
}
data, err := fsext.ReadFile(gs.FS, gs.Flags.ConfigFilePath)
if err != nil {
return Config{}, fmt.Errorf("couldn't load the configuration from %q: %w", gs.Flags.ConfigFilePath, err)
}
var conf Config
err = json.Unmarshal(data, &conf)
if err != nil {
return Config{}, fmt.Errorf("couldn't parse the configuration from %q: %w", gs.Flags.ConfigFilePath, err)
}
return conf, nil
}
// Serializes the configuration to a JSON file and writes it in the supplied
// location on the supplied filesystem
func writeDiskConfig(gs *state.GlobalState, conf Config) error {
data, err := json.MarshalIndent(conf, "", " ")
if err != nil {
return err
}
if err := gs.FS.MkdirAll(filepath.Dir(gs.Flags.ConfigFilePath), 0o755); err != nil {
return err
}
return fsext.WriteFile(gs.FS, gs.Flags.ConfigFilePath, data, 0o644)
}
// Reads configuration variables from the environment.
func readEnvConfig(envMap map[string]string) (Config, error) {
// TODO: replace envconfig and refactor the whole configuration from the ground up :/
conf := Config{}
err := envconfig.Process("", &conf, func(key string) (string, bool) {
v, ok := envMap[key]
return v, ok
})
return conf, err
}
// Assemble the final consolidated configuration from all of the different sources:
// - start with the CLI-provided options to get shadowed (non-Valid) defaults in there
// - add the global file config options
// - add the Runner-provided options (they may come from Bundle too if applicable)
// - add the environment variables
// - merge the user-supplied CLI flags back in on top, to give them the greatest priority
// - set some defaults if they weren't previously specified
// TODO: add better validation, more explicit default values and improve consistency between formats
// TODO: accumulate all errors and differentiate between the layers?
func getConsolidatedConfig(gs *state.GlobalState, cliConf Config, runnerOpts lib.Options) (conf Config, err error) {
// TODO: use errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig) where it makes sense?
fileConf, err := readDiskConfig(gs)
if err != nil {
return conf, err
}
envConf, err := readEnvConfig(gs.Env)
if err != nil {
return conf, err
}
conf = cliConf.Apply(fileConf)
conf = conf.Apply(Config{Options: runnerOpts})
conf = conf.Apply(envConf).Apply(cliConf)
conf = applyDefault(conf)
// TODO(imiric): Move this validation where it makes sense in the configuration
// refactor of #883. This repeats the trend stats validation already done
// for CLI flags in cmd.getOptions, in case other configuration sources
// (e.g. env vars) overrode our default value. This is not done in
// lib.Options.Validate to avoid circular imports.
if _, err = metrics.GetResolversForTrendColumns(conf.SummaryTrendStats); err != nil {
return conf, err
}
return conf, nil
}
// applyDefault applies the default options value if it is not specified.
// This happens with types which are not supported by "gopkg.in/guregu/null.v3".
//
// Note that if you add option default value here, also add it in command line argument help text.
func applyDefault(conf Config) Config {
if conf.SystemTags == nil {
conf.SystemTags = &metrics.DefaultSystemTagSet
}
if conf.SummaryTrendStats == nil {
conf.SummaryTrendStats = lib.DefaultSummaryTrendStats
}
defDNS := types.DefaultDNSConfig()
if !conf.DNS.TTL.Valid {
conf.DNS.TTL = defDNS.TTL
}
if !conf.DNS.Select.Valid {
conf.DNS.Select = defDNS.Select
}
if !conf.DNS.Policy.Valid {
conf.DNS.Policy = defDNS.Policy
}
if !conf.SetupTimeout.Valid {
conf.SetupTimeout.Duration = types.Duration(60 * time.Second)
}
if !conf.TeardownTimeout.Valid {
conf.TeardownTimeout.Duration = types.Duration(60 * time.Second)
}
return conf
}
func deriveAndValidateConfig(
conf Config, isExecutable func(string) bool, logger logrus.FieldLogger,
) (result Config, err error) {
result = conf
result.Options, err = executor.DeriveScenariosFromShortcuts(conf.Options, logger)
if err == nil {
err = validateConfig(result, isExecutable)
}
return result, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig)
}
func validateConfig(conf Config, isExecutable func(string) bool) error {
errList := conf.Validate()
for _, ec := range conf.Scenarios {
if err := validateScenarioConfig(ec, isExecutable); err != nil {
errList = append(errList, err)
}
}
return consolidateErrorMessage(errList, "There were problems with the specified script configuration:")
}
func consolidateErrorMessage(errList []error, title string) error {
if len(errList) == 0 {
return nil
}
errMsgParts := []string{title}
for _, err := range errList {
errMsgParts = append(errMsgParts, fmt.Sprintf("\t- %s", err.Error()))
}
return errors.New(strings.Join(errMsgParts, "\n"))
}
func validateScenarioConfig(conf lib.ExecutorConfig, isExecutable func(string) bool) error {
execFn := conf.GetExec()
if !isExecutable(execFn) {
return fmt.Errorf("executor %s: function '%s' not found in exports", conf.GetName(), execFn)
}
return nil
}