python-runtime-selfhosted.spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { readFileSync } from 'node:fs'
  2. import { resolve } from 'node:path'
  3. import { runInNewContext } from 'node:vm'
  4. import * as yaml from 'js-yaml'
  5. import { describe, expect, it } from 'vitest'
  6. const root = resolve(import.meta.dirname, '..')
  7. const workflow = yaml.load(readFileSync(resolve(root, '.github/workflows/build-exe-for-python-sdk.yml'), 'utf8')) as {
  8. jobs: Record<string, { 'runs-on': string; steps: Array<{ name?: string; id?: string; uses?: string; if?: string; shell?: string; run?: string; with?: Record<string, unknown> }> }>
  9. }
  10. const build = workflow.jobs.build!
  11. const selector = build['runs-on'].slice(3, -2).trim()
  12. const windows = ['self-hosted', 'dsh-win-ci', 'windows', 'x64']
  13. function context() {
  14. return {
  15. inputs: { ci: true, release: false },
  16. github: {
  17. repository: 'deepseek-harness/deepseek-harness',
  18. event_name: 'pull_request',
  19. ref: 'refs/pull/42/merge',
  20. event: { pull_request: {
  21. head: { repo: { full_name: 'deepseek-harness/deepseek-harness', fork: false } },
  22. user: { login: 'contributor' },
  23. } },
  24. },
  25. matrix: { target: 'node24-win-x64', runner: 'windows-2025' },
  26. vars: { DSH_CI_FAILOVER_WINDOWS: 'selfhosted' },
  27. fromJSON: JSON.parse,
  28. }
  29. }
  30. function route(value: ReturnType<typeof context>, expression = selector): unknown {
  31. // The workflow uses only comparisons, booleans and fromJSON; execute that exact expression.
  32. return runInNewContext(expression, value, { timeout: 1000 })
  33. }
  34. describe('Python runtime self-hosted routing', () => {
  35. it('routes same-repository member PRs and master CI to native x64 Windows', () => {
  36. expect(route(context())).toEqual(windows)
  37. const master = context()
  38. master.github.event_name = 'push'
  39. master.github.ref = 'refs/heads/master'
  40. expect(route(master)).toEqual(windows)
  41. })
  42. it.each([
  43. ['release caller', (value: ReturnType<typeof context>) => { value.inputs.release = true }],
  44. ['non-CI caller', (value: ReturnType<typeof context>) => { value.inputs.ci = false }],
  45. ['manual dispatch', (value: ReturnType<typeof context>) => { value.github.event_name = 'workflow_dispatch' }],
  46. ['pull_request_target', (value: ReturnType<typeof context>) => { value.github.event_name = 'pull_request_target' }],
  47. ['unknown event', (value: ReturnType<typeof context>) => { value.github.event_name = '' }],
  48. ['fork', (value: ReturnType<typeof context>) => { value.github.event.pull_request.head.repo.fork = true }],
  49. ['different repository head', (value: ReturnType<typeof context>) => { value.github.event.pull_request.head.repo.full_name = 'someone/fork' }],
  50. ['different caller repository', (value: ReturnType<typeof context>) => { value.github.repository = 'someone/fork' }],
  51. ['Dependabot author', (value: ReturnType<typeof context>) => { value.github.event.pull_request.user.login = 'dependabot[bot]' }],
  52. ['disabled failover', (value: ReturnType<typeof context>) => { value.vars.DSH_CI_FAILOVER_WINDOWS = '' }],
  53. ['unknown failover value', (value: ReturnType<typeof context>) => { value.vars.DSH_CI_FAILOVER_WINDOWS = 'hosted' }],
  54. ['branch push', (value: ReturnType<typeof context>) => { value.github.event_name = 'push'; value.github.ref = 'refs/heads/topic' }],
  55. ['tag push', (value: ReturnType<typeof context>) => { value.github.event_name = 'push'; value.github.ref = 'refs/tags/python-v1' }],
  56. ] as const)('keeps %s on the hosted fallback', (_name, change) => {
  57. const value = context()
  58. change(value)
  59. expect(route(value)).toBe('windows-2025')
  60. })
  61. it.each([
  62. ['node24-linux-x64', 'ubuntu-latest'],
  63. ['node24-linux-arm64', 'ubuntu-24.04-arm'],
  64. ['node24-macos-arm64', 'macos-latest'],
  65. ['node24-macos-x64', 'macos-15-intel'],
  66. ])('keeps %s hosted even with failover enabled', (target, runner) => {
  67. const value = context()
  68. value.matrix = { target, runner }
  69. expect(route(value)).toBe(runner)
  70. })
  71. it('keeps setup helper jobs on hosted images', () => {
  72. expect(workflow.jobs.plan!['runs-on']).toBe('ubuntu-latest')
  73. expect(workflow.jobs['sdk-wheel']!['runs-on']).toBe('ubuntu-latest')
  74. })
  75. it('isolates setup before pnpm and excludes shared installers and cache archives', () => {
  76. const privateSetup = build.steps.findIndex(step => step.id === 'private-windows')
  77. expect(privateSetup).toBeGreaterThan(0)
  78. expect(privateSetup).toBeLessThan(build.steps.findIndex(step => step.uses?.startsWith('pnpm/action-setup@')))
  79. for (const step of build.steps.filter(step => step.uses?.startsWith('actions/setup-python@') || step.uses?.startsWith('actions/cache@') || step.name === 'Install Python build tooling')) {
  80. expect(step.if).toBe("runner.environment != 'self-hosted'")
  81. }
  82. expect(build.steps.find(step => step.name?.startsWith('Enable Windows'))?.if).toBe("runner.os == 'Windows' && runner.environment != 'self-hosted'")
  83. expect(build.steps.find(step => step.uses?.startsWith('actions/setup-node@'))?.with?.cache).toContain("runner.environment != 'self-hosted'")
  84. expect(build.steps.at(-1)).toMatchObject({ if: "always() && steps.private-windows.outputs.root != ''", shell: 'pwsh' })
  85. const cleanup = build.steps.at(-1)!.run!
  86. expect(cleanup).toContain('"NODE_COMPILE_CACHE=" >> $env:GITHUB_ENV')
  87. expect(cleanup).toContain('"TMP=$env:RUNNER_TEMP" >> $env:GITHUB_ENV')
  88. expect(cleanup).toContain('"TEMP=$env:RUNNER_TEMP" >> $env:GITHUB_ENV')
  89. expect(cleanup).toContain('maxRetries: 10, retryDelay: 100')
  90. })
  91. it('pins portable Python without registry or shared cache writes', () => {
  92. const setup = readFileSync(resolve(root, 'scripts/setup-python-runtime-windows.ps1'), 'utf8')
  93. expect(setup).toContain('--no-bin --no-registry 3.10')
  94. expect(setup).toContain('--managed-python --no-python-downloads --seed')
  95. expect(setup).toContain('UV_PYTHON_INSTALL_REGISTRY')
  96. expect(setup).toContain('PNPM_CONFIG_STORE_DIR')
  97. expect(setup).toContain('PKG_CACHE_PATH')
  98. expect(setup).not.toMatch(/reg add|Set-ItemProperty|InstallAllUsers/)
  99. })
  100. })