|
| 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 | +} |
0 commit comments