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

Alert Monitor Alert - Move Rest Client to Common #7738

Merged
merged 7 commits into from
Jul 24, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"loc.messages.UpdatedRule": "Updated rule : '%s'",
"loc.messages.Couldnotfetchaccesstoken": "Could not fetch access token for Azure. Status Code: %s (%s) %s.",
"loc.messages.SPNExpiredCheck": "Check if the SPN is valid and not expired.",
"loc.messages.MSINotSupported": "Managed Service Identity(MSI) authentication is not supported for this task."
"loc.messages.FailedToGetAzureMetricAlerts": "Failed to get Application Insights Alert rule: %s. Error: %s",
"loc.messages.FailedToUpdateAzureMetricAlerts": "Failed to update Azure metric alert rule '%s' Resource. Error: %s"
}
36 changes: 3 additions & 33 deletions Tasks/AzureMonitorAlertsV0/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,50 +45,20 @@ describe("AzureMonitorAlerts Suite", () => {
tmr.run();

assert(tmr.stderr.length > 0 || tmr.errorIssues.length > 0, "should have written to stderr");
let expectedError = "Error: Failed request: (504)";
let expectedError = "Error: loc_mock_FailedToGetResourceID testResource.provider/type testResourceName failed (CODE: 501)";
assert(tmr.stdErrContained(expectedError) || tmr.createdErrorIssue(expectedError), "should have thrown proper error message");
assert(tmr.failed, "task should have failed");
done();
});

it("successfully updates rules with email notifications", (done: MochaDone) => {
let tmr : MockTestRunner = new MockTestRunner(path.join(__dirname, "L0AlertRuleWithEmailSuccess.js"));
tmr.run();

assert(tmr.stderr.length == 0 && tmr.errorIssues.length == 0, "should not have written to stderr");
assert(tmr.succeeded, "task should have succeeded");
done();
});

it("fails if PUT request to add alert rule fails", (done: MochaDone) => {
let tmr : MockTestRunner = new MockTestRunner(path.join(__dirname, "L0AddAlertRuleFails.js"));
tmr.run();

assert(tmr.stderr.length > 0 || tmr.errorIssues.length > 0, "should have written to stderr");
let expectedError = "Error: Failed request: (504)";
let expectedError = "Error: loc_mock_FailedToUpdateAzureMetricAlerts Rule1 failed (CODE: 501)";
assert(tmr.stdErrContained(expectedError) || tmr.createdErrorIssue(expectedError), "should have thrown proper error message");
assert(tmr.failed, "task should have failed");
done();
});

