Skip to content

Commit c820eee

Browse files
committed
Remove parallel for test
Signed-off-by: Spike Curtis <spike@coder.com>
1 parent c8dde6e commit c820eee

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

coderd/devtunnel/tunnel_test.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,33 @@ import (
1414
"testing"
1515
"time"
1616

17-
"cdr.dev/slog/sloggers/slogtest"
18-
"github.com/coder/coder/coderd/devtunnel"
1917
"github.com/stretchr/testify/assert"
2018
"github.com/stretchr/testify/require"
2119
"golang.zx2c4.com/wireguard/conn"
2220
"golang.zx2c4.com/wireguard/device"
2321
"golang.zx2c4.com/wireguard/tun/netstack"
2422
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
23+
24+
"cdr.dev/slog/sloggers/slogtest"
25+
"github.com/coder/coder/coderd/devtunnel"
2526
)
2627

2728
// The tunnel leaks a few goroutines that aren't impactful to production scenarios.
2829
// func TestMain(m *testing.M) {
2930
// goleak.VerifyTestMain(m)
3031
// }
3132

33+
// TestTunnel cannot run in parallel because we hardcode the UDP port used by the wireguard server.
34+
// nolint: tparallel
3235
func TestTunnel(t *testing.T) {
33-
t.Parallel()
34-
3536
ctx, cancelTun := context.WithCancel(context.Background())
3637
defer cancelTun()
3738

3839
server := http.Server{
3940
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4041
t.Log("got request for", r.URL)
42+
// Going to use something _slightly_ exotic so that we can't accidentally get some
43+
// default behavior creating a false positive on the test
4144
w.WriteHeader(http.StatusAccepted)
4245
}),
4346
BaseContext: func(_ net.Listener) context.Context {
@@ -60,12 +63,12 @@ func TestTunnel(t *testing.T) {
6063
t.Cleanup(func() { tun.Listener.Close() })
6164

6265
require.Eventually(t, func() bool {
63-
res, err := fTunServer.requestHttp()
66+
res, err := fTunServer.requestHTTP()
6467
require.NoError(t, err)
6568
defer res.Body.Close()
6669
_, _ = io.Copy(io.Discard, res.Body)
6770

68-
return res.StatusCode == http.StatusOK
71+
return res.StatusCode == http.StatusAccepted
6972
}, time.Minute, time.Second)
7073

7174
assert.NoError(t, server.Close())
@@ -103,9 +106,9 @@ const (
103106
)
104107

105108
var (
106-
serverIp = netip.AddrFrom16([16]byte{ipByte1, ipByte2, 15: 0x1})
107-
dnsIp = netip.AddrFrom4([4]byte{1, 1, 1, 1})
108-
clientIp = netip.AddrFrom16([16]byte{ipByte1, ipByte2, 15: 0x2})
109+
serverIP = netip.AddrFrom16([16]byte{ipByte1, ipByte2, 15: 0x1})
110+
dnsIP = netip.AddrFrom4([4]byte{1, 1, 1, 1})
111+
clientIP = netip.AddrFrom16([16]byte{ipByte1, ipByte2, 15: 0x2})
109112
)
110113

111114
func newFakeTunnelServer(t *testing.T) *fakeTunnelServer {
@@ -115,8 +118,8 @@ func newFakeTunnelServer(t *testing.T) *fakeTunnelServer {
115118
pub := priv.PublicKey()
116119
pubBytes := [32]byte(pub)
117120
tun, tnet, err := netstack.CreateNetTUN(
118-
[]netip.Addr{serverIp},
119-
[]netip.Addr{dnsIp},
121+
[]netip.Addr{serverIP},
122+
[]netip.Addr{dnsIP},
120123
1280,
121124
)
122125
require.NoError(t, err)
@@ -127,11 +130,14 @@ listen_port=%d`,
127130
wgPort,
128131
))
129132
require.NoError(t, err)
133+
t.Cleanup(func() {
134+
dev.Close()
135+
})
130136

131137
err = dev.Up()
132138
require.NoError(t, err)
133139

134-
server := newFakeTunnelHttpsServer(t, pubBytes)
140+
server := newFakeTunnelHTTPSServer(t, pubBytes)
135141

136142
return &fakeTunnelServer{
137143
t: t,
@@ -143,16 +149,16 @@ listen_port=%d`,
143149
}
144150
}
145151

146-
func newFakeTunnelHttpsServer(t *testing.T, pubBytes [32]byte) *httptest.Server {
152+
func newFakeTunnelHTTPSServer(t *testing.T, pubBytes [32]byte) *httptest.Server {
147153
handler := http.NewServeMux()
148154
handler.HandleFunc("/tun", func(writer http.ResponseWriter, request *http.Request) {
149155
assert.Equal(t, "POST", request.Method)
150156

151157
resp := devtunnel.ServerResponse{
152-
Hostname: fmt.Sprintf("[%s]", serverIp.String()),
153-
ServerIP: serverIp,
158+
Hostname: fmt.Sprintf("[%s]", serverIP.String()),
159+
ServerIP: serverIP,
154160
ServerPublicKey: hex.EncodeToString(pubBytes[:]),
155-
ClientIP: clientIp,
161+
ClientIP: clientIP,
156162
}
157163
b, err := json.Marshal(&resp)
158164
assert.NoError(t, err)
@@ -178,7 +184,7 @@ func (f *fakeTunnelServer) config() devtunnel.Config {
178184
err = f.device.IpcSet(fmt.Sprintf(`public_key=%x
179185
allowed_ip=%s/128`,
180186
pub[:],
181-
clientIp.String(),
187+
clientIP.String(),
182188
))
183189
require.NoError(f.t, err)
184190
return devtunnel.Config{
@@ -194,11 +200,11 @@ allowed_ip=%s/128`,
194200
}
195201
}
196202

197-
func (f *fakeTunnelServer) requestHttp() (*http.Response, error) {
203+
func (f *fakeTunnelServer) requestHTTP() (*http.Response, error) {
198204
transport := &http.Transport{
199205
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
200206
f.t.Log("Dial", network, addr)
201-
nc, err := f.tnet.DialContextTCPAddrPort(ctx, netip.AddrPortFrom(clientIp, 8090))
207+
nc, err := f.tnet.DialContextTCPAddrPort(ctx, netip.AddrPortFrom(clientIP, 8090))
202208
assert.NoError(f.t, err)
203209
return nc, err
204210
},
@@ -207,5 +213,5 @@ func (f *fakeTunnelServer) requestHttp() (*http.Response, error) {
207213
Transport: transport,
208214
Timeout: 10 * time.Second,
209215
}
210-
return client.Get(fmt.Sprintf("http://[%s]:8090", clientIp))
216+
return client.Get(fmt.Sprintf("http://[%s]:8090", clientIP))
211217
}

0 commit comments

Comments
 (0)