2022-02-15 04:53:28 +08:00
|
|
|
import { logger } from "@coder/logger"
|
2022-02-16 07:19:22 +08:00
|
|
|
import * as http from "http"
|
2020-02-21 05:50:01 +08:00
|
|
|
import * as path from "path"
|
2023-09-22 08:13:34 +08:00
|
|
|
import { ensureAddress } from "../../../src/node/app"
|
2021-07-28 07:52:57 +08:00
|
|
|
import { SettingsProvider, UpdateSettings } from "../../../src/node/settings"
|
|
|
|
import { LatestResponse, UpdateProvider } from "../../../src/node/update"
|
2023-09-22 08:13:34 +08:00
|
|
|
import { clean, mockLogger, tmpdir } from "../../utils/helpers"
|
2020-02-21 05:50:01 +08:00
|
|
|
|
2021-05-04 06:39:59 +08:00
|
|
|
describe("update", () => {
|
2020-02-21 05:50:01 +08:00
|
|
|
let version = "1.0.0"
|
|
|
|
let spy: string[] = []
|
|
|
|
const server = http.createServer((request: http.IncomingMessage, response: http.ServerResponse) => {
|
|
|
|
if (!request.url) {
|
|
|
|
throw new Error("no url")
|
|
|
|
}
|
2020-07-23 04:55:14 +08:00
|
|
|
|
2020-02-21 05:50:01 +08:00
|
|
|
spy.push(request.url)
|
2020-07-23 04:55:14 +08:00
|
|
|
|
|
|
|
// Return the latest version.
|
2020-02-21 05:50:01 +08:00
|
|
|
if (request.url === "/latest") {
|
|
|
|
const latest: LatestResponse = {
|
|
|
|
name: version,
|
|
|
|
}
|
2020-07-23 04:55:14 +08:00
|
|
|
response.writeHead(200)
|
2020-02-21 05:50:01 +08:00
|
|
|
return response.end(JSON.stringify(latest))
|
|
|
|
}
|
|
|
|
|
2022-02-15 04:53:28 +08:00
|
|
|
if (request.url === "/reject-status-code") {
|
|
|
|
response.writeHead(500)
|
|
|
|
return response.end("rejected status code test")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.url === "/no-location-header") {
|
|
|
|
response.writeHead(301, "testing", {
|
|
|
|
location: "",
|
|
|
|
})
|
|
|
|
return response.end("rejected status code test")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.url === "/with-location-header") {
|
|
|
|
response.writeHead(301, "testing", {
|
|
|
|
location: "/latest",
|
|
|
|
})
|
|
|
|
|
|
|
|
return response.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if url matches /redirect/${number}
|
|
|
|
// with optional trailing slash
|
|
|
|
const match = request.url.match(/\/redirect\/([0-9]+)\/?$/)
|
|
|
|
if (match) {
|
|
|
|
if (request.url === "/redirect/0") {
|
|
|
|
response.writeHead(200)
|
|
|
|
return response.end("done")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract 1 from the current redirect number
|
|
|
|
// i.e. /redirect/10 -> /redirect/9 -> /redirect/8
|
|
|
|
const currentRedirectNumber = parseInt(match[1])
|
|
|
|
const newRedirectNumber = currentRedirectNumber - 1
|
|
|
|
|
|
|
|
response.writeHead(302, "testing", {
|
|
|
|
location: `/redirect/${String(newRedirectNumber)}`,
|
|
|
|
})
|
|
|
|
return response.end("")
|
|
|
|
}
|
|
|
|
|
2020-07-23 04:55:14 +08:00
|
|
|
// Anything else is a 404.
|
|
|
|
response.writeHead(404)
|
2022-08-11 05:15:52 +08:00
|
|
|
return response.end("not found")
|
2020-02-21 05:50:01 +08:00
|
|
|
})
|
|
|
|
|
2021-12-18 03:06:52 +08:00
|
|
|
let _settings: SettingsProvider<UpdateSettings> | undefined
|
|
|
|
const settings = (): SettingsProvider<UpdateSettings> => {
|
|
|
|
if (!_settings) {
|
|
|
|
throw new Error("Settings provider has not been created")
|
|
|
|
}
|
|
|
|
return _settings
|
|
|
|
}
|
2020-02-21 05:50:01 +08:00
|
|
|
|
2020-10-21 07:05:58 +08:00
|
|
|
let _provider: UpdateProvider | undefined
|
|
|
|
const provider = (): UpdateProvider => {
|
2020-02-21 05:50:01 +08:00
|
|
|
if (!_provider) {
|
2021-12-18 03:06:52 +08:00
|
|
|
throw new Error("Update provider has not been created")
|
2020-02-21 05:50:01 +08:00
|
|
|
}
|
|
|
|
return _provider
|
|
|
|
}
|
|
|
|
|
2023-09-22 08:13:34 +08:00
|
|
|
let address = new URL("http://localhost")
|
2021-01-09 04:55:47 +08:00
|
|
|
beforeAll(async () => {
|
2021-12-18 03:06:52 +08:00
|
|
|
mockLogger()
|
|
|
|
|
|
|
|
const testName = "update"
|
|
|
|
await clean(testName)
|
|
|
|
const testDir = await tmpdir(testName)
|
|
|
|
const jsonPath = path.join(testDir, "update.json")
|
|
|
|
_settings = new SettingsProvider<UpdateSettings>(jsonPath)
|
|
|
|
|
2020-03-05 00:31:59 +08:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
server.on("error", reject)
|
|
|
|
server.on("listening", resolve)
|
|
|
|
server.listen({
|
|
|
|
port: 0,
|
|
|
|
host: "localhost",
|
|
|
|
})
|
|
|
|
})
|
2021-12-18 03:06:52 +08:00
|
|
|
|
2023-09-22 08:13:34 +08:00
|
|
|
const addr = ensureAddress(server, "http")
|
|
|
|
if (typeof addr === "string") {
|
|
|
|
throw new Error("unable to run update tests with unix sockets")
|
2021-12-18 03:06:52 +08:00
|
|
|
}
|
2023-09-22 08:13:34 +08:00
|
|
|
address = addr
|
|
|
|
address.pathname = "/latest"
|
|
|
|
_provider = new UpdateProvider(address.toString(), _settings)
|
2020-02-21 05:50:01 +08:00
|
|
|
})
|
|
|
|
|
2021-01-09 04:55:47 +08:00
|
|
|
afterAll(() => {
|
2020-02-21 05:50:01 +08:00
|
|
|
server.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-02-15 04:53:28 +08:00
|
|
|
jest.clearAllMocks()
|
2020-02-21 05:50:01 +08:00
|
|
|
spy = []
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should get the latest", async () => {
|
|
|
|
version = "2.1.0"
|
|
|
|
|
|
|
|
const p = provider()
|
|
|
|
const now = Date.now()
|
|
|
|
const update = await p.getUpdate()
|
|
|
|
|
2021-12-18 03:06:52 +08:00
|
|
|
await expect(settings().read()).resolves.toEqual({ update })
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(isNaN(update.checked)).toEqual(false)
|
2022-05-07 00:51:44 +08:00
|
|
|
expect(update.checked).toBeGreaterThanOrEqual(now)
|
|
|
|
expect(update.checked).toBeLessThanOrEqual(Date.now())
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(update.version).toStrictEqual("2.1.0")
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(spy).toEqual(["/latest"])
|
2020-02-21 05:50:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should keep existing information", async () => {
|
|
|
|
version = "3.0.1"
|
|
|
|
|
|
|
|
const p = provider()
|
|
|
|
const now = Date.now()
|
|
|
|
const update = await p.getUpdate()
|
|
|
|
|
2021-12-18 03:06:52 +08:00
|
|
|
await expect(settings().read()).resolves.toEqual({ update })
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(isNaN(update.checked)).toStrictEqual(false)
|
2022-05-07 00:51:44 +08:00
|
|
|
expect(update.checked).toBeLessThanOrEqual(now)
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(update.version).toStrictEqual("2.1.0")
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(spy).toEqual([])
|
2020-02-21 05:50:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should force getting the latest", async () => {
|
|
|
|
version = "4.1.1"
|
|
|
|
|
|
|
|
const p = provider()
|
|
|
|
const now = Date.now()
|
|
|
|
const update = await p.getUpdate(true)
|
|
|
|
|
2021-12-18 03:06:52 +08:00
|
|
|
await expect(settings().read()).resolves.toEqual({ update })
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(isNaN(update.checked)).toStrictEqual(false)
|
2022-04-20 04:46:23 +08:00
|
|
|
expect(update.checked).toBeGreaterThanOrEqual(now)
|
2022-05-07 00:51:44 +08:00
|
|
|
expect(update.checked).toBeLessThanOrEqual(Date.now())
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(update.version).toStrictEqual("4.1.1")
|
|
|
|
expect(spy).toStrictEqual(["/latest"])
|
2020-02-21 05:50:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should get latest after interval passes", async () => {
|
|
|
|
const p = provider()
|
|
|
|
await p.getUpdate()
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(spy).toEqual([])
|
2020-02-21 05:50:01 +08:00
|
|
|
|
|
|
|
let checked = Date.now() - 1000 * 60 * 60 * 23
|
2021-12-18 03:06:52 +08:00
|
|
|
await settings().write({ update: { checked, version } })
|
2020-02-21 05:50:01 +08:00
|
|
|
await p.getUpdate()
|
2021-01-09 04:55:47 +08:00
|
|
|
expect(spy).toEqual([])
|
2020-02-21 05:50:01 +08:00
|
|
|
|
|
|
|
checked = Date.now() - 1000 * 60 * 60 * 25
|
2021-12-18 03:06:52 +08:00
|
|
|
await settings().write({ update: { checked, version } })
|
2020-02-21 05:50:01 +08:00
|
|
|
|
|
|
|
const update = await p.getUpdate()
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(update.checked).not.toStrictEqual(checked)
|
|
|
|
expect(spy).toStrictEqual(["/latest"])
|
2020-02-21 05:50:01 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should check if it's the current version", async () => {
|
|
|
|
version = "9999999.99999.9999"
|
|
|
|
|
|
|
|
const p = provider()
|
|
|
|
let update = await p.getUpdate(true)
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(p.isLatestVersion(update)).toStrictEqual(false)
|
2020-02-21 05:50:01 +08:00
|
|
|
|
|
|
|
version = "0.0.0"
|
|
|
|
update = await p.getUpdate(true)
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(p.isLatestVersion(update)).toStrictEqual(true)
|
2020-02-21 05:50:01 +08:00
|
|
|
|
|
|
|
// Old version format; make sure it doesn't report as being later.
|
|
|
|
version = "999999.9999-invalid999.99.9"
|
|
|
|
update = await p.getUpdate(true)
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(p.isLatestVersion(update)).toStrictEqual(true)
|
2020-02-21 05:50:01 +08:00
|
|
|
})
|
|
|
|
|
2020-03-26 04:00:35 +08:00
|
|
|
it("should not reject if unable to fetch", async () => {
|
2021-12-18 03:06:52 +08:00
|
|
|
let provider = new UpdateProvider("invalid", settings())
|
2021-05-04 06:39:59 +08:00
|
|
|
let now = Date.now()
|
|
|
|
let update = await provider.getUpdate(true)
|
|
|
|
expect(isNaN(update.checked)).toStrictEqual(false)
|
2022-04-20 04:46:23 +08:00
|
|
|
expect(update.checked).toBeGreaterThanOrEqual(now)
|
2022-05-07 00:51:44 +08:00
|
|
|
expect(update.checked).toBeLessThanOrEqual(Date.now())
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(update.version).toStrictEqual("unknown")
|
2020-03-26 04:00:35 +08:00
|
|
|
|
2021-12-18 03:06:52 +08:00
|
|
|
provider = new UpdateProvider("http://probably.invalid.dev.localhost/latest", settings())
|
2021-05-04 06:39:59 +08:00
|
|
|
now = Date.now()
|
|
|
|
update = await provider.getUpdate(true)
|
|
|
|
expect(isNaN(update.checked)).toStrictEqual(false)
|
2022-04-20 04:46:23 +08:00
|
|
|
expect(update.checked).toBeGreaterThanOrEqual(now)
|
2022-05-07 00:51:44 +08:00
|
|
|
expect(update.checked).toBeLessThanOrEqual(Date.now())
|
2021-05-04 06:39:59 +08:00
|
|
|
expect(update.version).toStrictEqual("unknown")
|
2020-03-26 04:00:35 +08:00
|
|
|
})
|
2022-02-15 04:53:28 +08:00
|
|
|
|
|
|
|
it("should reject if response has status code 500", async () => {
|
2023-09-22 08:13:34 +08:00
|
|
|
address.pathname = "/reject-status-code"
|
|
|
|
const provider = new UpdateProvider(address.toString(), settings())
|
|
|
|
const update = await provider.getUpdate(true)
|
|
|
|
|
|
|
|
expect(update.version).toBe("unknown")
|
|
|
|
expect(logger.error).toHaveBeenCalled()
|
|
|
|
expect(logger.error).toHaveBeenCalledWith("Failed to get latest version", {
|
|
|
|
identifier: "error",
|
|
|
|
value: `${address.toString()}: 500`,
|
|
|
|
})
|
2022-02-15 04:53:28 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should reject if no location header provided", async () => {
|
2023-09-22 08:13:34 +08:00
|
|
|
address.pathname = "/no-location-header"
|
|
|
|
const provider = new UpdateProvider(address.toString(), settings())
|
|
|
|
const update = await provider.getUpdate(true)
|
|
|
|
|
|
|
|
expect(update.version).toBe("unknown")
|
|
|
|
expect(logger.error).toHaveBeenCalled()
|
|
|
|
expect(logger.error).toHaveBeenCalledWith("Failed to get latest version", {
|
|
|
|
identifier: "error",
|
|
|
|
value: `received redirect with no location header`,
|
|
|
|
})
|
2022-02-15 04:53:28 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should resolve the request with response.headers.location", async () => {
|
|
|
|
version = "4.1.1"
|
2023-09-22 08:13:34 +08:00
|
|
|
address.pathname = "/with-location-header"
|
|
|
|
const provider = new UpdateProvider(address.toString(), settings())
|
|
|
|
const update = await provider.getUpdate(true)
|
2022-02-15 04:53:28 +08:00
|
|
|
|
2023-09-22 08:13:34 +08:00
|
|
|
expect(logger.error).not.toHaveBeenCalled()
|
|
|
|
expect(update.version).toBe("4.1.1")
|
2022-02-15 04:53:28 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should reject if more than 10 redirects", async () => {
|
2023-09-22 08:13:34 +08:00
|
|
|
address.pathname = "/redirect/11"
|
|
|
|
const provider = new UpdateProvider(address.toString(), settings())
|
|
|
|
const update = await provider.getUpdate(true)
|
|
|
|
|
|
|
|
expect(update.version).toBe("unknown")
|
|
|
|
expect(logger.error).toHaveBeenCalled()
|
|
|
|
expect(logger.error).toHaveBeenCalledWith("Failed to get latest version", {
|
|
|
|
identifier: "error",
|
|
|
|
value: `reached max redirects`,
|
|
|
|
})
|
2022-02-15 04:53:28 +08:00
|
|
|
})
|
2020-02-21 05:50:01 +08:00
|
|
|
})
|