Skip to content

Commit

Permalink
test: fix flaky test-policy-integrity
Browse files Browse the repository at this point in the history
Split the test into three tests so that it doesn't time out.

Fixes: nodejs#40694
Fixes: nodejs#38088
  • Loading branch information
Trott committed Nov 9, 2021
1 parent b8661b4 commit 29fdfb3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 146 deletions.
2 changes: 0 additions & 2 deletions test/pummel/pummel.status
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ prefix pummel
[true] # This section applies to all platforms

[$system==win32]
# https://github.com/nodejs/node/issues/40694
test-policy-integrity: PASS,FLAKY

[$system==linux]
# https://github.com/nodejs/node/issues/38226
Expand Down
58 changes: 14 additions & 44 deletions test/pummel/test-policy-integrity-dep.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,6 @@ const parentBody = {
import(process.env.DEP_FILE)
`,
};
const workerSpawningBody = `
const path = require('path');
const { Worker } = require('worker_threads');
if (!process.env.PARENT_FILE) {
console.error(
'missing required PARENT_FILE env to determine worker entry point'
);
process.exit(33);
}
if (!process.env.DELETABLE_POLICY_FILE) {
console.error(
'missing required DELETABLE_POLICY_FILE env to check reloading'
);
process.exit(33);
}
const w = new Worker(path.resolve(process.env.PARENT_FILE));
w.on('exit', (status) => process.exit(status === 0 ? 0 : 1));
`;

let nextTestId = 1;
function newTestId() {
Expand All @@ -100,12 +82,11 @@ function drainQueue() {
if (toSpawn.length) {
const config = toSpawn.shift();
const {
shouldSucceed, // = (() => { throw new Error('required')})(),
preloads, // = (() =>{ throw new Error('required')})(),
entryPath, // = (() => { throw new Error('required')})(),
willDeletePolicy, // = (() => { throw new Error('required')})(),
onError, // = (() => { throw new Error('required')})(),
resources, // = (() => { throw new Error('required')})(),
shouldSucceed,
preloads,
entryPath,
onError,
resources,
parentPath,
depPath,
} = config;
Expand All @@ -118,7 +99,7 @@ function drainQueue() {
tmpdir.path,
`deletable-policy-${testId}.json`
);
const cliPolicy = willDeletePolicy ? tmpPolicyPath : policyPath;

fs.rmSync(configDirPath, { maxRetries: 3, recursive: true, force: true });
fs.mkdirSync(configDirPath, { recursive: true });
const manifest = {
Expand All @@ -140,15 +121,15 @@ function drainQueue() {
}
const manifestBody = JSON.stringify(manifest);
fs.writeFileSync(manifestPath, manifestBody);
if (cliPolicy === tmpPolicyPath) {
if (policyPath === tmpPolicyPath) {
fs.writeFileSync(tmpPolicyPath, manifestBody);
}
const spawnArgs = [
process.execPath,
[
'--unhandled-rejections=strict',
'--experimental-policy',
cliPolicy,
policyPath,
...preloads.flatMap((m) => ['-r', m]),
entryPath,
'--',
Expand Down Expand Up @@ -255,7 +236,6 @@ function fileExtensionFormat(extension, packageType) {
throw new Error('unknown format ' + extension);
}
for (const permutation of permutations({
entry: ['dep'],
preloads: [[], ['parent'], ['dep']],
onError: ['log', 'exit'],
parentExtension: ['.js', '.mjs', '.cjs'],
Expand All @@ -282,10 +262,9 @@ for (const permutation of permutations({
continue;
}
const depPath = `./dep${permutation.depExtension}`;
const workerSpawnerPath = './worker-spawner.cjs';
const entryPath = depPath;

const packageJSON = {
main: entryPath,
main: depPath,
type: permutation.packageType,
};
if (permutation.packageType === 'no-field') {
Expand All @@ -310,8 +289,7 @@ for (const permutation of permutations({
if (parentFormat !== 'commonjs') {
permutation.preloads = permutation.preloads.filter((_) => _ !== 'parent');
}
const hasParent =
permutation.entry !== 'dep' || permutation.preloads.includes('parent');
const hasParent = permutation.preloads.includes('parent');
if (hasParent) {
resources[parentPath] = {
body: parentBody[parentFormat],
Expand All @@ -328,12 +306,7 @@ for (const permutation of permutations({
throw new Error('unreachable');
}
}
if (permutation.entry === 'worker') {
resources[workerSpawnerPath] = {
body: workerSpawningBody,
integrities: hash('sha256', workerSpawningBody),
};
}

if (permutation.packageType !== 'no-package-json') {
let packageBody = JSON.stringify(packageJSON, null, 2);
let packageIntegrities = hash('sha256', packageBody);
Expand All @@ -360,18 +333,15 @@ for (const permutation of permutations({
integrities: packageIntegrities,
};
}
const willDeletePolicy = permutation.entry === 'worker';

if (permutation.onError === 'log') {
shouldSucceed = true;
}
tests.add(
JSON.stringify({
// hasParent,
// original: permutation,
onError: permutation.onError,
shouldSucceed,
entryPath,
willDeletePolicy,
entryPath: depPath,
preloads: permutation.preloads
.map((_) => {
return {
Expand Down
82 changes: 24 additions & 58 deletions test/pummel/test-policy-integrity-parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,6 @@ const parentBody = {
import(process.env.DEP_FILE)
`,
};
const workerSpawningBody = `
const path = require('path');
const { Worker } = require('worker_threads');
if (!process.env.PARENT_FILE) {
console.error(
'missing required PARENT_FILE env to determine worker entry point'
);
process.exit(33);
}
if (!process.env.DELETABLE_POLICY_FILE) {
console.error(
'missing required DELETABLE_POLICY_FILE env to check reloading'
);
process.exit(33);
}
const w = new Worker(path.resolve(process.env.PARENT_FILE));
w.on('exit', (status) => process.exit(status === 0 ? 0 : 1));
`;

