Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion scaletest/harness/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package harness

import (
"bufio"
"encoding/json"
"fmt"
"io"
"sort"
"strings"
"time"

"github.com/coder/coder/v2/coderd/httpapi"
"golang.org/x/exp/maps"
)

// Results is the full compiled results for a set of test runs.
Expand All @@ -33,6 +36,18 @@ type RunResult struct {
DurationMS int64 `json:"duration_ms"`
}

// MarshalJSON implements json.Marhshaler for RunResult.
func (r RunResult) MarshalJSON() ([]byte, error) {
type alias RunResult
return json.Marshal(&struct {
alias
Error string `json:"error"`
}{
alias: alias(r),
Error: fmt.Sprintf("%+v", r.Error),
})
}

// Results returns the results of the test run. Panics if the test run is not
// done yet.
func (r *TestRun) Result() RunResult {
Expand Down Expand Up @@ -88,7 +103,10 @@ func (h *TestHarness) Results() Results {
// PrintText prints the results as human-readable text to the given writer.
func (r *Results) PrintText(w io.Writer) {
var totalDuration time.Duration
for _, run := range r.Runs {
keys := maps.Keys(r.Runs)
sort.Strings(keys)
for _, key := range keys {
run := r.Runs[key]
totalDuration += time.Duration(run.Duration)
if run.Error == nil {
continue
Expand Down
93 changes: 88 additions & 5 deletions scaletest/harness/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@ package harness_test

import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/scaletest/harness"
)

type testError struct {
hidden error
}

func (e testError) Error() string {
return e.hidden.Error()
}

func Test_Results(t *testing.T) {
t.Parallel()

now := time.Date(2023, 10, 5, 12, 3, 56, 395813665, time.UTC)
results := harness.Results{
TotalRuns: 10,
TotalPass: 8,
Expand All @@ -26,7 +41,7 @@ func Test_Results(t *testing.T) {
ID: "0",
Logs: "test-0/0 log line 1\ntest-0/0 log line 2",
Error: xerrors.New("test-0/0 error"),
StartedAt: time.Now(),
StartedAt: now,
Duration: httpapi.Duration(time.Second),
DurationMS: 1000,
},
Expand All @@ -36,7 +51,17 @@ func Test_Results(t *testing.T) {
ID: "1",
Logs: "test-0/1 log line 1\ntest-0/1 log line 2",
Error: nil,
StartedAt: time.Now(),
StartedAt: now.Add(333 * time.Millisecond),
Duration: httpapi.Duration(time.Second),
DurationMS: 1000,
},
"test-0/2": {
FullID: "test-0/2",
TestName: "test-0",
ID: "2",
Logs: "test-0/2 log line 1\ntest-0/2 log line 2",
Error: testError{hidden: xerrors.New("test-0/2 error")},
StartedAt: now.Add(666 * time.Millisecond),
Duration: httpapi.Duration(time.Second),
DurationMS: 1000,
},
Expand All @@ -45,26 +70,84 @@ func Test_Results(t *testing.T) {
ElapsedMS: 1000,
}

expected := `
wantText := `
== FAIL: test-0/0

Error: test-0/0 error

Log:
test-0/0 log line 1

== FAIL: test-0/2

Error: test-0/2 error

Log:
test-0/2 log line 1


Test results:
Pass: 8
Fail: 2
Total: 10

Total duration: 1s
Avg. duration: 200ms
Avg. duration: 300ms
`
wantJSON := `{
"total_runs": 10,
"total_pass": 8,
"total_fail": 2,
"elapsed": "1s",
"elapsed_ms": 1000,
"runs": {
"test-0/0": {
"full_id": "test-0/0",
"test_name": "test-0",
"id": "0",
"logs": "test-0/0 log line 1\ntest-0/0 log line 2",
"started_at": "2023-10-05T12:03:56.395813665Z",
"duration": "1s",
"duration_ms": 1000,
"error": "test-0/0 error:\n github.com/coder/coder/v2/scaletest/harness_test.Test_Results\n [working_directory]results_test.go:43"
},
"test-0/1": {
"full_id": "test-0/1",
"test_name": "test-0",
"id": "1",
"logs": "test-0/1 log line 1\ntest-0/1 log line 2",
"started_at": "2023-10-05T12:03:56.728813665Z",
"duration": "1s",
"duration_ms": 1000,
"error": "\u003cnil\u003e"
},
"test-0/2": {
"full_id": "test-0/2",
"test_name": "test-0",
"id": "2",
"logs": "test-0/2 log line 1\ntest-0/2 log line 2",
"started_at": "2023-10-05T12:03:57.061813665Z",
"duration": "1s",
"duration_ms": 1000,
"error": "test-0/2 error"
Copy link
Member Author

@mafredri mafredri Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: This is a simple error (but not {}) because we don't implement unwrapping on testError (otherwise it'd look like test-0/0).

}
}
}
`
wd, err := os.Getwd()
require.NoError(t, err)
wantJSON = strings.Replace(wantJSON, "[working_directory]", wd+string(filepath.Separator), 1)

out := bytes.NewBuffer(nil)
results.PrintText(out)

require.Equal(t, expected, out.String())
assert.Empty(t, cmp.Diff(wantText, out.String()), "text result does not match (-want +got)")

out.Reset()
enc := json.NewEncoder(out)
enc.SetIndent("", "\t")
err = enc.Encode(results)
require.NoError(t, err)

assert.Empty(t, cmp.Diff(wantJSON, out.String()), "JSON result does not match (-want +got)")
}