-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathsshOverrides.test.ts
More file actions
355 lines (317 loc) · 9.72 KB
/
sshOverrides.test.ts
File metadata and controls
355 lines (317 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { vol } from "memfs";
import * as fsPromises from "node:fs/promises";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
applySettingOverrides,
buildSshOverrides,
} from "@/remote/sshOverrides";
import {
MockConfigurationProvider,
createMockLogger,
} from "../../mocks/testHelpers";
import type * as fs from "node:fs";
vi.mock("node:fs/promises", async () => {
const memfs: { fs: typeof fs } = await vi.importActual("memfs");
return memfs.fs.promises;
});
/** Helper to extract a single override by key from the result. */
function findOverride(
overrides: Array<{ key: string; value: unknown }>,
key: string,
): unknown {
return overrides.find((o) => o.key === key)?.value;
}
interface TimeoutCase {
timeout: number | undefined;
label: string;
}
describe("buildSshOverrides", () => {
const buildLogger = createMockLogger();
describe("remote platform", () => {
it("adds host when missing or OS differs", () => {
const config = new MockConfigurationProvider();
// New host is added alongside existing entries.
config.set("remote.SSH.remotePlatform", { "other-host": "darwin" });
expect(
findOverride(
buildSshOverrides(
config,
"new-host",
"linux",
undefined,
buildLogger,
),
"remote.SSH.remotePlatform",
),
).toEqual({ "other-host": "darwin", "new-host": "linux" });
// Existing host with wrong OS gets corrected.
config.set("remote.SSH.remotePlatform", { "my-host": "windows" });
expect(
findOverride(
buildSshOverrides(config, "my-host", "linux", undefined, buildLogger),
"remote.SSH.remotePlatform",
),
).toEqual({ "my-host": "linux" });
});
it("skips override when host already matches", () => {
const config = new MockConfigurationProvider();
config.set("remote.SSH.remotePlatform", { "my-host": "linux" });
expect(
findOverride(
buildSshOverrides(config, "my-host", "linux", undefined, buildLogger),
"remote.SSH.remotePlatform",
),
).toBeUndefined();
});
describe("RemoteCommand compatibility", () => {
it("removes host from remotePlatform when enableRemoteCommand is true", () => {
const config = new MockConfigurationProvider();
config.set("remote.SSH.enableRemoteCommand", true);
config.set("remote.SSH.remotePlatform", {
"my-host": "linux",
"other-host": "darwin",
});
expect(
findOverride(
buildSshOverrides(
config,
"my-host",
"linux",
"exec bash -l",
buildLogger,
),
"remote.SSH.remotePlatform",
),
).toEqual({ "other-host": "darwin" });
});
it("produces no override when host has no stale remotePlatform entry", () => {
const config = new MockConfigurationProvider();
config.set("remote.SSH.enableRemoteCommand", true);
config.set("remote.SSH.remotePlatform", {});
expect(
findOverride(
buildSshOverrides(
config,
"my-host",
"linux",
"exec bash -l",
buildLogger,
),
"remote.SSH.remotePlatform",
),
).toBeUndefined();
});
it("sets platform normally when enableRemoteCommand is false", () => {
const config = new MockConfigurationProvider();
config.set("remote.SSH.enableRemoteCommand", false);
config.set("remote.SSH.remotePlatform", {});
expect(
findOverride(
buildSshOverrides(
config,
"my-host",
"linux",
"exec bash -l",
buildLogger,
),
"remote.SSH.remotePlatform",
),
).toEqual({ "my-host": "linux" });
});
it.each(["none", "None", "NONE", "", undefined])(
"sets platform normally when remoteCommand is %j",
(cmd) => {
const config = new MockConfigurationProvider();
config.set("remote.SSH.enableRemoteCommand", true);
config.set("remote.SSH.remotePlatform", {});
expect(
findOverride(
buildSshOverrides(config, "my-host", "linux", cmd, buildLogger),
"remote.SSH.remotePlatform",
),
).toEqual({ "my-host": "linux" });
},
);
});
});
describe("connect timeout", () => {
it.each<TimeoutCase>([
{ timeout: undefined, label: "unset" },
{ timeout: 0, label: "zero" },
{ timeout: 15, label: "below minimum" },
{ timeout: 1799, label: "just under minimum" },
])("enforces minimum of 1800 when $label", ({ timeout }) => {
const config = new MockConfigurationProvider();
if (timeout !== undefined) {
config.set("remote.SSH.connectTimeout", timeout);
}
expect(
findOverride(
buildSshOverrides(config, "host", "linux", undefined, buildLogger),
"remote.SSH.connectTimeout",
),
).toBe(1800);
});
it.each<TimeoutCase>([
{ timeout: 1800, label: "exactly minimum" },
{ timeout: 3600, label: "above minimum" },
])("preserves timeout when $label", ({ timeout }) => {
const config = new MockConfigurationProvider();
config.set("remote.SSH.connectTimeout", timeout);
expect(
findOverride(
buildSshOverrides(config, "host", "linux", undefined, buildLogger),
"remote.SSH.connectTimeout",
),
).toBeUndefined();
});
});
describe("reconnection grace time", () => {
it("defaults to 8 hours when not configured", () => {
expect(
findOverride(
buildSshOverrides(
new MockConfigurationProvider(),
"host",
"linux",
undefined,
buildLogger,
),
"remote.SSH.reconnectionGraceTime",
),
).toBe(28800);
});
it("preserves any user-configured value", () => {
const config = new MockConfigurationProvider();
config.set("remote.SSH.reconnectionGraceTime", 3600);
expect(
findOverride(
buildSshOverrides(config, "host", "linux", undefined, buildLogger),
"remote.SSH.reconnectionGraceTime",
),
).toBeUndefined();
});
});
it.each([
{ key: "remote.SSH.serverShutdownTimeout", expected: 28800 },
{ key: "remote.SSH.maxReconnectionAttempts", expected: null },
])("defaults $key when not configured", ({ key, expected }) => {
const overrides = buildSshOverrides(
new MockConfigurationProvider(),
"host",
"linux",
undefined,
buildLogger,
);
expect(findOverride(overrides, key)).toBe(expected);
});
it("produces no overrides when all settings are already correct", () => {
const config = new MockConfigurationProvider();
config.set("remote.SSH.remotePlatform", { "my-host": "linux" });
config.set("remote.SSH.connectTimeout", 3600);
config.set("remote.SSH.reconnectionGraceTime", 7200);
config.set("remote.SSH.serverShutdownTimeout", 600);
config.set("remote.SSH.maxReconnectionAttempts", 4);
expect(
buildSshOverrides(config, "my-host", "linux", undefined, buildLogger),
).toHaveLength(0);
});
});
describe("applySettingOverrides", () => {
const settingsPath = "/settings.json";
const logger = createMockLogger();
beforeEach(() => {
vol.reset();
});
async function readSettings(): Promise<Record<string, unknown>> {
const raw = await fsPromises.readFile(settingsPath, "utf8");
return JSON.parse(raw) as Record<string, unknown>;
}
it("returns true when overrides list is empty", async () => {
expect(await applySettingOverrides(settingsPath, [], logger)).toBe(true);
});
it("creates file and applies overrides when file does not exist", async () => {
const ok = await applySettingOverrides(
settingsPath,
[
{
key: "remote.SSH.remotePlatform",
value: { "coder-gke--main": "linux" },
},
{ key: "remote.SSH.connectTimeout", value: 1800 },
{ key: "remote.SSH.reconnectionGraceTime", value: 28800 },
],
logger,
);
expect(ok).toBe(true);
expect(await readSettings()).toMatchObject({
"remote.SSH.remotePlatform": { "coder-gke--main": "linux" },
"remote.SSH.connectTimeout": 1800,
"remote.SSH.reconnectionGraceTime": 28800,
});
});
it("preserves existing settings when applying overrides", async () => {
vol.fromJSON({
[settingsPath]: JSON.stringify({
"remote.SSH.remotePlatform": { "coder-gke--main": "linux" },
"remote.SSH.connectTimeout": 15,
}),
});
await applySettingOverrides(
settingsPath,
[{ key: "remote.SSH.connectTimeout", value: 1800 }],
logger,
);
expect(await readSettings()).toMatchObject({
"remote.SSH.remotePlatform": { "coder-gke--main": "linux" },
"remote.SSH.connectTimeout": 1800,
});
});
it("handles JSONC with comments", async () => {
vol.fromJSON({
[settingsPath]: [
"{",
" // Platform overrides for remote SSH hosts",
' "remote.SSH.remotePlatform": { "coder-gke--main": "linux" },',
' "remote.SSH.connectTimeout": 15',
"}",
].join("\n"),
});
await applySettingOverrides(
settingsPath,
[{ key: "remote.SSH.connectTimeout", value: 1800 }],
logger,
);
const raw = await fsPromises.readFile(settingsPath, "utf8");
expect(raw).toContain("// Platform overrides for remote SSH hosts");
expect(raw).toContain("1800");
expect(raw).toContain('"remote.SSH.remotePlatform"');
});
it("writes null values literally instead of deleting the key", async () => {
const ok = await applySettingOverrides(
settingsPath,
[{ key: "remote.SSH.maxReconnectionAttempts", value: null }],
logger,
);
expect(ok).toBe(true);
const raw = await fsPromises.readFile(settingsPath, "utf8");
expect(raw).toContain('"remote.SSH.maxReconnectionAttempts": null');
});
it("returns false and logs warning when write fails", async () => {
vol.fromJSON({ [settingsPath]: "{}" });
const writeSpy = vi
.spyOn(fsPromises, "writeFile")
.mockRejectedValueOnce(new Error("EACCES: permission denied"));
const ok = await applySettingOverrides(
settingsPath,
[{ key: "remote.SSH.connectTimeout", value: 1800 }],
logger,
);
expect(ok).toBe(false);
expect(logger.warn).toHaveBeenCalledWith(
"Failed to configure settings",
expect.anything(),
);
writeSpy.mockRestore();
});
});