blob: 9d0783d07339e6b52bfd0fc22ebdf997aba56600 [file] [log] [blame]
// 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"
"log"
"os"
"path/filepath"
"fuchsia.googlesource.com/infra/infra/fuchsia"
"fuchsia.googlesource.com/infra/infra/fuchsia/testexec"
)
var (
// The path to the Fuchsia build output directory.
fuchsiaBuildDir string
// Human-friendly prefix for shard names.
shardPrefix string
// The filepath to write output to. If unspecified, stdout is used.
outputFile string
)
func init() {
flag.StringVar(&fuchsiaBuildDir, "fuchsia-build-dir", "", "(required) path to the fuchsia build output directory")
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()
if fuchsiaBuildDir == "" {
log.Fatal("must specify a Fuchsia build output directory")
}
// Load spec files.
pkgManifestPath := filepath.Join(fuchsiaBuildDir, fuchsia.PackageManifestName)
pkgManifest, err := fuchsia.LoadPackageManifest(pkgManifestPath)
if err != nil {
log.Fatalf("failed to load package manifest from %s: %v", pkgManifestPath, err)
}
specs, err := testexec.LoadTestSpecs(fuchsiaBuildDir, *pkgManifest)
if err != nil {
log.Fatalf("failed to load test specs from %s: %v", fuchsiaBuildDir, err)
}
// Load test platform environments.
platformManifestPath := filepath.Join(fuchsiaBuildDir, fuchsia.PlatformManifestName)
platforms, err := testexec.LoadPlatforms(platformManifestPath)
if err != nil {
log.Fatalf("failed to load test platforms from %s: %v", platformManifestPath, err)
}
// Verify that the produced specs specify valid test environments.
if err = testexec.ValidateTestSpecs(specs, platforms); err != nil {
log.Fatalf("invalid test environment(s) found: %v", err)
}
// Create shards and write them to an output file if specifed, else stdout.
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: %v", outputFile, err)
}
defer f.Close()
}
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")
if err := encoder.Encode(&shards); err != nil {
log.Fatal("failed to encode shards: ", err)
}
}