Skip to content

Commit

Permalink
made test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
bergjaak committed May 9, 2024
1 parent 06a62be commit a6f2c82
Show file tree
Hide file tree
Showing 5 changed files with 530 additions and 94 deletions.
17 changes: 10 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
"version": "0.2.0",
"configurations": [
{
// Has convenient settings for attaching to a NodeJS process for debugging purposes
// that are NOT the default and otherwise every developers has to configure for
// themselves again and again.
"type": "node",
"request": "launch",
"args": ["-a", "cli-integ-tests", "-t", "test resource import"],
"name": "debug integ tests",
"program": "/Users/bergjak/workplace/CDK/aws-cdk/packages/@aws-cdk-testing/cli-integ/bin/run-suite",
"console": "integratedTerminal",
"sourceMaps": true,
"skipFiles": [ "<node_internals>/**/*" ],
"request": "attach",
"name": "Attach to NodeJS",
// If we don't do this, every step-into into an async function call will go into
// NodeJS internals which are hard to step out of.
"skipFiles": [
"<node_internals>/**"
],
// Saves some button-pressing latency on attaching
"stopOnEntry": false
}
Expand Down
7 changes: 4 additions & 3 deletions packages/@aws-cdk-testing/cli-integ/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
"license": "Apache-2.0",
"devDependencies": {
"@aws-cdk/cdk-build-tools": "0.0.0",
"@types/semver": "^7.5.8",
"@types/yargs": "^15.0.19",
"@aws-cdk/pkglint": "0.0.0",
"@types/fs-extra": "^9.0.13",
"@types/glob": "^7.2.0",
"@types/npm": "^7.19.3",
"@aws-cdk/pkglint": "0.0.0"
"@types/semver": "^7.5.8",
"@types/yargs": "^15.0.19"
},
"dependencies": {
"@aws-sdk/client-cloudformation": "^3.572.0",
"@octokit/rest": "^18.12.0",
"aws-sdk": "^2.1610.0",
"axios": "^1.6.8",
Expand Down
27 changes: 9 additions & 18 deletions packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ class MigrateStack extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);

if (process.env.OMIT_TOPIC !== '1') {
if (process.env.INCLUDE_SINGLE_QUEUE === '1') {
const queue = new sqs.Queue(this, 'Queue', {
removalPolicy: process.env.ORPHAN_TOPIC ? cdk.RemovalPolicy.RETAIN : cdk.RemovalPolicy.DESTROY,
removalPolicy: (process.env.RETAIN_SINGLE_QUEUE === '1') ? cdk.RemovalPolicy.RETAIN : cdk.RemovalPolicy.DESTROY,
});

new cdk.CfnOutput(this, 'QueueName', {
Expand All @@ -199,26 +199,17 @@ class MigrateStack extends cdk.Stack {
new cdk.CfnOutput(this, 'QueueLogicalId', {
value: queue.node.defaultChild.logicalId,
});
}

if (process.env.LARGE_TEMPLATE) {
for (let i = 1; i <= 2; i++) {
const q = new sqs.Queue(this, `cdk-import-queue-test${i}`, {
enforceSSL: true,
removalPolicy: process.env.ORPHAN_TOPIC ? cdk.RemovalPolicy.RETAIN : cdk.RemovalPolicy.DESTROY,
});

new cdk.CfnOutput(this, `QueueLogicalId${i}`, {
value: q.node.defaultChild.logicalId,
});

new cdk.CfnOutput(this, `QueueUrl${i}`, {
value: q.queueUrl,
});
}
if (process.env.LARGE_TEMPLATE === '1') {
for (let i = 1; i <= 70; i++) {
new sqs.Queue(this, `cdk-import-queue-test${i}`, {
enforceSSL: true,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
}
}


if (process.env.SAMPLE_RESOURCES) {
const myTopic = new sns.Topic(this, 'migratetopic1', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promises as fs, existsSync } from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as CFN from '@aws-sdk/client-cloudformation';
import { integTest, cloneDirectory, shell, withDefaultFixture, retry, sleep, randomInteger, withSamIntegrationFixture, RESOURCES_DIR, withCDKMigrateFixture, withExtendedTimeoutFixture } from '../../lib';

jest.setTimeout(2 * 60 * 60_000); // Includes the time to acquire locks, worst-case single-threaded runtime
Expand Down Expand Up @@ -1575,62 +1576,60 @@ integTest('skips notice refresh', withDefaultFixture(async (fixture) => {
}));

/**
* Create a queue with a fresh name, redeploy orphaning the queue, then import it again
* Create a queue, orphan that queue, then import the queue.
*
* We want to test with a large template to make sure large templates can work with import.
*/
integTest('test resource import', withDefaultFixture(async (fixture) => {
// GIVEN
const outputsFile = path.join(fixture.integTestDir, 'outputs', 'outputs.json');
await fs.mkdir(path.dirname(outputsFile), { recursive: true });
const fullStackName = fixture.fullStackName('importable-stack');

// Initial deploy
await fixture.cdkDeploy('importable-stack', {
modEnv: { ORPHAN_TOPIC: '1', LARGE_TEMPLATE: '1' },
modEnv: { LARGE_TEMPLATE: '1', INCLUDE_SINGLE_QUEUE: '1', RETAIN_SINGLE_QUEUE: '1' },
options: ['--outputs-file', outputsFile],
});

const outputs = JSON.parse((await fs.readFile(outputsFile, { encoding: 'utf-8' })).toString());
fixture.log('Setup complete');
try {
// Deploy again, orphaning the queues
await fixture.cdkDeploy('importable-stack', {
modEnv: { OMIT_TOPIC: '1', LARGE_TEMPLATE: '1' },
});

const cfnClient = new CFN.CloudFormationClient();
let queues: { [queueLogicalId: string]: { QueueUrl: string } } = {};
try {
// Write a resource mapping file based on the ID from step one, then run an import
const mappingFile = path.join(fixture.integTestDir, 'outputs', 'mapping.json');
type QueueMap = {
[queueLogicalId: string]: { QueueUrl: string };
};

let queues: QueueMap = {};

for (let i = 1; i <= 2; i++) {
const queueUrl = outputs[fullStackName][`QueueUrl${i}`];
const queueLogicalId = outputs[fullStackName][`QueueLogicalId${i}`];
const fullStackName = fixture.fullStackName('importable-stack');
const queueUrl = outputs[fullStackName].QueueUrl;
const queueLogicalId = outputs[fullStackName].QueueLogicalId;
queues[queueLogicalId] = { QueueUrl: queueUrl };

queues[queueLogicalId] = { QueueUrl: queueUrl };
}
// Remove the queue from the stack but don't delete the queue from AWS
await fixture.cdkDeploy('importable-stack', {
modEnv: { LARGE_TEMPLATE: '1', INCLUDE_SINGLE_QUEUE: '0', RETAIN_SINGLE_QUEUE: '0' },
});
const cfnTemplateBeforeImport = await cfnClient.send(new CFN.GetTemplateCommand({ StackName: fullStackName }));
expect(cfnTemplateBeforeImport.TemplateBody).not.toContain(queueLogicalId);

const jsonFile = JSON.stringify(queues);
await fs.writeFile(
mappingFile,
jsonFile,
JSON.stringify(queues),
{ encoding: 'utf-8' },
);

// I AM NOT SURE IF SYNTH IS ACTUALLY NEEDED HERE
let out = await fixture.cdkSynth({ modEnv: { OMIT_TOPIC: '0', LARGE_TEMPLATE: '1' } });
// eslint-disable-next-line no-console
console.log(out);

out = await fixture.cdk(
// WHEN
await fixture.cdk(
['import', '-m', mappingFile, fixture.fullStackName('importable-stack')],
{ modEnv: { OMIT_TOPIC: '0', LARGE_TEMPLATE: '1' } },
{ modEnv: { LARGE_TEMPLATE: '1', INCLUDE_SINGLE_QUEUE: '1', RETAIN_SINGLE_QUEUE: '0' } },
);
// eslint-disable-next-line no-console
console.log(out);

// THEN
const describeStacksResponse = await cfnClient.send(new CFN.DescribeStacksCommand({ StackName: fullStackName }));
const cfnTemplateAfterImport = await cfnClient.send(new CFN.GetTemplateCommand({ StackName: fullStackName }));
expect(cfnTemplateAfterImport.TemplateBody).toContain(queueLogicalId);
expect(describeStacksResponse.Stacks![0].StackStatus).toEqual('IMPORT_COMPLETE');
expect(cfnTemplateAfterImport.TemplateBody).toContain(queueLogicalId);
} finally {
// Cleanup
// Clean up
await fixture.cdkDestroy('importable-stack');
}
}));
Expand All @@ -1641,10 +1640,9 @@ integTest('test migrate deployment for app with localfile source in migrate.json

// Initial deploy
await fixture.cdkDeploy('migrate-stack', {
modEnv: { ORPHAN_TOPIC: '1' },
modEnv: { LARGE_TEMPLATE: '0', RETAIN_SINGLE_QUEUE: '1', INCLUDE_SINGLE_QUEUE: '1' },
options: ['--outputs-file', outputsFile],
});

const outputs = JSON.parse((await fs.readFile(outputsFile, { encoding: 'utf-8' })).toString());
const stackName = fixture.fullStackName('migrate-stack');
const queueName = outputs[stackName].QueueName;
Expand All @@ -1667,7 +1665,7 @@ integTest('test migrate deployment for app with localfile source in migrate.json
// Create new stack from existing queue
try {
fixture.log(`Deploying new stack ${fixture.fullStackName}, migrating ${queueName} into stack`);
await fixture.cdkDeploy('migrate-stack');
await fixture.cdkDeploy('migrate-stack', { modEnv: { LARGE_TEMPLATE: '0', RETAIN_SINGLE_QUEUE: '0', INCLUDE_SINGLE_QUEUE: '1' } });
} finally {
// Cleanup
await fixture.cdkDestroy('migrate-stack');
Expand Down
Loading

0 comments on commit a6f2c82

Please sign in to comment.