Skip to content

Autoscale cmdlets and tests #269

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 2 commits into from
Mar 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void AddAlertRuleCommandParametersProcessing()
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.ResourceId = "/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[] {"[email protected],[email protected]"};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// ----------------------------------------------------------------------------------
//
// 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.Collections.Generic;
using System.Management.Automation;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Hyak.Common;
using Microsoft.Azure.Commands.Insights.Autoscale;
using Microsoft.Azure.Commands.Insights.OutputClasses;
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.Autoscale
{
public class AddAutoscaleSettingCommandTests
{
private readonly AddAutoscaleSettingCommand cmdlet;
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
private readonly Mock<IAutoscaleOperations> insightsAutoscaleOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private AzureOperationResponse response;
private string resourceGroup;
private string settingName;
private AutoscaleSettingCreateOrUpdateParameters createOrUpdatePrms;

public AddAutoscaleSettingCommandTests()
{
insightsAutoscaleOperationsMock = new Mock<IAutoscaleOperations>();
insightsManagementClientMock = new Mock<InsightsManagementClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new AddAutoscaleSettingCommand()
{
CommandRuntime = commandRuntimeMock.Object,
InsightsManagementClient = insightsManagementClientMock.Object
};

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

insightsAutoscaleOperationsMock.Setup(f => f.CreateOrUpdateSettingAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<AutoscaleSettingCreateOrUpdateParameters>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AzureOperationResponse>(response))
.Callback((string resourceGrp, string settingNm, AutoscaleSettingCreateOrUpdateParameters createOrUpdateParams, CancellationToken t) =>
{
resourceGroup = resourceGrp;
settingName = settingNm;
createOrUpdatePrms = createOrUpdateParams;
});

insightsManagementClientMock.SetupGet(f => f.AutoscaleOperations).Returns(this.insightsAutoscaleOperationsMock.Object);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void AddAutoscaleSettingCommandParametersProcessing()
{
var spec = this.CreateCompleteSpec(location: "East US", name: "SettingName", profiles: null);
var autoscaleRules = new List<ScaleRule> {this.CreateAutoscaleRule("IncommingReq")};
var autoscaleProfile = new List<AutoscaleProfile> {this.CreateAutoscaleProfile(autoscaleRules: autoscaleRules, fixedDate: true)};

// Testing with a complete spec as parameter (Update semantics)
// Add-AutoscaleSetting -SettingSpec <AutoscaleSettingResource> -ResourceGroup <String> [-DisableSetting [<SwitchParameter>]] [-AutoscaleProfiles <List[AutoscaleProfile]>] [-Profile <AzureProfile>] [<CommonParameters>]
// Add-AutoscaleSetting -SettingSpec $spec -ResourceGroup $Utilities.ResourceGroup
// A NOP
cmdlet.SettingSpec = spec;
cmdlet.ResourceGroup = Utilities.ResourceGroup;
cmdlet.ExecuteCmdlet();

Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
Assert.Equal("SettingName", this.settingName);
Assert.NotNull(this.createOrUpdatePrms);

// Add-AutoscaleSetting -SettingSpec <AutoscaleSettingResource> -ResourceGroup <String> [-DisableSetting [<SwitchParameter>]] [-AutoscaleProfiles <List[AutoscaleProfile]>] [-Profile <AzureProfile>] [<CommonParameters>]
// Add-AutoscaleSetting -SettingSpec $spec -ResourceGroup $Utilities.ResourceGroup -DisableSetting
// Disable the setting
cmdlet.DisableSetting = true;
cmdlet.ExecuteCmdlet();

Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
Assert.Equal("SettingName", this.settingName);
Assert.NotNull(this.createOrUpdatePrms);

// Add-AutoscaleSetting -SettingSpec <AutoscaleSettingResource> -ResourceGroup <String> [-DisableSetting [<SwitchParameter>]] [-AutoscaleProfiles <List[AutoscaleProfile]>] [-Profile <AzureProfile>] [<CommonParameters>]
// Adding a profile
cmdlet.AutoscaleProfiles = autoscaleProfile;
cmdlet.ExecuteCmdlet();

// Add-AutoscaleSetting -Location <String> -Name <String> -ResourceGroup <String> [-DisableSetting [<SwitchParameter>]] [-AutoscaleProfiles <List[AutoscaleProfile]>] -TargetResourceId <String> [-Profile <AzureProfile>] [<CommonParameters>]
cmdlet.SettingSpec = null;
cmdlet.Name = "SettingName";
cmdlet.Location = "East US";
cmdlet.ResourceGroup = Utilities.ResourceGroup;
cmdlet.TargetResourceId = Utilities.ResourceUri;
cmdlet.ExecuteCmdlet();
}

private ScaleRule CreateAutoscaleRule(string metricName = null)
{
var autocaseRuleCmd = new NewAutoscaleRuleCommand
{
MetricName = metricName ?? "Requests",
MetricResourceId = "/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo",
Operator = ComparisonOperationType.GreaterThan,
MetricStatistic = MetricStatisticType.Average,
Threshold = 10,
TimeGrain = TimeSpan.FromMinutes(1),
ScaleActionCooldown = TimeSpan.FromMinutes(5),
ScaleActionDirection = ScaleDirection.Increase,
ScaleActionScaleType = ScaleType.ChangeCount,
ScaleActionValue = "1"
};

return autocaseRuleCmd.CreateSettingRule();
}

private AutoscaleProfile CreateAutoscaleProfile(List<ScaleRule> autoscaleRules = null, bool fixedDate = true)
{
var autoscaleProfileCmd = new NewAutoscaleProfileCommandTests();

autoscaleProfileCmd.InitializeAutoscaleProfile(autoscaleRules);
if (fixedDate)
{
autoscaleProfileCmd.InitializeForFixedDate();
}
else
{
autoscaleProfileCmd.InitializeForRecurrentSchedule();
}

return autoscaleProfileCmd.Cmdlet.CreateSettingProfile();
}

private AutoscaleSettingResource CreateCompleteSpec(string location, string name, List<AutoscaleProfile> profiles = null)
{
if (profiles == null)
{
profiles = new List<AutoscaleProfile>() { this.CreateAutoscaleProfile() };
}

return new AutoscaleSettingResource
{
Location = location,
Name = name,
Properties = new AutoscaleSetting
{
Name = name,
Enabled = true,
Profiles = profiles,
TargetResourceUri = Utilities.ResourceUri
},
Tags = new LazyDictionary<string, string>()
};
}
}
}
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.Autoscale;
using Microsoft.Azure.Insights;
using Microsoft.Azure.Insights.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Xunit;

namespace Microsoft.Azure.Commands.Insights.Test.Autoscale
{
public class GetAutoscaleHistoryCommandTests
{
private readonly GetAutoscaleHistoryCommand 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 GetAutoscaleHistoryCommandTests()
{
insightsEventOperationsMock = new Mock<IEventOperations>();
insightsClientMock = new Mock<InsightsClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new GetAutoscaleHistoryCommand()
{
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 GetAutoscaleHistoryCommandParametersProcessing()
{
var startDate = DateTime.Now.AddSeconds(-1);

Test.Utilities.ExecuteVerifications(
cmdlet: cmdlet,
insinsightsEventOperationsMockightsClientMock: this.insightsEventOperationsMock,
requiredFieldName: "eventSource",
requiredFieldValue: GetAutoscaleHistoryCommand.AutoscaleEventSourceName,
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,135 @@
// ----------------------------------------------------------------------------------
//
// 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.Collections.Generic;
using System.Management.Automation;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Insights.Autoscale;
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.Autoscale
{
public class GetAutoscaleSettingCommandTests
{
private readonly GetAutoscaleSettingCommand cmdlet;
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
private readonly Mock<IAutoscaleOperations> insightsAutoscaleOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private AutoscaleSettingGetResponse response;
private AutoscaleSettingListResponse responseList;
private string resourceGroup;
private string settingName;
private string targetResourceUri = Utilities.ResourceUri;

public GetAutoscaleSettingCommandTests()
{
insightsAutoscaleOperationsMock = new Mock<IAutoscaleOperations>();
insightsManagementClientMock = new Mock<InsightsManagementClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new GetAutoscaleSettingCommand()
{
CommandRuntime = commandRuntimeMock.Object,
InsightsManagementClient = insightsManagementClientMock.Object
};

response = new AutoscaleSettingGetResponse()
{
RequestId = Guid.NewGuid().ToString(),
StatusCode = HttpStatusCode.OK,
Id = "",
Location = "",
Name = "",
Properties = null,
Tags = null,
};

responseList = new AutoscaleSettingListResponse()
{
RequestId = Guid.NewGuid().ToString(),
StatusCode = HttpStatusCode.OK,
AutoscaleSettingResourceCollection = new AutoscaleSettingResourceCollection()
{
Value = new List<AutoscaleSettingResource>()
{
new AutoscaleSettingResource(){
Id = "",
Location = "",
Name = "",
Properties = null,
Tags = null,
},
}
}
};

insightsAutoscaleOperationsMock.Setup(f => f.GetSettingAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AutoscaleSettingGetResponse>(response))
.Callback((string resourceGrp, string settingNm, CancellationToken t) =>
{
resourceGroup = resourceGrp;
settingName = settingNm;
});

insightsAutoscaleOperationsMock.Setup(f => f.ListSettingsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AutoscaleSettingListResponse>(responseList))
.Callback((string resourceGrp, string targetResourceId, CancellationToken t) =>
{
resourceGroup = resourceGrp;
targetResourceUri = targetResourceId;
});

insightsManagementClientMock.SetupGet(f => f.AutoscaleOperations).Returns(this.insightsAutoscaleOperationsMock.Object);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetAutoscaleSettingCommandParametersProcessing()
{
// Calling ListSettingsAsync
cmdlet.ResourceGroup = Utilities.ResourceGroup;
cmdlet.ExecuteCmdlet();

Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
Assert.Null(this.settingName);
Assert.Null(this.targetResourceUri);

// Calling GetSettingAsync
this.resourceGroup = null;
this.targetResourceUri = Utilities.ResourceUri;
cmdlet.Name = Utilities.Name;
cmdlet.ExecuteCmdlet();

Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
Assert.Equal(Utilities.Name, this.settingName);
Assert.Equal(Utilities.ResourceUri, this.targetResourceUri);

// Test deatiled output flag calling GetSettingAsync
this.resourceGroup = null;
this.settingName = null;
cmdlet.DetailedOutput = true;
cmdlet.ExecuteCmdlet();

Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
Assert.Equal(Utilities.Name, this.settingName);
Assert.Equal(Utilities.ResourceUri, this.targetResourceUri);
}
}
}
Loading