Skip to content

Commit 62e45ce

Browse files
committed
Merge pull request #221 from AuxMon/alertsAndTests
Adding AlertRule Cmdlets and scenario tests
2 parents fad1192 + 0c87433 commit 62e45ce

File tree

63 files changed

+5407
-118
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+5407
-118
lines changed

src/ResourceManager.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.StreamAnalytics.Te
4949
EndProject
5050
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Insights", "ResourceManager\Insights\Commands.Insights\Commands.Insights.csproj", "{DEA446A1-84E2-46CC-B780-EB4AFDE2460E}"
5151
EndProject
52+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Insights.Test", "ResourceManager\Insights\Commands.Insights.Test\Commands.Insights.Test.csproj", "{469F20E0-9D40-41AD-94C3-B47AD15A4C00}"
53+
EndProject
5254
Global
5355
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5456
Debug|Any CPU = Debug|Any CPU
@@ -131,6 +133,10 @@ Global
131133
{DEA446A1-84E2-46CC-B780-EB4AFDE2460E}.Debug|Any CPU.Build.0 = Debug|Any CPU
132134
{DEA446A1-84E2-46CC-B780-EB4AFDE2460E}.Release|Any CPU.ActiveCfg = Release|Any CPU
133135
{DEA446A1-84E2-46CC-B780-EB4AFDE2460E}.Release|Any CPU.Build.0 = Release|Any CPU
136+
{469F20E0-9D40-41AD-94C3-B47AD15A4C00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
137+
{469F20E0-9D40-41AD-94C3-B47AD15A4C00}.Debug|Any CPU.Build.0 = Debug|Any CPU
138+
{469F20E0-9D40-41AD-94C3-B47AD15A4C00}.Release|Any CPU.ActiveCfg = Release|Any CPU
139+
{469F20E0-9D40-41AD-94C3-B47AD15A4C00}.Release|Any CPU.Build.0 = Release|Any CPU
134140
EndGlobalSection
135141
GlobalSection(SolutionProperties) = preSolution
136142
HideSolutionNode = FALSE
@@ -144,5 +150,6 @@ Global
144150
{080B0477-7E52-4455-90AB-23BD13D1B1CE} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
145151
{56ED8C97-53B9-4DF6-ACB5-7E6800105BF8} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
146152
{7E6683BE-ECFF-4709-89EB-1325E9E70512} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
153+
{469F20E0-9D40-41AD-94C3-B47AD15A4C00} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
147154
EndGlobalSection
148155
EndGlobal
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Management.Automation;
17+
using System.Net;
18+
using System.Threading;
19+
using System.Threading.Tasks;
20+
using Microsoft.Azure.Commands.Insights.Alerts;
21+
using Microsoft.Azure.Management.Insights;
22+
using Microsoft.Azure.Management.Insights.Models;
23+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
24+
using Moq;
25+
using Xunit;
26+
27+
namespace Microsoft.Azure.Commands.Insights.Test.Alerts
28+
{
29+
public class AddAlertRuleCommandTests
30+
{
31+
private readonly AddAlertRuleCommand cmdlet;
32+
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
33+
private readonly Mock<IAlertOperations> insightsAlertRuleOperationsMock;
34+
private Mock<ICommandRuntime> commandRuntimeMock;
35+
private AzureOperationResponse response;
36+
private string resourceGroup;
37+
private RuleCreateOrUpdateParameters createOrUpdatePrms;
38+
39+
public AddAlertRuleCommandTests()
40+
{
41+
insightsAlertRuleOperationsMock = new Mock<IAlertOperations>();
42+
insightsManagementClientMock = new Mock<InsightsManagementClient>();
43+
commandRuntimeMock = new Mock<ICommandRuntime>();
44+
cmdlet = new AddAlertRuleCommand()
45+
{
46+
CommandRuntime = commandRuntimeMock.Object,
47+
InsightsManagementClient = insightsManagementClientMock.Object
48+
};
49+
50+
response = new AzureOperationResponse()
51+
{
52+
RequestId = Guid.NewGuid().ToString(),
53+
StatusCode = HttpStatusCode.OK,
54+
};
55+
56+
insightsAlertRuleOperationsMock.Setup(f => f.CreateOrUpdateRuleAsync(It.IsAny<string>(), It.IsAny<RuleCreateOrUpdateParameters>(), It.IsAny<CancellationToken>()))
57+
.Returns(Task.FromResult<AzureOperationResponse>(response))
58+
.Callback((string resourceGrp, RuleCreateOrUpdateParameters createOrUpdateParams, CancellationToken t) =>
59+
{
60+
resourceGroup = resourceGrp;
61+
createOrUpdatePrms = createOrUpdateParams;
62+
});
63+
64+
insightsManagementClientMock.SetupGet(f => f.AlertOperations).Returns(this.insightsAlertRuleOperationsMock.Object);
65+
}
66+
67+
[Fact]
68+
[Trait(Category.AcceptanceType, Category.CheckIn)]
69+
public void AddAlertRuleCommandParametersProcessing()
70+
{
71+
cmdlet.RuleType = AlertRuleTypes.Metric;
72+
cmdlet.Name = Utilities.Name;
73+
cmdlet.Location = "East US";
74+
cmdlet.ResourceGroup = Utilities.ResourceGroup;
75+
cmdlet.Operator = ConditionOperator.GreaterThan;
76+
cmdlet.Threshold = 1;
77+
cmdlet.ResourceUri = "/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo";
78+
cmdlet.MetricName = "Requests";
79+
cmdlet.TimeAggregationOperator = TimeAggregationOperator.Total;
80+
cmdlet.CustomEmails = new string[] {"[email protected],[email protected]"};
81+
82+
cmdlet.ExecuteCmdlet();
83+
84+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
85+
Assert.NotNull(this.createOrUpdatePrms);
86+
}
87+
}
88+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Management.Automation;
17+
using System.Threading;
18+
using System.Threading.Tasks;
19+
using Microsoft.Azure.Commands.Insights.Alerts;
20+
using Microsoft.Azure.Insights;
21+
using Microsoft.Azure.Insights.Models;
22+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
23+
using Moq;
24+
using Xunit;
25+
26+
namespace Microsoft.Azure.Commands.Insights.Test.Alerts
27+
{
28+
public class GetAlertHistoryCommandTests
29+
{
30+
private readonly GetAlertHistoryCommand cmdlet;
31+
private readonly Mock<InsightsClient> insightsClientMock;
32+
private readonly Mock<IEventOperations> insightsEventOperationsMock;
33+
private Mock<ICommandRuntime> commandRuntimeMock;
34+
private EventDataListResponse response;
35+
private string filter;
36+
private string selected;
37+
38+
public GetAlertHistoryCommandTests()
39+
{
40+
insightsEventOperationsMock = new Mock<IEventOperations>();
41+
insightsClientMock = new Mock<InsightsClient>();
42+
commandRuntimeMock = new Mock<ICommandRuntime>();
43+
cmdlet = new GetAlertHistoryCommand()
44+
{
45+
CommandRuntime = commandRuntimeMock.Object,
46+
InsightsClient = insightsClientMock.Object
47+
};
48+
49+
response = Test.Utilities.InitializeResponse();
50+
51+
insightsEventOperationsMock.Setup(f => f.ListEventsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
52+
.Returns(Task.FromResult<EventDataListResponse>(response))
53+
.Callback((string f, string s, CancellationToken t) =>
54+
{
55+
filter = f;
56+
selected = s;
57+
});
58+
59+
insightsClientMock.SetupGet(f => f.EventOperations).Returns(this.insightsEventOperationsMock.Object);
60+
}
61+
62+
[Fact]
63+
[Trait(Category.AcceptanceType, Category.CheckIn)]
64+
public void GetAlertHistoryCommandParametersProcessing()
65+
{
66+
var startDate = DateTime.Now.AddSeconds(-1);
67+
68+
Test.Utilities.ExecuteVerifications(
69+
cmdlet: cmdlet,
70+
insinsightsEventOperationsMockightsClientMock: this.insightsEventOperationsMock,
71+
requiredFieldName: "eventSource",
72+
requiredFieldValue: GetAlertHistoryCommand.AlertsEventSourceName,
73+
filter: ref this.filter,
74+
selected: ref this.selected,
75+
startDate: startDate,
76+
response: response);
77+
}
78+
}
79+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Management.Automation;
16+
using System.Threading;
17+
using System.Threading.Tasks;
18+
using Microsoft.Azure.Commands.Insights.Alerts;
19+
using Microsoft.Azure.Management.Insights;
20+
using Microsoft.Azure.Management.Insights.Models;
21+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
22+
using Moq;
23+
using Xunit;
24+
25+
namespace Microsoft.Azure.Commands.Insights.Test.Alerts
26+
{
27+
public class GetAlertRuleCommandTests
28+
{
29+
private readonly GetAlertRuleCommand cmdlet;
30+
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
31+
private readonly Mock<IAlertOperations> insightsAlertRuleOperationsMock;
32+
private Mock<ICommandRuntime> commandRuntimeMock;
33+
private RuleGetResponse singleResponse;
34+
private RuleListResponse listResponse;
35+
private string resourceGroup;
36+
private string ruleNameOrTargetUri;
37+
38+
public GetAlertRuleCommandTests()
39+
{
40+
insightsAlertRuleOperationsMock = new Mock<IAlertOperations>();
41+
insightsManagementClientMock = new Mock<InsightsManagementClient>();
42+
commandRuntimeMock = new Mock<ICommandRuntime>();
43+
cmdlet = new GetAlertRuleCommand()
44+
{
45+
CommandRuntime = commandRuntimeMock.Object,
46+
InsightsManagementClient = insightsManagementClientMock.Object
47+
};
48+
49+
listResponse = Utilities.InitializeRuleListResponse();
50+
singleResponse = Utilities.InitializeRuleGetResponse();
51+
52+
insightsAlertRuleOperationsMock.Setup(f => f.ListRulesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
53+
.Returns(Task.FromResult<RuleListResponse>(listResponse))
54+
.Callback((string resourceGrp, string nameOrTargetUri, CancellationToken t) =>
55+
{
56+
resourceGroup = resourceGrp;
57+
ruleNameOrTargetUri = nameOrTargetUri;
58+
});
59+
60+
insightsAlertRuleOperationsMock.Setup(f => f.GetRuleAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
61+
.Returns(Task.FromResult<RuleGetResponse>(singleResponse))
62+
.Callback((string resourceGrp, string nameOrTargetUri, CancellationToken t) =>
63+
{
64+
resourceGroup = resourceGrp;
65+
ruleNameOrTargetUri = nameOrTargetUri;
66+
});
67+
68+
insightsManagementClientMock.SetupGet(f => f.AlertOperations).Returns(this.insightsAlertRuleOperationsMock.Object);
69+
}
70+
71+
[Fact]
72+
[Trait(Category.AcceptanceType, Category.CheckIn)]
73+
public void GetAlertRuleCommandParametersProcessing()
74+
{
75+
// Setting required parameter
76+
cmdlet.ResourceGroup = Utilities.ResourceGroup;
77+
78+
Utilities.ExecuteVerifications(
79+
cmdlet: cmdlet,
80+
expectedResourceGroup: Utilities.ResourceGroup,
81+
resourceGroup: ref this.resourceGroup,
82+
nameOrTargetUri: ref this.ruleNameOrTargetUri);
83+
}
84+
}
85+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Management.Automation;
17+
using System.Net;
18+
using System.Threading;
19+
using System.Threading.Tasks;
20+
using Microsoft.Azure.Commands.Insights.Alerts;
21+
using Microsoft.Azure.Management.Insights;
22+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
23+
using Moq;
24+
using Xunit;
25+
26+
namespace Microsoft.Azure.Commands.Insights.Test.Alerts
27+
{
28+
public class RemoveAlertRuleCommandTests
29+
{
30+
private readonly RemoveAlertRuleCommand cmdlet;
31+
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
32+
private readonly Mock<IAlertOperations> insightsAlertRuleOperationsMock;
33+
private Mock<ICommandRuntime> commandRuntimeMock;
34+
private AzureOperationResponse response;
35+
private string resourceGroup;
36+
private string ruleNameOrTargetUri;
37+
38+
public RemoveAlertRuleCommandTests()
39+
{
40+
insightsAlertRuleOperationsMock = new Mock<IAlertOperations>();
41+
insightsManagementClientMock = new Mock<InsightsManagementClient>();
42+
commandRuntimeMock = new Mock<ICommandRuntime>();
43+
cmdlet = new RemoveAlertRuleCommand()
44+
{
45+
CommandRuntime = commandRuntimeMock.Object,
46+
InsightsManagementClient = insightsManagementClientMock.Object
47+
};
48+
49+
response = new AzureOperationResponse()
50+
{
51+
RequestId = Guid.NewGuid().ToString(),
52+
StatusCode = HttpStatusCode.OK,
53+
};
54+
55+
insightsAlertRuleOperationsMock.Setup(f => f.DeleteRuleAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
56+
.Returns(Task.FromResult<AzureOperationResponse>(response))
57+
.Callback((string resourceGrp, string ruleName, CancellationToken t) =>
58+
{
59+
resourceGroup = resourceGrp;
60+
ruleNameOrTargetUri = ruleName;
61+
});
62+
63+
insightsManagementClientMock.SetupGet(f => f.AlertOperations).Returns(this.insightsAlertRuleOperationsMock.Object);
64+
}
65+
66+
[Fact]
67+
[Trait(Category.AcceptanceType, Category.CheckIn)]
68+
public void RemoveAlertRuleCommandParametersProcessing()
69+
{
70+
cmdlet.ResourceGroup = Utilities.ResourceGroup;
71+
cmdlet.Name = Utilities.Name;
72+
73+
cmdlet.ExecuteCmdlet();
74+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
75+
Assert.Equal(Utilities.Name, this.ruleNameOrTargetUri);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)