ReviewOS

also looking at this

stacks/drone

feat: own runtime firmware candidates

#49
Merged chris wants to merge codex/runtime-candidate-ownership into main
24 files +1245 -92
tests/unit/updates-preparation-cli.test.tsadded+45-0
Changes to tests/unit/updates-preparation-cli.test.ts
@@ -0,0 +1,45 @@
1import { afterEach, expect, test } from 'bun:test'
2import { command } from '@stacksjs/testing'
3import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4import { tmpdir } from 'node:os'
5import { join } from 'node:path'
6import { enrollRuntimeTrust } from '../../resources/functions/drone/updates/enrollment'
7import { createRuntimeTrustStore } from '../../resources/functions/drone/updates/store'
8import { repository, platform, target } from '../fixtures/updates/repository'
9
10const roots: string[] = []
11afterEach(async () => { for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true }) })
12
13test('native preparation requires protected trust and an explicit clock, authenticates the image and preserves failure status', async () => {
14 const root = await mkdtemp(join(tmpdir(), 'drone-prepare-cli-')); roots.push(root)
15 const source = join(root, 'repo'), fixture = await repository(source, { custom: { releaseSequence: 9 } })
16 const enrollmentDirectory = join(root, 'enrollment')
17 const enrolled = enrollRuntimeTrust({ target, platform, trustedRootSha256: fixture.rootSha256, minimumReleaseSequence: 7, trustedRoot: fixture.rootPath, output: enrollmentDirectory })
18 const created = await createRuntimeTrustStore({ enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, output: join(root, 'store') })
19 const flags = ['--enrollment', enrollmentDirectory, '--enrollment-sha256', enrolled.enrollmentSha256, '--store-sha256', created.storeSha256]
20 const sourceFlags = ['--repository', source, '--trusted-system-clock-source', 'operator-managed-test-clock']
21 const prepared = await command(['updates:prepare', join(root, 'store'), ...flags, ...sourceFlags, '--output', join(root, 'candidate')]).withTimeout(20000)
22 expect(prepared.exitCode).toBe(0)
23 const value = JSON.parse(prepared.stdout)
24 expect(value.store.releaseFloor).toBe(9)
25 expect(value.candidate.sha256).toBe(fixture.imageHash)
26 expect(value.trustStatePersisted).toBe(true)
27 expect(value.physicalDeploymentAllowed).toBe(false)
28 const inspected = await command(['updates:inspect', join(root, 'candidate'), '--target', target, '--platform', platform, '--root-sha256', fixture.rootSha256, '--minimum-release-sequence', '9']).withTimeout(20000)
29 expect(inspected.exitCode).toBe(0)
30 expect(JSON.parse(inspected.stdout)).toEqual(value.candidate)
31 const missingClock = await command(['updates:prepare', join(root, 'store'), ...flags, '--repository', source, '--output', join(root, 'missing-clock')]).withTimeout(20000)
32 expect(missingClock.exitCode).toBe(1)
33 expect(missingClock.stdout).toBe('')
34 await writeFile(fixture.imagePath, Buffer.alloc(fixture.image.length, 0xff))
35 const rejected = await command(['updates:prepare', join(root, 'store'), ...flags, ...sourceFlags, '--output', join(root, 'bad-image')]).withTimeout(20000)
36 expect(rejected.exitCode).toBe(1)
37 expect(rejected.stdout).toBe('')
38 expect(rejected.stderr).toBe('Runtime image preparation unavailable: invalid trust, clock, rejected image or output failure\n')
39 const status = await command(['updates:store', join(root, 'store'), ...flags]).withTimeout(20000)
40 expect(status.exitCode).toBe(0)
41 const stopped = JSON.parse(status.stdout)
42 expect(stopped.releaseFloor).toBe(9)
43 expect(stopped.attempt.phase).toBe('maintenance')
44 expect(stopped.physicalDeploymentAllowed).toBe(false)
45}, 120000)