Skip to content

Commit ac3dfae

Browse files
committed
Merge pull request #269 from AuxMon/autoscaleAndTests
Autoscale cmdlets and tests
2 parents ad866cf + bcb3005 commit ac3dfae

File tree

43 files changed

+5384
-860
lines changed

Some content is hidden

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

43 files changed

+5384
-860
lines changed

src/ResourceManager/Insights/Commands.Insights.Test/Alerts/AddAlertRuleCommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void AddAlertRuleCommandParametersProcessing()
7474
cmdlet.ResourceGroup = Utilities.ResourceGroup;
7575
cmdlet.Operator = ConditionOperator.GreaterThan;
7676
cmdlet.Threshold = 1;
77-
cmdlet.ResourceUri = "/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo";
77+
cmdlet.ResourceId = "/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo";
7878
cmdlet.MetricName = "Requests";
7979
cmdlet.TimeAggregationOperator = TimeAggregationOperator.Total;
8080
cmdlet.CustomEmails = new string[] {"[email protected],[email protected]"};
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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.Collections.Generic;
17+
using System.Management.Automation;
18+
using System.Net;
19+
using System.Threading;
20+
using System.Threading.Tasks;
21+
using Hyak.Common;
22+
using Microsoft.Azure.Commands.Insights.Autoscale;
23+
using Microsoft.Azure.Commands.Insights.OutputClasses;
24+
using Microsoft.Azure.Management.Insights;
25+
using Microsoft.Azure.Management.Insights.Models;
26+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
27+
using Moq;
28+
using Xunit;
29+
30+
namespace Microsoft.Azure.Commands.Insights.Test.Autoscale
31+
{
32+
public class AddAutoscaleSettingCommandTests
33+
{
34+
private readonly AddAutoscaleSettingCommand cmdlet;
35+
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
36+
private readonly Mock<IAutoscaleOperations> insightsAutoscaleOperationsMock;
37+
private Mock<ICommandRuntime> commandRuntimeMock;
38+
private AzureOperationResponse response;
39+
private string resourceGroup;
40+
private string settingName;
41+
private AutoscaleSettingCreateOrUpdateParameters createOrUpdatePrms;
42+
43+
public AddAutoscaleSettingCommandTests()
44+
{
45+
insightsAutoscaleOperationsMock = new Mock<IAutoscaleOperations>();
46+
insightsManagementClientMock = new Mock<InsightsManagementClient>();
47+
commandRuntimeMock = new Mock<ICommandRuntime>();
48+
cmdlet = new AddAutoscaleSettingCommand()
49+
{
50+
CommandRuntime = commandRuntimeMock.Object,
51+
InsightsManagementClient = insightsManagementClientMock.Object
52+
};
53+
54+
response = new AzureOperationResponse()
55+
{
56+
RequestId = Guid.NewGuid().ToString(),
57+
StatusCode = HttpStatusCode.OK,
58+
};
59+
60+
insightsAutoscaleOperationsMock.Setup(f => f.CreateOrUpdateSettingAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<AutoscaleSettingCreateOrUpdateParameters>(), It.IsAny<CancellationToken>()))
61+
.Returns(Task.FromResult<AzureOperationResponse>(response))
62+
.Callback((string resourceGrp, string settingNm, AutoscaleSettingCreateOrUpdateParameters createOrUpdateParams, CancellationToken t) =>
63+
{
64+
resourceGroup = resourceGrp;
65+
settingName = settingNm;
66+
createOrUpdatePrms = createOrUpdateParams;
67+
});
68+
69+
insightsManagementClientMock.SetupGet(f => f.AutoscaleOperations).Returns(this.insightsAutoscaleOperationsMock.Object);
70+
}
71+
72+
[Fact]
73+
[Trait(Category.AcceptanceType, Category.CheckIn)]
74+
public void AddAutoscaleSettingCommandParametersProcessing()
75+
{
76+
var spec = this.CreateCompleteSpec(location: "East US", name: "SettingName", profiles: null);
77+
var autoscaleRules = new List<ScaleRule> {this.CreateAutoscaleRule("IncommingReq")};
78+
var autoscaleProfile = new List<AutoscaleProfile> {this.CreateAutoscaleProfile(autoscaleRules: autoscaleRules, fixedDate: true)};
79+
80+
// Testing with a complete spec as parameter (Update semantics)
81+
// Add-AutoscaleSetting -SettingSpec <AutoscaleSettingResource> -ResourceGroup <String> [-DisableSetting [<SwitchParameter>]] [-AutoscaleProfiles <List[AutoscaleProfile]>] [-Profile <AzureProfile>] [<CommonParameters>]
82+
// Add-AutoscaleSetting -SettingSpec $spec -ResourceGroup $Utilities.ResourceGroup
83+
// A NOP
84+
cmdlet.SettingSpec = spec;
85+
cmdlet.ResourceGroup = Utilities.ResourceGroup;
86+
cmdlet.ExecuteCmdlet();
87+
88+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
89+
Assert.Equal("SettingName", this.settingName);
90+
Assert.NotNull(this.createOrUpdatePrms);
91+
92+
// Add-AutoscaleSetting -SettingSpec <AutoscaleSettingResource> -ResourceGroup <String> [-DisableSetting [<SwitchParameter>]] [-AutoscaleProfiles <List[AutoscaleProfile]>] [-Profile <AzureProfile>] [<CommonParameters>]
93+
// Add-AutoscaleSetting -SettingSpec $spec -ResourceGroup $Utilities.ResourceGroup -DisableSetting
94+
// Disable the setting
95+
cmdlet.DisableSetting = true;
96+
cmdlet.ExecuteCmdlet();
97+
98+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
99+
Assert.Equal("SettingName", this.settingName);
100+
Assert.NotNull(this.createOrUpdatePrms);
101+
102+
// Add-AutoscaleSetting -SettingSpec <AutoscaleSettingResource> -ResourceGroup <String> [-DisableSetting [<SwitchParameter>]] [-AutoscaleProfiles <List[AutoscaleProfile]>] [-Profile <AzureProfile>] [<CommonParameters>]
103+
// Adding a profile
104+
cmdlet.AutoscaleProfiles = autoscaleProfile;
105+
cmdlet.ExecuteCmdlet();
106+
107+
// Add-AutoscaleSetting -Location <String> -Name <String> -ResourceGroup <String> [-DisableSetting [<SwitchParameter>]] [-AutoscaleProfiles <List[AutoscaleProfile]>] -TargetResourceId <String> [-Profile <AzureProfile>] [<CommonParameters>]
108+
cmdlet.SettingSpec = null;
109+
cmdlet.Name = "SettingName";
110+
cmdlet.Location = "East US";
111+
cmdlet.ResourceGroup = Utilities.ResourceGroup;
112+
cmdlet.TargetResourceId = Utilities.ResourceUri;
113+
cmdlet.ExecuteCmdlet();
114+
}
115+
116+
private ScaleRule CreateAutoscaleRule(string metricName = null)
117+
{
118+
var autocaseRuleCmd = new NewAutoscaleRuleCommand
119+
{
120+
MetricName = metricName ?? "Requests",
121+
MetricResourceId = "/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo",
122+
Operator = ComparisonOperationType.GreaterThan,
123+
MetricStatistic = MetricStatisticType.Average,
124+
Threshold = 10,
125+
TimeGrain = TimeSpan.FromMinutes(1),
126+
ScaleActionCooldown = TimeSpan.FromMinutes(5),
127+
ScaleActionDirection = ScaleDirection.Increase,
128+
ScaleActionScaleType = ScaleType.ChangeCount,
129+
ScaleActionValue = "1"
130+
};
131+
132+
return autocaseRuleCmd.CreateSettingRule();
133+
}
134+
135+
private AutoscaleProfile CreateAutoscaleProfile(List<ScaleRule> autoscaleRules = null, bool fixedDate = true)
136+
{
137+
var autoscaleProfileCmd = new NewAutoscaleProfileCommandTests();
138+
139+
autoscaleProfileCmd.InitializeAutoscaleProfile(autoscaleRules);
140+
if (fixedDate)
141+
{
142+
autoscaleProfileCmd.InitializeForFixedDate();
143+
}
144+
else
145+
{
146+
autoscaleProfileCmd.InitializeForRecurrentSchedule();
147+
}
148+
149+
return autoscaleProfileCmd.Cmdlet.CreateSettingProfile();
150+
}
151+
152+
private AutoscaleSettingResource CreateCompleteSpec(string location, string name, List<AutoscaleProfile> profiles = null)
153+
{
154+
if (profiles == null)
155+
{
156+
profiles = new List<AutoscaleProfile>() { this.CreateAutoscaleProfile() };
157+
}
158+
159+
return new AutoscaleSettingResource
160+
{
161+
Location = location,
162+
Name = name,
163+
Properties = new AutoscaleSetting
164+
{
165+
Name = name,
166+
Enabled = true,
167+
Profiles = profiles,
168+
TargetResourceUri = Utilities.ResourceUri
169+
},
170+
Tags = new LazyDictionary<string, string>()
171+
};
172+
}
173+
}
174+
}
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.Autoscale;
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.Autoscale
27+
{
28+
public class GetAutoscaleHistoryCommandTests
29+
{
30+
private readonly GetAutoscaleHistoryCommand 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 GetAutoscaleHistoryCommandTests()
39+
{
40+
insightsEventOperationsMock = new Mock<IEventOperations>();
41+
insightsClientMock = new Mock<InsightsClient>();
42+
commandRuntimeMock = new Mock<ICommandRuntime>();
43+
cmdlet = new GetAutoscaleHistoryCommand()
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 GetAutoscaleHistoryCommandParametersProcessing()
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: GetAutoscaleHistoryCommand.AutoscaleEventSourceName,
73+
filter: ref this.filter,
74+
selected: ref this.selected,
75+
startDate: startDate,
76+
response: response);
77+
}
78+
}
79+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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.Collections.Generic;
17+
using System.Management.Automation;
18+
using System.Net;
19+
using System.Threading;
20+
using System.Threading.Tasks;
21+
using Microsoft.Azure.Commands.Insights.Autoscale;
22+
using Microsoft.Azure.Management.Insights;
23+
using Microsoft.Azure.Management.Insights.Models;
24+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
25+
using Moq;
26+
using Xunit;
27+
28+
namespace Microsoft.Azure.Commands.Insights.Test.Autoscale
29+
{
30+
public class GetAutoscaleSettingCommandTests
31+
{
32+
private readonly GetAutoscaleSettingCommand cmdlet;
33+
private readonly Mock<InsightsManagementClient> insightsManagementClientMock;
34+
private readonly Mock<IAutoscaleOperations> insightsAutoscaleOperationsMock;
35+
private Mock<ICommandRuntime> commandRuntimeMock;
36+
private AutoscaleSettingGetResponse response;
37+
private AutoscaleSettingListResponse responseList;
38+
private string resourceGroup;
39+
private string settingName;
40+
private string targetResourceUri = Utilities.ResourceUri;
41+
42+
public GetAutoscaleSettingCommandTests()
43+
{
44+
insightsAutoscaleOperationsMock = new Mock<IAutoscaleOperations>();
45+
insightsManagementClientMock = new Mock<InsightsManagementClient>();
46+
commandRuntimeMock = new Mock<ICommandRuntime>();
47+
cmdlet = new GetAutoscaleSettingCommand()
48+
{
49+
CommandRuntime = commandRuntimeMock.Object,
50+
InsightsManagementClient = insightsManagementClientMock.Object
51+
};
52+
53+
response = new AutoscaleSettingGetResponse()
54+
{
55+
RequestId = Guid.NewGuid().ToString(),
56+
StatusCode = HttpStatusCode.OK,
57+
Id = "",
58+
Location = "",
59+
Name = "",
60+
Properties = null,
61+
Tags = null,
62+
};
63+
64+
responseList = new AutoscaleSettingListResponse()
65+
{
66+
RequestId = Guid.NewGuid().ToString(),
67+
StatusCode = HttpStatusCode.OK,
68+
AutoscaleSettingResourceCollection = new AutoscaleSettingResourceCollection()
69+
{
70+
Value = new List<AutoscaleSettingResource>()
71+
{
72+
new AutoscaleSettingResource(){
73+
Id = "",
74+
Location = "",
75+
Name = "",
76+
Properties = null,
77+
Tags = null,
78+
},
79+
}
80+
}
81+
};
82+
83+
insightsAutoscaleOperationsMock.Setup(f => f.GetSettingAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
84+
.Returns(Task.FromResult<AutoscaleSettingGetResponse>(response))
85+
.Callback((string resourceGrp, string settingNm, CancellationToken t) =>
86+
{
87+
resourceGroup = resourceGrp;
88+
settingName = settingNm;
89+
});
90+
91+
insightsAutoscaleOperationsMock.Setup(f => f.ListSettingsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
92+
.Returns(Task.FromResult<AutoscaleSettingListResponse>(responseList))
93+
.Callback((string resourceGrp, string targetResourceId, CancellationToken t) =>
94+
{
95+
resourceGroup = resourceGrp;
96+
targetResourceUri = targetResourceId;
97+
});
98+
99+
insightsManagementClientMock.SetupGet(f => f.AutoscaleOperations).Returns(this.insightsAutoscaleOperationsMock.Object);
100+
}
101+
102+
[Fact]
103+
[Trait(Category.AcceptanceType, Category.CheckIn)]
104+
public void GetAutoscaleSettingCommandParametersProcessing()
105+
{
106+
// Calling ListSettingsAsync
107+
cmdlet.ResourceGroup = Utilities.ResourceGroup;
108+
cmdlet.ExecuteCmdlet();
109+
110+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
111+
Assert.Null(this.settingName);
112+
Assert.Null(this.targetResourceUri);
113+
114+
// Calling GetSettingAsync
115+
this.resourceGroup = null;
116+
this.targetResourceUri = Utilities.ResourceUri;
117+
cmdlet.Name = Utilities.Name;
118+
cmdlet.ExecuteCmdlet();
119+
120+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
121+
Assert.Equal(Utilities.Name, this.settingName);
122+
Assert.Equal(Utilities.ResourceUri, this.targetResourceUri);
123+
124+
// Test deatiled output flag calling GetSettingAsync
125+
this.resourceGroup = null;
126+
this.settingName = null;
127+
cmdlet.DetailedOutput = true;
128+
cmdlet.ExecuteCmdlet();
129+
130+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
131+
Assert.Equal(Utilities.Name, this.settingName);
132+
Assert.Equal(Utilities.ResourceUri, this.targetResourceUri);
133+
}
134+
}
135+
}

0 commit comments

Comments
 (0)