public void AlertRuleCrudTest() { var handler = new RecordedDelegatingHandler() { }; using (UndoContext context = UndoContext.Current) { context.Start(); var alertsClient = GetAlertsClient(handler); // provision a hosted service to consume by the test case string newServiceName = TestUtilities.GenerateName("cs"); var deploymentStatus = ProvisionHostedService(newServiceName); var deployment = deploymentStatus.Deployments.FirstOrDefault(); var id = HttpMockServer.GetVariable("RuleID", Guid.NewGuid().ToString()); Assert.NotNull(deployment); // now the actual test case Rule newRule = new Rule { Name = TestUtilities.GenerateName("rule"), Description = "rule description", Id = id, IsEnabled = false, Condition = new ThresholdRuleCondition { Operator = ConditionOperator.GreaterThanOrEqual, Threshold = 80.0, WindowSize = TimeSpan.FromMinutes(5), DataSource = new RuleMetricDataSource { MetricName = "Percentage CPU", ResourceId = string.Format("/hostedservices/{0}/deployments/{1}/roles/{2}", newServiceName, deployment.Name, deployment.Roles.FirstOrDefault().RoleName), MetricNamespace = MetricNamespace.None } } }; newRule.Actions.Add(new RuleEmailAction()); // Create the rule AzureOperationResponse createRuleResponse = alertsClient.Rules.CreateOrUpdate(new RuleCreateOrUpdateParameters {Rule = newRule}); Assert.Equal(HttpStatusCode.Created, createRuleResponse.StatusCode); Console.WriteLine("Created alert rule {0}", newRule.Name); // Retrieve the rule RuleGetResponse getRuleResponse = alertsClient.Rules.Get(newRule.Id); Assert.Equal(HttpStatusCode.OK, getRuleResponse.StatusCode); Assert.True(AreEquivalent(newRule, getRuleResponse.Rule), "The retrieved rule is not equivalent to the original rule"); // Get incidents for the rule IncidentListResponse incidentListResponse = alertsClient.Incidents.ListForRule(newRule.Id, true); Assert.Equal(HttpStatusCode.OK, incidentListResponse.StatusCode); Assert.False(incidentListResponse.Any(), string.Format("There should be no active incident for the rule [{0}].", newRule.Name)); // Validate rule is included by list operation RuleListResponse listRulesResponse = alertsClient.Rules.List(); Assert.Equal(HttpStatusCode.OK, getRuleResponse.StatusCode); Assert.True(listRulesResponse.Value.Any(), "The alertClient.Rules.List() call returned an empty collection of alert rules"); Assert.True(listRulesResponse.Value.Any(rc => rc.Id.Equals(newRule.Id)), "The newly created rule is not in present in the collection of rules returned by alertClient.Rules.List()"); // Update and validate the rule Rule updatedRule = newRule; updatedRule.Description = "updated description"; ((ThresholdRuleCondition)(updatedRule.Condition)).Threshold = 60.0; AzureOperationResponse updateRuleResponse = alertsClient.Rules.CreateOrUpdate(new RuleCreateOrUpdateParameters {Rule = updatedRule}); Assert.Equal(HttpStatusCode.OK, updateRuleResponse.StatusCode); getRuleResponse = alertsClient.Rules.Get(newRule.Id); Assert.Equal(HttpStatusCode.OK, getRuleResponse.StatusCode); Assert.True(AreEquivalent(updatedRule, getRuleResponse.Rule), "The retrieved rule is not equivalent to the updated rule"); // Delete the rule AzureOperationResponse deleteResponse = alertsClient.Rules.Delete(newRule.Id); Assert.Equal(HttpStatusCode.OK, deleteResponse.StatusCode); // Validate the rule was deleted Assert.Throws<CloudException>(() => alertsClient.Rules.Get(newRule.Id)); listRulesResponse = alertsClient.Rules.List(); Assert.Equal(HttpStatusCode.OK, getRuleResponse.StatusCode); if (listRulesResponse.Value.Count > 0) { var foundDeletedRule = listRulesResponse.Value.Any(rc => rc.Id.Equals(newRule.Id)); Assert.False(foundDeletedRule, string.Format("Rule [{0}] is found after its deletion.", newRule.Name)); } } }
public AlertsClient GetAlertsClient(RecordedDelegatingHandler handler) { handler.IsPassThrough = true; return TestBase.GetServiceClient<AlertsClient>(new RDFETestEnvironmentFactory()).WithHandler(handler); }