The tunnel leaks a few goroutines that aren't impactful to production scenarios. func TestMain(m *testing.M) { goleak.VerifyTestMain(m) }
(t *testing.T)
| 32 | // } |
| 33 | |
| 34 | func TestTunnel(t *testing.T) { |
| 35 | t.Parallel() |
| 36 | |
| 37 | if runtime.GOOS == "windows" { |
| 38 | t.Skip("these tests are flaky on windows and cause the tests to fail with '(unknown)' and no output, see https://github.com/coder/internal/issues/579") |
| 39 | } |
| 40 | |
| 41 | cases := []struct { |
| 42 | name string |
| 43 | version tunnelsdk.TunnelVersion |
| 44 | }{ |
| 45 | { |
| 46 | name: "V1", |
| 47 | version: tunnelsdk.TunnelVersion1, |
| 48 | }, |
| 49 | { |
| 50 | name: "V2", |
| 51 | version: tunnelsdk.TunnelVersion2, |
| 52 | }, |
| 53 | } |
| 54 | |
| 55 | for _, c := range cases { |
| 56 | t.Run(c.name, func(t *testing.T) { |
| 57 | t.Parallel() |
| 58 | |
| 59 | ctx, cancelTun := context.WithCancel(context.Background()) |
| 60 | defer cancelTun() |
| 61 | |
| 62 | server := http.Server{ |
| 63 | ReadHeaderTimeout: time.Minute, |
| 64 | Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 65 | t.Log("got request for", r.URL) |
| 66 | // Going to use something _slightly_ exotic so that we can't |
| 67 | // accidentally get some default behavior creating a false |
| 68 | // positive on the test |
| 69 | w.WriteHeader(http.StatusAccepted) |
| 70 | }), |
| 71 | BaseContext: func(_ net.Listener) context.Context { |
| 72 | return ctx |
| 73 | }, |
| 74 | } |
| 75 | |
| 76 | tunServer := newTunnelServer(t) |
| 77 | cfg := tunServer.config(t, c.version) |
| 78 | |
| 79 | tun, err := devtunnel.NewWithConfig(ctx, testutil.Logger(t), cfg) |
| 80 | require.NoError(t, err) |
| 81 | require.Len(t, tun.OtherURLs, 1) |
| 82 | t.Log(tun.URL, tun.OtherURLs[0]) |
| 83 | |
| 84 | hostSplit := strings.SplitN(tun.URL.Host, ".", 2) |
| 85 | require.Len(t, hostSplit, 2) |
| 86 | require.Equal(t, hostSplit[1], tunServer.api.BaseURL.Host) |
| 87 | |
| 88 | // Verify the hostname using the same logic as the tunnel server. |
| 89 | ip1, urls := tunServer.api.WireguardPublicKeyToIPAndURLs(cfg.PublicKey, c.version) |
| 90 | require.Len(t, urls, 2) |
| 91 | require.Equal(t, urls[0].String(), tun.URL.String()) |
nothing calls this directly
no test coverage detected