Skip to content

Commit

Permalink
Added unit tests for dynamic metric alert (#6949)
Browse files Browse the repository at this point in the history
* Added unit tests for dynamic metric alert

* Don't exclude from build and test
  • Loading branch information
meday89 authored and erich-wang committed Jul 17, 2019
1 parent ab2f7b2 commit a9c9861
Show file tree
Hide file tree
Showing 7 changed files with 1,646 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ public void CreateUpdateMultiResourceMetricAlertRuleTest()
Utilities.AreEqual(expectedParams, result);
}

[Fact]
[Trait("Category", "Mock")]
public void CreateUpdateDynamicMetricAlertRuleTest()
{
MetricAlertResource expectedParams = GetSampleDynamicMetricRuleResourceParams();
var handler = new RecordedDelegatingHandler();
var insightClient = GetMonitorManagementClient(handler);
var serializedObject = Microsoft.Rest.Serialization.SafeJsonConvert.SerializeObject(expectedParams, insightClient.SerializationSettings);

var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(serializedObject)
};

handler = new RecordedDelegatingHandler(expectedResponse);
insightClient = GetMonitorManagementClient(handler);

var result = insightClient.MetricAlerts.CreateOrUpdate(resourceGroupName: "rg1", ruleName: "Rule1", parameters: expectedParams);
Utilities.AreEqual(expectedParams, result);
}

[Fact]
[Trait("Category", "Mock")]
public void ListMetricAlertRuleByResourceGroupTest()
Expand All @@ -78,6 +99,27 @@ public void ListMetricAlertRuleByResourceGroupTest()
Utilities.AreEqual(expectedResponseValue, actualResponse.ToList<MetricAlertResource>());
}

[Fact]
[Trait("Category", "Mock")]
public void CreateUpdateDynamicMultiResourceMetricAlertRuleTest()
{
MetricAlertResource expectedParams = GetSampleMultiResourceDynamicMetricRuleResourceParams();
var handler = new RecordedDelegatingHandler();
var insightClient = GetMonitorManagementClient(handler);
var serializedObject = Microsoft.Rest.Serialization.SafeJsonConvert.SerializeObject(expectedParams, insightClient.SerializationSettings);

var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(serializedObject)
};

handler = new RecordedDelegatingHandler(expectedResponse);
insightClient = GetMonitorManagementClient(handler);

var result = insightClient.MetricAlerts.CreateOrUpdate(resourceGroupName: "rg1", ruleName: "Rule2", parameters: expectedParams);
Utilities.AreEqual(expectedParams, result);
}

[Fact]
[Trait("Category", "Mock")]
public void ListMetricAlertsBySubscriptionTest()
Expand Down Expand Up @@ -162,6 +204,48 @@ public void GetMultiResourceMetricAlertTest()
Utilities.AreEqual(expectedParams, result);
}

[Fact]
[Trait("Category", "Mock")]
public void GetDynamicMetricAlertTest()
{
MetricAlertResource expectedParams = GetSampleDynamicMetricRuleResourceParams();
var handler = new RecordedDelegatingHandler();
var insightClient = GetMonitorManagementClient(handler);
var serializedObject = Microsoft.Rest.Serialization.SafeJsonConvert.SerializeObject(expectedParams, insightClient.SerializationSettings);

var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(serializedObject)
};

handler = new RecordedDelegatingHandler(expectedResponse);
insightClient = GetMonitorManagementClient(handler);

var result = insightClient.MetricAlerts.Get(resourceGroupName: "rg1", ruleName: "Rule1");
Utilities.AreEqual(expectedParams, result);
}

[Fact]
[Trait("Category", "Mock")]
public void GetMultiResourceDynamicMetricAlertTest()
{
MetricAlertResource expectedParams = GetSampleMultiResourceDynamicMetricRuleResourceParams();
var handler = new RecordedDelegatingHandler();
var insightClient = GetMonitorManagementClient(handler);
var serializedObject = Microsoft.Rest.Serialization.SafeJsonConvert.SerializeObject(expectedParams, insightClient.SerializationSettings);

var expectedResponse = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(serializedObject)
};

handler = new RecordedDelegatingHandler(expectedResponse);
insightClient = GetMonitorManagementClient(handler);

var result = insightClient.MetricAlerts.Get(resourceGroupName: "rg1", ruleName: "Rule1");
Utilities.AreEqual(expectedParams, result);
}

private MetricAlertStatusCollection GetMetricAlertStatus()
{
MetricAlertStatusProperties statusProps = new MetricAlertStatusProperties()
Expand Down Expand Up @@ -225,8 +309,49 @@ private List<MetricAlertResource> GetSampleMetricAlertCollection()
{
ActionGroupId = "/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourcegroups/default-activitylogalerts/providers/microsoft.insights/actiongroups/anashah"
}
}
)
}),
new MetricAlertResource(
description: "alert description",
severity: 3,
location: "Location",
enabled: true,
scopes: new List<string>()
{
"/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourceGroups/SanjaychResourceGroup/providers/Microsoft.Compute/virtualMachines/SCCMDemo2"
},
evaluationFrequency: new TimeSpan(hours: 0, minutes: 5, seconds: 0),
windowSize: new TimeSpan(hours: 0, minutes: 5, seconds: 0),
criteria: GetSampleDynamicMetricCriteria(),
actions: new List<MetricAlertAction>()
{
new MetricAlertAction()
{
ActionGroupId = "/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourcegroups/custommetricdemo/providers/microsoft.insights/actiongroups/scactiongroup"
}
}
),
new MetricAlertResource(
description: "alert description",
severity: 3,
location: "global",
enabled: true,
targetResourceRegion: "southcentralus",
targetResourceType: "Microsoft.Compute/virtualMachines",
scopes: new List<string>()
{
"/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourceGroups/SanjaychResourceGroup/providers/Microsoft.Compute/virtualMachines/SCCMDemo2",
"/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourceGroups/SanjaychResourceGroup/providers/Microsoft.Compute/virtualMachines/SCCMDemo3"
},
evaluationFrequency: new TimeSpan(hours: 0, minutes: 5, seconds: 0),
windowSize: new TimeSpan(hours: 0, minutes: 5 ,seconds: 0),
criteria: GetSampleDynamicMetricCriteria(),
actions: new List<MetricAlertAction>()
{
new MetricAlertAction()
{
ActionGroupId = "/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourcegroups/default-activitylogalerts/providers/microsoft.insights/actiongroups/anashah"
}
})
};
}

