| // 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 ( |
| "encoding/json" |
| "flag" |
| "io/ioutil" |
| "log" |
| "os" |
| |
| "fuchsia.googlesource.com/infra/infra/fuchsia/testexec" |
| ) |
| |
| // Flags. |
| var ( |
| // The path to the Fuchsia build output directory. |
| fuchsiaBuildDir string |
| |
| // The filepath containing a set of supported platforms. |
| platforms string |
| |
| // The architecture the tests were built for. |
| targetArch string |
| |
| // Human-friendly prefix for shard names. |
| shardPrefix string |
| |
| // The filepath to write output to. If unspecified, stdout is used. |
| outputFile string |
| ) |
| |
| func init() { |
| // TODO(mknyszek): Extend this in the future with options that influence |
| // the sharding algorithm/policy. |
| flag.StringVar(&fuchsiaBuildDir, "fuchsia-build-dir", "", "(required) path to the fuchsia build output directory") |
| flag.StringVar(&platforms, "platforms-file", "", "(required) path to a file containing a JSON of valid platforms") |
| flag.StringVar(&targetArch, "target-arch", "", "(required) target architecture") |
| flag.StringVar(&shardPrefix, "shard-prefix", "", "human-friendly prefix for shard names") |
| flag.StringVar(&outputFile, "output-file", "", "path to a file which will contain the shards as JSON, default is stdout") |
| } |
| |
| func main() { |
| flag.Parse() |
| |
| // Validate options. |
| if fuchsiaBuildDir == "" { |
| log.Fatal("must specify a Fuchsia build output directory") |
| } |
| if platforms == "" { |
| log.Fatal("must specify a file with a set of valid platforms") |
| } |
| if targetArch == "" { |
| log.Fatal("must specify a target CPU architecture") |
| } |
| |
| // Load platforms file. |
| bytes, err := ioutil.ReadFile(platforms) |
| if err != nil { |
| log.Fatal("failed to read platforms file:", err) |
| } |
| platforms := testexec.TestDevicePlatforms{} |
| if err := json.Unmarshal(bytes, &platforms); err != nil { |
| log.Fatal("failed to decode platforms file:", err) |
| } |
| |
| // Load spec files. |
| specs, err := testexec.LoadTestSpecs(fuchsiaBuildDir) |
| if err != nil { |
| log.Fatalf("failed to load test specs in %s: %s", fuchsiaBuildDir, err.Error()) |
| } |
| if len(specs) == 0 { |
| log.Fatal("no specs found in directory, exiting...") |
| } |
| for _, spec := range specs { |
| if err := spec.Validate(targetArch, platforms); err != nil { |
| log.Fatal("found invalid spec: ", err) |
| } |
| } |
| |
| // Create shards. |
| shards := &testexec.Shards{ |
| Shards: makeShards(specs, shardPrefix), |
| } |
| |
| f := os.Stdout |
| if outputFile != "" { |
| var err error |
| f, err = os.Create(outputFile) |
| if err != nil { |
| log.Fatalf("unable to create %s: %s", outputFile, err.Error()) |
| } |
| defer f.Close() |
| } |
| |
| // Write shards to output. |
| encoder := json.NewEncoder(f) |
| encoder.SetIndent("", " ") |
| if err := encoder.Encode(&shards); err != nil { |
| log.Fatal("failed to encode shards: ", err) |
| } |
| } |