[testsharder] Move makeShards() into testexec package
Stylistic/aesthetic move; the method seemed appropriate in the testexec
package. Moreover, as the sharding algorithm becomes more
complicated/nuanced, I believe it will serve to be a part of a larger
library.
Change-Id: Ib3065b83dca5a293afb325f3adcd257cb70f1177
diff --git a/cmd/testsharder/algorithm.go b/cmd/testsharder/algorithm.go
deleted file mode 100644
index 1ebb99f..0000000
--- a/cmd/testsharder/algorithm.go
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2018 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-package main
-
-import (
- "fmt"
- "sort"
-
- "fuchsia.googlesource.com/infra/infra/fuchsia/testexec"
-)
-
-// makeShards is the core algorithm to this tool. It takes a set of test specs and produces
-// a set of shards which may then be converted into Swarming tasks.
-//
-// This is the most naive algorithm at the moment. It just merges all tests together which
-// have the same environment setting into the same shard.
-func makeShards(specs []testexec.TestSpec, prefix string) []*testexec.Shard {
- // Collect the order of the shards so our shard ordering is deterministic with
- // respect to the input.
- envToSuites := make(map[testexec.Environment][]testexec.Test)
- envs := []testexec.Environment{}
- for _, spec := range specs {
- for _, env := range spec.Envs {
- if _, ok := envToSuites[env]; !ok {
- envs = append(envs, env)
- }
- envToSuites[env] = append(envToSuites[env], spec.Test)
- }
- }
-
- shards := make([]*testexec.Shard, 0, len(envs))
- for i, env := range envs {
- tests := envToSuites[env]
- sort.Slice(tests, func(i, j int) bool {
- return tests[i].Location < tests[j].Location
- })
- name := fmt.Sprintf("%04d", i)
- if prefix != "" {
- name = fmt.Sprintf("%s-%s", prefix, name)
- }
- shards = append(shards, &testexec.Shard{
- Name: name,
- Tests: tests,
- Env: env,
- })
- }
-
- return shards
-}
diff --git a/cmd/testsharder/main.go b/cmd/testsharder/main.go
index 9d0783d..7f524ac 100644
--- a/cmd/testsharder/main.go
+++ b/cmd/testsharder/main.go
@@ -63,7 +63,7 @@
// Create shards and write them to an output file if specifed, else stdout.
shards := &testexec.Shards{
- Shards: makeShards(specs, shardPrefix),
+ Shards: testexec.MakeShards(specs, shardPrefix),
}
f := os.Stdout
diff --git a/fuchsia/testexec/shard.go b/fuchsia/testexec/shard.go
index 44c575b..1a183a0 100644
--- a/fuchsia/testexec/shard.go
+++ b/fuchsia/testexec/shard.go
@@ -3,6 +3,11 @@
// found in the LICENSE file.
package testexec
+import (
+ "fmt"
+ "sort"
+)
+
// Shards is a JSON representation of a set of shards, used for serialization.
type Shards struct {
// Shards is a list of test shards.
@@ -20,3 +25,40 @@
// Env is a generalized notion of the execution environment for the shard.
Env Environment `json:"environment"`
}
+
+// MakeShards is the core algorithm to this tool. It takes a set of test specs and produces
+// a set of shards which may then be converted into Swarming tasks.
+//
+// This is the most naive algorithm at the moment. It just merges all tests together which
+// have the same environment setting into the same shard.
+func MakeShards(specs []TestSpec, prefix string) []*Shard {
+ // Collect the order of the shards so our shard ordering is deterministic with
+ // respect to the input.
+ envToSuites := make(map[Environment][]Test)
+ envs := []Environment{}
+ for _, spec := range specs {
+ for _, env := range spec.Envs {
+ if _, ok := envToSuites[env]; !ok {
+ envs = append(envs, env)
+ }
+ envToSuites[env] = append(envToSuites[env], spec.Test)
+ }
+ }
+ shards := make([]*Shard, 0, len(envs))
+ for i, env := range envs {
+ tests := envToSuites[env]
+ sort.Slice(tests, func(i, j int) bool {
+ return tests[i].Location < tests[j].Location
+ })
+ name := fmt.Sprintf("%04d", i)
+ if prefix != "" {
+ name = fmt.Sprintf("%s-%s", prefix, name)
+ }
+ shards = append(shards, &Shard{
+ Name: name,
+ Tests: tests,
+ Env: env,
+ })
+ }
+ return shards
+}
diff --git a/cmd/testsharder/algorithm_test.go b/fuchsia/testexec/shard_test.go
similarity index 91%
rename from cmd/testsharder/algorithm_test.go
rename to fuchsia/testexec/shard_test.go
index 4b96a14..13f6628 100644
--- a/cmd/testsharder/algorithm_test.go
+++ b/fuchsia/testexec/shard_test.go
@@ -1,7 +1,7 @@
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-package main
+package testexec_test
import (
"reflect"
@@ -43,7 +43,7 @@
},
}
t.Run("Ensure prefix applied", func(t *testing.T) {
- shards := makeShards([]testexec.TestSpec{spec1}, "prefix")
+ shards := testexec.MakeShards([]testexec.TestSpec{spec1}, "prefix")
expect := []*testexec.Shard{
{
Name: "prefix-0000",
@@ -59,7 +59,7 @@
})
t.Run("Ensure env shared", func(t *testing.T) {
- shards := makeShards([]testexec.TestSpec{spec1, spec2, spec3}, "")
+ shards := testexec.MakeShards([]testexec.TestSpec{spec1, spec2, spec3}, "")
expect := []*testexec.Shard{
// Ensure that the order of the shards is the order in which their
// corresponding environments appear in the input. This is the simplest