Expand Down Expand Up @@ -256,6 +381,32 @@ private MetricAlertResource GetSampleMetricRuleResourceParams()
);
}

private MetricAlertResource GetSampleDynamicMetricRuleResourceParams()
{
MetricAlertMultipleResourceMultipleMetricCriteria metricCriteria = GetSampleDynamicMetricCriteria();

return new MetricAlertResource(
description: "alert description",
severity: 3,
location: "Location",
enabled: true,
scopes: new List<string>()
{
"/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourceGroups/SanjaychResourceGroup/providers/Microsoft.Compute/virtualMachines/SCCMDemo2"
},
evaluationFrequency: new TimeSpan(hours: 0, minutes: 5, seconds: 0),
windowSize: new TimeSpan(hours: 0, minutes: 5, seconds: 0),
criteria: metricCriteria,
actions: new List<MetricAlertAction>()
{
new MetricAlertAction()
{
ActionGroupId = "/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourcegroups/custommetricdemo/providers/microsoft.insights/actiongroups/scactiongroup"
}
}
);
}

private MetricAlertResource GetSampleMultiResourceMetricRuleResourceParams()
{
MetricAlertMultipleResourceMultipleMetricCriteria metricCriteria = GetSampleMultiResourceMetricCriteria();
Expand Down Expand Up @@ -285,6 +436,35 @@ private MetricAlertResource GetSampleMultiResourceMetricRuleResourceParams()
);
}

private MetricAlertResource GetSampleMultiResourceDynamicMetricRuleResourceParams()
{
MetricAlertMultipleResourceMultipleMetricCriteria metricCriteria = GetSampleDynamicMetricCriteria();

return new MetricAlertResource(
description: "alert description",
severity: 3,
location: "global",
enabled: true,
targetResourceRegion: "southcentralus",
targetResourceType: "Microsoft.Compute/virtualMachines",
scopes: new List<string>()
{
"/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourceGroups/SanjaychResourceGroup/providers/Microsoft.Compute/virtualMachines/SCCMDemo2",
"/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourceGroups/SanjaychResourceGroup/providers/Microsoft.Compute/virtualMachines/SCCMDemo3"
},
evaluationFrequency: new TimeSpan(hours: 0, minutes: 5, seconds: 0),
windowSize: new TimeSpan(hours: 0, minutes: 5, seconds: 0),
criteria: metricCriteria,
actions: new List<MetricAlertAction>()
{
new MetricAlertAction()
{
ActionGroupId = "/subscriptions/80430018-24ee-4b28-a7bd-fb23b5a221d6/resourcegroups/default-activitylogalerts/providers/microsoft.insights/actiongroups/anashah"
}
}
);
}

private static MetricAlertSingleResourceMultipleMetricCriteria GetSampleMetricCriteria()
{
MetricDimension metricDimension = new MetricDimension()
Expand Down Expand Up @@ -315,6 +495,43 @@ private static MetricAlertSingleResourceMultipleMetricCriteria GetSampleMetricCr
);
}

private static MetricAlertMultipleResourceMultipleMetricCriteria GetSampleDynamicMetricCriteria()
{
MetricDimension metricDimension = new MetricDimension()
{
Name = "name1",
OperatorProperty = "Include",
Values = new List<string>()
{
"Primary"
}
};

MetricDimension[] metricDimensions = new MetricDimension[1] { metricDimension };

return new MetricAlertMultipleResourceMultipleMetricCriteria(
allOf: new List<MultiMetricCriteria>()
{
new DynamicMetricCriteria()
{
MetricName = "Metric Name",
Name = "metric1",
Dimensions = metricDimensions,
MetricNamespace = "Namespace",
AlertSensitivity= "High",
IgnoreDataBefore = null,
FailingPeriods = new DynamicThresholdFailingPeriods()
{
MinFailingPeriodsToAlert = 4,
NumberOfEvaluationPeriods = 4
},
OperatorProperty = "GreaterThan",
TimeAggregation = "Avergage"
}
}
) ;
}

private static MetricAlertMultipleResourceMultipleMetricCriteria GetSampleMultiResourceMetricCriteria()
{
MetricDimension metricDimension = new MetricDimension()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
<ExcludeFromBuild>true</ExcludeFromBuild>
<ExcludeFromTest>true</ExcludeFromTest>
<ExcludeFromBuild>false</ExcludeFromBuild>
<ExcludeFromTest>false</ExcludeFromTest>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit a9c9861

Please sign in to comment.