Skip to content

Adding AlertRule Cmdlets and scenario tests #221

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 5 commits into from
Mar 4, 2015
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
7 changes: 7 additions & 0 deletions src/ResourceManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.StreamAnalytics.Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Insights", "ResourceManager\Insights\Commands.Insights\Commands.Insights.csproj", "{DEA446A1-84E2-46CC-B780-EB4AFDE2460E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Insights.Test", "ResourceManager\Insights\Commands.Insights.Test\Commands.Insights.Test.csproj", "{469F20E0-9D40-41AD-94C3-B47AD15A4C00}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -131,6 +133,10 @@ Global
{DEA446A1-84E2-46CC-B780-EB4AFDE2460E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEA446A1-84E2-46CC-B780-EB4AFDE2460E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEA446A1-84E2-46CC-B780-EB4AFDE2460E}.Release|Any CPU.Build.0 = Release|Any CPU
{469F20E0-9D40-41AD-94C3-B47AD15A4C00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{469F20E0-9D40-41AD-94C3-B47AD15A4C00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{469F20E0-9D40-41AD-94C3-B47AD15A4C00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{469F20E0-9D40-41AD-94C3-B47AD15A4C00}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -144,5 +150,6 @@ Global
{080B0477-7E52-4455-90AB-23BD13D1B1CE} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
{56ED8C97-53B9-4DF6-ACB5-7E6800105BF8} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
{7E6683BE-ECFF-4709-89EB-1325E9E70512} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
{469F20E0-9D40-41AD-94C3-B47AD15A4C00} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Management.Automation;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Insights.Alerts;
using Microsoft.Azure.Management.Insights;
using Microsoft.Azure.Management.Insights.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Xunit;

namespace Microsoft.Azure.Commands.Insights.Test.Alerts
{
public class AddAlertRuleCommandTests
{
private readonly AddAlertRuleCommand cmdlet;
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
private readonly Mock<IAlertOperations> insightsAlertRuleOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private AzureOperationResponse response;
private string resourceGroup;
private RuleCreateOrUpdateParameters createOrUpdatePrms;

public AddAlertRuleCommandTests()
{
insightsAlertRuleOperationsMock = new Mock<IAlertOperations>();
insightsManagementClientMock = new Mock<InsightsManagementClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new AddAlertRuleCommand()
{
CommandRuntime = commandRuntimeMock.Object,
InsightsManagementClient = insightsManagementClientMock.Object
};

response = new AzureOperationResponse()
{
RequestId = Guid.NewGuid().ToString(),
StatusCode = HttpStatusCode.OK,
};

insightsAlertRuleOperationsMock.Setup(f => f.CreateOrUpdateRuleAsync(It.IsAny<string>(), It.IsAny<RuleCreateOrUpdateParameters>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AzureOperationResponse>(response))
.Callback((string resourceGrp, RuleCreateOrUpdateParameters createOrUpdateParams, CancellationToken t) =>
{
resourceGroup = resourceGrp;
createOrUpdatePrms = createOrUpdateParams;
});

insightsManagementClientMock.SetupGet(f => f.AlertOperations).Returns(this.insightsAlertRuleOperationsMock.Object);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void AddAlertRuleCommandParametersProcessing()
{
cmdlet.RuleType = AlertRuleTypes.Metric;
cmdlet.Name = Utilities.Name;
cmdlet.Location = "East US";
cmdlet.ResourceGroup = Utilities.ResourceGroup;
cmdlet.Operator = ConditionOperator.GreaterThan;
cmdlet.Threshold = 1;
cmdlet.ResourceUri = "/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo";
cmdlet.MetricName = "Requests";
cmdlet.TimeAggregationOperator = TimeAggregationOperator.Total;
cmdlet.CustomEmails = new string[] {"gu@macrosoft.com,h@dd.com"};

cmdlet.ExecuteCmdlet();

Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
Assert.NotNull(this.createOrUpdatePrms);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Insights.Alerts;
using Microsoft.Azure.Insights;
using Microsoft.Azure.Insights.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Xunit;

namespace Microsoft.Azure.Commands.Insights.Test.Alerts
{
public class GetAlertHistoryCommandTests
{
private readonly GetAlertHistoryCommand cmdlet;
private readonly Mock<InsightsClient> insightsClientMock;
private readonly Mock<IEventOperations> insightsEventOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private EventDataListResponse response;
private string filter;
private string selected;

public GetAlertHistoryCommandTests()
{
insightsEventOperationsMock = new Mock<IEventOperations>();
insightsClientMock = new Mock<InsightsClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new GetAlertHistoryCommand()
{
CommandRuntime = commandRuntimeMock.Object,
InsightsClient = insightsClientMock.Object
};

response = Test.Utilities.InitializeResponse();

insightsEventOperationsMock.Setup(f => f.ListEventsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<EventDataListResponse>(response))
.Callback((string f, string s, CancellationToken t) =>
{
filter = f;
selected = s;
});

insightsClientMock.SetupGet(f => f.EventOperations).Returns(this.insightsEventOperationsMock.Object);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetAlertHistoryCommandParametersProcessing()
{
var startDate = DateTime.Now.AddSeconds(-1);

Test.Utilities.ExecuteVerifications(
cmdlet: cmdlet,
insinsightsEventOperationsMockightsClientMock: this.insightsEventOperationsMock,
requiredFieldName: "eventSource",
requiredFieldValue: GetAlertHistoryCommand.AlertsEventSourceName,
filter: ref this.filter,
selected: ref this.selected,
startDate: startDate,
response: response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Insights.Alerts;
using Microsoft.Azure.Management.Insights;
using Microsoft.Azure.Management.Insights.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Xunit;

namespace Microsoft.Azure.Commands.Insights.Test.Alerts
{
public class GetAlertRuleCommandTests
{
private readonly GetAlertRuleCommand cmdlet;
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
private readonly Mock<IAlertOperations> insightsAlertRuleOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private RuleGetResponse singleResponse;
private RuleListResponse listResponse;
private string resourceGroup;
private string ruleNameOrTargetUri;

public GetAlertRuleCommandTests()
{
insightsAlertRuleOperationsMock = new Mock<IAlertOperations>();
insightsManagementClientMock = new Mock<InsightsManagementClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new GetAlertRuleCommand()
{
CommandRuntime = commandRuntimeMock.Object,
InsightsManagementClient = insightsManagementClientMock.Object
};

listResponse = Utilities.InitializeRuleListResponse();
singleResponse = Utilities.InitializeRuleGetResponse();

insightsAlertRuleOperationsMock.Setup(f => f.ListRulesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<RuleListResponse>(listResponse))
.Callback((string resourceGrp, string nameOrTargetUri, CancellationToken t) =>
{
resourceGroup = resourceGrp;
ruleNameOrTargetUri = nameOrTargetUri;
});

insightsAlertRuleOperationsMock.Setup(f => f.GetRuleAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<RuleGetResponse>(singleResponse))
.Callback((string resourceGrp, string nameOrTargetUri, CancellationToken t) =>
{
resourceGroup = resourceGrp;
ruleNameOrTargetUri = nameOrTargetUri;
});

insightsManagementClientMock.SetupGet(f => f.AlertOperations).Returns(this.insightsAlertRuleOperationsMock.Object);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetAlertRuleCommandParametersProcessing()
{
// Setting required parameter
cmdlet.ResourceGroup = Utilities.ResourceGroup;

Utilities.ExecuteVerifications(
cmdlet: cmdlet,
expectedResourceGroup: Utilities.ResourceGroup,
resourceGroup: ref this.resourceGroup,
nameOrTargetUri: ref this.ruleNameOrTargetUri);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Management.Automation;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Insights.Alerts;
using Microsoft.Azure.Management.Insights;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Xunit;

namespace Microsoft.Azure.Commands.Insights.Test.Alerts
{
public class RemoveAlertRuleCommandTests
{
private readonly RemoveAlertRuleCommand cmdlet;
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
private readonly Mock<IAlertOperations> insightsAlertRuleOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private AzureOperationResponse response;
private string resourceGroup;
private string ruleNameOrTargetUri;

public RemoveAlertRuleCommandTests()
{
insightsAlertRuleOperationsMock = new Mock<IAlertOperations>();
insightsManagementClientMock = new Mock<InsightsManagementClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new RemoveAlertRuleCommand()
{
CommandRuntime = commandRuntimeMock.Object,
InsightsManagementClient = insightsManagementClientMock.Object
};

response = new AzureOperationResponse()
{
RequestId = Guid.NewGuid().ToString(),
StatusCode = HttpStatusCode.OK,
};

insightsAlertRuleOperationsMock.Setup(f => f.DeleteRuleAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AzureOperationResponse>(response))
.Callback((string resourceGrp, string ruleName, CancellationToken t) =>
{
resourceGroup = resourceGrp;
ruleNameOrTargetUri = ruleName;
});

insightsManagementClientMock.SetupGet(f => f.AlertOperations).Returns(this.insightsAlertRuleOperationsMock.Object);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void RemoveAlertRuleCommandParametersProcessing()
{
cmdlet.ResourceGroup = Utilities.ResourceGroup;
cmdlet.Name = Utilities.Name;

cmdlet.ExecuteCmdlet();
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
Assert.Equal(Utilities.Name, this.ruleNameOrTargetUri);
}
}
}
Loading