Skip to content

Commit

Permalink
fix(cli): fixing CLI formatting for transactions (#2790)
Browse files Browse the repository at this point in the history
Fixing bugs on CLI formatting for transactions
  • Loading branch information
danielbdias authored Jun 22, 2023
1 parent 419c300 commit b7d0af5
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/get_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var getCmd = &cobra.Command{
return "", err
}

if output == string(formatters.JSON) {
if output == string(formatters.JSON) || output == string(formatters.Pretty) {
ctx = context.WithValue(ctx, "X-Tracetest-Augmented", true)
}

Expand Down
8 changes: 7 additions & 1 deletion cli/formatters/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (f TransactionFormatter) getTableHeader() *simpletable.Header {
{Text: "NAME"},
{Text: "VERSION"},
{Text: "STEPS"},
{Text: "RUNS"},
{Text: "LAST RUN TIME"},
{Text: "LAST RUN SUCCESSES"},
{Text: "LAST RUN FAILURES"},
Expand All @@ -134,10 +135,14 @@ func (f TransactionFormatter) getTableHeader() *simpletable.Header {

func (f TransactionFormatter) getTableRow(t openapi.TransactionResource) ([]*simpletable.Cell, error) {
version := 1

runs := 0
lastRunTime := ""
passes := 0
fails := 0

steps := len(t.Spec.Steps)

if t.Spec.Version != nil {
version = int(*t.Spec.Version)
}
Expand All @@ -147,7 +152,7 @@ func (f TransactionFormatter) getTableRow(t openapi.TransactionResource) ([]*sim
runs = int(*t.Spec.Summary.Runs)
}

if t.Spec.Summary.LastRun != nil {
if t.Spec.Summary.LastRun != nil && !t.Spec.Summary.LastRun.GetTime().IsZero() {
lastRunTime = t.Spec.Summary.LastRun.GetTime().String()
}

Expand All @@ -161,6 +166,7 @@ func (f TransactionFormatter) getTableRow(t openapi.TransactionResource) ([]*sim
{Text: *t.Spec.Id},
{Text: *t.Spec.Name},
{Text: strconv.Itoa(version)},
{Text: fmt.Sprint(steps)},
{Text: fmt.Sprint(runs)},
{Text: lastRunTime},
{Text: fmt.Sprint(passes)},
Expand Down
67 changes: 67 additions & 0 deletions testing/cli-e2etest/helpers/table_unmarshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package helpers

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
)

type tableColumnMetadata struct {
Name string
Len int
}

func UnmarshalTable(t *testing.T, data string) []map[string]string {
lines := strings.Split(data, "\n")
require.GreaterOrEqual(t, len(lines), 2) // it should have at least two lines

columnsMetadata := inferTableColumns(lines)
result := []map[string]string{}

lastLineIndexToParse := len(lines) - 2 // minus two, one to convert to index and another because the last line is always an empty line
for i := 2; i <= lastLineIndexToParse; i++ {
parsedResult := parseTableLine(columnsMetadata, lines[i])
result = append(result, parsedResult)
}

return result
}

func inferTableColumns(tableLines []string) []tableColumnMetadata {
headerLine := tableLines[0]
separatorLine := tableLines[1]

columns := strings.Split(separatorLine, " ")

result := []tableColumnMetadata{}

parserStart := 0
for _, column := range columns {
columnLen := len(column)

columnName := strings.TrimSpace(headerLine[parserStart : parserStart+columnLen])
parserStart = parserStart + columnLen + 1 // we need to shift one item for the next start

result = append(result, tableColumnMetadata{
Len: columnLen,
Name: columnName,
})
}

return result
}

func parseTableLine(columnsMetadata []tableColumnMetadata, line string) map[string]string {
result := map[string]string{}

parserStart := 0
for _, column := range columnsMetadata {
value := strings.TrimSpace(line[parserStart : parserStart+column.Len])
parserStart = parserStart + column.Len + 1

result[column.Name] = value
}

return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package transaction

import (
"fmt"
"strings"
"testing"

"github.com/kubeshop/tracetest/cli-e2etest/environment"
Expand Down Expand Up @@ -80,7 +79,8 @@ func TestGetTransaction(t *testing.T) {
result := tracetestcli.Exec(t, "get transaction --id Qti5R3_VR --output json", tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, result, 0)

transaction := helpers.UnmarshalJSON[types.TransactionResource](t, result.StdOut)
// it should return an augmented resource on get
transaction := helpers.UnmarshalJSON[types.AugmentedTransactionResource](t, result.StdOut)

require.Equal("Transaction", transaction.Type)
require.Equal("Qti5R3_VR", transaction.Spec.ID)
Expand All @@ -89,6 +89,9 @@ func TestGetTransaction(t *testing.T) {
require.Len(transaction.Spec.Steps, 2)
require.Equal("9wtAH2_Vg", transaction.Spec.Steps[0])
require.Equal("ajksdkasjbd", transaction.Spec.Steps[1])
require.Equal(0, transaction.Spec.Summary.Runs)
require.Equal(0, transaction.Spec.Summary.LastRun.Fails)
require.Equal(0, transaction.Spec.Summary.LastRun.Passes)
})

t.Run("get with pretty format", func(t *testing.T) {
Expand All @@ -100,9 +103,19 @@ func TestGetTransaction(t *testing.T) {
// Then it should print a table with 4 lines printed: header, separator, transaction item and empty line
result := tracetestcli.Exec(t, "get transaction --id Qti5R3_VR --output pretty", tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, result, 0)
require.Contains(result.StdOut, "New Transaction")

lines := strings.Split(result.StdOut, "\n")
require.Len(lines, 4)
parsedTable := helpers.UnmarshalTable(t, result.StdOut)
require.Len(parsedTable, 1)

singleLine := parsedTable[0]

require.Equal("Qti5R3_VR", singleLine["ID"])
require.Equal("New Transaction", singleLine["NAME"])
require.Equal("1", singleLine["VERSION"])
require.Equal("2", singleLine["STEPS"])
require.Equal("0", singleLine["RUNS"])
require.Equal("", singleLine["LAST RUN TIME"])
require.Equal("0", singleLine["LAST RUN SUCCESSES"])
require.Equal("0", singleLine["LAST RUN FAILURES"])
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package transaction

import (
"fmt"
"strings"
"testing"

"github.com/kubeshop/tracetest/cli-e2etest/environment"
Expand Down Expand Up @@ -187,12 +186,38 @@ func TestListTransactions(t *testing.T) {
result := tracetestcli.Exec(t, "list transaction --sortBy name --sortDirection asc --output pretty", tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, result, 0)

lines := strings.Split(result.StdOut, "\n")
require.Len(lines, 6)

require.Contains(lines[2], "Another Transaction")
require.Contains(lines[3], "New Transaction")
require.Contains(lines[4], "One More Transaction")
parsedTable := helpers.UnmarshalTable(t, result.StdOut)
require.Len(parsedTable, 3)

firstLine := parsedTable[0]
require.Equal("asuhfdkj", firstLine["ID"])
require.Equal("Another Transaction", firstLine["NAME"])
require.Equal("1", firstLine["VERSION"])
require.Equal("4", firstLine["STEPS"])
require.Equal("0", firstLine["RUNS"])
require.Equal("", firstLine["LAST RUN TIME"])
require.Equal("0", firstLine["LAST RUN SUCCESSES"])
require.Equal("0", firstLine["LAST RUN FAILURES"])

secondLine := parsedTable[1]
require.Equal("Qti5R3_VR", secondLine["ID"])
require.Equal("New Transaction", secondLine["NAME"])
require.Equal("1", secondLine["VERSION"])
require.Equal("2", secondLine["STEPS"])
require.Equal("0", secondLine["RUNS"])
require.Equal("", secondLine["LAST RUN TIME"])
require.Equal("0", secondLine["LAST RUN SUCCESSES"])
require.Equal("0", secondLine["LAST RUN FAILURES"])

thirdLine := parsedTable[2]
require.Equal("i2ug34j", thirdLine["ID"])
require.Equal("One More Transaction", thirdLine["NAME"])
require.Equal("1", thirdLine["VERSION"])
require.Equal("3", thirdLine["STEPS"])
require.Equal("0", thirdLine["RUNS"])
require.Equal("", thirdLine["LAST RUN TIME"])
require.Equal("0", thirdLine["LAST RUN SUCCESSES"])
require.Equal("0", thirdLine["LAST RUN FAILURES"])
})

t.Run("list with YAML format skipping the first and taking two items", func(t *testing.T) {
Expand Down

0 comments on commit b7d0af5

Please sign in to comment.