diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index f49666d..7661c56 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -34,3 +34,29 @@ jobs: assert.ok(fs.readFileSync('src/index.js','utf8').includes(`const VERSION = "${version}"`)); JS npm pack --dry-run + image-smoke: + name: Docker image stdio smoke (no network) + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 + with: + node-version: '20' + - run: npm ci --ignore-scripts --no-audit --no-fund + - name: Check exact npm release availability + id: npm_release + run: node scripts/npm-release-availability.mjs + - name: Build this PR Dockerfile and compare against published npm + if: steps.npm_release.outputs.published == 'true' + shell: bash + run: | + set -euo pipefail + baseline=$(mktemp -d) + trap 'rm -rf "$baseline"' EXIT + version=$(node -p "JSON.parse(require('fs').readFileSync('package.json')).version") + npm install --prefix "$baseline" --ignore-scripts --no-audit --no-fund \ + --registry=https://registry.npmjs.org "cloudgpu-mcp@$version" + docker build --pull -t cloudgpu-image-smoke . + timeout --signal=TERM --kill-after=10s 120s \ + node scripts/docker-smoke.mjs cloudgpu-image-smoke "$baseline" diff --git a/Dockerfile b/Dockerfile index c6cac75..a7ed585 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,3 @@ FROM node:20-alpine -RUN npm install -g cloudgpu-mcp@0.15.0 +RUN npm install -g cloudgpu-mcp@0.16.6 ENTRYPOINT ["cloudgpu-mcp"] diff --git a/scripts/docker-smoke.mjs b/scripts/docker-smoke.mjs new file mode 100644 index 0000000..122f308 --- /dev/null +++ b/scripts/docker-smoke.mjs @@ -0,0 +1,88 @@ +// Compare the built image with an independently installed published npm package. +// Only initialize, initialized and tools/list are sent; never tools/call. +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import {spawnSync} from 'node:child_process'; +import {Client} from '@modelcontextprotocol/sdk/client/index.js'; +import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js'; + +const [image, baselineDir] = process.argv.slice(2); +assert.ok(image && baselineDir, 'Usage: node scripts/docker-smoke.mjs IMAGE NPM_PREFIX'); +const {version} = JSON.parse(fs.readFileSync('package.json', 'utf8')); +const baseline = path.resolve(baselineDir, 'node_modules/cloudgpu-mcp'); +assert.equal(JSON.parse(fs.readFileSync(path.join(baseline, 'package.json'), 'utf8')).version, version); +const temporary = fs.mkdtempSync(path.join(os.tmpdir(), 'cloudgpu-image-smoke-')); +const containers = new Set(); +function docker(args) { + const result = spawnSync('docker', args, {encoding: 'utf8', timeout: 15000}); + assert.equal(result.status, 0, result.stderr || String(result.error)); + return result.stdout.trim(); +} +function cleanup() { + for (const name of containers) spawnSync('docker', ['rm', '-f', name], {timeout: 15000, stdio: 'ignore'}); + fs.rmSync(temporary, {recursive: true, force: true}); +} +for (const signal of ['SIGINT', 'SIGTERM']) process.once(signal, () => {cleanup(); process.exit(1);}); + +async function discover(command, args, env) { + const transport = new StdioClientTransport({command, args, env, stderr: 'pipe'}); + let stderr = ''; + transport.stderr.on('data', data => {stderr = (stderr + data).slice(-8192);}); + const client = new Client({name: 'docker-image-smoke', version: '1.0.0'}); + try { + await client.connect(transport, {timeout: 15000}); + assert.deepEqual(client.getServerVersion(), {name: 'cloudgpu', version}); + const tools = []; + let cursor; + do { + const result = await client.listTools(cursor ? {cursor} : {}, {timeout: 15000}); + tools.push(...result.tools); + cursor = result.nextCursor; + } while (cursor); + assert.ok(tools.length > 0, 'tools/list must not be empty'); + assert.equal(new Set(tools.map(tool => tool.name)).size, tools.length); + return tools.sort((a, b) => a.name.localeCompare(b.name)); + } catch (error) { + throw new Error(`${command} stdio discovery failed: ${error.message}\n${stderr}`, {cause: error}); + } finally { + await client.close(); + await transport.close(); + } +} + +try { + for (const readOnly of ['0', '1']) { + const env = { + PATH: process.env.PATH, + CLOUDGPU_API_KEY: 'dummy-image-smoke-not-a-real-key', + CLOUDGPU_BASE_URL: 'http://127.0.0.1:9', + CLOUDGPU_CONFIG_DIR: path.join(temporary, `empty-${readOnly}`), + CLOUDGPU_READ_ONLY: readOnly, + }; + const expected = await discover(process.execPath, [path.join(baseline, 'src/index.js')], env); + const name = `cloudgpu-smoke-${process.pid}-${readOnly}`; + containers.add(name); + // No host mounts, credentials or network. Keep the stopped container to check its exit. + const args = ['run', '--name', name, '--network', 'none', '--read-only', + '--tmpfs', '/tmp:rw,noexec,nosuid,size=16m', '-i']; + for (const [key, value] of Object.entries({...env, CLOUDGPU_CONFIG_DIR: '/tmp/empty-config'})) { + if (key !== 'PATH') args.push('-e', `${key}=${value}`); + } + args.push(image); + try { + const actual = await discover('docker', args, {PATH: process.env.PATH}); + assert.deepEqual(actual, expected, `Published npm tool names/schemas differ (readOnly=${readOnly})`); + const state = JSON.parse(docker(['inspect', '--format', '{{json .State}}', name])); + assert.equal(state.Running, false, 'Container must exit after stdin closes'); + assert.equal(state.ExitCode, 0, 'Container must shut down cleanly'); + console.log(`Image ${version}: readOnly=${readOnly}, ${actual.length} tools match published npm names and full schemas; clean exit`); + } finally { + docker(['rm', '-f', name]); + containers.delete(name); + } + } +} finally { + cleanup(); +} diff --git a/scripts/npm-release-availability.mjs b/scripts/npm-release-availability.mjs new file mode 100644 index 0000000..3d79947 --- /dev/null +++ b/scripts/npm-release-availability.mjs @@ -0,0 +1,44 @@ +// A not-yet-published release may skip image acceptance; lookup failures must fail CI. +import fs from 'node:fs'; +import {pathToFileURL} from 'node:url'; + +export async function checkRelease({name, version}, {fetchImpl = fetch, env = {}} = {}) { + const url = `https://registry.npmjs.org/${encodeURIComponent(name)}`; + const get = target => fetchImpl(target, {redirect: 'error', signal: AbortSignal.timeout(15000)}); + const exact = await get(`${url}/${encodeURIComponent(version)}`); + let published; + if (exact.status === 200) { + const record = await exact.json(); + if (record.name !== name || record.version !== version) throw new Error('npm version identity mismatch'); + published = true; + } else if (exact.status === 404) { + // A 404 alone could mean a missing package or an unexpected registry response. + // Require successful package metadata confirming this exact version is absent. + const response = await get(url); + if (response.status !== 200) throw new Error(`npm package lookup failed: HTTP ${response.status}`); + const pack = await response.json(); + if (pack.name !== name || !pack.versions || typeof pack.versions !== 'object' || Array.isArray(pack.versions)) { + throw new Error('Invalid npm package metadata'); + } + if (Object.hasOwn(pack.versions, version)) throw new Error('Inconsistent npm version lookup'); + published = false; + } else { + throw new Error(`npm exact version lookup failed: HTTP ${exact.status}`); + } + if (env.GITHUB_OUTPUT) fs.appendFileSync(env.GITHUB_OUTPUT, `published=${published}\n`); + if (!published) { + const message = `Docker image smoke SKIPPED: ${name}@${version} is not published on npm. No image build or smoke acceptance was performed. Metadata and unit tests still run.\n`; + console.log(message.trim()); + if (env.GITHUB_STEP_SUMMARY) fs.appendFileSync(env.GITHUB_STEP_SUMMARY, message); + } + return published; +} + +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { + try { + await checkRelease(JSON.parse(fs.readFileSync('package.json', 'utf8')), {env: process.env}); + } catch (error) { + console.error(`npm availability check failed; image acceptance is not skipped: ${error.message}`); + process.exitCode = 1; + } +} diff --git a/test/npm-release-availability.test.js b/test/npm-release-availability.test.js new file mode 100644 index 0000000..1d654e7 --- /dev/null +++ b/test/npm-release-availability.test.js @@ -0,0 +1,58 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import {checkRelease} from '../scripts/npm-release-availability.mjs'; +const release = {name: 'cloudgpu-mcp', version: '0.99.0'}; +const response = (status, body) => ({status, json: async () => body}); +const pack = {name: release.name, versions: {'0.16.6': {version: '0.16.6'}}}; + +test('published exact version runs image acceptance without using latest', async () => { + const calls = []; + assert.equal(await checkRelease(release, {fetchImpl: async (url, options) => { + calls.push(url); assert.equal(options.redirect, 'error'); assert.ok(options.signal); + return response(200, release); + }}), true); + assert.deepEqual(calls, ['https://registry.npmjs.org/cloudgpu-mcp/0.99.0']); +}); + +test('E404 skips only after successful package metadata confirms exact version absent', async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'npm-release-test-')); + try { + const env = {GITHUB_OUTPUT: path.join(dir, 'output'), GITHUB_STEP_SUMMARY: path.join(dir, 'summary')}; + const calls = []; + assert.equal(await checkRelease(release, {env, fetchImpl: async url => { + calls.push(url); return calls.length === 1 ? response(404, {error: 'version not found'}) : response(200, pack); + }}), false); + assert.deepEqual(calls, ['https://registry.npmjs.org/cloudgpu-mcp/0.99.0', 'https://registry.npmjs.org/cloudgpu-mcp']); + assert.equal(fs.readFileSync(env.GITHUB_OUTPUT, 'utf8'), 'published=false\n'); + assert.match(fs.readFileSync(env.GITHUB_STEP_SUMMARY, 'utf8'), /SKIPPED.*0\.99\.0.*not published/); + assert.match(fs.readFileSync(env.GITHUB_STEP_SUMMARY, 'utf8'), /No image build or smoke acceptance was performed/); + } finally {fs.rmSync(dir, {recursive: true, force: true});} +}); + +test('network, timeout, authorization, rate limit and server errors never become an unpublished skip', async () => { + for (const status of [401, 403, 429, 500, 503]) { + await assert.rejects(checkRelease(release, {fetchImpl: async () => response(status)}), /lookup failed/); + } + for (const message of ['network unavailable', 'request timed out']) { + await assert.rejects(checkRelease(release, {fetchImpl: async () => {throw new Error(message);}}), new RegExp(message)); + } +}); + +test('E404 with missing package, broken metadata or inconsistent exact version fails closed', async () => { + for (const fallback of [response(404), response(403), response(503), + response(200, {}), response(200, {...pack, versions: []}), + response(200, {...pack, versions: {[release.version]: release}})]) { + let calls = 0; + await assert.rejects(checkRelease(release, {fetchImpl: async () => ++calls === 1 ? response(404) : fallback})); + } + let calls = 0; + await assert.rejects(checkRelease(release, {fetchImpl: async () => { + if (++calls === 1) return response(404); + throw new Error('fallback network failure'); + }}), /network failure/); + await assert.rejects(checkRelease(release, {fetchImpl: async () => response(200, {...release, version: '0.16.6'})}), /identity mismatch/); + await assert.rejects(checkRelease(release, {fetchImpl: async () => ({status: 200, json: async () => {throw new Error('invalid JSON');}})}), /invalid JSON/); +}); diff --git a/test/release-metadata.test.js b/test/release-metadata.test.js index 3163c02..e502f13 100644 --- a/test/release-metadata.test.js +++ b/test/release-metadata.test.js @@ -2,3 +2,11 @@ import test from 'node:test';import assert from 'node:assert/strict';import fs f test('npm and MCP Registry metadata share the repository-owned namespace and version',()=>{ const p=JSON.parse(fs.readFileSync('package.json')),s=JSON.parse(fs.readFileSync('server.json')),l=JSON.parse(fs.readFileSync('package-lock.json'));assert.equal(p.mcpName,s.name);assert.equal(s.name,'io.github.cloudgpu-app/cloudgpu-mcp');assert.equal(s.version,p.version);assert.equal(s.packages[0].version,p.version);assert.equal(l.version,p.version);assert.equal(l.packages[''].version,p.version);assert.equal(s.repository.url,'https://github.com/cloudgpu-app/cloudgpu-mcp');assert.ok(p.repository.url.includes(s.repository.url));assert.ok(fs.readFileSync('src/index.js','utf8').includes(`const VERSION = "${p.version}"`)); }); + +test('Docker installs the exact package version declared for this release',()=>{ + const p=JSON.parse(fs.readFileSync('package.json')); + const dockerfile=fs.readFileSync('Dockerfile','utf8'); + const installs=[...dockerfile.matchAll(/^\s*RUN\s+npm\s+install\s+-g\s+(cloudgpu-mcp(?:@\S+)?)\s*$/gm)].map(match=>match[1]); + assert.deepEqual(installs,[`${p.name}@${p.version}`], + 'Keep the Docker npm pin in sync with package.json when releasing'); +});