computerAuth.live.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // @vitest-environment node
  2. import {expect,it} from 'vitest'
  3. import {mkdtemp,readdir,readFile,rm} from 'node:fs/promises'
  4. import {tmpdir} from 'node:os'
  5. import {join} from 'node:path'
  6. import {randomUUID} from 'node:crypto'
  7. import {ContainerComputerProvider,type ComputerSpecification} from '../../src/runner/computers/container'
  8. import {ComputerMaintenance} from '../../src/runner/computers/maintenance'
  9. it.skipIf(!process.env.YAOYAO_COMPUTER_IMAGE)('migrates a legacy home and keeps real browser and encrypted application authorization through rebuild and restore',async()=>{
  10. const home=await mkdtemp(join(tmpdir(),'yaoyao-auth-live-'))
  11. const provider=new ContainerComputerProvider('docker',randomUUID(),home)
  12. const spec:ComputerSpecification={id:randomUUID(),ownerKey:randomUUID(),imageId:process.env.YAOYAO_COMPUTER_IMAGE!}
  13. const script=await readFile(new URL('../fixtures/computer_auth.mjs',import.meta.url),'utf8')
  14. const execute=(argv:string[],user:'cua'|'root'='cua')=>provider.execute(spec,argv,{authorize:()=>{},user,timeout:60000}).catch(error=>{throw new Error(`${argv[0]} ${argv.at(-1)}: ${error.stderr??error.message}`)})
  15. const maintenance=new ComputerMaintenance({status:()=>[{environmentId:spec.id,status:'free'}]} as any,provider,home)
  16. let derivedImage:string|undefined
  17. try{
  18. // Exercise the real upgrade path from the old workspace-only container.
  19. const workspace=await (provider as any).workspace(provider.validateSpecification(spec))
  20. const created=await provider.run('docker',(provider as any).args(provider.validateSpecification(spec),workspace))
  21. const legacyId=created.stdout.trim();await provider.run('docker',['start',legacyId])
  22. expect(JSON.parse((await execute(['node','--input-type=module','-e',script,'login'])).stdout)).toEqual({browser:true,encryptedApplicationToken:true})
  23. await execute(['sh','-c','mkdir -p /root/.config; printf root-fixture > /root/.config/authorization'],'root')
  24. const identity=(await execute(['cat','/var/lib/dbus/machine-id'])).stdout
  25. await provider.remove(spec)
  26. const migrated=await provider.ensure(spec,()=>{})
  27. expect(migrated.containerId).not.toBe(legacyId)
  28. expect((await execute(['cat','/etc/machine-id'])).stdout).toBe(identity)
  29. expect((await execute(['cat','/root/.config/authorization'],'root')).stdout).toBe('root-fixture')
  30. expect(JSON.parse((await execute(['node','--input-type=module','-e',script,'check'])).stdout).browser).toBe(true)
  31. await maintenance.rebuild(spec);await provider.ensure(spec,()=>{})
  32. expect(JSON.parse((await execute(['node','--input-type=module','-e',script,'check'])).stdout).encryptedApplicationToken).toBe(true)
  33. await provider.stop(spec)
  34. derivedImage=(await provider.run('docker',['commit',(await provider.inspect(spec))!.containerId],{timeout:60000})).stdout.trim()
  35. await provider.remove(spec);spec.imageId=derivedImage
  36. await provider.ensure(spec,()=>{})
  37. expect(JSON.parse((await execute(['node','--input-type=module','-e',script,'check'])).stdout).browser).toBe(true)
  38. expect(await readdir(join(home,'computer-backups',spec.id))).toHaveLength(1)
  39. await maintenance.backup(spec,join(home,'snapshot'))
  40. await provider.ensure(spec,()=>{})
  41. await execute(['sh','-c','rm -rf ~/.config/yaoyao-auth-fixture ~/.local/share/keyrings/yaoyao-auth-fixture ~/.config/yaoyao-auth-browser'])
  42. await maintenance.restore(spec,join(home,'snapshot'))
  43. await provider.ensure(spec,()=>{})
  44. expect(JSON.parse((await execute(['node','--input-type=module','-e',script,'check'])).stdout).browser).toBe(true)
  45. expect((await execute(['cat','/etc/machine-id'])).stdout).toBe(identity)
  46. }finally{await provider.remove(spec);if(derivedImage)await provider.run('docker',['image','rm',derivedImage]);await rm(home,{recursive:true,force:true})}
  47. },180000)