From 5006edcee7527166e367684217502117cae0fe00 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Wed, 13 Mar 2024 09:49:08 -0500 Subject: [PATCH] test: apptest was accidently choosing ports in use Apptest requires a port without a listening server to test failure cases. This port was chosen and had a chance of actually being provisioned. To prevent this accident, a port <1k is chosen, since those will never be allocated. --- coderd/workspaceapps/apptest/setup.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/coderd/workspaceapps/apptest/setup.go b/coderd/workspaceapps/apptest/setup.go index d19156cb4c8f1..702789e4cf76f 100644 --- a/coderd/workspaceapps/apptest/setup.go +++ b/coderd/workspaceapps/apptest/setup.go @@ -309,15 +309,31 @@ func createWorkspaceWithApps(t *testing.T, client *codersdk.Client, orgID uuid.U }, }, workspaceMutators...) + // Intentionally going to choose a port that will never be chosen. + // Ports <1k will never be selected. 396 is for some old OS over IP. + // It will never likely be provisioned. Using quick timeout since + // it's all localhost + fakeAppURL := "http://127.1.0.1:396" + conn, err := net.DialTimeout("tcp", fakeAppURL, time.Millisecond*100) + if err == nil { + // In the absolute rare case someone hits this. Writing code to find a free port + // seems like a waste of time to program and run. + _ = conn.Close() + t.Errorf("an unused port is required for the fake app. "+ + "The url %q happens to be an active port. If you hit this, then this test"+ + "will need to be modified to run on your system. Or you can stop serving an"+ + "app on that port.", fakeAppURL) + t.FailNow() + } + appURL := fmt.Sprintf("%s://127.0.0.1:%d?%s", scheme, port, proxyTestAppQuery) protoApps := []*proto.App{ { Slug: proxyTestAppNameFake, DisplayName: proxyTestAppNameFake, SharingLevel: proto.AppSharingLevel_OWNER, - // Hopefully this IP and port doesn't exist. - Url: "http://127.1.0.1:65535", - Subdomain: true, + Url: fakeAppURL, + Subdomain: true, }, { Slug: proxyTestAppNameOwner,