Skip to content

Commit 6c76732

Browse files
committed
fix(scaletest): output error and trace instead of {} for json output
1 parent 3044091 commit 6c76732

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

scaletest/harness/results.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package harness
22

33
import (
44
"bufio"
5+
"encoding/json"
56
"fmt"
67
"io"
78
"strings"
@@ -33,6 +34,18 @@ type RunResult struct {
3334
DurationMS int64 `json:"duration_ms"`
3435
}
3536

37+
// MarshalJSON implements json.Marhshaler for RunResult.
38+
func (r RunResult) MarshalJSON() ([]byte, error) {
39+
type alias RunResult
40+
return json.Marshal(&struct {
41+
alias
42+
Error string `json:"error"`
43+
}{
44+
alias: alias(r),
45+
Error: fmt.Sprintf("%+v", r.Error),
46+
})
47+
}
48+
3649
// Results returns the results of the test run. Panics if the test run is not
3750
// done yet.
3851
func (r *TestRun) Result() RunResult {

scaletest/harness/results_test.go

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@ package harness_test
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"testing"
67
"time"
78

9+
"github.com/google/go-cmp/cmp"
10+
"github.com/stretchr/testify/assert"
811
"github.com/stretchr/testify/require"
912
"golang.org/x/xerrors"
1013

1114
"github.com/coder/coder/v2/coderd/httpapi"
1215
"github.com/coder/coder/v2/scaletest/harness"
1316
)
1417

18+
type testError struct {
19+
hidden error
20+
}
21+
22+
func (e testError) Error() string {
23+
return e.hidden.Error()
24+
}
25+
1526
func Test_Results(t *testing.T) {
1627
t.Parallel()
1728

29+
now := time.Date(2023, 10, 5, 12, 3, 56, 395813665, time.UTC)
1830
results := harness.Results{
1931
TotalRuns: 10,
2032
TotalPass: 8,
@@ -26,7 +38,7 @@ func Test_Results(t *testing.T) {
2638
ID: "0",
2739
Logs: "test-0/0 log line 1\ntest-0/0 log line 2",
2840
Error: xerrors.New("test-0/0 error"),
29-
StartedAt: time.Now(),
41+
StartedAt: now,
3042
Duration: httpapi.Duration(time.Second),
3143
DurationMS: 1000,
3244
},
@@ -36,7 +48,17 @@ func Test_Results(t *testing.T) {
3648
ID: "1",
3749
Logs: "test-0/1 log line 1\ntest-0/1 log line 2",
3850
Error: nil,
39-
StartedAt: time.Now(),
51+
StartedAt: now.Add(333 * time.Millisecond),
52+
Duration: httpapi.Duration(time.Second),
53+
DurationMS: 1000,
54+
},
55+
"test-0/2": {
56+
FullID: "test-0/2",
57+
TestName: "test-0",
58+
ID: "2",
59+
Logs: "test-0/2 log line 1\ntest-0/2 log line 2",
60+
Error: testError{hidden: xerrors.New("test-0/2 error")},
61+
StartedAt: now.Add(666 * time.Millisecond),
4062
Duration: httpapi.Duration(time.Second),
4163
DurationMS: 1000,
4264
},
@@ -45,26 +67,81 @@ func Test_Results(t *testing.T) {
4567
ElapsedMS: 1000,
4668
}
4769

48-
expected := `
70+
wantText := `
4971
== FAIL: test-0/0
5072
5173
Error: test-0/0 error
5274
5375
Log:
5476
test-0/0 log line 1
5577
78+
== FAIL: test-0/2
79+
80+
Error: test-0/2 error
81+
82+
Log:
83+
test-0/2 log line 1
84+
5685
5786
Test results:
5887
Pass: 8
5988
Fail: 2
6089
Total: 10
6190
6291
Total duration: 1s
63-
Avg. duration: 200ms
92+
Avg. duration: 300ms
93+
`
94+
wantJSON := `{
95+
"total_runs": 10,
96+
"total_pass": 8,
97+
"total_fail": 2,
98+
"elapsed": "1s",
99+
"elapsed_ms": 1000,
100+
"runs": {
101+
"test-0/0": {
102+
"full_id": "test-0/0",
103+
"test_name": "test-0",
104+
"id": "0",
105+
"logs": "test-0/0 log line 1\ntest-0/0 log line 2",
106+
"started_at": "2023-10-05T12:03:56.395813665Z",
107+
"duration": "1s",
108+
"duration_ms": 1000,
109+
"error": "test-0/0 error:\n github.com/coder/coder/v2/scaletest/harness_test.Test_Results\n /home/coder/src/coder/coder/scaletest/harness/results_test.go:40"
110+
},
111+
"test-0/1": {
112+
"full_id": "test-0/1",
113+
"test_name": "test-0",
114+
"id": "1",
115+
"logs": "test-0/1 log line 1\ntest-0/1 log line 2",
116+
"started_at": "2023-10-05T12:03:56.728813665Z",
117+
"duration": "1s",
118+
"duration_ms": 1000,
119+
"error": "\u003cnil\u003e"
120+
},
121+
"test-0/2": {
122+
"full_id": "test-0/2",
123+
"test_name": "test-0",
124+
"id": "2",
125+
"logs": "test-0/2 log line 1\ntest-0/2 log line 2",
126+
"started_at": "2023-10-05T12:03:57.061813665Z",
127+
"duration": "1s",
128+
"duration_ms": 1000,
129+
"error": "test-0/2 error"
130+
}
131+
}
132+
}
64133
`
65134

66135
out := bytes.NewBuffer(nil)
67136
results.PrintText(out)
68137

69-
require.Equal(t, expected, out.String())
138+
assert.Empty(t, cmp.Diff(wantText, out.String()), "text result does not match (-want +got)")
139+
140+
out.Reset()
141+
enc := json.NewEncoder(out)
142+
enc.SetIndent("", "\t")
143+
err := enc.Encode(results)
144+
require.NoError(t, err)
145+
146+
assert.Empty(t, cmp.Diff(wantJSON, out.String()), "JSON result does not match (-want +got)")
70147
}

0 commit comments

Comments
 (0)