Skip to content

Commit 4cba83b

Browse files
authored
test: apptest was accidently choosing ports in use (#12580)
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.
1 parent 14130de commit 4cba83b

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

coderd/workspaceapps/apptest/setup.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,31 @@ func createWorkspaceWithApps(t *testing.T, client *codersdk.Client, orgID uuid.U
309309
},
310310
}, workspaceMutators...)
311311

312+
// Intentionally going to choose a port that will never be chosen.
313+
// Ports <1k will never be selected. 396 is for some old OS over IP.
314+
// It will never likely be provisioned. Using quick timeout since
315+
// it's all localhost
316+
fakeAppURL := "http://127.1.0.1:396"
317+
conn, err := net.DialTimeout("tcp", fakeAppURL, time.Millisecond*100)
318+
if err == nil {
319+
// In the absolute rare case someone hits this. Writing code to find a free port
320+
// seems like a waste of time to program and run.
321+
_ = conn.Close()
322+
t.Errorf("an unused port is required for the fake app. "+
323+
"The url %q happens to be an active port. If you hit this, then this test"+
324+
"will need to be modified to run on your system. Or you can stop serving an"+
325+
"app on that port.", fakeAppURL)
326+
t.FailNow()
327+
}
328+
312329
appURL := fmt.Sprintf("%s://127.0.0.1:%d?%s", scheme, port, proxyTestAppQuery)
313330
protoApps := []*proto.App{
314331
{
315332
Slug: proxyTestAppNameFake,
316333
DisplayName: proxyTestAppNameFake,
317334
SharingLevel: proto.AppSharingLevel_OWNER,
318-
// Hopefully this IP and port doesn't exist.
319-
Url: "http://127.1.0.1:65535",
320-
Subdomain: true,
335+
Url: fakeAppURL,
336+
Subdomain: true,
321337
},
322338
{
323339
Slug: proxyTestAppNameOwner,

0 commit comments

Comments
 (0)