Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proposal pre/post test steps #210

Merged
merged 31 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2660fec
hooks
usmanmani1122 Jan 1, 2025
8e3f3e4
Merge branch "usman/prepare-test-hook" into branch "usman/before-afte…
usmanmani1122 Jan 1, 2025
df80864
nit
usmanmani1122 Jan 1, 2025
0554920
fixes
usmanmani1122 Jan 3, 2025
5d4e465
Merge branch 'main' into usman/before-after-test-scripts
usmanmani1122 Jan 3, 2025
2c94fef
document prepare-test hook
usmanmani1122 Jan 16, 2025
a689ecc
address comments
usmanmani1122 Jan 22, 2025
61371f5
Merge branch 'main' into usman/before-after-test-scripts
usmanmani1122 Jan 22, 2025
9f24947
increase test timeout
usmanmani1122 Jan 22, 2025
3d6780c
increase test timeout 2.0
usmanmani1122 Jan 22, 2025
5b4c029
fix for spawn
usmanmani1122 Jan 22, 2025
608e2da
:)
usmanmani1122 Jan 22, 2025
0ee2a77
increase test timeout 3.0
usmanmani1122 Jan 22, 2025
7ad7d92
revert
usmanmani1122 Jan 22, 2025
f55ef1f
address comments
usmanmani1122 Jan 23, 2025
8b22861
Empty
usmanmani1122 Jan 23, 2025
b89fe4f
Empty
usmanmani1122 Jan 23, 2025
6d5c227
Empty
usmanmani1122 Jan 23, 2025
f121f67
Merge branch 'main' into usman/before-after-test-scripts
usmanmani1122 Jan 24, 2025
679a083
Address comments
usmanmani1122 Jan 24, 2025
f983826
lint
usmanmani1122 Jan 24, 2025
36a2a20
use tmp directory
usmanmani1122 Jan 27, 2025
5d54940
nit
usmanmani1122 Jan 28, 2025
ef3d10b
address comments + fix bug
usmanmani1122 Jan 30, 2025
963e59f
lint
usmanmani1122 Jan 30, 2025
1a137a4
nit
usmanmani1122 Jan 31, 2025
12c5042
nit
usmanmani1122 Feb 4, 2025
ac2c07b
Merge branch 'main' into usman/before-after-test-scripts
usmanmani1122 Feb 4, 2025
b34bdb6
Merge branch 'main' into usman/before-after-test-scripts
usmanmani1122 Feb 7, 2025
73a9c4b
add test + host folder notes
usmanmani1122 Feb 7, 2025
9d67bc1
lint
usmanmani1122 Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ If the proposal is _pending_ and does not yet have a number, use a letter. The p
- `package.json` specifies what kind of proposal it is in a `agoricProposal` field. If it's a "Software Upgrade Proposal" it also includes additional parameters.
- `use.sh` is the script that will be run in the USE stage of the build
- `test.sh` is the script that will be _included_ in the TEST stage of the build, and run in CI
- `setup-test.sh` is an optional script which can be used to run setup steps _inside_ the container. It runs _before_ the chain starts.
- `teardown-test.sh` is an optional script which can be used to run teardown steps _inside_ the container. It runs _after_ the chain stops.
- `before-test-run.sh` is an optional script which can be used to run setup steps on the _host_ (like starting a follower). It runs _before_ the container launches. It needs to be placed in the **host** folder.
- `after-test-run.sh` is an optional script which can be used to run teardown steps on the _host_ (like stopping a follower). It runs _after_ the container exits. It needs to be placed in the **host** folder.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be more explicit about what the host folder is for. There has been some confusion: Agoric/agoric-sdk#10947 (comment)

Suggested change
- `before-test-run.sh` is an optional script which can be used to run setup steps on the _host_ (like starting a follower). It runs _before_ the container launches. It needs to be placed in the **host** folder.
- `after-test-run.sh` is an optional script which can be used to run teardown steps on the _host_ (like stopping a follower). It runs _after_ the container exits. It needs to be placed in the **host** folder.
- `host` folder is for ___
- `host/before-test-run.sh` is an optional script which can be used to run setup steps on the _host_ (like starting a follower). It runs _before_ the container launches.
- `host/after-test-run.sh` is an optional script which can be used to run teardown steps on the _host_ (like stopping a follower). It runs _after_ the container exits.

While you're at it a change like this would help,

 - `test` folder is for files that should be included only in the `test` image

