Files
vercel/test/lib/deployment/test-deployment.js
Nathan Rajlich 8a68211cad [all] Rename packages to vercel and add logic to publish legacy package names to npm (#4233)
https://vercel.com/blog/zeit-is-now-vercel

* Updates all org packages from `@now` to `@vercel`
* Updates Now CLI package name from `now` to `vercel`
  * Packages contains `"bin"` entries for _both_ `vercel` and `now` in the package.json
* Updates `now-client` package name to `@vercel/client` (org scoped, for authenticity)

There is also a new `publish-legacy.sh` script which ensures that all the legacy package names (i.e. `now`, `now-client`, `@now/node`, etc.) will still be published as well.

We will remove this legacy publishing logic on Jan 1, 2021.
2020-05-05 23:43:57 +00:00

260 lines
7.6 KiB
JavaScript

const assert = require('assert');
const bufferReplace = require('buffer-replace');
const fs = require('fs');
const json5 = require('json5');
const glob = require('util').promisify(require('glob'));
const path = require('path');
const { spawn } = require('child_process');
const fetch = require('./fetch-retry.js');
const { nowDeploy } = require('./now-deploy.js');
async function packAndDeploy(builderPath) {
await spawnAsync('npm', ['--loglevel', 'warn', 'pack'], {
stdio: 'inherit',
cwd: builderPath,
});
const tarballs = await glob('*.tgz', { cwd: builderPath });
const tgzPath = path.join(builderPath, tarballs[0]);
console.log('tgzPath', tgzPath);
const url = await nowDeployIndexTgz(tgzPath);
await fetchTgzUrl(`https://${url}`);
fs.unlinkSync(tgzPath);
return url;
}
const RANDOMNESS_PLACEHOLDER_STRING = 'RANDOMNESS_PLACEHOLDER';
async function testDeployment(
{ builderUrl, buildUtilsUrl },
fixturePath,
buildDelegate
) {
console.log('testDeployment', fixturePath);
const globResult = await glob(`${fixturePath}/**`, {
nodir: true,
dot: true,
});
const bodies = globResult.reduce((b, f) => {
const r = path.relative(fixturePath, f);
b[r] = fs.readFileSync(f);
return b;
}, {});
const randomness = Math.floor(Math.random() * 0x7fffffff)
.toString(16)
.repeat(6)
.slice(0, RANDOMNESS_PLACEHOLDER_STRING.length);
for (const file of Object.keys(bodies)) {
bodies[file] = bufferReplace(
bodies[file],
RANDOMNESS_PLACEHOLDER_STRING,
randomness
);
}
// we use json5 to allow comments for probes
const nowJson = json5.parse(bodies['now.json']);
if (process.env.NOW_BUILDER_DEBUG) {
if (!nowJson.build) {
nowJson.build = {};
}
if (!nowJson.build.env) {
nowJson.build.env = {};
}
nowJson.build.env.NOW_BUILDER_DEBUG = process.env.NOW_BUILDER_DEBUG;
}
for (const build of nowJson.builds || []) {
if (builderUrl) {
if (builderUrl === '@canary') {
build.use = `${build.use}@canary`;
} else {
build.use = `https://${builderUrl}`;
}
}
if (buildUtilsUrl) {
build.config = build.config || {};
const { config } = build;
if (buildUtilsUrl === '@canary') {
const buildUtils = config.useBuildUtils || '@vercel/build-utils';
config.useBuildUtils = `${buildUtils}@canary`;
} else {
config.useBuildUtils = `https://${buildUtilsUrl}`;
}
}
if (buildDelegate) {
buildDelegate(build);
}
}
bodies['now.json'] = Buffer.from(JSON.stringify(nowJson));
delete bodies['probe.js'];
const { deploymentId, deploymentUrl } = await nowDeploy(bodies, randomness);
for (const probe of nowJson.probes || []) {
console.log('testing', JSON.stringify(probe));
if (probe.delay) {
await new Promise(resolve => setTimeout(resolve, probe.delay));
continue;
}
const probeUrl = `https://${deploymentUrl}${probe.path}`;
const fetchOpts = {
...probe.fetchOptions,
method: probe.method,
headers: { ...probe.headers },
};
if (probe.body) {
fetchOpts.headers['content-type'] = 'application/json';
fetchOpts.body = JSON.stringify(probe.body);
}
const { text, resp } = await fetchDeploymentUrl(probeUrl, fetchOpts);
console.log('finished testing', JSON.stringify(probe));
if (probe.status) {
if (probe.status !== resp.status) {
throw new Error(
`Fetched page ${probeUrl} does not return the status ${probe.status} Instead it has ${resp.status}`
);
}
}
if (probe.mustContain || probe.mustNotContain) {
const shouldContain = !!probe.mustContain;
const containsIt = text.includes(probe.mustContain);
if (
(!containsIt && probe.mustContain) ||
(containsIt && probe.mustNotContain)
) {
fs.writeFileSync(path.join(__dirname, 'failed-page.txt'), text);
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Fetched page ${probeUrl} does${
shouldContain ? ' not' : ''
} contain ${
shouldContain ? probe.mustContain : probe.mustNotContain
}.` +
(shouldContain ? ` Instead it contains ${text.slice(0, 60)}` : '') +
` Response headers:\n ${headers}`
);
}
} else if (probe.responseHeaders) {
// eslint-disable-next-line no-loop-func
Object.keys(probe.responseHeaders).forEach(header => {
const actual = resp.headers.get(header);
const expected = probe.responseHeaders[header];
const isEqual = Array.isArray(expected)
? expected.every(h => actual.includes(h))
: expected.startsWith('/') && expected.endsWith('/')
? new RegExp(expected.slice(1, -1)).test(actual)
: expected === actual;
if (!isEqual) {
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Page ${probeUrl} does not have header ${header}.\n\nExpected: ${expected}.\nActual: ${headers}`
);
}
});
} else if (probe.notResponseHeaders) {
Object.keys(probe.notResponseHeaders).forEach(header => {
const headerValue = resp.headers.get(header);
const expected = probe.notResponseHeaders[header];
if (headerValue === expected) {
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Page ${probeUrl} invalid page header ${header}.\n\n Did not expect: ${header}=${expected}.\nBut got ${headers}`
);
}
});
} else if (!probe.status) {
assert(false, 'probe must have a test condition');
}
}
const probeJsFullPath = path.resolve(fixturePath, 'probe.js');
if (fs.existsSync(probeJsFullPath)) {
await require(probeJsFullPath)({ deploymentUrl, fetch, randomness });
}
return { deploymentId, deploymentUrl };
}
async function nowDeployIndexTgz(file) {
const bodies = {
'index.tgz': fs.readFileSync(file),
'now.json': Buffer.from(JSON.stringify({ version: 2 })),
};
return (await nowDeploy(bodies)).deploymentUrl;
}
async function fetchDeploymentUrl(url, opts) {
for (let i = 0; i < 50; i += 1) {
const resp = await fetch(url, opts);
const text = await resp.text();
if (text && !text.includes('Join Free')) {
return { resp, text };
}
await new Promise(r => setTimeout(r, 1000));
}
throw new Error(`Failed to wait for deployment READY. Url is ${url}`);
}
async function fetchTgzUrl(url) {
for (let i = 0; i < 500; i += 1) {
const resp = await fetch(url);
if (resp.status === 200) {
const buffer = await resp.buffer();
if (buffer[0] === 0x1f) {
// tgz beginning
return;
}
}
await new Promise(r => setTimeout(r, 1000));
}
throw new Error(`Failed to wait for builder url READY. Url is ${url}`);
}
async function spawnAsync(...args) {
return await new Promise((resolve, reject) => {
const child = spawn(...args);
let result;
if (child.stdout) {
result = '';
child.stdout.on('data', chunk => {
result += chunk.toString();
});
}
child.on('error', reject);
child.on('close', (code, signal) => {
if (code !== 0) {
if (result) console.log(result);
reject(new Error(`Exited with ${code || signal}`));
return;
}
resolve(result);
});
});
}
module.exports = {
packAndDeploy,
testDeployment,
};