Skip to content

Commit

Permalink
feat(integ-tests): add ability to wait for assertions to pass (#22335)
Browse files Browse the repository at this point in the history
This PR adds a `waitForAssertions` method that allows you to wait until an assertion passes. In order to accomplish this I've copied from the [asynchronous providers](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.custom_resources-readme.html#asynchronous-providers-iscomplete) implementation in the `@aws-cdk/custom-resources` module.

I've updated 3 integration tests to use this functionality to demonstrate the possible use cases.


----

### All Submissions:

* [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
corymhall committed Oct 12, 2022
1 parent b4c2e4d commit 700f9c4
Show file tree
Hide file tree
Showing 46 changed files with 9,213 additions and 121 deletions.
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-autoscaling-hooktargets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@
"devDependencies": {
"@aws-cdk/assertions": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-ssm": "0.0.0",
"@aws-cdk/cdk-build-tools": "0.0.0",
"@aws-cdk/integ-runner": "0.0.0",
"@aws-cdk/integ-tests": "0.0.0",
"@aws-cdk/cfn2ts": "0.0.0",
"@aws-cdk/pkglint": "0.0.0",
"@types/jest": "^27.5.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import * as scaling from '@aws-cdk/aws-autoscaling';
import { Vpc, InstanceType, InstanceClass, InstanceSize, OperatingSystemType, UserData } from '@aws-cdk/aws-ec2';
import { Queue } from '@aws-cdk/aws-sqs';
import { StringParameter } from '@aws-cdk/aws-ssm';
import * as cdk from '@aws-cdk/core';
import { IntegTest, ExpectedResult } from '@aws-cdk/integ-tests';
import { Construct } from 'constructs';
import { QueueHook } from '../lib';

const app = new cdk.App();

class TestStack extends cdk.Stack {
public readonly queueUrl: string;
public readonly groupName: string;
public readonly hookName: string;
constructor(scope: Construct, id: string) {
super(scope, id);

const queue = new Queue(this, 'HookQueue');
this.queueUrl = queue.queueUrl;
const group = new scaling.AutoScalingGroup(this, 'Group', {
vpc: new Vpc(this, 'Vpc'),
maxCapacity: 1,
minCapacity: 0,
instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.SMALL),
machineImage: {
getImage: () => {
return {
osType: OperatingSystemType.LINUX,
userData: UserData.forLinux(),
imageId: StringParameter.fromStringParameterName(this, 'al2022AMI', '/aws/service/ami-amazon-linux-latest/al2022-ami-kernel-default-x86_64').stringValue,
};
},
},
});
this.groupName = group.autoScalingGroupName;
const hook = group.addLifecycleHook('scaleout', {
lifecycleTransition: scaling.LifecycleTransition.INSTANCE_LAUNCHING,
notificationTarget: new QueueHook(queue),
});
this.hookName = hook.lifecycleHookName;

}
}

const testCase = new TestStack(app, 'integ-autoscalinghook-queue');
const integ = new IntegTest(app, 'queue-hook-test', {
testCases: [testCase],
});

const setDesired = integ.assertions.awsApiCall('AutoScaling', 'setDesiredCapacity', {
AutoScalingGroupName: testCase.groupName,
DesiredCapacity: 1,
});


const message = integ.assertions.awsApiCall('SQS', 'receiveMessage', {
QueueUrl: testCase.queueUrl,
});
message.assertAtPath(
'Messages.0.Body.LifecycleTransition',
ExpectedResult.stringLikeRegexp('autoscaling:EC2_INSTANCE_LAUNCHING'),
).waitForAssertions();

const token = message.getAttString('Messages.0.Body.LifecycleActionToken');

const completeAction = integ.assertions.awsApiCall('AutoScaling', 'completeLifecycleAction', {
AutoScalingGroupName: testCase.groupName,
LifecycleActionResult: 'CONTINUE',
LifecycleActionToken: token,
LifecycleHookName: testCase.hookName,
});

setDesired.next(
message.next(
completeAction,
),
);
Loading

0 comments on commit 700f9c4

Please sign in to comment.