@@ -2,19 +2,31 @@ package harness_test
2
2
3
3
import (
4
4
"bytes"
5
+ "encoding/json"
5
6
"testing"
6
7
"time"
7
8
9
+ "github.com/google/go-cmp/cmp"
10
+ "github.com/stretchr/testify/assert"
8
11
"github.com/stretchr/testify/require"
9
12
"golang.org/x/xerrors"
10
13
11
14
"github.com/coder/coder/v2/coderd/httpapi"
12
15
"github.com/coder/coder/v2/scaletest/harness"
13
16
)
14
17
18
+ type testError struct {
19
+ hidden error
20
+ }
21
+
22
+ func (e testError ) Error () string {
23
+ return e .hidden .Error ()
24
+ }
25
+
15
26
func Test_Results (t * testing.T ) {
16
27
t .Parallel ()
17
28
29
+ now := time .Date (2023 , 10 , 5 , 12 , 3 , 56 , 395813665 , time .UTC )
18
30
results := harness.Results {
19
31
TotalRuns : 10 ,
20
32
TotalPass : 8 ,
@@ -26,7 +38,7 @@ func Test_Results(t *testing.T) {
26
38
ID : "0" ,
27
39
Logs : "test-0/0 log line 1\n test-0/0 log line 2" ,
28
40
Error : xerrors .New ("test-0/0 error" ),
29
- StartedAt : time . Now () ,
41
+ StartedAt : now ,
30
42
Duration : httpapi .Duration (time .Second ),
31
43
DurationMS : 1000 ,
32
44
},
@@ -36,7 +48,17 @@ func Test_Results(t *testing.T) {
36
48
ID : "1" ,
37
49
Logs : "test-0/1 log line 1\n test-0/1 log line 2" ,
38
50
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\n test-0/2 log line 2" ,
60
+ Error : testError {hidden : xerrors .New ("test-0/2 error" )},
61
+ StartedAt : now .Add (666 * time .Millisecond ),
40
62
Duration : httpapi .Duration (time .Second ),
41
63
DurationMS : 1000 ,
42
64
},
@@ -45,26 +67,81 @@ func Test_Results(t *testing.T) {
45
67
ElapsedMS : 1000 ,
46
68
}
47
69
48
- expected := `
70
+ wantText := `
49
71
== FAIL: test-0/0
50
72
51
73
Error: test-0/0 error
52
74
53
75
Log:
54
76
test-0/0 log line 1
55
77
78
+ == FAIL: test-0/2
79
+
80
+ Error: test-0/2 error
81
+
82
+ Log:
83
+ test-0/2 log line 1
84
+
56
85
57
86
Test results:
58
87
Pass: 8
59
88
Fail: 2
60
89
Total: 10
61
90
62
91
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
+ }
64
133
`
65
134
66
135
out := bytes .NewBuffer (nil )
67
136
results .PrintText (out )
68
137
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)" )
70
147
}
0 commit comments