-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathrelayfile-binary.test.ts
More file actions
266 lines (231 loc) · 8.82 KB
/
relayfile-binary.test.ts
File metadata and controls
266 lines (231 loc) · 8.82 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
import { createHash } from 'node:crypto';
import { EventEmitter } from 'node:events';
import type { ClientRequest } from 'node:http';
import path from 'node:path';
import { Readable, Writable } from 'node:stream';
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
const TEST_HOME = vi.hoisted(() => '/tmp/agent-relay-relayfile-binary-test-home');
const ORIGINAL_RELAYFILE_ROOT = process.env.RELAYFILE_ROOT;
const platformMock = vi.hoisted(() => vi.fn(() => 'linux'));
const archMock = vi.hoisted(() => vi.fn(() => 'x64'));
const homedirMock = vi.hoisted(() => vi.fn(() => '/tmp/agent-relay-relayfile-binary-test-home'));
const httpsGetMock = vi.hoisted(() => vi.fn());
const fsMocks = vi.hoisted(() => ({
accessSync: vi.fn(),
chmodSync: vi.fn(),
createWriteStream: vi.fn(),
existsSync: vi.fn(),
mkdirSync: vi.fn(),
readFileSync: vi.fn(),
renameSync: vi.fn(),
rmSync: vi.fn(),
writeFileSync: vi.fn(),
}));
vi.mock('node:os', async () => {
const actual = await vi.importActual<typeof import('node:os')>('node:os');
return {
...actual,
arch: archMock,
homedir: homedirMock,
platform: platformMock,
default: {
...actual,
arch: archMock,
homedir: homedirMock,
platform: platformMock,
},
};
});
vi.mock('node:https', async () => {
const actual = await vi.importActual<typeof import('node:https')>('node:https');
return {
...actual,
get: httpsGetMock,
default: {
...actual,
get: httpsGetMock,
},
};
});
vi.mock('node:fs', async () => {
const actual = await vi.importActual<typeof import('node:fs')>('node:fs');
return {
...actual,
accessSync: fsMocks.accessSync,
chmodSync: fsMocks.chmodSync,
createWriteStream: fsMocks.createWriteStream,
existsSync: fsMocks.existsSync,
mkdirSync: fsMocks.mkdirSync,
readFileSync: fsMocks.readFileSync,
renameSync: fsMocks.renameSync,
rmSync: fsMocks.rmSync,
writeFileSync: fsMocks.writeFileSync,
};
});
import { ensureRelayfileMountBinary } from './relayfile-binary.js';
type QueuedResponse = {
body?: Buffer | string;
headers?: Record<string, string>;
statusCode?: number;
url?: RegExp | string;
};
let realFs: typeof import('node:fs');
let requestedUrls: string[] = [];
let queuedResponses: QueuedResponse[] = [];
function getCachePaths(relayfileRoot?: string) {
const cacheDir = relayfileRoot ? path.join(relayfileRoot, 'bin') : path.join(TEST_HOME, '.agent-relay', 'bin');
return {
cacheDir,
cachePath: path.join(cacheDir, 'relayfile-mount'),
versionPath: path.join(cacheDir, 'relayfile-mount.version'),
};
}
function queueResponse(response: QueuedResponse): void {
queuedResponses.push(response);
}
function sha256(value: Buffer | string): string {
return createHash('sha256').update(value).digest('hex');
}
beforeAll(async () => {
realFs = await vi.importActual<typeof import('node:fs')>('node:fs');
});
beforeEach(() => {
requestedUrls = [];
queuedResponses = [];
realFs.rmSync(TEST_HOME, { recursive: true, force: true });
realFs.mkdirSync(TEST_HOME, { recursive: true });
delete process.env.RELAYFILE_ROOT;
platformMock.mockReset();
archMock.mockReset();
homedirMock.mockReset();
httpsGetMock.mockReset();
Object.values(fsMocks).forEach((mock) => mock.mockReset());
platformMock.mockReturnValue('linux');
archMock.mockReturnValue('x64');
homedirMock.mockReturnValue(TEST_HOME);
fsMocks.accessSync.mockImplementation(realFs.accessSync as any);
fsMocks.chmodSync.mockImplementation(realFs.chmodSync as any);
fsMocks.createWriteStream.mockImplementation((filePath: string, options?: { mode?: number }) => {
const chunks: Buffer[] = [];
const stream = new Writable({
final(callback) {
realFs.writeFileSync(filePath, Buffer.concat(chunks), { mode: options?.mode });
callback();
},
write(chunk, _encoding, callback) {
chunks.push(Buffer.isBuffer(chunk) ? Buffer.from(chunk) : Buffer.from(chunk));
callback();
},
}) as Writable & { close: (callback: () => void) => void };
stream.close = (callback: () => void) => {
callback();
};
return stream as any;
});
fsMocks.existsSync.mockImplementation(realFs.existsSync as any);
fsMocks.mkdirSync.mockImplementation(realFs.mkdirSync as any);
fsMocks.readFileSync.mockImplementation(realFs.readFileSync as any);
fsMocks.renameSync.mockImplementation(realFs.renameSync as any);
fsMocks.rmSync.mockImplementation(realFs.rmSync as any);
fsMocks.writeFileSync.mockImplementation(realFs.writeFileSync as any);
httpsGetMock.mockImplementation((url: string | URL, callback: (res: Readable) => void) => {
const currentUrl = String(url);
requestedUrls.push(currentUrl);
const nextResponse = queuedResponses.shift();
if (!nextResponse) {
throw new Error(`Unexpected https.get call for ${currentUrl}`);
}
if (typeof nextResponse.url === 'string') {
expect(currentUrl).toBe(nextResponse.url);
} else if (nextResponse.url) {
expect(currentUrl).toMatch(nextResponse.url);
}
const response = Readable.from(nextResponse.body === undefined ? [] : [nextResponse.body]) as Readable & {
headers: Record<string, string>;
statusCode?: number;
};
response.statusCode = nextResponse.statusCode ?? 200;
response.headers = nextResponse.headers ?? {};
const request = new EventEmitter() as ClientRequest;
queueMicrotask(() => {
callback(response);
});
return request;
});
});
afterEach(() => {
realFs.rmSync(TEST_HOME, { recursive: true, force: true });
if (ORIGINAL_RELAYFILE_ROOT === undefined) {
delete process.env.RELAYFILE_ROOT;
} else {
process.env.RELAYFILE_ROOT = ORIGINAL_RELAYFILE_ROOT;
}
});
describe('ensureRelayfileMountBinary', () => {
it('downloads the platform-specific binary and writes it to the cache', async () => {
const binaryName = 'relayfile-mount-linux-amd64';
queueResponse({
body: 'relayfile-binary',
url: /\/relayfile-mount-linux-amd64$/,
});
queueResponse({
body: `${sha256('relayfile-binary')} ${binaryName}\n`,
url: /\/checksums\.txt$/,
});
const installedPath = await ensureRelayfileMountBinary();
const { cachePath, versionPath } = getCachePaths();
expect(installedPath).toBe(cachePath);
expect(requestedUrls).toHaveLength(2);
expect(requestedUrls[0]).toMatch(/\/relayfile-mount-linux-amd64$/);
expect(requestedUrls[1]).toMatch(/\/checksums\.txt$/);
expect(realFs.readFileSync(cachePath, 'utf8')).toBe('relayfile-binary');
expect(realFs.readFileSync(versionPath, 'utf8')).toBe('0.1.6\n');
});
it('reuses the cached binary when the version matches', async () => {
const { cacheDir, cachePath, versionPath } = getCachePaths();
realFs.mkdirSync(cacheDir, { recursive: true });
realFs.writeFileSync(cachePath, 'cached-binary', 'utf8');
realFs.chmodSync(cachePath, 0o755);
realFs.writeFileSync(versionPath, '0.1.6\n', 'utf8');
await expect(ensureRelayfileMountBinary()).resolves.toBe(cachePath);
expect(httpsGetMock).not.toHaveBeenCalled();
expect(realFs.readFileSync(cachePath, 'utf8')).toBe('cached-binary');
});
it('installs the binary under RELAYFILE_ROOT/bin when overridden', async () => {
const relayfileRoot = path.join(TEST_HOME, 'custom-relayfile');
const binaryName = 'relayfile-mount-linux-amd64';
process.env.RELAYFILE_ROOT = relayfileRoot;
queueResponse({
body: 'relayfile-binary',
url: /\/relayfile-mount-linux-amd64$/,
});
queueResponse({
body: `${sha256('relayfile-binary')} ${binaryName}\n`,
url: /\/checksums\.txt$/,
});
const installedPath = await ensureRelayfileMountBinary();
const { cachePath, versionPath } = getCachePaths(relayfileRoot);
expect(installedPath).toBe(cachePath);
expect(realFs.readFileSync(cachePath, 'utf8')).toBe('relayfile-binary');
expect(realFs.readFileSync(versionPath, 'utf8')).toBe('0.1.6\n');
expect(realFs.existsSync(getCachePaths().cachePath)).toBe(false);
});
it('throws when the downloaded binary checksum does not match', async () => {
const binaryName = 'relayfile-mount-linux-amd64';
const { cacheDir, cachePath, versionPath } = getCachePaths();
queueResponse({
body: 'corrupt-binary',
url: /\/relayfile-mount-linux-amd64$/,
});
queueResponse({
body: `${'0'.repeat(64)} ${binaryName}\n`,
url: /\/checksums\.txt$/,
});
await expect(ensureRelayfileMountBinary()).rejects.toThrow(
`Checksum mismatch for ${binaryName}: expected ${'0'.repeat(64)}, got ${sha256('corrupt-binary')}`
);
expect(realFs.existsSync(cachePath)).toBe(false);
expect(realFs.existsSync(versionPath)).toBe(false);
expect(realFs.existsSync(cacheDir) ? realFs.readdirSync(cacheDir).filter((entry) => entry.includes('.download')) : []).toEqual([]);
});
});