|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
| 5 | + "io/fs" |
4 | 6 | "testing"
|
5 | 7 | "time"
|
6 | 8 |
|
7 | 9 | "github.com/mstoykov/envconfig"
|
8 | 10 | "github.com/stretchr/testify/assert"
|
| 11 | + "github.com/stretchr/testify/require" |
9 | 12 | "gopkg.in/guregu/null.v3"
|
10 | 13 |
|
| 14 | + "go.k6.io/k6/cmd/state" |
11 | 15 | "go.k6.io/k6/errext"
|
12 | 16 | "go.k6.io/k6/errext/exitcodes"
|
13 | 17 | "go.k6.io/k6/lib"
|
14 | 18 | "go.k6.io/k6/lib/executor"
|
| 19 | + "go.k6.io/k6/lib/fsext" |
15 | 20 | "go.k6.io/k6/lib/types"
|
16 | 21 | )
|
17 | 22 |
|
@@ -202,3 +207,201 @@ func TestDeriveAndValidateConfig(t *testing.T) {
|
202 | 207 | })
|
203 | 208 | }
|
204 | 209 | }
|
| 210 | + |
| 211 | +func TestReadDiskConfigWithDefaultFlags(t *testing.T) { |
| 212 | + t.Parallel() |
| 213 | + memfs := fsext.NewMemMapFs() |
| 214 | + |
| 215 | + conf := []byte(`{"iterations":1028,"cloud":{"field1":"testvalue"}}`) |
| 216 | + defaultConfigPath := ".config/loadimpact/k6/config.json" |
| 217 | + require.NoError(t, fsext.WriteFile(memfs, defaultConfigPath, conf, 0o644)) |
| 218 | + |
| 219 | + defaultFlags := state.GetDefaultFlags(".config") |
| 220 | + gs := &state.GlobalState{ |
| 221 | + FS: memfs, |
| 222 | + Flags: defaultFlags, |
| 223 | + DefaultFlags: defaultFlags, |
| 224 | + } |
| 225 | + c, err := readDiskConfig(gs) |
| 226 | + require.NoError(t, err) |
| 227 | + |
| 228 | + assert.Equal(t, c.Iterations.Int64, int64(1028)) |
| 229 | + assert.JSONEq(t, `{"field1":"testvalue"}`, string(c.Cloud)) |
| 230 | +} |
| 231 | + |
| 232 | +func TestReadDiskConfigCustomFilePath(t *testing.T) { |
| 233 | + t.Parallel() |
| 234 | + memfs := fsext.NewMemMapFs() |
| 235 | + |
| 236 | + conf := []byte(`{"iterations":1028,"cloud":{"field1":"testvalue"}}`) |
| 237 | + require.NoError(t, fsext.WriteFile(memfs, "custom-path/config.json", conf, 0o644)) |
| 238 | + |
| 239 | + defaultFlags := state.GetDefaultFlags(".config") |
| 240 | + gs := &state.GlobalState{ |
| 241 | + FS: memfs, |
| 242 | + Flags: defaultFlags, |
| 243 | + DefaultFlags: defaultFlags, |
| 244 | + } |
| 245 | + gs.Flags.ConfigFilePath = "custom-path/config.json" |
| 246 | + |
| 247 | + c, err := readDiskConfig(gs) |
| 248 | + require.NoError(t, err) |
| 249 | + |
| 250 | + assert.Equal(t, c.Iterations.Int64, int64(1028)) |
| 251 | + assert.JSONEq(t, `{"field1":"testvalue"}`, string(c.Cloud)) |
| 252 | +} |
| 253 | + |
| 254 | +func TestReadDiskConfigNotFoundSilenced(t *testing.T) { |
| 255 | + t.Parallel() |
| 256 | + memfs := fsext.NewMemMapFs() |
| 257 | + |
| 258 | + // Put the file into a different and unexpected directory |
| 259 | + conf := []byte(`{"iterations":1028,"cloud":{"field1":"testvalue"}}`) |
| 260 | + defaultConfigPath := ".config/unknown-folder/k6/config.json" |
| 261 | + require.NoError(t, fsext.WriteFile(memfs, defaultConfigPath, conf, 0o644)) |
| 262 | + |
| 263 | + defaultFlags := state.GetDefaultFlags(".config") |
| 264 | + gs := &state.GlobalState{ |
| 265 | + FS: memfs, |
| 266 | + Flags: defaultFlags, |
| 267 | + DefaultFlags: defaultFlags, |
| 268 | + } |
| 269 | + c, err := readDiskConfig(gs) |
| 270 | + assert.NoError(t, err) |
| 271 | + assert.Empty(t, c) |
| 272 | +} |
| 273 | + |
| 274 | +func TestReadDiskConfigNotJSONExtension(t *testing.T) { |
| 275 | + t.Parallel() |
| 276 | + memfs := fsext.NewMemMapFs() |
| 277 | + |
| 278 | + conf := []byte(`{"iterations":1028,"cloud":{"field1":"testvalue"}}`) |
| 279 | + require.NoError(t, fsext.WriteFile(memfs, "custom-path/config.txt", conf, 0o644)) |
| 280 | + |
| 281 | + defaultFlags := state.GetDefaultFlags(".config") |
| 282 | + gs := &state.GlobalState{ |
| 283 | + FS: memfs, |
| 284 | + DefaultFlags: defaultFlags, |
| 285 | + Flags: defaultFlags, |
| 286 | + } |
| 287 | + gs.Flags.ConfigFilePath = "custom-path/config.txt" |
| 288 | + |
| 289 | + c, err := readDiskConfig(gs) |
| 290 | + require.NoError(t, err) |
| 291 | + |
| 292 | + assert.Equal(t, c.Iterations.Int64, int64(1028)) |
| 293 | + assert.JSONEq(t, `{"field1":"testvalue"}`, string(c.Cloud)) |
| 294 | +} |
| 295 | + |
| 296 | +func TestReadDiskConfigNotJSONContentError(t *testing.T) { |
| 297 | + t.Parallel() |
| 298 | + memfs := fsext.NewMemMapFs() |
| 299 | + |
| 300 | + conf := []byte(`bad json format`) |
| 301 | + defaultConfigPath := ".config/loadimpact/k6/config.json" |
| 302 | + require.NoError(t, fsext.WriteFile(memfs, defaultConfigPath, conf, 0o644)) |
| 303 | + |
| 304 | + gs := &state.GlobalState{ |
| 305 | + FS: memfs, |
| 306 | + Flags: state.GetDefaultFlags(".config"), |
| 307 | + } |
| 308 | + _, err := readDiskConfig(gs) |
| 309 | + var serr *json.SyntaxError |
| 310 | + assert.ErrorAs(t, err, &serr) |
| 311 | +} |
| 312 | + |
| 313 | +func TestReadDiskConfigNotFoundErrorWithCustomPath(t *testing.T) { |
| 314 | + t.Parallel() |
| 315 | + memfs := fsext.NewMemMapFs() |
| 316 | + |
| 317 | + defaultFlags := state.GetDefaultFlags(".config") |
| 318 | + gs := &state.GlobalState{ |
| 319 | + FS: memfs, |
| 320 | + Flags: defaultFlags, |
| 321 | + DefaultFlags: defaultFlags, |
| 322 | + } |
| 323 | + gs.Flags.ConfigFilePath = ".config/my-custom-path/k6/config.json" |
| 324 | + |
| 325 | + c, err := readDiskConfig(gs) |
| 326 | + assert.ErrorIs(t, err, fs.ErrNotExist) |
| 327 | + assert.Empty(t, c) |
| 328 | +} |
| 329 | + |
| 330 | +func TestWriteDiskConfigWithDefaultFlags(t *testing.T) { |
| 331 | + t.Parallel() |
| 332 | + memfs := fsext.NewMemMapFs() |
| 333 | + |
| 334 | + defaultFlags := state.GetDefaultFlags(".config") |
| 335 | + gs := &state.GlobalState{ |
| 336 | + FS: memfs, |
| 337 | + Flags: defaultFlags, |
| 338 | + DefaultFlags: defaultFlags, |
| 339 | + } |
| 340 | + |
| 341 | + c := Config{WebDashboard: null.BoolFrom(true)} |
| 342 | + err := writeDiskConfig(gs, c) |
| 343 | + require.NoError(t, err) |
| 344 | + |
| 345 | + finfo, err := memfs.Stat(".config/loadimpact/k6/config.json") |
| 346 | + require.NoError(t, err) |
| 347 | + assert.NotEmpty(t, finfo.Size()) |
| 348 | +} |
| 349 | + |
| 350 | +func TestWriteDiskConfigOverwrite(t *testing.T) { |
| 351 | + t.Parallel() |
| 352 | + memfs := fsext.NewMemMapFs() |
| 353 | + |
| 354 | + conf := []byte(`{"iterations":1028,"cloud":{"field1":"testvalue"}}`) |
| 355 | + defaultConfigPath := ".config/loadimpact/k6/config.json" |
| 356 | + require.NoError(t, fsext.WriteFile(memfs, defaultConfigPath, conf, 0o644)) |
| 357 | + |
| 358 | + defaultFlags := state.GetDefaultFlags(".config") |
| 359 | + gs := &state.GlobalState{ |
| 360 | + FS: memfs, |
| 361 | + Flags: defaultFlags, |
| 362 | + DefaultFlags: defaultFlags, |
| 363 | + } |
| 364 | + |
| 365 | + c := Config{WebDashboard: null.BoolFrom(true)} |
| 366 | + err := writeDiskConfig(gs, c) |
| 367 | + require.NoError(t, err) |
| 368 | +} |
| 369 | + |
| 370 | +func TestWriteDiskConfigCustomPath(t *testing.T) { |
| 371 | + t.Parallel() |
| 372 | + memfs := fsext.NewMemMapFs() |
| 373 | + |
| 374 | + defaultFlags := state.GetDefaultFlags(".config") |
| 375 | + gs := &state.GlobalState{ |
| 376 | + FS: memfs, |
| 377 | + Flags: defaultFlags, |
| 378 | + DefaultFlags: defaultFlags, |
| 379 | + } |
| 380 | + gs.Flags.ConfigFilePath = "my-custom-path/config.json" |
| 381 | + |
| 382 | + c := Config{WebDashboard: null.BoolFrom(true)} |
| 383 | + err := writeDiskConfig(gs, c) |
| 384 | + require.NoError(t, err) |
| 385 | +} |
| 386 | + |
| 387 | +func TestWriteDiskConfigNoJSONContentError(t *testing.T) { |
| 388 | + t.Parallel() |
| 389 | + memfs := fsext.NewMemMapFs() |
| 390 | + |
| 391 | + defaultFlags := state.GetDefaultFlags(".config") |
| 392 | + gs := &state.GlobalState{ |
| 393 | + FS: memfs, |
| 394 | + Flags: defaultFlags, |
| 395 | + DefaultFlags: defaultFlags, |
| 396 | + } |
| 397 | + |
| 398 | + c := Config{ |
| 399 | + WebDashboard: null.BoolFrom(true), |
| 400 | + Options: lib.Options{ |
| 401 | + Cloud: []byte(`invalid-json`), |
| 402 | + }, |
| 403 | + } |
| 404 | + err := writeDiskConfig(gs, c) |
| 405 | + var serr *json.SyntaxError |
| 406 | + assert.ErrorAs(t, err, &serr) |
| 407 | +} |
0 commit comments