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

add --keep argument to allow test resources to be kept #5

Merged
merged 1 commit into from
May 16, 2021
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ getOptions() - get options for making requests to AWS

## Running with `jest`

- `yarn jest '--profile=default' '--stack=my-service-dev' --runInBand`
### Arguments
- When running tests with `jest` using `sls-test-tools` matchers there are certain parameters needed for `sls-test-tools` to make assertions.
- These are passed as command line arguments, using quotation to match `jests` convention on test arguments.

**Required**
- `'--stack=my-service-dev'` - the CloudFormation stack name of the stack under test.

**Optional**
- `'--profile=[PROFILE NAME]'` (will default to `default`)
- `'--region=[AWS Region]'` (will default to `eu-west-2`)
- `'--keep=true'` - keeps testing resources up to avoid creation throttles (e.g. SQS Queue created for EventBridge assertions)

- To avoid issues we recommend `--runInBand`

```
import { AWSClient, EventBridge } from "sls-test-tools";
Expand Down
39 changes: 31 additions & 8 deletions src/helpers/eventBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ export default class EventBridge {
async init(eventBridgeName) {
this.eventBridgeClient = new AWSClient.EventBridge();
this.eventBridgeName = eventBridgeName;
const keepArg = process.argv.filter((x) => x.startsWith("--keep="))[0];
this.keep = keepArg ? keepArg.split("=")[1] : false;
this.sqsClient = new AWSClient.SQS();
if (!this.keep) {
console.info("If running repeatedly add '--keep=true' to keep testing resources up to avoid creation throttles")
}
const queueName = `${eventBridgeName}-testing-queue`;
const queueResult = await this.sqsClient
.createQueue({
Expand Down Expand Up @@ -87,6 +92,8 @@ export default class EventBridge {
})
.promise();

await this.getEvents(); // need to clear this manual published event from the SQS observer queue.
agwhi marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we always want to delete the events?

It could be worth splitting this into two functions (eg. peekEvents, popEvents).
We would have to set the VisibilityTimeout to 0 when we create the queue so that peek doesn't change the visibly


return result;
}

Expand All @@ -99,6 +106,19 @@ export default class EventBridge {

const result = await this.sqsClient.receiveMessage(queueParams).promise();

const messageHandlers = result.Messages.map(
agwhi marked this conversation as resolved.
Show resolved Hide resolved
message => ({
Id: message.MessageId,
ReceiptHandle: message.ReceiptHandle,
})
)
if (messageHandlers.length > 0) {
await this.sqsClient.deleteMessageBatch({
Entries : messageHandlers,
QueueUrl: this.QueueUrl,
}).promise();
}

return result;
}

Expand All @@ -108,17 +128,20 @@ export default class EventBridge {
QueueUrl: this.QueueUrl,
})
.promise();

return result;
}

async destroy() {
const result = await this.sqsClient
.deleteQueue({
QueueUrl: this.QueueUrl,
})
.promise();

return result;
if(!this.keep) {
await this.sqsClient
.deleteQueue({
QueueUrl: this.QueueUrl,
})
.promise();
} else {
await this.clear();
}

return true;
}
}