Skip to content

Commit

Permalink
Add target testing command
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Doan <andy@foundries.io>
  • Loading branch information
doanac committed Jun 17, 2020
1 parent c0d4ef6 commit 024bf22
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
59 changes: 59 additions & 0 deletions client/foundries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 := ""
Expand Down
107 changes: 107 additions & 0 deletions subcommands/targets/tests.go
Original file line number Diff line number Diff line change
@@ -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 <target> [<test-id>]",
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)
}
}

0 comments on commit 024bf22

Please sign in to comment.