Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests for dynamic metric alert #6949

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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