Exemplo n.º 1
0
        public void NewScheduledQueryRuleCommandParametersProcessing()
        {
            cmdlet.Name = "LogSearchAlertName";
            cmdlet.ResourceGroupName = Utilities.ResourceGroup;
            cmdlet.Location          = Location;
            cmdlet.Description       = "A Log Search Alert";

            Hashtable tags = new Hashtable();

            cmdlet.Tag     = tags;
            cmdlet.Enabled = true;

            ScheduledQueryRuleAznsAction       aznsAction       = new ScheduledQueryRuleAznsAction(new AzNsActionGroup(new string[] { "AG1", "AG2" }, "Email Subject for Log Search Alert", "custom webhook payload"));
            ScheduledQueryRuleLogMetricTrigger logMetricTrigger = new ScheduledQueryRuleLogMetricTrigger(new LogMetricTrigger("GreaterThan", 15, "Total"));
            ScheduledQueryRuleTriggerCondition triggerCondition = new ScheduledQueryRuleTriggerCondition(new TriggerCondition("GreaterThan", 15, logMetricTrigger));
            ScheduledQueryRuleAlertingAction   alertingAction   = new ScheduledQueryRuleAlertingAction(new AlertingAction(severity: "2", aznsAction: aznsAction, trigger: triggerCondition, throttlingInMin: 5));

            cmdlet.Action = new PSScheduledQueryRuleAlertingAction(alertingAction);

            ScheduledQueryRuleSchedule schedule = new ScheduledQueryRuleSchedule(new Schedule(5, 5));

            cmdlet.Schedule = new PSScheduledQueryRuleSchedule(schedule);

            ScheduledQueryRuleSource source = new ScheduledQueryRuleSource(new Source(query: "union *", dataSourceId: "dataSourceId", authorizedResources: new string[] { "authResource1", "authResource2" }, queryType: "ResultCount"));

            cmdlet.Source = new PSScheduledQueryRuleSource(source);

            cmdlet.ExecuteCmdlet();

            Assert.Equal("LogSearchAlertName", this.ruleName);
            Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);

            Assert.NotNull(this.createOrUpdatePrms);

            Assert.Equal("A Log Search Alert", this.createOrUpdatePrms.Description);
            Assert.Equal("true", this.createOrUpdatePrms.Enabled);
            //Assert.Equal(tags, this.createOrUpdatePrms.Tags);

            Assert.Null(this.createOrUpdatePrms.Id);
            Assert.Equal(Location, this.createOrUpdatePrms.Location);

            Assert.NotNull(this.createOrUpdatePrms.Action);

            Assert.NotNull(this.createOrUpdatePrms.Schedule);
            Assert.Equal(5, this.createOrUpdatePrms.Schedule.FrequencyInMinutes);
            Assert.Equal(5, this.createOrUpdatePrms.Schedule.TimeWindowInMinutes);

            Assert.NotNull(this.createOrUpdatePrms.Source);
            Assert.Equal("union *", this.createOrUpdatePrms.Source.Query);
            Assert.Equal("dataSourceId", this.createOrUpdatePrms.Source.DataSourceId);
            Assert.Equal(new string[] { "authResource1", "authResource2" }, this.createOrUpdatePrms.Source.AuthorizedResources);
            Assert.Equal("ResultCount", this.createOrUpdatePrms.Source.QueryType);
        }
        public SetScheduledQueryRuleTests(Xunit.Abstractions.ITestOutputHelper output)
        {
            ServiceManagement.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagement.Common.Models.XunitTracingInterceptor(output));
            TestExecutionHelpers.SetUpSessionAndProfile();
            sqrOperationsMock           = new Mock <IScheduledQueryRulesOperations>();
            monitorManagementClientMock = new Mock <MonitorManagementClient>()
            {
                CallBase = true
            };
            commandRuntimeMock = new Mock <ICommandRuntime>();

            ScheduledQueryRuleAznsAction       aznsAction       = new ScheduledQueryRuleAznsAction(new AzNsActionGroup());
            ScheduledQueryRuleTriggerCondition triggerCondition = new ScheduledQueryRuleTriggerCondition(new TriggerCondition("GreaterThan", 15));
            ScheduledQueryRuleAlertingAction   alertingAction   = new ScheduledQueryRuleAlertingAction(new AlertingAction("2", aznsAction, triggerCondition));

            ScheduledQueryRuleSchedule schedule = new ScheduledQueryRuleSchedule(new Schedule(5, 5));

            ScheduledQueryRuleSource source = new ScheduledQueryRuleSource(new Source("union *", "dataSourceId", new string[] { "authResource1", "authResource2" }, "ResultCount"));

            //testing update of "description" field
            cmdlet = new SetScheduledQueryRuleCommand
            {
                CommandRuntime          = commandRuntimeMock.Object,
                MonitorManagementClient = monitorManagementClientMock.Object,
                Source      = new PSScheduledQueryRuleSource(source),
                Schedule    = new PSScheduledQueryRuleSchedule(schedule),
                Action      = new PSScheduledQueryRuleAlertingAction(alertingAction),
                Description = "A Log Search Alert description"
            };

            response = new AzureOperationResponse <LogSearchRuleResource>()
            {
                Body = new LogSearchRuleResource()
            };

            sqrOperationsMock.Setup(f => f.GetWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult <Microsoft.Rest.Azure.AzureOperationResponse <LogSearchRuleResource> >(response))
            .Callback((string resourceGrp, string name, Dictionary <string, List <string> > customHeaders, CancellationToken cancellationToken) =>
            {
                this.resourceGroup = resourceGrp;
                this.ruleName      = name;
                this.updatePrms    = response.Body;
            });

            sqrOperationsMock.Setup(f => f.CreateOrUpdateWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <LogSearchRuleResource>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult <Microsoft.Rest.Azure.AzureOperationResponse <LogSearchRuleResource> >(response))
            .Callback((string resourceGrp, string name, LogSearchRuleResource updateParams, Dictionary <string, List <string> > headers, CancellationToken t) =>
            {
                this.resourceGroup = resourceGrp;
                this.ruleName      = name;
                this.updatePrms    = updateParams;
            });

            monitorManagementClientMock.SetupGet(f => f.ScheduledQueryRules).Returns(this.sqrOperationsMock.Object);

            // Setup Confirmation
            commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>())).Returns(true);
            commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>())).Returns(true);
            commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(true);
            commandRuntimeMock.Setup(f => f.ShouldContinue(It.IsAny <string>(), It.IsAny <string>())).Returns(true);
        }