| // Copyright 2020 The Chromium OS 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 ( |
| "context" |
| "fmt" |
| "os" |
| |
| "github.com/golang/protobuf/jsonpb" |
| "github.com/golang/protobuf/proto" |
| "github.com/maruel/subcommands" |
| |
| rtd "go.chromium.org/chromiumos/config/go/api/test/rtd/v1" |
| "go.chromium.org/luci/auth/client/authcli" |
| "go.chromium.org/luci/common/cli" |
| "go.chromium.org/luci/common/errors" |
| "go.chromium.org/luci/common/logging/gologger" |
| "go.chromium.org/luci/hardcoded/chromeinfra" |
| ) |
| |
| const tnullName = "remoteTestDrivers/tnull" |
| const specPath = "../../metadata/generated/config.cfg" |
| |
| type run struct { |
| subcommands.CommandRunBase |
| |
| invocationPath string |
| } |
| |
| func runSteps() *subcommands.Command { |
| return &subcommands.Command{ |
| UsageLine: "run-steps -input_json /path/to/input.json", |
| ShortDesc: "run a Steps object.", |
| CommandRun: func() subcommands.CommandRun { |
| r := &run{} |
| r.Flags.StringVar(&r.invocationPath, "input_json", "", "Path that contains-JSON encoded rtd.Invocation object") |
| return r |
| }, |
| } |
| } |
| |
| func (r *run) Run(a subcommands.Application, args []string, env subcommands.Env) int { |
| if err := r.innerRun(a, args, env); err != nil { |
| fmt.Fprintf(a.GetErr(), "%s\n", err) |
| return 1 |
| } |
| return 0 |
| } |
| |
| func (r *run) innerRun(a subcommands.Application, args []string, env subcommands.Env) error { |
| if err := r.validateArgs(); err != nil { |
| return errors.Annotate(err, "validation").Err() |
| } |
| var inv rtd.Invocation |
| if err := readJSONPb(r.invocationPath, &inv); err != nil { |
| return errors.Annotate(err, "reading in the Invocation").Err() |
| } |
| reqs := inv.Requests |
| for _, req := range reqs { |
| fmt.Fprintf(a.GetOut(), "req %s: 'executing' test %s\n", req.Name, req.Test) |
| } |
| return nil |
| } |
| |
| func (r *run) validateArgs() error { |
| if r.invocationPath == "" { |
| return fmt.Errorf("input_json must specify an rtd.Invocation") |
| } |
| return nil |
| } |
| |
| // readJSONPb reads a JSON string from inFile and unpacks it as a proto. |
| // Unexpected fields are ignored. |
| func readJSONPb(inFile string, payload proto.Message) error { |
| r, err := os.Open(inFile) |
| if err != nil { |
| return errors.Annotate(err, "read JSON pb").Err() |
| } |
| defer r.Close() |
| |
| unmarshaler := jsonpb.Unmarshaler{AllowUnknownFields: true} |
| if err := unmarshaler.Unmarshal(r, payload); err != nil { |
| return errors.Annotate(err, "read JSON pb").Err() |
| } |
| return nil |
| } |
| |
| func main() { |
| application := &cli.Application{ |
| Name: "TNull", |
| Title: "A minimal Remote Test Driver for testing API and faking out aspects of the RTD for meta-testing purposes.", |
| Context: func(ctx context.Context) context.Context { |
| return gologger.StdConfig.Use(ctx) |
| }, |
| Commands: []*subcommands.Command{ |
| subcommands.CmdHelp, |
| authcli.SubcommandInfo(chromeinfra.DefaultAuthOptions(), "whoami", false), |
| authcli.SubcommandLogin(chromeinfra.DefaultAuthOptions(), "login", false), |
| authcli.SubcommandLogout(chromeinfra.DefaultAuthOptions(), "logout", false), |
| runSteps(), |
| }, |
| } |
| os.Exit(subcommands.Run(application, nil)) |
| } |