From 024bf225042f35076455321b2d5e0af918498d8d Mon Sep 17 00:00:00 2001 From: Andy Doan Date: Tue, 2 Jun 2020 13:33:03 -0500 Subject: [PATCH] Add target testing command Signed-off-by: Andy Doan --- client/foundries.go | 59 +++++++++++++++++++ subcommands/targets/tests.go | 107 +++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 subcommands/targets/tests.go diff --git a/client/foundries.go b/client/foundries.go index 370cb676..53fb4d63 100644 --- a/client/foundries.go +++ b/client/foundries.go @@ -173,6 +173,29 @@ type TufCustom struct { OverridesSha string `json:"meta-subscriber-overrides-sha,omitempty"` } +type TargetTestResults struct { + Name string `json:"name"` + Status string `json:"status"` + Details string `json:"details"` +} + +type TargetTest struct { + Name string `json:"name"` + Id string `json:"test-id"` + DeviceUUID string `json:"device-uuid"` + Status string `json:"status"` + Details string `json:"details"` + CreatedOn float32 `json:"created-on"` + CompletedOn float32 `json:"completed-on"` + Results []TargetTestResults `json:"results"` +} + +type TargetTestList struct { + Tests []TargetTest `json:"tests"` + Total int `json:"total"` + Next *string `json:"next"` +} + func (d Device) Online(inactiveHoursThreshold int) bool { if len(d.LastSeen) == 0 { return false @@ -657,6 +680,42 @@ func (a *Api) TargetDeleteTargets(factory string, target_names []string) (string return pr.JobServUrl + "runs/UpdateTargets/console.log", nil } +func (a *Api) TargetTests(factory string, target int) (*TargetTestList, error) { + url := a.serverUrl + "/ota/factories/" + factory + "/targets/" + strconv.Itoa(target) + "/testing/" + logrus.Debugf("TargetTests with url: %s", url) + return a.TargetTestsCont(url) +} + +func (a *Api) TargetTestsCont(url string) (*TargetTestList, error) { + body, err := a.Get(url) + if err != nil { + return nil, err + } + + tests := TargetTestList{} + err = json.Unmarshal(*body, &tests) + if err != nil { + return nil, err + } + return &tests, nil +} + +func (a *Api) TargetTestResults(factory string, target int, testId string) (*TargetTest, error) { + url := a.serverUrl + "/ota/factories/" + factory + "/targets/" + strconv.Itoa(target) + "/testing/" + testId + "/" + logrus.Debugf("TargetTests with url: %s", url) + body, err := a.Get(url) + if err != nil { + return nil, err + } + + test := TargetTest{} + err = json.Unmarshal(*body, &test) + if err != nil { + return nil, err + } + return &test, nil +} + func (a *Api) JobservTail(url string) { offset := 0 status := "" diff --git a/subcommands/targets/tests.go b/subcommands/targets/tests.go new file mode 100644 index 00000000..784f7798 --- /dev/null +++ b/subcommands/targets/tests.go @@ -0,0 +1,107 @@ +package targets + +import ( + "fmt" + "os" + "strconv" + "time" + + "github.com/cheynewallace/tabby" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "github.com/foundriesio/fioctl/client" +) + +func init() { + cmd.AddCommand(&cobra.Command{ + Use: "tests []", + Short: "Show testing done against a target", + Run: doShowTests, + Args: cobra.RangeArgs(1, 2), + }) +} + +func timestamp(ts float32) string { + if ts == 0 { + return "" + } + secs := int64(ts) + nsecs := int64((ts - float32(secs)) * 1e9) + return time.Unix(secs, nsecs).UTC().String() +} + +func list(factory string, target int) { + t := tabby.New() + t.AddHeader("NAME", "STATUS", "ID", "CREATED AT", "DEVICE") + + var tl *client.TargetTestList + for { + var err error + if tl == nil { + tl, err = api.TargetTests(factory, target) + } else { + if tl.Next != nil { + tl, err = api.TargetTestsCont(*tl.Next) + } else { + break + } + } + if err != nil { + fmt.Print("ERROR: ") + fmt.Println(err) + os.Exit(1) + } + for _, test := range tl.Tests { + created := timestamp(test.CreatedOn) + t.AddLine(test.Name, test.Status, test.Id, created, test.DeviceUUID) + } + } + t.Print() +} + +func show(factory string, target int, testId string) { + test, err := api.TargetTestResults(factory, target, testId) + if err != nil { + fmt.Print("ERROR: ") + fmt.Println(err) + os.Exit(1) + } + fmt.Println("Name: ", test.Name) + fmt.Println("Status: ", test.Status) + fmt.Println("Created: ", timestamp(test.CreatedOn)) + fmt.Println("Completed:", timestamp(test.CompletedOn)) + fmt.Println("Device: ", test.DeviceUUID) + if len(test.Details) > 0 { + fmt.Println("Details:") + fmt.Println(test.Details) + } + if test.Results != nil { + fmt.Println("") + t := tabby.New() + t.AddHeader("TEST RESULT", "STATUS") + for _, result := range test.Results { + t.AddLine(result.Name, result.Status) + } + t.Print() + } +} + +func doShowTests(cmd *cobra.Command, args []string) { + factory := viper.GetString("factory") + target, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Print("ERROR:") + fmt.Println(err) + os.Exit(1) + } + if len(args) == 1 { + logrus.Debugf("Showing target testing for %s %d", factory, target) + list(factory, target) + } else { + testId := args[1] + logrus.Debugf("Showing target test results for %s %d - %s", factory, target, testId) + show(factory, target, testId) + } +}