Skip to content

Commit 6a00296

Browse files
Smart tests final touches (#169)
1 parent 1023ec0 commit 6a00296

File tree

2 files changed

+84
-33
lines changed

2 files changed

+84
-33
lines changed
Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"io"
66
"strings"
77
"text/tabwriter"
8+
"time"
89

910
"github.com/signadot/cli/internal/config"
1011
"github.com/signadot/cli/internal/print"
1112
"github.com/signadot/cli/internal/sdtab"
12-
"github.com/signadot/cli/internal/utils"
1313
"github.com/signadot/go-sdk/models"
14+
"github.com/xeonx/timeago"
1415
)
1516

1617
func PrintTestExecution(oFmt config.OutputFormat, w io.Writer, tx *models.TestExecution) error {
@@ -62,16 +63,14 @@ func printTestExecutionDetails(w io.Writer, tx *models.TestExecution) error {
6263
if tx.Spec.ExecutionContext != nil {
6364
ec := tx.Spec.ExecutionContext
6465
fmt.Fprintf(tw, "Cluster:\t%s\n", ec.Cluster)
65-
if ec.Routing != nil {
66-
if ec.Routing.Sandbox != "" {
67-
fmt.Fprintf(tw, "Sandbox:\t%s\n", ec.Routing.Sandbox)
68-
} else if ec.Routing.Routegroup != "" {
69-
fmt.Fprintf(tw, "Routegroup:\t%s\n", ec.Routing.Routegroup)
70-
}
71-
}
66+
fmt.Fprintf(tw, "Environment:\t%s\n", getTXEnvironment(tx))
7267
}
7368

74-
fmt.Fprintf(tw, "Created:\t%s\n", utils.FormatTimestamp(tx.CreatedAt))
69+
createdAt, duration := getTXCreatedAtAndDuration(tx)
70+
fmt.Fprintf(tw, "Created:\t%s\n", createdAt)
71+
if len(duration) != 0 {
72+
fmt.Fprintf(tw, "Duration:\t%s\n", duration)
73+
}
7574
fmt.Fprintf(tw, "Phase:\t%s", tx.Status.Phase)
7675
if tx.Status.FinalState != nil {
7776
if tx.Status.FinalState.Failed != nil {
@@ -117,11 +116,13 @@ func getResults(tx *models.TestExecution) string {
117116
}
118117

119118
type testExecRow struct {
120-
ID string `sdtab:"ID"`
121-
Source string `sdtab:"SOURCE"`
122-
TestName string `sdtab:"TESTNAME"`
123-
Phase string `sdtab:"PHASE"`
124-
CreatedAt string `sdtab:"CREATED"`
119+
ID string `sdtab:"ID"`
120+
Source string `sdtab:"SOURCE"`
121+
TestName string `sdtab:"TESTNAME"`
122+
Environment string `sdtab:"ENVIRONMENT"`
123+
CreatedAt string `sdtab:"CREATED AT"`
124+
Duration string `sdtab:"DURATION"`
125+
Status string `sdtab:"STATUS"`
125126
}
126127

127128
func printTestExecutionsTable(w io.Writer, txs []*models.TestexecutionsQueryResult) error {
@@ -141,12 +142,18 @@ func printTestExecutionsTable(w io.Writer, txs []*models.TestexecutionsQueryResu
141142
testName = tx.Spec.Hosted.TestName
142143
}
143144
}
145+
146+
createdAt, duration := getTXCreatedAtAndDuration(tx)
147+
environment := getTXEnvironment(tx)
148+
144149
tab.AddRow(testExecRow{
145-
ID: tx.ID,
146-
Source: source,
147-
TestName: truncateTestName(testName),
148-
CreatedAt: tx.CreatedAt,
149-
Phase: tx.Status.Phase,
150+
ID: tx.ID,
151+
Source: source,
152+
TestName: truncateTestName(testName),
153+
Environment: environment,
154+
CreatedAt: createdAt,
155+
Duration: duration,
156+
Status: tx.Status.Phase,
150157
})
151158
}
152159
return tab.Flush()
@@ -159,3 +166,44 @@ func truncateTestName(tn string) string {
159166
}
160167
return tn
161168
}
169+
170+
func getTXCreatedAtAndDuration(tx *models.TestExecution) (createdAtStr string, durationStr string) {
171+
var createdAt *time.Time
172+
173+
createdAtRaw := tx.CreatedAt
174+
if len(createdAtRaw) != 0 {
175+
t, err := time.Parse(time.RFC3339, createdAtRaw)
176+
if err != nil {
177+
return "", ""
178+
}
179+
180+
createdAt = &t
181+
createdAtStr = timeago.NoMax(timeago.English).Format(t)
182+
}
183+
184+
finishedAtRaw := tx.Status.FinishedAt
185+
if createdAt != nil && len(finishedAtRaw) != 0 {
186+
finishedAt, err := time.Parse(time.RFC3339, finishedAtRaw)
187+
if err != nil {
188+
return "", ""
189+
}
190+
191+
durationTime := finishedAt.Sub(*createdAt)
192+
durationStr = durationTime.String()
193+
}
194+
195+
return createdAtStr, durationStr
196+
}
197+
198+
func getTXEnvironment(tx *models.TestExecution) string {
199+
routingContext := tx.Spec.ExecutionContext.Routing
200+
201+
switch {
202+
case routingContext == nil:
203+
case len(routingContext.Sandbox) > 0:
204+
return fmt.Sprintf("sandbox=%s", routingContext.Sandbox)
205+
case len(routingContext.Routegroup) > 0:
206+
return fmt.Sprintf("routegroup=%s", routingContext.Routegroup)
207+
}
208+
return "baseline"
209+
}

internal/command/smarttest/run_output.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"strings"
88
"sync"
9+
"text/tabwriter"
910
"time"
1011

1112
"github.com/fatih/color"
@@ -33,7 +34,7 @@ func newDefaultRunOutput(cfg *config.SmartTestRun, wOut io.Writer, runID string)
3334
}
3435

3536
func (o *defaultRunOutput) start() {
36-
fmt.Fprintf(o.wOut, "Created test run %q in cluster %q.\n\n", o.runID, o.cfg.Cluster)
37+
fmt.Fprintf(o.wOut, "Created test run with ID %q in cluster %q.\n\n", o.runID, o.cfg.Cluster)
3738
}
3839

3940
func (o *defaultRunOutput) setTestXs(txs []*models.TestExecution) {
@@ -82,7 +83,8 @@ func (o *defaultRunOutput) renderTestXsTable(txs []*models.TestExecution, runnin
8283
runningIcon = "🟡"
8384
}
8485

85-
fmt.Fprintf(o.wOut, "Test run status:\n")
86+
tw := tabwriter.NewWriter(o.wOut, 0, 0, 3, ' ', 0)
87+
fmt.Fprintf(tw, "Test run status:\n")
8688
for _, tx := range txs {
8789
var icon, statusText string
8890
switch tx.Status.Phase {
@@ -105,31 +107,32 @@ func (o *defaultRunOutput) renderTestXsTable(txs []*models.TestExecution, runnin
105107
icon = "⚪"
106108
statusText = tx.Status.Phase
107109
}
108-
109-
// add some padding to completely overwrite lines
110-
padding := " "
111-
fmt.Fprintf(o.wOut, "%s\t%s\t[%s]%s\n", icon, tx.Spec.External.TestName, statusText, padding)
110+
fmt.Fprintf(tw, "%s\t%s\t[ID: %s, STATUS: %s]\n", icon, tx.Spec.External.TestName,
111+
tx.ID, statusText)
112112
}
113+
tw.Flush()
113114
}
114115

115116
func (o *defaultRunOutput) renderTestXsSummary(txs []*models.TestExecution) {
116-
fmt.Fprint(o.wOut, "\nTest run summary:\n")
117+
tw := tabwriter.NewWriter(o.wOut, 0, 0, 3, ' ', 0)
118+
fmt.Fprint(tw, "\nTest run summary:\n")
117119

118-
fmt.Fprint(o.wOut, "* Executions\n")
119-
fmt.Fprint(o.wOut, "\t"+o.getExecutionsDetails(txs)+"\n")
120+
fmt.Fprint(tw, "* Executions\n")
121+
fmt.Fprint(tw, "\t"+o.getExecutionsDetails(txs)+"\n")
120122

121123
diffMsg := getDiffsDetails(txs...)
122124
if diffMsg != "" {
123-
fmt.Fprint(o.wOut, "* Diffs\n")
124-
fmt.Fprint(o.wOut, "\t"+diffMsg+"\n")
125+
fmt.Fprint(tw, "* Diffs\n")
126+
fmt.Fprint(tw, "\t"+diffMsg+"\n")
125127
}
126128

127129
checksMsg := getChecksDetails(txs...)
128130
if checksMsg != "" {
129-
fmt.Fprint(o.wOut, "* Checks\n")
130-
fmt.Fprint(o.wOut, "\t"+checksMsg+"\n")
131+
fmt.Fprint(tw, "* Checks\n")
132+
fmt.Fprint(tw, "\t"+checksMsg+"\n")
131133
}
132-
fmt.Fprint(o.wOut, "\n")
134+
fmt.Fprint(tw, "\n")
135+
tw.Flush()
133136
}
134137

135138
func (o *defaultRunOutput) getExecutionsDetails(txs []*models.TestExecution) string {

0 commit comments

Comments
 (0)