Skip to content

Commit 21e113d

Browse files
authored
feat: add sharding support (#2023)
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
1 parent 0bf4992 commit 21e113d

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

.vscode/launch.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"./testdata/e2e/examples",
1717
"--config",
1818
"./testdata/e2e/config.yaml",
19+
"--remarshal",
1920
]
2021
},
2122
{

pkg/commands/test/command.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type options struct {
6161
values []string
6262
clusters []string
6363
remarshal bool
64+
shardIndex int
65+
shardCount int
6466
}
6567

6668
func Command() *cobra.Command {
@@ -268,7 +270,10 @@ func Command() *cobra.Command {
268270
}
269271
fmt.Fprintf(out, "- NoCluster %v\n", options.noCluster)
270272
fmt.Fprintf(out, "- PauseOnFailure %v\n", options.pauseOnFailure)
271-
// loading tests
273+
if options.shardCount > 0 {
274+
fmt.Fprintf(out, "- Shard %v / %v\n", options.shardIndex, options.shardCount)
275+
}
276+
// load tests
272277
fmt.Fprintln(out, "Loading tests...")
273278
if err := fsutils.CheckFolders(options.testDirs...); err != nil {
274279
return err
@@ -285,6 +290,16 @@ func Command() *cobra.Command {
285290
if err != nil {
286291
return err
287292
}
293+
// TODO: we may want to find a sort key here ?
294+
if options.shardCount > 0 && options.shardIndex < options.shardCount {
295+
shardLen := float64(len(tests)) / float64(options.shardCount)
296+
shardStart := int(shardLen * float64(options.shardIndex))
297+
shardEnd := int(shardLen * float64(options.shardIndex+1))
298+
if options.shardIndex == options.shardCount-1 {
299+
shardEnd = len(tests)
300+
}
301+
tests = tests[shardStart:shardEnd]
302+
}
288303
var testToRun []discovery.Test
289304
for _, test := range tests {
290305
if test.Err != nil {
@@ -294,7 +309,7 @@ func Command() *cobra.Command {
294309
testToRun = append(testToRun, test)
295310
}
296311
}
297-
// loading tests
312+
// load values
298313
fmt.Fprintln(out, "Loading values...")
299314
values, err := values.Load(options.values...)
300315
if err != nil {
@@ -378,6 +393,9 @@ func Command() *cobra.Command {
378393
cmd.Flags().StringSliceVar(&options.selector, "selector", nil, "Selector (label query) to filter on")
379394
// external values
380395
cmd.Flags().StringSliceVar(&options.values, "values", nil, "Values passed to the tests")
396+
// sharding
397+
cmd.Flags().IntVar(&options.shardIndex, "shard-index", 0, "Current shard index (if `--shard-count` > 0)")
398+
cmd.Flags().IntVar(&options.shardCount, "shard-count", 0, "Number of shards")
381399
// others
382400
cmd.Flags().BoolVar(&options.noColor, "no-color", false, "Removes output colors")
383401
cmd.Flags().BoolVar(&options.remarshal, "remarshal", false, "Remarshals tests yaml to apply anchors before parsing")

testdata/commands/test/help.txt

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Flags:
5050
--report-name string The name of the report to create (default "chainsaw-report")
5151
--report-path string The path of the report to create
5252
--selector strings Selector (label query) to filter on
53+
--shard-count int Number of shards
54+
--shard-index --shard-count Current shard index (if --shard-count > 0)
5355
--skip-delete If set, do not delete the resources after running the tests
5456
--template If set, resources will be considered for templating (default true)
5557
--test-dir strings Directories containing test cases to run

website/docs/reference/commands/chainsaw_test.md

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ chainsaw test [flags]... [test directories]...
5555
--report-name string The name of the report to create (default "chainsaw-report")
5656
--report-path string The path of the report to create
5757
--selector strings Selector (label query) to filter on
58+
--shard-count int Number of shards
59+
--shard-index --shard-count Current shard index (if --shard-count > 0)
5860
--skip-delete If set, do not delete the resources after running the tests
5961
--template If set, resources will be considered for templating (default true)
6062
--test-dir strings Directories containing test cases to run

0 commit comments

Comments
 (0)