Best K6 code snippet using cmd.getConfigConsolidationTestCases
config_consolidation_test.go
Source:config_consolidation_test.go
...145 options opts146 expected exp147 customValidator func(t *testing.T, c Config)148}149func getConfigConsolidationTestCases() []configConsolidationTestCase {150 defaultConfig := func(jsonConfig string) afero.Fs {151 return getFS([]file{{152 filepath.Join(".config", "loadimpact", "k6", defaultConfigFileName), // TODO: improve153 jsonConfig,154 }})155 }156 I := null.IntFrom // shortcut for "Valid" (i.e. user-specified) ints157 // This is a function, because some of these test cases actually need for the init() functions158 // to be executed, since they depend on defaultConfigFilePath159 return []configConsolidationTestCase{160 // Check that no options will result in 1 VU 1 iter value for execution161 {opts{}, exp{}, verifyOneIterPerOneVU},162 // Verify some CLI errors163 {opts{cli: []string{"--blah", "blah"}}, exp{cliParseError: true}, nil},164 {opts{cli: []string{"--duration", "blah"}}, exp{cliParseError: true}, nil},165 {opts{cli: []string{"--duration", "1000"}}, exp{cliParseError: true}, nil}, // intentionally unsupported166 {opts{cli: []string{"--iterations", "blah"}}, exp{cliParseError: true}, nil},167 {opts{cli: []string{"--execution", ""}}, exp{cliParseError: true}, nil},168 {opts{cli: []string{"--stage", "10:20s"}}, exp{cliReadError: true}, nil},169 {opts{cli: []string{"--stage", "1000:20"}}, exp{cliReadError: true}, nil}, // intentionally unsupported170 // Check if CLI shortcuts generate correct execution values171 {opts{cli: []string{"--vus", "1", "--iterations", "5"}}, exp{}, verifySharedIters(I(1), I(5))},172 {opts{cli: []string{"-u", "2", "-i", "6"}}, exp{}, verifySharedIters(I(2), I(6))},173 {opts{cli: []string{"-d", "123s"}}, exp{}, verifyConstLoopingVUs(null.NewInt(1, false), 123*time.Second)},174 {opts{cli: []string{"-u", "3", "-d", "30s"}}, exp{}, verifyConstLoopingVUs(I(3), 30*time.Second)},175 {opts{cli: []string{"-u", "4", "--duration", "60s"}}, exp{}, verifyConstLoopingVUs(I(4), 1*time.Minute)},176 {177 opts{cli: []string{"--stage", "20s:10", "-s", "3m:5"}},178 exp{},179 verifyRampingVUs(null.NewInt(1, false), buildStages(20, 10, 180, 5)),180 },181 {182 opts{cli: []string{"-s", "1m6s:5", "--vus", "10"}},183 exp{},184 verifyRampingVUs(null.NewInt(10, true), buildStages(66, 5)),185 },186 {opts{cli: []string{"-u", "1", "-i", "6", "-d", "10s"}}, exp{}, func(t *testing.T, c Config) {187 verifySharedIters(I(1), I(6))(t, c)188 sharedIterConfig, ok := c.Scenarios[lib.DefaultScenarioName].(executor.SharedIterationsConfig)189 require.True(t, ok)190 assert.Equal(t, sharedIterConfig.MaxDuration.TimeDuration(), 10*time.Second)191 }},192 // This should get a validation error since VUs are more than the shared iterations193 {opts{cli: []string{"--vus", "10", "-i", "6"}}, exp{validationErrors: true}, verifySharedIters(I(10), I(6))},194 {opts{cli: []string{"-s", "10s:5", "-s", "10s:"}}, exp{validationErrors: true}, nil},195 {opts{fs: defaultConfig(`{"stages": [{"duration": "20s"}], "vus": 10}`)}, exp{validationErrors: true}, nil},196 // These should emit a derivation error197 {opts{cli: []string{"-u", "2", "-d", "10s", "-s", "10s:20"}}, exp{derivationError: true}, nil},198 {opts{cli: []string{"-u", "3", "-i", "5", "-s", "10s:20"}}, exp{derivationError: true}, nil},199 {opts{cli: []string{"-u", "3", "-d", "0"}}, exp{derivationError: true}, nil},200 {201 opts{runner: &lib.Options{202 VUs: null.IntFrom(5),203 Duration: types.NullDurationFrom(44 * time.Second),204 Stages: []lib.Stage{205 {Duration: types.NullDurationFrom(3 * time.Second), Target: I(20)},206 },207 }}, exp{derivationError: true}, nil,208 },209 {opts{fs: defaultConfig(`{"scenarios": {}}`)}, exp{logWarning: true}, verifyOneIterPerOneVU},210 // Test if environment variable shortcuts are working as expected211 {opts{env: []string{"K6_VUS=5", "K6_ITERATIONS=15"}}, exp{}, verifySharedIters(I(5), I(15))},212 {opts{env: []string{"K6_VUS=10", "K6_DURATION=20s"}}, exp{}, verifyConstLoopingVUs(I(10), 20*time.Second)},213 {opts{env: []string{"K6_VUS=10", "K6_DURATION=10000"}}, exp{}, verifyConstLoopingVUs(I(10), 10*time.Second)},214 {215 opts{env: []string{"K6_STAGES=2m30s:11,1h1m:100"}},216 exp{},217 verifyRampingVUs(null.NewInt(1, false), buildStages(150, 11, 3660, 100)),218 },219 {220 opts{env: []string{"K6_STAGES=100s:100,0m30s:0", "K6_VUS=0"}},221 exp{},222 verifyRampingVUs(null.NewInt(0, true), buildStages(100, 100, 30, 0)),223 },224 {opts{env: []string{"K6_STAGES=1000:100"}}, exp{consolidationError: true}, nil}, // intentionally unsupported225 // Test if JSON configs work as expected226 {opts{fs: defaultConfig(`{"iterations": 77, "vus": 7}`)}, exp{}, verifySharedIters(I(7), I(77))},227 {opts{fs: defaultConfig(`wrong-json`)}, exp{consolidationError: true}, nil},228 {opts{fs: getFS(nil), cli: []string{"--config", "/my/config.file"}}, exp{consolidationError: true}, nil},229 // Test combinations between options and levels230 {opts{cli: []string{"--vus", "1"}}, exp{}, verifyOneIterPerOneVU},231 {opts{cli: []string{"--vus", "10"}}, exp{logWarning: true}, verifyOneIterPerOneVU},232 {233 opts{234 fs: getFS([]file{{"/my/config.file", `{"vus": 8, "duration": "2m"}`}}),235 cli: []string{"--config", "/my/config.file"},236 }, exp{}, verifyConstLoopingVUs(I(8), 120*time.Second),237 },238 {239 opts{240 fs: getFS([]file{{"/my/config.file", `{"duration": 20000}`}}),241 cli: []string{"--config", "/my/config.file"},242 }, exp{}, verifyConstLoopingVUs(null.NewInt(1, false), 20*time.Second),243 },244 {245 opts{246 fs: defaultConfig(`{"stages": [{"duration": "20s", "target": 20}], "vus": 10}`),247 env: []string{"K6_DURATION=15s"},248 cli: []string{"--stage", ""},249 },250 exp{logWarning: true},251 verifyOneIterPerOneVU,252 },253 {254 opts{255 runner: &lib.Options{VUs: null.IntFrom(5), Duration: types.NullDurationFrom(50 * time.Second)},256 cli: []string{"--stage", "5s:5"},257 },258 exp{},259 verifyRampingVUs(I(5), buildStages(5, 5)),260 },261 {262 opts{263 fs: defaultConfig(`{"stages": [{"duration": "20s", "target": 10}]}`),264 runner: &lib.Options{VUs: null.IntFrom(5)},265 },266 exp{},267 verifyRampingVUs(I(5), buildStages(20, 10)),268 },269 {270 opts{271 fs: defaultConfig(`{"stages": [{"duration": "20s", "target": 10}]}`),272 runner: &lib.Options{VUs: null.IntFrom(5)},273 env: []string{"K6_VUS=15", "K6_ITERATIONS=17"},274 },275 exp{},276 verifySharedIters(I(15), I(17)),277 },278 {279 opts{280 fs: defaultConfig(`{"stages": [{"duration": "11s", "target": 11}]}`),281 runner: &lib.Options{VUs: null.IntFrom(22)},282 env: []string{"K6_VUS=33"},283 cli: []string{"--stage", "44s:44", "-s", "55s:55"},284 },285 exp{},286 verifyRampingVUs(null.NewInt(33, true), buildStages(44, 44, 55, 55)),287 },288 // TODO: test the future full overwriting of the duration/iterations/stages/execution options289 {290 opts{291 fs: defaultConfig(`{292 "scenarios": { "someKey": {293 "executor": "constant-vus", "vus": 10, "duration": "60s", "gracefulStop": "10s",294 "startTime": "70s", "env": {"test": "mest"}, "exec": "someFunc"295 }}}`),296 env: []string{"K6_ITERATIONS=25"},297 cli: []string{"--vus", "12"},298 },299 exp{},300 verifySharedIters(I(12), I(25)),301 },302 {303 opts{304 fs: defaultConfig(`{"scenarios": { "foo": {305 "executor": "constant-vus", "vus": 2, "duration": "1d",306 "gracefulStop": "10000", "startTime": 1000.5307 }}}`),308 }, exp{}, func(t *testing.T, c Config) {309 exec := c.Scenarios["foo"]310 require.NotEmpty(t, exec)311 require.IsType(t, executor.ConstantVUsConfig{}, exec)312 clvc, ok := exec.(executor.ConstantVUsConfig)313 require.True(t, ok)314 assert.Equal(t, null.IntFrom(2), clvc.VUs)315 assert.Equal(t, types.NullDurationFrom(24*time.Hour), clvc.Duration)316 assert.Equal(t, types.NullDurationFrom(time.Second+500*time.Microsecond), clvc.StartTime)317 assert.Equal(t, types.NullDurationFrom(10*time.Second), clvc.GracefulStop)318 },319 },320 {321 opts{322 fs: defaultConfig(`{"scenarios": { "def": {323 "executor": "externally-controlled", "vus": 15, "duration": "2h"324 }}}`),325 },326 exp{},327 verifyExternallyExecuted("def", I(15), 2*time.Hour),328 },329 // TODO: test execution-segment330 // Just in case, verify that no options will result in the same 1 vu 1 iter config331 {opts{}, exp{}, verifyOneIterPerOneVU},332 // Test system tags333 {opts{}, exp{}, func(t *testing.T, c Config) {334 assert.Equal(t, &metrics.DefaultSystemTagSet, c.Options.SystemTags)335 }},336 {opts{cli: []string{"--system-tags", `""`}}, exp{}, func(t *testing.T, c Config) {337 assert.Equal(t, metrics.SystemTagSet(0), *c.Options.SystemTags)338 }},339 {340 opts{341 runner: &lib.Options{342 SystemTags: metrics.NewSystemTagSet(metrics.TagSubproto, metrics.TagURL),343 },344 },345 exp{},346 func(t *testing.T, c Config) {347 assert.Equal(348 t,349 *metrics.NewSystemTagSet(metrics.TagSubproto, metrics.TagURL),350 *c.Options.SystemTags,351 )352 },353 },354 // Test summary trend stats355 {opts{}, exp{}, func(t *testing.T, c Config) {356 assert.Equal(t, lib.DefaultSummaryTrendStats, c.Options.SummaryTrendStats)357 }},358 {opts{cli: []string{"--summary-trend-stats", ""}}, exp{}, func(t *testing.T, c Config) {359 assert.Equal(t, []string{}, c.Options.SummaryTrendStats)360 }},361 {opts{cli: []string{"--summary-trend-stats", "coun"}}, exp{consolidationError: true}, nil},362 {opts{cli: []string{"--summary-trend-stats", "med,avg,p("}}, exp{consolidationError: true}, nil},363 {opts{cli: []string{"--summary-trend-stats", "med,avg,p(-1)"}}, exp{consolidationError: true}, nil},364 {opts{cli: []string{"--summary-trend-stats", "med,avg,p(101)"}}, exp{consolidationError: true}, nil},365 {opts{cli: []string{"--summary-trend-stats", "med,avg,p(99.999)"}}, exp{}, func(t *testing.T, c Config) {366 assert.Equal(t, []string{"med", "avg", "p(99.999)"}, c.Options.SummaryTrendStats)367 }},368 {369 opts{runner: &lib.Options{SummaryTrendStats: []string{"avg", "p(90)", "count"}}},370 exp{},371 func(t *testing.T, c Config) {372 assert.Equal(t, []string{"avg", "p(90)", "count"}, c.Options.SummaryTrendStats)373 },374 },375 {opts{cli: []string{}}, exp{}, func(t *testing.T, c Config) {376 assert.Equal(t, types.DNSConfig{377 TTL: null.NewString("5m", false),378 Select: types.NullDNSSelect{DNSSelect: types.DNSrandom, Valid: false},379 Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},380 }, c.Options.DNS)381 }},382 {opts{env: []string{"K6_DNS=ttl=5,select=roundRobin"}}, exp{}, func(t *testing.T, c Config) {383 assert.Equal(t, types.DNSConfig{384 TTL: null.StringFrom("5"),385 Select: types.NullDNSSelect{DNSSelect: types.DNSroundRobin, Valid: true},386 Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},387 }, c.Options.DNS)388 }},389 {opts{env: []string{"K6_DNS=ttl=inf,select=random,policy=preferIPv6"}}, exp{}, func(t *testing.T, c Config) {390 assert.Equal(t, types.DNSConfig{391 TTL: null.StringFrom("inf"),392 Select: types.NullDNSSelect{DNSSelect: types.DNSrandom, Valid: true},393 Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv6, Valid: true},394 }, c.Options.DNS)395 }},396 // This is functionally invalid, but will error out in validation done in js.parseTTL().397 {opts{cli: []string{"--dns", "ttl=-1"}}, exp{}, func(t *testing.T, c Config) {398 assert.Equal(t, types.DNSConfig{399 TTL: null.StringFrom("-1"),400 Select: types.NullDNSSelect{DNSSelect: types.DNSrandom, Valid: false},401 Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},402 }, c.Options.DNS)403 }},404 {opts{cli: []string{"--dns", "ttl=0,blah=nope"}}, exp{cliReadError: true}, nil},405 {opts{cli: []string{"--dns", "ttl=0"}}, exp{}, func(t *testing.T, c Config) {406 assert.Equal(t, types.DNSConfig{407 TTL: null.StringFrom("0"),408 Select: types.NullDNSSelect{DNSSelect: types.DNSrandom, Valid: false},409 Policy: types.NullDNSPolicy{DNSPolicy: types.DNSpreferIPv4, Valid: false},410 }, c.Options.DNS)411 }},412 {opts{cli: []string{"--dns", "ttl=5s,select="}}, exp{cliReadError: true}, nil},413 {414 opts{fs: defaultConfig(`{"dns": {"ttl": "0", "select": "roundRobin", "policy": "onlyIPv4"}}`)},415 exp{},416 func(t *testing.T, c Config) {417 assert.Equal(t, types.DNSConfig{418 TTL: null.StringFrom("0"),419 Select: types.NullDNSSelect{DNSSelect: types.DNSroundRobin, Valid: true},420 Policy: types.NullDNSPolicy{DNSPolicy: types.DNSonlyIPv4, Valid: true},421 }, c.Options.DNS)422 },423 },424 {425 opts{426 fs: defaultConfig(`{"dns": {"ttl": "0"}}`),427 env: []string{"K6_DNS=ttl=30,policy=any"},428 },429 exp{},430 func(t *testing.T, c Config) {431 assert.Equal(t, types.DNSConfig{432 TTL: null.StringFrom("30"),433 Select: types.NullDNSSelect{DNSSelect: types.DNSrandom, Valid: false},434 Policy: types.NullDNSPolicy{DNSPolicy: types.DNSany, Valid: true},435 }, c.Options.DNS)436 },437 },438 {439 // CLI overrides all, falling back to env440 opts{441 fs: defaultConfig(`{"dns": {"ttl": "60", "select": "first"}}`),442 env: []string{"K6_DNS=ttl=30,select=random,policy=any"},443 cli: []string{"--dns", "ttl=5"},444 },445 exp{},446 func(t *testing.T, c Config) {447 assert.Equal(t, types.DNSConfig{448 TTL: null.StringFrom("5"),449 Select: types.NullDNSSelect{DNSSelect: types.DNSrandom, Valid: true},450 Policy: types.NullDNSPolicy{DNSPolicy: types.DNSany, Valid: true},451 }, c.Options.DNS)452 },453 },454 {455 opts{env: []string{"K6_NO_SETUP=true", "K6_NO_TEARDOWN=false"}},456 exp{},457 func(t *testing.T, c Config) {458 assert.Equal(t, null.BoolFrom(true), c.Options.NoSetup)459 assert.Equal(t, null.BoolFrom(false), c.Options.NoTeardown)460 },461 },462 {463 opts{env: []string{"K6_NO_SETUP=false", "K6_NO_TEARDOWN=bool"}},464 exp{465 consolidationError: true,466 },467 nil,468 },469 // TODO: test for differences between flagsets470 // TODO: more tests in general, especially ones not related to execution parameters...471 }472}473func runTestCase(t *testing.T, testCase configConsolidationTestCase, subCmd string) {474 t.Logf("Test for `k6 %s` with opts=%#v and exp=%#v\n", subCmd, testCase.options, testCase.expected)475 ts := newGlobalTestState(t)476 ts.args = append([]string{"k6", subCmd}, testCase.options.cli...)477 ts.envVars = buildEnvMap(testCase.options.env)478 if testCase.options.fs != nil {479 ts.globalState.fs = testCase.options.fs480 }481 rootCmd := newRootCommand(ts.globalState)482 cmd, args, err := rootCmd.cmd.Find(ts.args[1:])483 require.NoError(t, err)484 err = cmd.ParseFlags(args)485 if testCase.expected.cliParseError {486 require.Error(t, err)487 return488 }489 require.NoError(t, err)490 flagSet := cmd.Flags()491 // TODO: remove these hacks when we improve the configuration...492 var cliConf Config493 if flagSet.Lookup("out") != nil {494 cliConf, err = getConfig(flagSet)495 } else {496 opts, errOpts := getOptions(flagSet)497 cliConf, err = Config{Options: opts}, errOpts498 }499 if testCase.expected.cliReadError {500 require.Error(t, err)501 return502 }503 require.NoError(t, err)504 var opts lib.Options505 if testCase.options.runner != nil {506 opts = *testCase.options.runner507 }508 consolidatedConfig, err := getConsolidatedConfig(ts.globalState, cliConf, opts)509 if testCase.expected.consolidationError {510 require.Error(t, err)511 return512 }513 require.NoError(t, err)514 derivedConfig := consolidatedConfig515 derivedConfig.Options, err = executor.DeriveScenariosFromShortcuts(consolidatedConfig.Options, ts.logger)516 if testCase.expected.derivationError {517 require.Error(t, err)518 return519 }520 require.NoError(t, err)521 if warnings := ts.loggerHook.Drain(); testCase.expected.logWarning {522 assert.NotEmpty(t, warnings)523 } else {524 assert.Empty(t, warnings)525 }526 validationErrors := derivedConfig.Validate()527 if testCase.expected.validationErrors {528 assert.NotEmpty(t, validationErrors)529 } else {530 assert.Empty(t, validationErrors)531 }532 if testCase.customValidator != nil {533 testCase.customValidator(t, derivedConfig)534 }535}536func TestConfigConsolidation(t *testing.T) {537 t.Parallel()538 for tcNum, testCase := range getConfigConsolidationTestCases() {539 tcNum, testCase := tcNum, testCase540 subCommands := testCase.options.cmds541 if subCommands == nil { // handle the most common case542 subCommands = []string{"run", "archive", "cloud"}543 }544 for fsNum, subCmd := range subCommands {545 fsNum, subCmd := fsNum, subCmd546 t.Run(547 fmt.Sprintf("TestCase#%d_FlagSet#%d", tcNum, fsNum),548 func(t *testing.T) {549 t.Parallel()550 runTestCase(t, testCase, subCmd)551 },552 )...
getConfigConsolidationTestCases
Using AI Code Generation
1import (2func main() {3 clt, err := client.NewClientFromConfigFile("config.json")4 if err != nil {5 fmt.Println(err)6 }7 cmd := types.NewCmd(clt)8 resp, err := cmd.GetConfigConsolidationTestCases()9 if err != nil {10 fmt.Println(err)11 }12 fmt.Println(resp)13}14import (15func main() {16 clt, err := client.NewClientFromConfigFile("config.json")17 if err != nil {18 fmt.Println(err)19 }20 cmd := types.NewCmd(clt)21 resp, err := cmd.GetConfigConsolidationTestCases()22 if err != nil {23 fmt.Println(err)24 }25 fmt.Println(resp)26}27import (28func main() {
getConfigConsolidationTestCases
Using AI Code Generation
1func getConfigConsolidationTestCases(cmd *cobra.Command, args []string) {2}3func getConfigConsolidationTestCases(cmd *cobra.Command, args []string) {4}5func getConfigConsolidationTestCases(cmd *cobra.Command, args []string) {6}7func getConfigConsolidationTestCases(cmd *cobra.Command, args []string) {8}9func getConfigConsolidationTestCases(cmd *cobra.Command, args []string) {10}11func getConfigConsolidationTestCases(cmd *cobra.Command, args []string) {12}13func getConfigConsolidationTestCases(cmd *cobra.Command, args []string) {14}15func getConfigConsolidationTestCases(cmd *cobra.Command, args []string) {16}17func getConfigConsolidationTestCases(cmd *cobra.Command, args []string) {18}
getConfigConsolidationTestCases
Using AI Code Generation
1func getConfigConsolidationTestCases() []testCase {2 testCases = append(testCases, testCase{3 args: args{4 },5 expected: &cobra.Command{6 },7 })8 testCases = append(testCases, testCase{9 args: args{10 },11 expected: &cobra.Command{12 },13 })14}15func getConfigConsolidationTestCases() []testCase {16 testCases = append(testCases, testCase{17 args: args{18 },19 expected: &cobra.Command{20 },21 })22 testCases = append(testCases, testCase{23 args: args{24 },
getConfigConsolidationTestCases
Using AI Code Generation
1func getConfigConsolidationTestCases() []testcase {2 return []testcase{3 {4 args: args{5 config: config{6 },7 },8 want: want{9 config: config{10 },11 },12 },13 }14}15func TestGetConfig(t *testing.T) {16 cmd := cmd{}17 for _, tt := range getConfigConsolidationTestCases() {18 t.Run(tt.name, func(t *testing.T) {19 cmd.getConfig(tt.args.config)20 if !reflect.DeepEqual(cmd.config, tt.want.config) {21 t.Errorf("getConfig() got = %v, want %v", cmd.config, tt.want.config)22 }23 })24 }25}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!