This is not blocking feedback. If something is delayed by this PR the docs improvements can come later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added


## Development

Expand Down
12 changes: 9 additions & 3 deletions packages/synthetic-chain/public/upgrade-test-scripts/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ source ./env_setup.sh

cd /usr/src/proposals/"$PROPOSAL/" || fail "Proposal $PROPOSAL does not exist"

if test -f prepare-test.sh
if test -f setup-test.sh
then
echo "[$PROPOSAL] Running prepare-test.sh"
./prepare-test.sh
echo "[$PROPOSAL] Running setup-test.sh"
./setup-test.sh
fi

echo "[$PROPOSAL] Starting agd"
Expand All @@ -44,3 +44,9 @@ echo "[$PROPOSAL] Running test.sh."
echo "[$PROPOSAL] Testing completed."

killAgd

if test -f teardown-test.sh
then
echo "[$PROPOSAL] Running teardown-test.sh"
./teardown-test.sh
fi
8 changes: 4 additions & 4 deletions packages/synthetic-chain/src/cli/dockerfileGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ENV \
UPGRADE_INFO=${JSON.stringify(encodeUpgradeInfo(upgradeInfo))} \
SKIP_PROPOSAL_VALIDATION=${skipProposalValidation}

COPY --exclude=test --exclude=test.sh --link --chmod=755 ./proposals/${path} /usr/src/proposals/${path}
COPY --exclude=host --exclude=test --exclude=test.sh --link --chmod=755 ./proposals/${path} /usr/src/proposals/${path}
COPY --link --chmod=755 ./upgrade-test-scripts/env_setup.sh ./upgrade-test-scripts/run_prepare.sh ./upgrade-test-scripts/start_to_to.sh /usr/src/upgrade-test-scripts/
WORKDIR /usr/src/upgrade-test-scripts
SHELL ["/bin/bash", "-c"]
Expand All @@ -100,7 +100,7 @@ FROM ghcr.io/agoric/agoric-sdk:${sdkImageTag} as execute-${proposalName}
WORKDIR /usr/src/upgrade-test-scripts

# base is a fresh sdk image so set up the proposal and its dependencies
COPY --exclude=test --exclude=test.sh --link --chmod=755 ./proposals/${path} /usr/src/proposals/${path}
COPY --exclude=host --exclude=test --exclude=test.sh --link --chmod=755 ./proposals/${path} /usr/src/proposals/${path}
COPY --link --chmod=755 ./upgrade-test-scripts/env_setup.sh ./upgrade-test-scripts/run_execute.sh ./upgrade-test-scripts/start_to_to.sh ./upgrade-test-scripts/install_deps.sh /usr/src/upgrade-test-scripts/
RUN --mount=type=cache,target=/root/.yarn ./install_deps.sh ${path}

Expand All @@ -122,7 +122,7 @@ RUN ./run_execute.sh ${planName}
# EVAL ${proposalName}
FROM use-${lastProposal.proposalName} as eval-${proposalName}

COPY --exclude=test --exclude=test.sh --link --chmod=755 ./proposals/${path} /usr/src/proposals/${path}
COPY --exclude=host --exclude=test --exclude=test.sh --link --chmod=755 ./proposals/${path} /usr/src/proposals/${path}

WORKDIR /usr/src/upgrade-test-scripts

Expand Down Expand Up @@ -171,7 +171,7 @@ FROM use-${proposalName} as test-${proposalName}
# Previous stages copied excluding test files (see COPY above). It would be good
# to copy only missing files, but there may be none. Fortunately, copying extra
# does not invalidate other images because nothing depends on this layer.
COPY --link --chmod=755 ./proposals/${path} /usr/src/proposals/${path}
COPY --exclude=host --link --chmod=755 ./proposals/${path} /usr/src/proposals/${path}

WORKDIR /usr/src/upgrade-test-scripts

