Skip to content

Commit fd5850a

Browse files
committed
Use pkill
1 parent fffe83a commit fd5850a

File tree

6 files changed

+59
-10
lines changed

6 files changed

+59
-10
lines changed

site/e2e/helpers.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, Page } from "@playwright/test"
2-
import { spawn } from "child_process"
2+
import { ChildProcess, exec, spawn } from "child_process"
33
import { randomUUID } from "crypto"
44
import path from "path"
55
import express from "express"
@@ -19,6 +19,7 @@ import { port } from "./playwright.config"
1919
import * as ssh from "ssh2"
2020
import { Duplex } from "stream"
2121
import { WorkspaceBuildParameter } from "api/typesGenerated"
22+
import axios from "axios"
2223

2324
// createWorkspace creates a workspace for a template.
2425
// It does not wait for it to be running, but it does navigate to the page.
@@ -229,7 +230,10 @@ export const buildWorkspaceWithParameters = async (
229230

230231
// startAgent runs the coder agent with the provided token.
231232
// It awaits the agent to be ready before returning.
232-
export const startAgent = async (page: Page, token: string): Promise<void> => {
233+
export const startAgent = async (
234+
page: Page,
235+
token: string,
236+
): Promise<ChildProcess> => {
233237
return startAgentWithCommand(page, token, "go", "run", coderMainPath())
234238
}
235239

@@ -308,14 +312,14 @@ export const startAgentWithCommand = async (
308312
token: string,
309313
command: string,
310314
...args: string[]
311-
): Promise<void> => {
315+
): Promise<ChildProcess> => {
312316
const cp = spawn(command, [...args, "agent", "--no-reap"], {
313317
env: {
314318
...process.env,
315319
CODER_AGENT_URL: "http://localhost:" + port,
316320
CODER_AGENT_TOKEN: token,
317-
CODER_AGENT_PPROF_ADDRESS: "127.0.0.1:2114",
318-
CODER_AGENT_PROMETHEUS_ADDRESS: "127.0.0.1:6061",
321+
CODER_AGENT_PPROF_ADDRESS: "127.0.0.1:6061",
322+
CODER_AGENT_PROMETHEUS_ADDRESS: "127.0.0.1:2114",
319323
},
320324
})
321325
cp.stdout.on("data", (data: Buffer) => {
@@ -332,6 +336,36 @@ export const startAgentWithCommand = async (
332336
})
333337

334338
await page.getByTestId("agent-status-ready").waitFor({ state: "visible" })
339+
return cp
340+
}
341+
342+
export const stopAgent = async (cp: ChildProcess) => {
343+
exec(`pkill -P ${cp.pid}`, (error) => {
344+
if (error) {
345+
throw new Error(`exec error: ${JSON.stringify(error)}`)
346+
}
347+
})
348+
await waitUntilUrlIsNotResponding("http://127.0.0.1:2114")
349+
}
350+
351+
const waitUntilUrlIsNotResponding = async (url: string) => {
352+
const maxRetries = 30
353+
const retryIntervalMs = 1000
354+
let retries = 0
355+
356+
while (retries < maxRetries) {
357+
try {
358+
await axios.get(url)
359+
} catch (error) {
360+
return
361+
}
362+
363+
retries++
364+
await new Promise((resolve) => setTimeout(resolve, retryIntervalMs))
365+
}
366+
throw new Error(
367+
`URL ${url} is still responding after ${maxRetries * retryIntervalMs}ms`,
368+
)
335369
}
336370

337371
const coderMainPath = (): string => {

site/e2e/tests/app.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
createTemplate,
66
createWorkspace,
77
startAgent,
8+
stopAgent,
89
stopWorkspace,
910
} from "../helpers"
1011
import { beforeCoderTest } from "../hooks"
@@ -49,7 +50,7 @@ test("app", async ({ context, page }) => {
4950
],
5051
})
5152
const workspaceName = await createWorkspace(page, template)
52-
await startAgent(page, token)
53+
const agent = await startAgent(page, token)
5354

5455
// Wait for the web terminal to open in a new tab
5556
const pagePromise = context.waitForEvent("page")
@@ -59,4 +60,5 @@ test("app", async ({ context, page }) => {
5960
await app.getByText(appContent).isVisible()
6061

6162
await stopWorkspace(page, workspaceName)
63+
await stopAgent(agent)
6264
})

site/e2e/tests/listTemplates.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { test, expect } from "@playwright/test"
2+
import { beforeCoderTest } from "../hooks"
3+
4+
test.beforeEach(async ({ page }) => await beforeCoderTest(page))
25

36
test("list templates", async ({ page, baseURL }) => {
47
await page.goto(`${baseURL}/templates`, { waitUntil: "domcontentloaded" })

site/e2e/tests/outdatedAgent.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
downloadCoderVersion,
77
sshIntoWorkspace,
88
startAgentWithCommand,
9+
stopAgent,
910
stopWorkspace,
1011
} from "../helpers"
1112
import { beforeCoderTest } from "../hooks"
@@ -35,7 +36,7 @@ test("ssh with agent " + agentVersion, async ({ page }) => {
3536
})
3637
const workspaceName = await createWorkspace(page, template)
3738
const binaryPath = await downloadCoderVersion(agentVersion)
38-
await startAgentWithCommand(page, token, binaryPath)
39+
const agent = await startAgentWithCommand(page, token, binaryPath)
3940

4041
const client = await sshIntoWorkspace(page, workspaceName)
4142
await new Promise<void>((resolve, reject) => {
@@ -55,4 +56,5 @@ test("ssh with agent " + agentVersion, async ({ page }) => {
5556
})
5657

5758
await stopWorkspace(page, workspaceName)
59+
await stopAgent(agent)
5860
})

site/e2e/tests/outdatedCLI.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
downloadCoderVersion,
77
sshIntoWorkspace,
88
startAgent,
9+
stopAgent,
910
stopWorkspace,
1011
} from "../helpers"
1112
import { beforeCoderTest } from "../hooks"
@@ -34,7 +35,7 @@ test("ssh with client " + clientVersion, async ({ page }) => {
3435
],
3536
})
3637
const workspaceName = await createWorkspace(page, template)
37-
await startAgent(page, token)
38+
const agent = await startAgent(page, token)
3839
const binaryPath = await downloadCoderVersion(clientVersion)
3940

4041
const client = await sshIntoWorkspace(page, workspaceName, binaryPath)
@@ -55,4 +56,5 @@ test("ssh with client " + clientVersion, async ({ page }) => {
5556
})
5657

5758
await stopWorkspace(page, workspaceName)
59+
await stopAgent(agent)
5860
})

site/e2e/tests/webTerminal.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { test } from "@playwright/test"
2-
import { createTemplate, createWorkspace, startAgent } from "../helpers"
2+
import {
3+
createTemplate,
4+
createWorkspace,
5+
startAgent,
6+
stopAgent,
7+
} from "../helpers"
38
import { randomUUID } from "crypto"
49
import { beforeCoderTest } from "../hooks"
510

@@ -28,7 +33,7 @@ test("web terminal", async ({ context, page }) => {
2833
],
2934
})
3035
await createWorkspace(page, template)
31-
await startAgent(page, token)
36+
const agent = await startAgent(page, token)
3237

3338
// Wait for the web terminal to open in a new tab
3439
const pagePromise = context.waitForEvent("page")
@@ -50,4 +55,5 @@ test("web terminal", async ({ context, page }) => {
5055
}
5156
await new Promise((r) => setTimeout(r, 250))
5257
}
58+
await stopAgent(agent)
5359
})

0 commit comments

Comments
 (0)