it("successfully fetches access token", (done: MochaDone) => {
let tmr : MockTestRunner = new MockTestRunner(path.join(__dirname, "L0AccessTokenFetchSuccess.js"));
tmr.run();

assert(tmr.stderr.length === 0 && tmr.errorIssues.length === 0, "should not have written to stderr");
assert((tmr.stdout.match(/Requesting for Auth Token: https:\/\/login.windows.net\/tenantId\/oauth2\/token\//g) || []).length === 3, "should have made network call to fetch access token thrice");
assert((tmr.stdout.match(/Returning authorization token from cache/g) || []).length === 1, "should have returned access token from cache once");
assert(!tmr.succeeded, "task should have failed");
done();
});

it("fails if task is not able to fetch access token", (done: MochaDone) => {
let tmr : MockTestRunner = new MockTestRunner(path.join(__dirname, "L0AccessTokenFetchFailure.js"));
tmr.run();

assert(tmr.stderr.length > 0 || tmr.errorIssues.length > 0, "should have written to stderr");
let expectedError = "loc_mock_Couldnotfetchaccesstoken 401 loc_mock_SPNExpiredCheck";
assert(tmr.stdErrContained(expectedError) || tmr.createdErrorIssue(expectedError), "should have thrown proper error message")
assert(tmr.failed, "task should have failed");
done();
});
});
30 changes: 0 additions & 30 deletions Tasks/AzureMonitorAlertsV0/Tests/L0AccessTokenFetchFailure.ts

This file was deleted.

74 changes: 0 additions & 74 deletions Tasks/AzureMonitorAlertsV0/Tests/L0AccessTokenFetchSuccess.ts

This file was deleted.

23 changes: 14 additions & 9 deletions Tasks/AzureMonitorAlertsV0/Tests/L0AddAlertRuleFails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ tmr.setInput("ResourceType", "testResource.provider/type");
tmr.setInput("ResourceName", "testResourceName");
tmr.setInput("AlertRules", JSON.stringify(mocks.mockAlertRules));

nock("https://login.windows.net", {
reqheaders: {
"content-type": "application/x-www-form-urlencoded; charset=utf-8"
}
})
.post("/tenantId/oauth2/token/")
.reply(200, {
access_token: "accessToken"
}).persist();

nock("http://example.com", {
reqheaders: {
'authorization': 'Bearer accessToken',
Expand All @@ -26,15 +36,11 @@ nock("http://example.com", {
nock("http://example.com", {
reqheaders: {
"authorization": "Bearer accessToken",
"accept": "application/json",
"content-type": "application/json; charset=utf-8",
"user-agent": "TFS_useragent"
}
})
.get("/subscriptions/sId/resourceGroups/testRg/resources")
.query({
"$filter": "resourceType EQ 'testResource.provider/type' AND name EQ 'testResourceName'" ,
"api-version": "2017-05-10"
})
.get("/subscriptions/sId/resources?$filter=resourceType%20EQ%20%27testResource.provider%2Ftype%27%20AND%20name%20EQ%20%27testResourceName%27&api-version=2016-07-01")
.reply(200, {
value: [{
id: "id",
Expand All @@ -47,16 +53,15 @@ nock("http://example.com", {
nock("http://example.com", {
reqheaders: {
"authorization": "Bearer accessToken",
"accept": "application/json",
"content-type": "application/json; charset=utf-8",
"user-agent": "TFS_useragent"
}
})
.put(/\/subscriptions\/sId\/resourceGroups\/testRg\/providers\/Microsoft.insights\/alertrules\/Rule1/, mocks.getMetricRequestBody("Rule1", "testlocation", "GreaterThan", "metric1", "20", "PT5M"))
.query({
"api-version": "2016-03-01"
})
.reply(504);
.reply(501, 'failed');

tmr.registerMock("./authorizationclient", mocks);
tmr.registerMock("./utility", mocks.getUtilityMock());
tmr.run();
30 changes: 19 additions & 11 deletions Tasks/AzureMonitorAlertsV0/Tests/L0AddAlertsSuccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ tmr.setInput("ResourceType", "testResource.provider/type");
tmr.setInput("ResourceName", "testResourceName");
tmr.setInput("AlertRules", JSON.stringify(mocks.mockAlertRules));

nock("https://login.windows.net", {
reqheaders: {
"content-type": "application/x-www-form-urlencoded; charset=utf-8"
}
})
.post("/tenantId/oauth2/token/")
.reply(200, {
access_token: "accessToken"
}).persist();


nock("http://example.com", {
reqheaders: {
'authorization': 'Bearer accessToken',
Expand All @@ -26,15 +37,11 @@ nock("http://example.com", {
nock("http://example.com", {
reqheaders: {
"authorization": "Bearer accessToken",
"accept": "application/json",
"content-type": "application/json; charset=utf-8",
"user-agent": "TFS_useragent"
}
})
.get("/subscriptions/sId/resourceGroups/testRg/resources")
.query({
"$filter": "resourceType EQ 'testResource.provider/type' AND name EQ 'testResourceName'" ,
"api-version": "2017-05-10"
})
.get("/subscriptions/sId/resources?$filter=resourceType%20EQ%20%27testResource.provider%2Ftype%27%20AND%20name%20EQ%20%27testResourceName%27&api-version=2016-07-01")
.reply(200, {
value: [{
id: "id",
Expand All @@ -44,10 +51,12 @@ nock("http://example.com", {
})
.persist();


console.log(mocks.getMetricRequestBody("Rule1", "testlocation", "GreaterThan", "metric1", "20", "PT5M"));
nock("http://example.com", {
reqheaders: {
"authorization": "Bearer accessToken",
"accept": "application/json",
"content-type": "application/json; charset=utf-8",
"user-agent": "TFS_useragent"
}
})
Expand All @@ -57,10 +66,12 @@ nock("http://example.com", {
})
.reply(201);

console.log(mocks.getMetricRequestBody("Rule2", "testlocation", "LessThanOrEqual", "metric2", "10", "PT10M"));

nock("http://example.com", {
reqheaders: {
"authorization": "Bearer accessToken",
"accept": "application/json",
"content-type": "application/json; charset=utf-8",
"user-agent": "TFS_useragent"
}
})
Expand All @@ -69,7 +80,4 @@ nock("http://example.com", {
"api-version": "2016-03-01"
})
.reply(201);

tmr.registerMock("./authorizationclient", mocks);
tmr.registerMock("./utility", mocks.getUtilityMock());
tmr.run();
24 changes: 15 additions & 9 deletions Tasks/AzureMonitorAlertsV0/Tests/L0AlertRuleGetResourceFail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,36 @@ tmr.setInput("ResourceType", "testResource.provider/type");
tmr.setInput("ResourceName", "testResourceName");
tmr.setInput("AlertRules", JSON.stringify(mocks.mockAlertRules));

nock("https://login.windows.net", {
reqheaders: {
"content-type": "application/x-www-form-urlencoded; charset=utf-8"
}
})
.post("/tenantId/oauth2/token/")
.reply(200, {
access_token: "accessToken"
}).persist();

nock("http://example.com", {
reqheaders: {
'authorization': 'Bearer accessToken',
"accept": "application/json",
"content-type": "application/json; charset=utf-8",
"user-agent": "TFS_useragent"
}
})
.get(/\/subscriptions\/sId\/resourceGroups\/testRg\/providers\/Microsoft.insights\/alertrules\/Rule1/)
.get("/subscriptions/sId/resourceGroups/testRg/providers/microsoft.insights/alertrules/Rule1")
.query({"api-version": "2016-03-01"})
.reply(404);

nock("http://example.com", {
reqheaders: {
"authorization": "Bearer accessToken",
"accept": "application/json",
"content-type": "application/json; charset=utf-8",
"user-agent": "TFS_useragent"
}
})
.get("/subscriptions/sId/resourceGroups/testRg/resources")
.query({
"$filter": "resourceType EQ 'testResource.provider/type' AND name EQ 'testResourceName'" ,
"api-version": "2017-05-10"
})
.reply(504)
.get("/subscriptions/sId/resources?$filter=resourceType%20EQ%20%27testResource.provider%2Ftype%27%20AND%20name%20EQ%20%27testResourceName%27&api-version=2016-07-01")
.reply(501, 'failed')
.persist();

tmr.registerMock("./authorizationclient", mocks);
Expand Down
14 changes: 12 additions & 2 deletions Tasks/AzureMonitorAlertsV0/Tests/L0AlertRuleNameConflictFail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ tmr.setInput("ResourceType", "testResource.provider/type");
tmr.setInput("ResourceName", "testResourceName");
tmr.setInput("AlertRules", JSON.stringify(mocks.mockAlertRules));

nock("https://login.windows.net", {
reqheaders: {
"content-type": "application/x-www-form-urlencoded; charset=utf-8"
}
})
.post("/tenantId/oauth2/token/")
.reply(200, {
access_token: "accessToken"
}).persist();

nock("http://example.com", {
reqheaders: {
'authorization': 'Bearer accessToken',
"accept": "application/json",
"content-type": "application/json; charset=utf-8",
"user-agent": "TFS_useragent"
}
})
.get(/\/subscriptions\/sId\/resourceGroups\/testRg\/providers\/Microsoft.insights\/alertrules\/Rule1/)
.get("/subscriptions/sId/resourceGroups/testRg/providers/microsoft.insights/alertrules/Rule1")
.query({"api-version": "2016-03-01"})
.reply(200, {
id: "id",
Expand Down
Loading