Expand Down
90 changes: 79 additions & 11 deletions packages/synthetic-chain/src/cli/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import { execSync } from 'node:child_process';
import { realpathSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { existsSync, realpathSync } from 'node:fs';
import { resolve as resolvePath } from 'node:path';
import { ProposalInfo, imageNameForProposal } from './proposals.js';

const createMessageFile = () => {
const messageFileName = `${new Date().getTime()}.tmp`;
const messageFilePath = `/tmp/${messageFileName}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very least this should use tmpdir, but I would even prefer to use tmp. That package also makes removal easier. Finally, I did prefer when the proposal name was included in the tmp file name, it makes debugging easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure adding a dependency of tmp package was really needed here
Added

spawnSync('touch', [messageFilePath]);
return [messageFileName, messageFilePath];
};

const executeHostScriptIfPresent = (
proposal: ProposalInfo,
scriptName: string,
) => {
const scriptPath = `${resolvePath('.')}/proposals/${proposal.path}/host/${scriptName}`;
if (existsSync(scriptPath)) {
console.log(
`Running script ${scriptName} for proposal ${proposal.proposalName}`,
);
spawnSync(scriptPath, { env: process.env, stdio: 'inherit' });
}
};

/**
* Used to propagate a SLOGFILE environment variable into Docker containers.
* Any file identified by such a variable will be created if it does not already
Expand All @@ -14,20 +35,50 @@ const propagateSlogfile = env => {
const { SLOGFILE } = env;
if (!SLOGFILE) return [];

execSync('touch "$SLOGFILE"');
return ['-e', 'SLOGFILE', '-v', `"$SLOGFILE:${realpathSync(SLOGFILE)}"`];
spawnSync('touch', [SLOGFILE]);

return [
'--env',
`SLOGFILE=${SLOGFILE}`,
'--volume',
`${SLOGFILE}:${realpathSync(SLOGFILE)}`,
];
};

export const runTestImage = (proposal: ProposalInfo) => {
const [messageFileName, messageFilePath] = createMessageFile();

const containerFilePath = `/root/${messageFileName}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we would have the message file in the container use a static name (not even related to the proposal name) to avoid having to plumb an extra env.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Container file doesn't necessarily need to use the proposal name in it's path, but we still need to pass the host file path to the host start script so that the follower can mount that file in it's container

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right but the code above will make the container file name dynamic, without any way for the container script to figure out what that name is. Or am I missing something?

process.env.MESSAGE_FILE_PATH = messageFilePath;

executeHostScriptIfPresent(proposal, 'before-test-run.sh');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be preferable to plumb the messageFilePath into executeHostScriptIfPresent and for that spawn to use the modified env of the child process only. Mutating the current process env goes against best practices.

You can either spread the current env, or use inheritance ({__proto__: process.env, MESSAGE_FILE_PATH: messageFilePath})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


console.log(`Running test image for proposal ${proposal.proposalName}`);
const { name } = imageNameForProposal(proposal, 'test');
const slogOpts = propagateSlogfile(process.env);
// 'rm' to remove the container when it exits
const cmd = `docker run ${slogOpts.join(' ')} --rm ${name}`;
execSync(cmd, { stdio: 'inherit' });
spawnSync(
'docker',
[
'run',
'--env',
`MESSAGE_FILE_PATH=${containerFilePath}`,
'--mount',
`source=${messageFilePath},target=${containerFilePath},type=bind`,
'--network',
'host',
'--rm',
...propagateSlogfile(process.env),
name,
],
{ stdio: 'inherit' },
);

spawnSync('rm', ['--force', messageFilePath]);

executeHostScriptIfPresent(proposal, 'after-test-run.sh');
};

export const debugTestImage = (proposal: ProposalInfo) => {
executeHostScriptIfPresent(proposal, 'before-test-run.sh');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, we don't expect tests run in debug mode to have a message file? Is there not a risk that host scripts would expect a message file and break if not available?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point
Done

const { name } = imageNameForProposal(proposal, 'test');
console.log(
`
Expand All @@ -46,8 +97,25 @@ export const debugTestImage = (proposal: ProposalInfo) => {
`,
);

const slogOpts = propagateSlogfile(process.env);
// start the chain with ports mapped
const cmd = `docker run ${slogOpts.join(' ')} --publish 26657:26657 --publish 1317:1317 --publish 9090:9090 --interactive --tty --entrypoint /usr/src/upgrade-test-scripts/start_agd.sh ${name}`;
execSync(cmd, { stdio: 'inherit' });
spawnSync(
'docker',
[
'run',
'--entrypoint',
'/usr/src/upgrade-test-scripts/start_agd.sh',
'--interactive',
'--publish',
'1317:1317',
'--publish',
'9090:9090',
'--publish',
'26657:26657',
'--tty',
...propagateSlogfile(process.env),
name,
],
{ stdio: 'inherit' },
);
executeHostScriptIfPresent(proposal, 'after-test-run.sh');
};