@@ -2,19 +2,34 @@ package harness_test
2
2
3
3
import (
4
4
"bytes"
5
+ "encoding/json"
6
+ "os"
7
+ "path/filepath"
8
+ "strings"
5
9
"testing"
6
10
"time"
7
11
12
+ "github.com/google/go-cmp/cmp"
13
+ "github.com/stretchr/testify/assert"
8
14
"github.com/stretchr/testify/require"
9
15
"golang.org/x/xerrors"
10
16
11
17
"github.com/coder/coder/v2/coderd/httpapi"
12
18
"github.com/coder/coder/v2/scaletest/harness"
13
19
)
14
20
21
+ type testError struct {
22
+ hidden error
23
+ }
24
+
25
+ func (e testError ) Error () string {
26
+ return e .hidden .Error ()
27
+ }
28
+
15
29
func Test_Results (t * testing.T ) {
16
30
t .Parallel ()
17
31
32
+ now := time .Date (2023 , 10 , 5 , 12 , 3 , 56 , 395813665 , time .UTC )
18
33
results := harness.Results {
19
34
TotalRuns : 10 ,
20
35
TotalPass : 8 ,
@@ -26,7 +41,7 @@ func Test_Results(t *testing.T) {
26
41
ID : "0" ,
27
42
Logs : "test-0/0 log line 1\n test-0/0 log line 2" ,
28
43
Error : xerrors .New ("test-0/0 error" ),
29
- StartedAt : time . Now () ,
44
+ StartedAt : now ,
30
45
Duration : httpapi .Duration (time .Second ),
31
46
DurationMS : 1000 ,
32
47
},
@@ -36,7 +51,17 @@ func Test_Results(t *testing.T) {
36
51
ID : "1" ,
37
52
Logs : "test-0/1 log line 1\n test-0/1 log line 2" ,
38
53
Error : nil ,
39
- StartedAt : time .Now (),
54
+ StartedAt : now .Add (333 * time .Millisecond ),
55
+ Duration : httpapi .Duration (time .Second ),
56
+ DurationMS : 1000 ,
57
+ },
58
+ "test-0/2" : {
59
+ FullID : "test-0/2" ,
60
+ TestName : "test-0" ,
61
+ ID : "2" ,
62
+ Logs : "test-0/2 log line 1\n test-0/2 log line 2" ,
63
+ Error : testError {hidden : xerrors .New ("test-0/2 error" )},
64
+ StartedAt : now .Add (666 * time .Millisecond ),
40
65
Duration : httpapi .Duration (time .Second ),
41
66
DurationMS : 1000 ,
42
67
},
@@ -45,26 +70,85 @@ func Test_Results(t *testing.T) {
45
70
ElapsedMS : 1000 ,
46
71
}
47
72
48
- expected := `
73
+ wantText := `
49
74
== FAIL: test-0/0
50
75
51
76
Error: test-0/0 error
52
77
53
78
Log:
54
79
test-0/0 log line 1
55
80
81
+ == FAIL: test-0/2
82
+
83
+ Error: test-0/2 error
84
+
85
+ Log:
86
+ test-0/2 log line 1
87
+
56
88
57
89
Test results:
58
90
Pass: 8
59
91
Fail: 2
60
92
Total: 10
61
93
62
94
Total duration: 1s
63
- Avg. duration: 200ms
95
+ Avg. duration: 300ms
64
96
`
97
+ wantJSON := `{
98
+ "total_runs": 10,
99
+ "total_pass": 8,
100
+ "total_fail": 2,
101
+ "elapsed": "1s",
102
+ "elapsed_ms": 1000,
103
+ "runs": {
104
+ "test-0/0": {
105
+ "full_id": "test-0/0",
106
+ "test_name": "test-0",
107
+ "id": "0",
108
+ "logs": "test-0/0 log line 1\ntest-0/0 log line 2",
109
+ "started_at": "2023-10-05T12:03:56.395813665Z",
110
+ "duration": "1s",
111
+ "duration_ms": 1000,
112
+ "error": "test-0/0 error:\n github.com/coder/coder/v2/scaletest/harness_test.Test_Results\n [working_directory]/results_test.go:43"
113
+ },
114
+ "test-0/1": {
115
+ "full_id": "test-0/1",
116
+ "test_name": "test-0",
117
+ "id": "1",
118
+ "logs": "test-0/1 log line 1\ntest-0/1 log line 2",
119
+ "started_at": "2023-10-05T12:03:56.728813665Z",
120
+ "duration": "1s",
121
+ "duration_ms": 1000,
122
+ "error": "\u003cnil\u003e"
123
+ },
124
+ "test-0/2": {
125
+ "full_id": "test-0/2",
126
+ "test_name": "test-0",
127
+ "id": "2",
128
+ "logs": "test-0/2 log line 1\ntest-0/2 log line 2",
129
+ "started_at": "2023-10-05T12:03:57.061813665Z",
130
+ "duration": "1s",
131
+ "duration_ms": 1000,
132
+ "error": "test-0/2 error"
133
+ }
134
+ }
135
+ }
136
+ `
137
+ wd , err := os .Getwd ()
138
+ require .NoError (t , err )
139
+ wd = filepath .ToSlash (wd ) // Hello there Windows, my friend...
140
+ wantJSON = strings .Replace (wantJSON , "[working_directory]" , wd , 1 )
65
141
66
142
out := bytes .NewBuffer (nil )
67
143
results .PrintText (out )
68
144
69
- require .Equal (t , expected , out .String ())
145
+ assert .Empty (t , cmp .Diff (wantText , out .String ()), "text result does not match (-want +got)" )
146
+
147
+ out .Reset ()
148
+ enc := json .NewEncoder (out )
149
+ enc .SetIndent ("" , "\t " )
150
+ err = enc .Encode (results )
151
+ require .NoError (t , err )
152
+
153
+ assert .Empty (t , cmp .Diff (wantJSON , out .String ()), "JSON result does not match (-want +got)" )
70
154
}
0 commit comments