|
| 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.Alerts; |
| 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.Alerts |
| 29 | +{ |
| 30 | + public class AddAzureRmLogAlertRuleTests |
| 31 | + { |
| 32 | + private readonly AddAzureRmLogAlertRuleCommand cmdlet; |
| 33 | + private readonly Mock<InsightsManagementClient> insightsManagementClientMock; |
| 34 | + private readonly Mock<IAlertOperations> insightsAlertRuleOperationsMock; |
| 35 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 36 | + private AzureOperationResponse response; |
| 37 | + private string resourceGroup; |
| 38 | + private RuleCreateOrUpdateParameters createOrUpdatePrms; |
| 39 | + |
| 40 | + public AddAzureRmLogAlertRuleTests() |
| 41 | + { |
| 42 | + insightsAlertRuleOperationsMock = new Mock<IAlertOperations>(); |
| 43 | + insightsManagementClientMock = new Mock<InsightsManagementClient>(); |
| 44 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 45 | + cmdlet = new AddAzureRmLogAlertRuleCommand() |
| 46 | + { |
| 47 | + CommandRuntime = commandRuntimeMock.Object, |
| 48 | + InsightsManagementClient = insightsManagementClientMock.Object |
| 49 | + }; |
| 50 | + |
| 51 | + response = new AzureOperationResponse() |
| 52 | + { |
| 53 | + RequestId = Guid.NewGuid().ToString(), |
| 54 | + StatusCode = HttpStatusCode.OK, |
| 55 | + }; |
| 56 | + |
| 57 | + insightsAlertRuleOperationsMock.Setup(f => f.CreateOrUpdateRuleAsync(It.IsAny<string>(), It.IsAny<RuleCreateOrUpdateParameters>(), It.IsAny<CancellationToken>())) |
| 58 | + .Returns(Task.FromResult<AzureOperationResponse>(response)) |
| 59 | + .Callback((string resourceGrp, RuleCreateOrUpdateParameters createOrUpdateParams, CancellationToken t) => |
| 60 | + { |
| 61 | + resourceGroup = resourceGrp; |
| 62 | + createOrUpdatePrms = createOrUpdateParams; |
| 63 | + }); |
| 64 | + |
| 65 | + insightsManagementClientMock.SetupGet(f => f.AlertOperations).Returns(this.insightsAlertRuleOperationsMock.Object); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 70 | + public void AddAlertRuleCommandParametersProcessing() |
| 71 | + { |
| 72 | + // Null actions |
| 73 | + cmdlet.Name = Utilities.Name; |
| 74 | + cmdlet.Location = "East US"; |
| 75 | + cmdlet.ResourceGroup = Utilities.ResourceGroup; |
| 76 | + cmdlet.TargetResourceId = "/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo"; |
| 77 | + cmdlet.Actions = null; |
| 78 | + |
| 79 | + cmdlet.ExecuteCmdlet(); |
| 80 | + |
| 81 | + this.AssertResult( |
| 82 | + location: "East US", |
| 83 | + tagsKey: "hidden-link:/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo", |
| 84 | + isEnabled: true, |
| 85 | + actionsNull: true, |
| 86 | + actionsCount: 0); |
| 87 | + |
| 88 | + // Null actions and disabled |
| 89 | + cmdlet.DisableRule = true; |
| 90 | + |
| 91 | + cmdlet.ExecuteCmdlet(); |
| 92 | + |
| 93 | + this.AssertResult( |
| 94 | + location: "East US", |
| 95 | + tagsKey: "hidden-link:/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo", |
| 96 | + isEnabled: false, |
| 97 | + actionsNull: true, |
| 98 | + actionsCount: 0); |
| 99 | + |
| 100 | + // Empty actions |
| 101 | + cmdlet.DisableRule = false; |
| 102 | + cmdlet.Actions = new List<RuleAction>(); |
| 103 | + |
| 104 | + cmdlet.ExecuteCmdlet(); |
| 105 | + |
| 106 | + this.AssertResult( |
| 107 | + location: "East US", |
| 108 | + tagsKey: "hidden-link:/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo", |
| 109 | + isEnabled: true, |
| 110 | + actionsNull: false, |
| 111 | + actionsCount: 0); |
| 112 | + |
| 113 | + // Non-empty actions (one action) |
| 114 | + List<string> eMails = new List<string>(); |
| 115 | + |
| 116 | + RuleAction ruleAction = new RuleEmailAction |
| 117 | + { |
| 118 | + SendToServiceOwners = true, |
| 119 | + CustomEmails = eMails |
| 120 | + }; |
| 121 | + |
| 122 | + cmdlet.Actions.Add(ruleAction); |
| 123 | + |
| 124 | + cmdlet.ExecuteCmdlet(); |
| 125 | + |
| 126 | + this.AssertResult( |
| 127 | + location: "East US", |
| 128 | + tagsKey: "hidden-link:/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo", |
| 129 | + isEnabled: true, |
| 130 | + actionsNull: false, |
| 131 | + actionsCount: 1); |
| 132 | + |
| 133 | + // Non-empty actions (two actions) |
| 134 | + var properties = new Dictionary<string, string>(); |
| 135 | + properties.Add("hello", "goodbye"); |
| 136 | + ruleAction = new RuleWebhookAction |
| 137 | + { |
| 138 | + ServiceUri = "http://bueno.net", |
| 139 | + Properties = properties |
| 140 | + }; |
| 141 | + |
| 142 | + cmdlet.Actions.Add(ruleAction); |
| 143 | + |
| 144 | + cmdlet.ExecuteCmdlet(); |
| 145 | + |
| 146 | + this.AssertResult( |
| 147 | + location: "East US", |
| 148 | + tagsKey: "hidden-link:/subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/misitiooeltuyo", |
| 149 | + isEnabled: true, |
| 150 | + actionsNull: false, |
| 151 | + actionsCount: 2); |
| 152 | + } |
| 153 | + |
| 154 | + private void AssertResult( |
| 155 | + string location, |
| 156 | + string tagsKey, |
| 157 | + bool isEnabled, |
| 158 | + bool actionsNull, |
| 159 | + int actionsCount |
| 160 | + ) |
| 161 | + { |
| 162 | + Assert.Equal(Utilities.ResourceGroup, this.resourceGroup); |
| 163 | + Assert.Equal(location, this.createOrUpdatePrms.Location); |
| 164 | + Assert.True(this.createOrUpdatePrms.Tags.ContainsKey(tagsKey)); |
| 165 | + |
| 166 | + Assert.NotNull(this.createOrUpdatePrms); |
| 167 | + |
| 168 | + if (actionsNull) |
| 169 | + { |
| 170 | + Assert.Null(this.createOrUpdatePrms.Properties.Actions); |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + Assert.NotNull(this.createOrUpdatePrms.Properties.Actions); |
| 175 | + Assert.Equal(actionsCount, this.createOrUpdatePrms.Properties.Actions.Count); |
| 176 | + } |
| 177 | + |
| 178 | + Assert.Equal(Utilities.Name, this.createOrUpdatePrms.Properties.Name); |
| 179 | + Assert.Equal(isEnabled, this.createOrUpdatePrms.Properties.IsEnabled); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments