| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, readFileSync |
| 3 | import { createServer } |
| 4 | import { tmpdir } |
| 5 | import path from 'node:path |
| 6 | import { promisify } |
| 7 | import type { INestApplication |
| 8 | import request from 'supertest |
| 9 | import { afterAll, beforeAll |
| 10 | |
| 11 | import { type Database, |
| 12 | import { eq } |
| 13 | |
| 14 | import { DATABASE } |
| 15 | import { PullRefsModule } |
| 16 | import { RepositoryMaterializerService } |
| 17 | import { PushTransactionService } |
| 18 | import { WalStoreService } |
| 19 | import { StorageQuotaService } |
| 20 | import { PullRefsService } |
| 21 | import { createUlid } |
| 22 | import { hasBackends, signUp |
| 23 | |
| 24 | describe.skipIf(!hasBackends |
| 25 | let app: |
| 26 | let origin: |
| 27 | let upstream: |
| 28 | let forker: |
| 29 | let work: |
| 30 | const owner = |
| 31 | const contributor = |
| 32 | let repo: |
| 33 | let fork: |
| 34 | let sshPort: |
| 35 | let sshKey: |
| 36 | let keys: |
| 37 | |
| 38 | const api = |
| 39 | const pulls = |
| 40 | `/api/repositories/${owner}/${ |
| 41 | const url = |
| 42 | `${origin.replace( |
| 43 | const upstreamUrl = |
| 44 | const git = |
| 45 | execFileSync( |
| 46 | 'git', |
| 47 | ['-c', 'user.name=E2E' |
| 48 | { cwd: work, |
| 49 | ).trim(); |
| 50 | // The server runs in this process, so anything that talks to it must not block the event loop. |
| 51 | const run = |
| 52 | promisify(execFile)('git |
| 53 | cwd: work, |
| 54 | env: { ...process |
| 55 | }); |
| 56 | const commit = |
| 57 | git('checkout', |
| 58 | writeFileSync(path.join |
| 59 | git('add', |
| 60 | git('commit', |
| 61 | return git('rev-parse |
| 62 | }; |
| 63 | const pullRefs = |
| 64 | const { stdout |
| 65 | return Object.fromEntries |
| 66 | stdout |
| 67 | .split('\n') |
| 68 | .filter(Boolean) |
| 69 | .map((line) |
| 70 | ) as Record< |
| 71 | }; |
| 72 | const fetchRef = |
| 73 | await run(['fetch |
| 74 | return `refs/fetched/${ref |
| 75 | }; |
| 76 | const parentsOf = |
| 77 | git('log', |
| 78 | |
| 79 | /** Fetches over SSH as the upstream owner, tracing the packets so a test can read which protocol was spoken. */ |
| 80 | const sshFetch = |
| 81 | run( |
| 82 | [ |
| 83 | '-c', |
| 84 | `protocol.version=${version}`, |
| 85 | 'fetch', |
| 86 | `ssh://git@127.0.0.1:${sshPort}/${ |
| 87 | refspec, |
| 88 | ], |
| 89 | { |
| 90 | GIT_TRACE_PACKET: '1', |
| 91 | GIT_SSH_COMMAND: `ssh -i ${sshKey |
| 92 | }, |
| 93 | ); |
| 94 | |
| 95 | beforeAll(async () |
| 96 | work = mkdtempSync( |
| 97 | keys = mkdtempSync( |
| 98 | const hostKey = |
| 99 | sshKey = path. |
| 100 | for (const file |
| 101 | execFileSync('ssh-keygen', |
| 102 | } |
| 103 | sshPort = await |
| 104 | const probe = |
| 105 | const { port |
| 106 | probe.close(() |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | ({ app, origin |
| 111 | GIT_SSH_HOST_KEY: readFileSync(hostKey |
| 112 | GIT_SSH_PORT: String(sshPort |
| 113 | })); |
| 114 | upstream = await |
| 115 | forker = await |
| 116 | |
| 117 | repo = ( |
| 118 | await api() |
| 119 | .post('/api/repositories') |
| 120 | .set('cookie', |
| 121 | .send({ name: |
| 122 | .expect(201) |
| 123 | ).body.slug; |
| 124 | await api() |
| 125 | .post('/api/ssh-keys') |
| 126 | .set('cookie', |
| 127 | .send({ title: |
| 128 | .expect(201); |
| 129 | |
| 130 | git('init', |
| 131 | writeFileSync(path.join |
| 132 | git('add', |
| 133 | git('commit', |
| 134 | await run(['push |
| 135 | |
| 136 | fork = ( |
| 137 | await api() |
| 138 | .post(`/api/repositories/${owner |
| 139 | .set('cookie', |
| 140 | .send({ name: |
| 141 | .expect(201) |
| 142 | ).body.slug; |
| 143 | git('checkout', |
| 144 | git('checkout', |
| 145 | commit('feature', |
| 146 | commit('other', |
| 147 | await run([ |
| 148 | 'push', |
| 149 | '-q', |
| 150 | url(contributor, forker |
| 151 | 'feature', |
| 152 | 'other', |
| 153 | ]); |
| 154 | |
| 155 | await api() |
| 156 | .post(pulls()) |
| 157 | .set('cookie', |
| 158 | .send({ title: |
| 159 | .expect(201); |
| 160 | }, 120_000); |
| 161 | |
| 162 | afterAll(async () |
| 163 | await app?.close |
| 164 | for (const directory |
| 165 | if (directory) rmSync |
| 166 | } |
| 167 | }); |
| 168 | |
| 169 | it('publishes a fork request as its head and a test merge into the base', |
| 170 | const feature = |
| 171 | const main = |
| 172 | |
| 173 | await vi.waitFor |
| 174 | async () => |
| 175 | const refs = |
| 176 | expect(refs['refs/pull/1/head |
| 177 | expect(refs['refs/pull/1/merge |
| 178 | }, |
| 179 | { timeout: 15_000, interval |
| 180 | ); |
| 181 | |
| 182 | expect(await parentsOf |
| 183 | expect( |
| 184 | git('ls-tree', |
| 185 | '\n', |
| 186 | ), |
| 187 | ).toEqual(['feature.txt' |
| 188 | }); |
| 189 | |
| 190 | it('fetches over protocol v2, asking only for the prefix it wants', |
| 191 | const { stderr |
| 192 | [ |
| 193 | '-c', |
| 194 | 'protocol.version=2', |
| 195 | 'fetch', |
| 196 | upstreamUrl(), |
| 197 | '+refs/pull/1/head:refs/fetched/v2', |
| 198 | ], |
| 199 | { GIT_TRACE_PACKET: '1' |
| 200 | ); |
| 201 | |
| 202 | expect(stderr).toContain |
| 203 | expect(stderr).toContain |
| 204 | expect(stderr).not |
| 205 | expect(git('rev-parse |
| 206 | git('rev-parse', |
| 207 | ); |
| 208 | }); |
| 209 | |
| 210 | it('fetches over SSH with protocol v2 when the client asks for it', |
| 211 | const { stderr |
| 212 | () => sshFetch( |
| 213 | { timeout: 10_000, interval |
| 214 | ); |
| 215 | |
| 216 | expect(stderr).toContain |
| 217 | expect(stderr).toContain |
| 218 | expect(git('rev-parse |
| 219 | git('rev-parse', |
| 220 | ); |
| 221 | }); |
| 222 | |
| 223 | it('stays on v0 over SSH for a client that does not ask', |
| 224 | const { stderr |
| 225 | () => sshFetch( |
| 226 | { timeout: 10_000, interval |
| 227 | ); |
| 228 | |
| 229 | expect(stderr).not |
| 230 | expect(git('rev-parse |
| 231 | git('rev-parse', |
| 232 | ); |
| 233 | }); |
| 234 | |
| 235 | it('follows the head when the fork pushes again', |
| 236 | const feature = |
| 237 | await run(['push |
| 238 | |
| 239 | await vi.waitFor |
| 240 | async () => |
| 241 | { timeout: 15_000, interval |
| 242 | ); |
| 243 | await vi.waitFor |
| 244 | async () => |
| 245 | expect(await parentsOf |
| 246 | git('rev-parse', |
| 247 | feature, |
| 248 | ]), |
| 249 | { timeout: 15_000, interval |
| 250 | ); |
| 251 | }); |
| 252 | |
| 253 | it('rebuilds the test merge when the base moves', |
| 254 | const main = |
| 255 | await run(['push |
| 256 | |
| 257 | await vi.waitFor |
| 258 | async () => |
| 259 | expect(await parentsOf |
| 260 | main, |
| 261 | git('rev-parse', |
| 262 | ]), |
| 263 | { timeout: 15_000, interval |
| 264 | ); |
| 265 | }); |
| 266 | |
| 267 | it('drops the test merge while the request conflicts, and keeps the head', |
| 268 | commit('main', |
| 269 | await run(['push |
| 270 | |
| 271 | await vi.waitFor |
| 272 | async () => |
| 273 | const refs = |
| 274 | expect(refs['refs/pull/1/merge |
| 275 | expect(refs['refs/pull/1/head |
| 276 | }, |
| 277 | { timeout: 15_000, interval |
| 278 | ); |
| 279 | }); |
| 280 | |
| 281 | it('refuses a client push to a pull request ref', |
| 282 | await expect( |
| 283 | run(['push', |
| 284 | ).rejects.toThrow() |
| 285 | expect((await pullRefs |
| 286 | git('rev-parse', |
| 287 | ); |
| 288 | }); |
| 289 | |
| 290 | it('pins a merged request to the head that merged', |
| 291 | const other = |
| 292 | const { body |
| 293 | .post(pulls()) |
| 294 | .set('cookie', |
| 295 | .send({ title: |
| 296 | .expect(201); |
| 297 | await api() |
| 298 | .post(pulls(` |
| 299 | .set('cookie', |
| 300 | .send({ method: |
| 301 | .expect(201); |
| 302 | |
| 303 | await vi.waitFor |
| 304 | async () => |
| 305 | expect((await pullRefs |
| 306 | { timeout: 15_000, interval |
| 307 | ); |
| 308 | }); |
| 309 | |
| 310 | it('clears a refusal once the refs are current again, whether or not that took a write', |
| 311 | const { body |
| 312 | const refuse = |
| 313 | app |
| 314 | .get<Database |
| 315 | .update(schema.pullRequest |
| 316 | .set({ pullRefsBlocked: |
| 317 | .where(eq(schema |
| 318 | const cleared = |
| 319 | vi.waitFor( |
| 320 | async () => |
| 321 | expect( |
| 322 | (await api(). |
| 323 | ).toBeNull(), |
| 324 | { timeout: 15_000, interval |
| 325 | ); |
| 326 | |
| 327 | await refuse(); |
| 328 | app.get(PullRefsService |
| 329 | await cleared(); |
| 330 | |
| 331 | await refuse(); |
| 332 | const feature = |
| 333 | await run(['push |
| 334 | await cleared(); |
| 335 | expect((await pullRefs |
| 336 | }); |
| 337 | |
| 338 | it.each([ |
| 339 | 'accounting rollback', |
| 340 | 'lost WAL response', |
| 341 | 'lost database response', |
| 342 | ])( |
| 343 | 'recovers a committed pack after %s without rewriting refs or double counting', |
| 344 | async (failure) |
| 345 | const { body: |
| 346 | const db = |
| 347 | const service = |
| 348 | const quota = |
| 349 | const push = |
| 350 | .select(PullRefsModule) |
| 351 | .get(PushTransactionService, |
| 352 | const store = |
| 353 | const writes = |
| 354 | db |
| 355 | .select() |
| 356 | .from(schema.pullRequestRefWrite |
| 357 | .where(eq(schema |
| 358 | const pending = |
| 359 | db |
| 360 | .select() |
| 361 | .from(schema.pullRequestRefWritePending |
| 362 | .where(eq(schema |
| 363 | // Settle earlier background work before injecting one failure. |
| 364 | await service.sync |
| 365 | const before = |
| 366 | const background = |
| 367 | .spyOn(service, |
| 368 | .mockImplementation(() => |
| 369 | const reserve = |
| 370 | const commitPush = |
| 371 | const pushSpy = |
| 372 | const reservation = |
| 373 | try { |
| 374 | commit('feature', |
| 375 | await run([ |
| 376 | 'push', |
| 377 | '-q', |
| 378 | url(contributor, forker |
| 379 | 'feature', |
| 380 | ]); |
| 381 | pushSpy.mockClear(); |
| 382 | reservation.mockClear(); |
| 383 | const error = |
| 384 | if (failure === |
| 385 | pushSpy.mockImplementationOnce(async |
| 386 | await commitPush(options |
| 387 | throw error; |
| 388 | }); |
| 389 | } else { |
| 390 | reservation.mockImplementationOnce(async |
| 391 | if (failure === |
| 392 | return reserve(request |
| 393 | await write(tx |
| 394 | throw error; |
| 395 | }); |
| 396 | } |
| 397 | await reserve(request |
| 398 | throw error; |
| 399 | }); |
| 400 | } |
| 401 | await expect(service |
| 402 | expect((await pullRefs |
| 403 | git('rev-parse', |
| 404 | ); |
| 405 | const intents = |
| 406 | expect(intents).toHaveLength |
| 407 | failure === 'lost database response' |
| 408 | ); |
| 409 | expect(await writes |
| 410 | before.length + ( |
| 411 | ); |
| 412 | |
| 413 | // A new instance has no in-memory knowledge of the failed sync. |
| 414 | const restarted = |
| 415 | db, |
| 416 | app.get(RepositoryMaterializerService |
| 417 | push, |
| 418 | quota, |
| 419 | store, |
| 420 | ); |
| 421 | await restarted.sync |
| 422 | const after = |
| 423 | expect(after).toHaveLength |
| 424 | const added = |
| 425 | (row) => |
| 426 | )!; |
| 427 | const [storedPull] |
| 428 | .select() |
| 429 | .from(schema.pullRequest |
| 430 | .where(eq(schema |
| 431 | const index = |
| 432 | expect(added.size |
| 433 | index!.index.layers |
| 434 | .size, |
| 435 | ); |
| 436 | expect(added.size |
| 437 | expect(await pending |
| 438 | |
| 439 | // A stale recovery attempt may still see an intent already accounted for. |
| 440 | await db |
| 441 | .insert(schema.pullRequestRefWritePending |
| 442 | .values({ id: |
| 443 | await restarted.sync |
| 444 | expect(await writes |
| 445 | expect(await pending |
| 446 | expect(pushSpy).toHaveBeenCalledTimes |
| 447 | expect(reservation).toHaveBeenCalledTimes |
| 448 | } finally { |
| 449 | background.mockRestore(); |
| 450 | pushSpy.mockRestore(); |
| 451 | reservation.mockRestore(); |
| 452 | } |
| 453 | }, |
| 454 | 30_000, |
| 455 | ); |
| 456 | |
| 457 | it('records the intent before the quota transaction holds a connection', |
| 458 | const { body: |
| 459 | const db = |
| 460 | const quota = |
| 461 | const reserve = |
| 462 | let intents = |
| 463 | const reservation = |
| 464 | .spyOn(quota, |
| 465 | .mockImplementationOnce(async ( |
| 466 | intents = ( |
| 467 | await db |
| 468 | .select() |
| 469 | .from(schema.pullRequestRefWritePending |
| 470 | .where(eq(schema |
| 471 | ).length; |
| 472 | return reserve(request |
| 473 | }); |
| 474 | try { |
| 475 | commit('feature', |
| 476 | await run(['push |
| 477 | await vi.waitFor |
| 478 | timeout: 15_000, |
| 479 | interval: 200, |
| 480 | }); |
| 481 | } finally { |
| 482 | reservation.mockRestore(); |
| 483 | } |
| 484 | }); |
| 485 | |
| 486 | it('forgets an intent that never reached the log once it is past its grace, and keeps a young one', |
| 487 | const { body: |
| 488 | const db = |
| 489 | const abandoned = |
| 490 | const young = |
| 491 | await db.insert |
| 492 | { id: abandoned, |
| 493 | { id: young, |
| 494 | ]); |
| 495 | |
| 496 | await app.get |
| 497 | |
| 498 | const left = |
| 499 | .select({ id: |
| 500 | .from(schema.pullRequestRefWritePending |
| 501 | .where(eq(schema |
| 502 | expect(left).toEqual |
| 503 | await db |
| 504 | .delete(schema.pullRequestRefWritePending |
| 505 | .where(eq(schema |
| 506 | }); |
| 507 | |
| 508 | it('leaves pull request refs behind when the repository is forked', |
| 509 | const username = |
| 510 | const later = |
| 511 | const { body |
| 512 | .post(`/api/repositories/${owner |
| 513 | .set('cookie', |
| 514 | .send({ name: |
| 515 | .expect(201); |
| 516 | |
| 517 | expect(await pullRefs |
| 518 | }); |
| 519 | }); |