let nextTestId = 1;
function newTestId() {
Expand All @@ -100,12 +82,12 @@ function drainQueue() {
if (toSpawn.length) {
const config = toSpawn.shift();
const {
shouldSucceed, // = (() => { throw new Error('required')})(),
preloads, // = (() =>{ throw new Error('required')})(),
entryPath, // = (() => { throw new Error('required')})(),
willDeletePolicy, // = (() => { throw new Error('required')})(),
onError, // = (() => { throw new Error('required')})(),
resources, // = (() => { throw new Error('required')})(),
shouldSucceed,
preloads,
entryPath,
willDeletePolicy,
onError,
resources,
parentPath,
depPath,
} = config;
Expand Down Expand Up @@ -255,7 +237,6 @@ function fileExtensionFormat(extension, packageType) {
throw new Error('unknown format ' + extension);
}
for (const permutation of permutations({
entry: ['worker', 'parent', 'dep'],
preloads: [[], ['parent'], ['dep']],
onError: ['log', 'exit'],
parentExtension: ['.js', '.mjs', '.cjs'],
Expand All @@ -282,12 +263,7 @@ for (const permutation of permutations({
continue;
}
const depPath = `./dep${permutation.depExtension}`;
const workerSpawnerPath = './worker-spawner.cjs';
const entryPath = {
dep: depPath,
parent: parentPath,
worker: workerSpawnerPath,
}[permutation.entry];
const entryPath = parentPath;
const packageJSON = {
main: entryPath,
type: permutation.packageType,
Expand All @@ -314,30 +290,22 @@ for (const permutation of permutations({
if (parentFormat !== 'commonjs') {
permutation.preloads = permutation.preloads.filter((_) => _ !== 'parent');
}
const hasParent =
permutation.entry !== 'dep' || permutation.preloads.includes('parent');
if (hasParent) {
resources[parentPath] = {
body: parentBody[parentFormat],
integrities: hash('sha256', parentBody[parentFormat]),
};
if (permutation.parentIntegrity === 'invalid') {
resources[parentPath].body += '\n// INVALID INTEGRITY';
shouldSucceed = false;
} else if (permutation.parentIntegrity === 'missing') {
resources[parentPath].integrities = null;
shouldSucceed = false;
} else if (permutation.parentIntegrity === 'match') {
} else {
throw new Error('unreachable');
}
}
if (permutation.entry === 'worker') {
resources[workerSpawnerPath] = {
body: workerSpawningBody,
integrities: hash('sha256', workerSpawningBody),
};

resources[parentPath] = {
body: parentBody[parentFormat],
integrities: hash('sha256', parentBody[parentFormat]),
};
if (permutation.parentIntegrity === 'invalid') {
resources[parentPath].body += '\n// INVALID INTEGRITY';
shouldSucceed = false;
} else if (permutation.parentIntegrity === 'missing') {
resources[parentPath].integrities = null;
shouldSucceed = false;
} else if (permutation.parentIntegrity === 'match') {
} else {
throw new Error('unreachable');
}

if (permutation.packageType !== 'no-package-json') {
let packageBody = JSON.stringify(packageJSON, null, 2);
let packageIntegrities = hash('sha256', packageBody);
Expand All @@ -364,18 +332,16 @@ for (const permutation of permutations({
integrities: packageIntegrities,
};
}
const willDeletePolicy = permutation.entry === 'worker';

if (permutation.onError === 'log') {
shouldSucceed = true;
}
tests.add(
JSON.stringify({
// hasParent,
// original: permutation,
onError: permutation.onError,
shouldSucceed,
entryPath,
willDeletePolicy,
willDeletePolicy: false,
preloads: permutation.preloads
.map((_) => {
return {
Expand Down
Loading

0 comments on commit 29fdfb3

Please sign in to comment.