Skip to content

fix(e2e): delete cloudwatch alarms in After call #3956

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

Merged
merged 1 commit into from
Sep 15, 2022
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
3 changes: 1 addition & 2 deletions features/cloudwatch/cloudwatch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Feature: Amazon CloudWatch
Given I create a CloudWatch alarm with prefix "aws-js-sdk-alarm"
And I list the CloudWatch alarms
Then the list should contain the CloudWatch alarm
And I delete the CloudWatch alarm

Scenario: Error handling
Given I create a CloudWatch alarm with name ""
Given I create a CloudWatch alarm with prefix ""
Then the error code should be "ValidationError"
74 changes: 28 additions & 46 deletions features/cloudwatch/step_definitions/cloudwatch.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@
const { Before, Given, Then } = require("@cucumber/cucumber");
const { After, Before, Given, Then } = require("@cucumber/cucumber");

Before({ tags: "@cloudwatch" }, function (scenario, callback) {
Before({ tags: "@cloudwatch" }, function () {
const { CloudWatch } = require("../../../clients/client-cloudwatch");
this.service = new CloudWatch({});
callback();
});

Given("I create a CloudWatch alarm with prefix {string}", function (name, callback) {
const timestamp = new Date().getTime();
this.cloudWatchAlarm = {
AlarmName: name,
MetricName: "aws-sdk-js-metric-" + timestamp,
Namespace: "aws-sdk-js-namespace" + timestamp,
ComparisonOperator: "GreaterThanThreshold",
EvaluationPeriods: 5,
Period: 60,
Statistic: "Average",
Threshold: 50.0,
};

this.cloudWatchAlarm.AlarmName += "-" + timestamp;

this.request(null, "putMetricAlarm", this.cloudWatchAlarm, callback, undefined);
After({ tags: "@cloudwatch" }, async function () {
if (this.alarmName) {
await this.service.deleteAlarms({ AlarmNames: [this.alarmName] });
this.alarmName = undefined;
}
});

Given("I create a CloudWatch alarm with name {string}", function (name, callback) {
const timestamp = new Date().getTime();
this.cloudWatchAlarm = {
AlarmName: name,
MetricName: "aws-sdk-js-metric-" + timestamp,
Namespace: "aws-sdk-js-namespace" + timestamp,
ComparisonOperator: "GreaterThanThreshold",
EvaluationPeriods: 5,
Period: 60,
Statistic: "Average",
Threshold: 50.0,
};

this.request(null, "putMetricAlarm", this.cloudWatchAlarm, callback, false);
Given("I create a CloudWatch alarm with prefix {string}", async function (prefix) {
const alarmName = this.uniqueName(prefix);
try {
this.data = await this.service.putMetricAlarm({
AlarmName: alarmName,
ComparisonOperator: "GreaterThanThreshold",
EvaluationPeriods: 5,
MetricName: "CPUUtilization",
Namespace: "AWS/EC2",
Period: 60,
Statistic: "Average",
Threshold: 50.0,
});
this.alarmName = alarmName;
} catch (error) {
this.error = error;
}
});

Given("I list the CloudWatch alarms", function (callback) {
const params = {
MetricName: this.cloudWatchAlarm.MetricName,
Namespace: this.cloudWatchAlarm.Namespace,
};
this.request(null, "describeAlarmsForMetric", params, callback);
Given("I list the CloudWatch alarms", async function () {
this.data = await this.service.describeAlarms({ AlarmNames: [this.alarmName] });
});

Then("the list should contain the CloudWatch alarm", function (callback) {
const name = this.cloudWatchAlarm.AlarmName;
Then("the list should contain the CloudWatch alarm", function () {
const name = this.alarmName;
this.assert.contains(this.data.MetricAlarms, function (alarm) {
return alarm.AlarmName === name;
});
callback();
});

Then("I delete the CloudWatch alarm", function (callback) {
this.request(null, "deleteAlarms", { AlarmNames: [this.cloudWatchAlarm.AlarmName] }, callback);
});