chore: fix flake in tunnel (#9746)

This commit is contained in:
Dean Sheather 2023-09-19 11:54:56 -07:00 committed by GitHub
parent 70e481e7a5
commit eda32659a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 13 deletions

View File

@ -189,23 +189,34 @@ func newTunnelServer(t *testing.T) *tunnelServer {
require.Equal(t, "https", baseURLParsed.Scheme) require.Equal(t, "https", baseURLParsed.Scheme)
baseURLParsed.Host = net.JoinHostPort("tunnel.coder.com", baseURLParsed.Port()) baseURLParsed.Host = net.JoinHostPort("tunnel.coder.com", baseURLParsed.Port())
wireguardPort := freeUDPPort(t)
key, err := tunnelsdk.GeneratePrivateKey() key, err := tunnelsdk.GeneratePrivateKey()
require.NoError(t, err) require.NoError(t, err)
options := &tunneld.Options{ // Sadly the tunnel server needs to be passed a port number and can't be
BaseURL: baseURLParsed, // passed an active listener (because wireguard needs to make the listener),
WireguardEndpoint: fmt.Sprintf("127.0.0.1:%d", wireguardPort), // so we may need to try a few times to get a free port.
WireguardPort: wireguardPort, var td *tunneld.API
WireguardKey: key, for i := 0; i < 10; i++ {
WireguardMTU: tunneld.DefaultWireguardMTU, wireguardPort := freeUDPPort(t)
WireguardServerIP: tunneld.DefaultWireguardServerIP, options := &tunneld.Options{
WireguardNetworkPrefix: tunneld.DefaultWireguardNetworkPrefix, BaseURL: baseURLParsed,
} WireguardEndpoint: fmt.Sprintf("127.0.0.1:%d", wireguardPort),
WireguardPort: wireguardPort,
WireguardKey: key,
WireguardMTU: tunneld.DefaultWireguardMTU,
WireguardServerIP: tunneld.DefaultWireguardServerIP,
WireguardNetworkPrefix: tunneld.DefaultWireguardNetworkPrefix,
}
td, err := tunneld.New(options) td, err = tunneld.New(options)
require.NoError(t, err) if err == nil {
break
}
t.Logf("failed to create tunnel server on port %d: %s", wireguardPort, err)
}
if td == nil {
t.Fatal("failed to create tunnel server in 10 attempts")
}
handler = td.Router() handler = td.Router()
t.Cleanup(func() { t.Cleanup(func() {
_ = td.Close() _ = td.Close()