Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 27 additions & 7 deletions .github/workflows/sweep.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ jobs:
# The AWS roles exist for this repository only.
if: github.repository == 'serverless/examples' && needs.select.outputs.count != '0'
runs-on: ubuntu-latest
# The nightly job tests two Framework versions, each with its run and remove.
timeout-minutes: 80
# The nightly job tests two Framework versions, each with its run and remove
# (15 + 15 minutes), then removes each stage (up to 20 minutes each).
timeout-minutes: 100
permissions:
id-token: write
contents: read
Expand Down Expand Up @@ -136,13 +137,24 @@ jobs:
aws-region: ${{ vars.EXAMPLES_REGION }}
role-duration-seconds: 3600
mask-aws-account-id: true
- name: Choose the stage
- name: Choose the stages
id: stage
env:
SECOND: ${{ needs.select.outputs.second }}
run: |
stage=$(node --input-type=module -e 'import { newStage } from "./scripts/test-example.mjs"; console.log(newStage())')
new_stage() { node --input-type=module -e 'import { newStage } from "./scripts/test-example.mjs"; console.log(newStage())'; }
stage=$(new_stage)
echo "stage=$stage" >> "$GITHUB_OUTPUT"
echo "Stage: $stage"
# The versions run one after another with the same stage. The region is
# The second version gets its own stage: redeploying a stage right after
# removing it recreates IAM roles under the same names, and Lambda can
# reject a role that IAM has not finished propagating.
if [[ -n "$SECOND" ]]; then
stage2=$(new_stage)
echo "stage2=$stage2" >> "$GITHUB_OUTPUT"
echo "Second stage: $stage2"
fi
# The versions run one after another, each with its own stage. The region is
# AWS_REGION from the credentials step, unless the manifest sets one.
- name: Test with ${{ needs.select.outputs.first }}
env:
Expand Down Expand Up @@ -175,7 +187,7 @@ jobs:
id: second
if: ${{ !cancelled() && steps.stage.outcome == 'success' && needs.select.outputs.second != '' }}
env:
STAGE: ${{ steps.stage.outputs.stage }}
STAGE: ${{ steps.stage.outputs.stage2 }}
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY_DEV }}
SERVERLESS_PLATFORM_STAGE: dev
run: |
Expand All @@ -199,7 +211,7 @@ jobs:
mask-aws-account-id: true
unset-current-credentials: true
# A cancelled or timed-out job gives the runner only seconds to remove;
# this step removes whatever the stage still has.
# these steps remove whatever each stage still has.
- name: Remove what the stage left
if: always() && steps.stage.outputs.stage != ''
# Remove's own 15-minute budget plus the listing and install around it.
Expand All @@ -209,3 +221,11 @@ jobs:
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY_DEV }}
SERVERLESS_PLATFORM_STAGE: dev
run: node scripts/test-example.mjs "$EXAMPLE_DIR" --remove-only --stage "$STAGE"
- name: Remove what the second stage left
if: always() && steps.stage.outputs.stage2 != ''
timeout-minutes: 20
env:
STAGE: ${{ steps.stage.outputs.stage2 }}
SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY_DEV }}
SERVERLESS_PLATFORM_STAGE: dev
run: node scripts/test-example.mjs "$EXAMPLE_DIR" --remove-only --stage "$STAGE"
21 changes: 13 additions & 8 deletions scripts/test-example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
copyExample,
installCommands,
Interrupted,
COMPOSE_FILES,
isComposeRoot,
runCommand,
runOrThrow,
Expand Down Expand Up @@ -233,10 +234,7 @@ export async function runExample({
let leftBehind;
try {
try {
if (frameworkVersion !== 'latest') {
const dirs = compose ? serviceDirs(copy) : [copy];
for (const d of dirs) rewriteFrameworkVersion(d, frameworkVersion);
}
if (frameworkVersion !== 'latest') pinFrameworkVersion(copy, compose, frameworkVersion);
resolvedVersion = await readFrameworkVersion(exec, copy, Math.min(VERSION_TIMEOUT_MS, left()), signal);

await attempt('install', async () => {
Expand Down Expand Up @@ -349,7 +347,7 @@ export async function removeOnly({
try {
const compose = isComposeRoot(copy);
const dirs = compose ? serviceDirs(copy) : [copy];
if (frameworkVersion !== 'latest') for (const d of dirs) rewriteFrameworkVersion(d, frameworkVersion);
if (frameworkVersion !== 'latest') pinFrameworkVersion(copy, compose, frameworkVersion);

const listed = await listStageStacks(exec, copy, stage, region);
if (listed.error) log(`[list-stacks] could not list the stacks in ${region} (${listed.error}); running remove anyway`);
Expand Down Expand Up @@ -584,10 +582,17 @@ function readServiceName(root, dir) {
return name;
}

// The installer picks the CLI from the config file in the directory the command
// runs in: serverless-compose.yml for the whole Compose run, else serverless.yml.
// Service files get the version too; there it is only a range check.
function pinFrameworkVersion(copy, compose, version) {
const files = compose ? serviceDirs(copy).map(findServiceFile) : [findServiceFile(copy)];
if (compose) files.push(COMPOSE_FILES.map((f) => path.join(copy, f)).find((f) => fs.existsSync(f)));
for (const file of files) if (file) rewriteFrameworkVersion(file, version);
}

// Replaces the top-level frameworkVersion line, or adds one at the top.
function rewriteFrameworkVersion(dir, version) {
const file = findServiceFile(dir);
if (!file) return;
function rewriteFrameworkVersion(file, version) {
const text = fs.readFileSync(file, 'utf8');
const line = `frameworkVersion: '${version}'`;
const pattern = /^frameworkVersion[ \t]*:.*$/m;
Expand Down
31 changes: 31 additions & 0 deletions tests/runner.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,37 @@ test('a compose root rewrites frameworkVersion in each service copy', async () =
assert.match(seen.worker, /^frameworkVersion: 'canary'$/m);
});

// The installer picks the CLI from serverless-compose.yml for the whole Compose
// run; a service's frameworkVersion is only a range check.
test('a compose root writes frameworkVersion into serverless-compose.yml for the run and the remove', async () => {
const root = composeFixture();
const seen = [];
const read = ({ cwd }) => {
seen.push(fs.readFileSync(path.join(cwd, 'serverless-compose.yml'), 'utf8'));
return { status: 0 };
};
await run(root, { fx: fakeExec({ 'serverless deploy': read }), args: { frameworkVersion: 'canary' } });
assert.match(seen[0], /^frameworkVersion: 'canary'$/m);
assert.match(seen[0], /^services:$/m);
const fx = fakeExec({ 'aws cloudformation': stackList(['hello-api-exabc123def']), 'serverless remove': read });
const { code } = await cli([DIR, '--remove-only', '--stage', 'exabc123def', '--framework-version', '4.4.7'], root, { exec: fx.exec, env: { AWS_REGION: 'eu-west-1' } });
assert.equal(code, 0);
assert.match(seen[1], /^frameworkVersion: '4\.4\.7'$/m);
});

test('a compose root with latest leaves serverless-compose.yml as it is', async () => {
const root = composeFixture();
let text;
const fx = fakeExec({
'serverless deploy': ({ cwd }) => {
text = fs.readFileSync(path.join(cwd, 'serverless-compose.yml'), 'utf8');
return { status: 0 };
},
});
await run(root, { fx });
assert.doesNotMatch(text, /frameworkVersion/);
});

test('a compose root reports each service stack left behind', async () => {
const { result } = await run(composeFixture(), { fx: fakeExec({ 'serverless remove': { status: 1 } }) });
assert.equal(result.failedStep, 'remove');
Expand Down
16 changes: 14 additions & 2 deletions tests/workflows.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ test('the ci static job has no secrets and only reads contents', () => {
for (const command of ['npm test', 'npm run validate', 'npm run check-docs']) assert.ok(stat.includes(`run: ${command}`), command);
});

// ci runs one Framework version per job; the sweep's nightly job runs two.
const LIVE_TIMEOUT = { 'ci.yml': 50, 'sweep.yml': 80 };
// Each version: a 15-minute run and a 15-minute remove; each stage: a 20-minute
// final remove. ci runs one version and one stage per job; the sweep's nightly
// job runs two of each.
const LIVE_TIMEOUT = { 'ci.yml': 50, 'sweep.yml': 100 };

for (const file of ['ci.yml', 'sweep.yml']) {
test(`${file}: the live job has the limits, concurrency group and cleanup step`, () => {
Expand All @@ -114,6 +116,16 @@ for (const file of ['ci.yml', 'sweep.yml']) {
});
}

// Redeploying a stage right after removing it recreates IAM roles under the same
// names, and Lambda can reject a role IAM has not finished propagating.
test('sweep.yml: the second version deploys to its own stage, and both stages are removed', () => {
const live = job(read('sweep.yml'), 'live');
assert.match(live, /echo "stage2=\$stage2" >> "\$GITHUB_OUTPUT"/);
const second = live.slice(live.indexOf('id: second'));
assert.match(second.slice(0, second.indexOf('run:')), /STAGE: \$\{\{ steps\.stage\.outputs\.stage2 \}\}/);
assert.match(live, /if: always\(\) && steps\.stage\.outputs\.stage2 != ''\n(.*\n)*?\s+STAGE: \$\{\{ steps\.stage\.outputs\.stage2 \}\}\n(.*\n)*?\s+run: node scripts\/test-example\.mjs "\$EXAMPLE_DIR" --remove-only --stage "\$STAGE"/);
});

test('no workflow names a GitHub environment', () => {
for (const file of FILES) assert.doesNotMatch(read(file), /^\s*environment:/m, file);
});
Expand Down