@@ -17,7 +17,6 @@ import (
17
17
"os"
18
18
"runtime"
19
19
"strings"
20
- "sync"
21
20
"testing"
22
21
"time"
23
22
@@ -45,12 +44,11 @@ func TestServer(t *testing.T) {
45
44
require .NoError (t , err )
46
45
defer closeFunc ()
47
46
ctx , cancelFunc := context .WithCancel (context .Background ())
48
- done := make ( chan struct {} )
47
+ defer cancelFunc ( )
49
48
root , cfg := clitest .New (t , "server" , "--address" , ":0" , "--postgres-url" , connectionURL )
49
+ errC := make (chan error )
50
50
go func () {
51
- defer close (done )
52
- err = root .ExecuteContext (ctx )
53
- require .ErrorIs (t , err , context .Canceled )
51
+ errC <- root .ExecuteContext (ctx )
54
52
}()
55
53
var client * codersdk.Client
56
54
require .Eventually (t , func () bool {
@@ -71,8 +69,9 @@ func TestServer(t *testing.T) {
71
69
})
72
70
require .NoError (t , err )
73
71
cancelFunc ()
74
- <- done
72
+ require . ErrorIs ( t , <- errC , context . Canceled )
75
73
})
74
+
76
75
t .Run ("Development" , func (t * testing.T ) {
77
76
t .Parallel ()
78
77
ctx , cancelFunc := context .WithCancel (context .Background ())
@@ -82,26 +81,12 @@ func TestServer(t *testing.T) {
82
81
83
82
root , cfg := clitest .New (t , "server" , "--dev" , "--tunnel=false" , "--address" , ":0" )
84
83
var buf strings.Builder
84
+ errC := make (chan error )
85
85
root .SetOutput (& buf )
86
- var wg sync.WaitGroup
87
- wg .Add (1 )
88
86
go func () {
89
- defer wg .Done ()
90
-
91
- err := root .ExecuteContext (ctx )
92
- require .ErrorIs (t , err , context .Canceled )
93
-
94
- // Verify that credentials were output to the terminal.
95
- assert .Contains (t , buf .String (), fmt .Sprintf ("email: %s" , wantEmail ), "expected output %q; got no match" , wantEmail )
96
- // Check that the password line is output and that it's non-empty.
97
- if _ , after , found := strings .Cut (buf .String (), "password: " ); found {
98
- before , _ , _ := strings .Cut (after , "\n " )
99
- before = strings .Trim (before , "\r " ) // Ensure no control character is left.
100
- assert .NotEmpty (t , before , "expected non-empty password; got empty" )
101
- } else {
102
- t .Error ("expected password line output; got no match" )
103
- }
87
+ errC <- root .ExecuteContext (ctx )
104
88
}()
89
+
105
90
var token string
106
91
require .Eventually (t , func () bool {
107
92
var err error
@@ -119,8 +104,20 @@ func TestServer(t *testing.T) {
119
104
require .NoError (t , err )
120
105
121
106
cancelFunc ()
122
- wg .Wait ()
107
+ require .ErrorIs (t , <- errC , context .Canceled )
108
+
109
+ // Verify that credentials were output to the terminal.
110
+ assert .Contains (t , buf .String (), fmt .Sprintf ("email: %s" , wantEmail ), "expected output %q; got no match" , wantEmail )
111
+ // Check that the password line is output and that it's non-empty.
112
+ if _ , after , found := strings .Cut (buf .String (), "password: " ); found {
113
+ before , _ , _ := strings .Cut (after , "\n " )
114
+ before = strings .Trim (before , "\r " ) // Ensure no control character is left.
115
+ assert .NotEmpty (t , before , "expected non-empty password; got empty" )
116
+ } else {
117
+ t .Error ("expected password line output; got no match" )
118
+ }
123
119
})
120
+
124
121
// Duplicated test from "Development" above to test setting email/password via env.
125
122
// Cannot run parallel due to os.Setenv.
126
123
//nolint:paralleltest
@@ -136,18 +133,11 @@ func TestServer(t *testing.T) {
136
133
root , cfg := clitest .New (t , "server" , "--dev" , "--tunnel=false" , "--address" , ":0" )
137
134
var buf strings.Builder
138
135
root .SetOutput (& buf )
139
- var wg sync.WaitGroup
140
- wg .Add (1 )
136
+ errC := make (chan error )
141
137
go func () {
142
- defer wg .Done ()
143
-
144
- err := root .ExecuteContext (ctx )
145
- require .ErrorIs (t , err , context .Canceled )
146
-
147
- // Verify that credentials were output to the terminal.
148
- assert .Contains (t , buf .String (), fmt .Sprintf ("email: %s" , wantEmail ), "expected output %q; got no match" , wantEmail )
149
- assert .Contains (t , buf .String (), fmt .Sprintf ("password: %s" , wantPassword ), "expected output %q; got no match" , wantPassword )
138
+ errC <- root .ExecuteContext (ctx )
150
139
}()
140
+
151
141
var token string
152
142
require .Eventually (t , func () bool {
153
143
var err error
@@ -165,8 +155,12 @@ func TestServer(t *testing.T) {
165
155
require .NoError (t , err )
166
156
167
157
cancelFunc ()
168
- wg .Wait ()
158
+ require .ErrorIs (t , <- errC , context .Canceled )
159
+ // Verify that credentials were output to the terminal.
160
+ assert .Contains (t , buf .String (), fmt .Sprintf ("email: %s" , wantEmail ), "expected output %q; got no match" , wantEmail )
161
+ assert .Contains (t , buf .String (), fmt .Sprintf ("password: %s" , wantPassword ), "expected output %q; got no match" , wantPassword )
169
162
})
163
+
170
164
t .Run ("TLSBadVersion" , func (t * testing.T ) {
171
165
t .Parallel ()
172
166
ctx , cancelFunc := context .WithCancel (context .Background ())
@@ -202,10 +196,12 @@ func TestServer(t *testing.T) {
202
196
certPath , keyPath := generateTLSCertificate (t )
203
197
root , cfg := clitest .New (t , "server" , "--dev" , "--tunnel=false" , "--address" , ":0" ,
204
198
"--tls-enable" , "--tls-cert-file" , certPath , "--tls-key-file" , keyPath )
199
+ errC := make (chan error )
205
200
go func () {
206
- err := root .ExecuteContext (ctx )
207
- require .ErrorIs (t , err , context .Canceled )
201
+ errC <- root .ExecuteContext (ctx )
208
202
}()
203
+
204
+ // Verify HTTPS
209
205
var accessURLRaw string
210
206
require .Eventually (t , func () bool {
211
207
var err error
@@ -226,6 +222,9 @@ func TestServer(t *testing.T) {
226
222
}
227
223
_ , err = client .HasFirstUser (ctx )
228
224
require .NoError (t , err )
225
+
226
+ cancelFunc ()
227
+ require .ErrorIs (t , <- errC , context .Canceled )
229
228
})
230
229
// This cannot be ran in parallel because it uses a signal.
231
230
//nolint:paralleltest
@@ -284,14 +283,12 @@ func TestServer(t *testing.T) {
284
283
ctx , cancelFunc := context .WithCancel (context .Background ())
285
284
defer cancelFunc ()
286
285
root , _ := clitest .New (t , "server" , "--dev" , "--tunnel=false" , "--address" , ":0" , "--trace=true" )
287
- done := make (chan struct {} )
286
+ errC := make (chan error )
288
287
go func () {
289
- defer close (done )
290
- err := root .ExecuteContext (ctx )
291
- require .ErrorIs (t , err , context .Canceled )
288
+ errC <- root .ExecuteContext (ctx )
292
289
}()
293
290
cancelFunc ()
294
- <- done
291
+ require . ErrorIs ( t , <- errC , context . Canceled )
295
292
require .Error (t , goleak .Find ())
296
293
})
297
294
}
0 commit comments