forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MetricsAdvisor] Added tests for the AnomalyAlertConfiguration CRUD o…
…perations (Azure#17671)
- Loading branch information
1 parent
eab659e
commit 467ab7b
Showing
28 changed files
with
6,828 additions
and
1,351 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/DisposableAlertConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Azure.AI.MetricsAdvisor.Administration; | ||
using Azure.AI.MetricsAdvisor.Models; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.AI.MetricsAdvisor.Tests | ||
{ | ||
/// <summary> | ||
/// Represents an <see cref="AnomalyAlertConfiguration"/> that has been created for testing purposes. | ||
/// In order to create a new instance of this class, the <see cref="CreateAlertConfigurationAsync"/> | ||
/// static method must be invoked. The created configuration will be deleted upon disposal. | ||
/// </summary> | ||
public class DisposableAlertConfiguration : IAsyncDisposable | ||
{ | ||
/// <summary> | ||
/// The client to use for deleting the configuration upon disposal. | ||
/// </summary> | ||
private readonly MetricsAdvisorAdministrationClient _adminClient; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DisposableAlertConfiguration"/> class. | ||
/// </summary> | ||
/// <param name="adminClient">The client to use for deleting the configuration upon disposal.</param> | ||
/// <param name="id">The identifier of the alert configuration this instance is associated with.</param> | ||
private DisposableAlertConfiguration(MetricsAdvisorAdministrationClient adminClient, string id) | ||
{ | ||
_adminClient = adminClient; | ||
Id = id; | ||
} | ||
|
||
/// <summary> | ||
/// The identifier of the alert configuration this instance is associated with. | ||
/// </summary> | ||
public string Id { get; } | ||
|
||
/// <summary> | ||
/// Creates an alert configuration using the specified <see cref="MetricsAdvisorAdministrationClient"/>. | ||
/// A <see cref="DisposableAlertConfiguration"/> instance is returned, from which the ID of the created | ||
/// configuration can be obtained. Upon disposal, the associated configuration will be deleted. | ||
/// </summary> | ||
/// <param name="adminClient">The client to use for creating and for deleting the configuration.</param> | ||
/// <param name="hook">Specifies how the created <see cref="AnomalyAlertConfiguration"/> should be configured.</param> | ||
/// <returns>A <see cref="DisposableAlertConfiguration"/> instance from which the ID of the created configuration can be obtained.</returns> | ||
public static async Task<DisposableAlertConfiguration> CreateAlertConfigurationAsync(MetricsAdvisorAdministrationClient adminClient, AnomalyAlertConfiguration alertConfiguration) | ||
{ | ||
string configId = await adminClient.CreateAlertConfigurationAsync(alertConfiguration); | ||
|
||
Assert.That(configId, Is.Not.Null.And.Not.Empty); | ||
|
||
return new DisposableAlertConfiguration(adminClient, configId); | ||
} | ||
|
||
/// <summary> | ||
/// Deletes the configuration this instance is associated with. | ||
/// </summary> | ||
public async ValueTask DisposeAsync() => await _adminClient.DeleteAlertConfigurationAsync(Id); | ||
} | ||
} |
987 changes: 987 additions & 0 deletions
987
...icsAdvisor/tests/MetricsAdvisorAdministrationClient/AnomalyAlertConfigurationLiveTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
179 changes: 179 additions & 0 deletions
179
...MetricsAdvisor/tests/MetricsAdvisorAdministrationClient/AnomalyAlertConfigurationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using Azure.AI.MetricsAdvisor.Administration; | ||
using Azure.AI.MetricsAdvisor.Models; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.AI.MetricsAdvisor.Tests | ||
{ | ||
public class AnomalyAlertConfigurationTests : ClientTestBase | ||
{ | ||
public AnomalyAlertConfigurationTests(bool isAsync) : base(isAsync) | ||
{ | ||
} | ||
|
||
private string FakeGuid => "00000000-0000-0000-0000-000000000000"; | ||
|
||
[Test] | ||
public void CreateAlertConfigurationValidatesArguments() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
Assert.That(() => adminClient.CreateAlertConfigurationAsync(null), Throws.InstanceOf<ArgumentNullException>()); | ||
|
||
Assert.That(() => adminClient.CreateAlertConfiguration(null), Throws.InstanceOf<ArgumentNullException>()); | ||
} | ||
|
||
[Test] | ||
public void CreateAlertConfigurationRespectsTheCancellationToken() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
var metricConfigs = new List<MetricAnomalyAlertConfiguration>() | ||
{ | ||
new MetricAnomalyAlertConfiguration(FakeGuid, MetricAnomalyAlertScope.GetScopeForWholeSeries()) | ||
}; | ||
var config = new AnomalyAlertConfiguration("configName", new List<string>(), metricConfigs); | ||
|
||
using var cancellationSource = new CancellationTokenSource(); | ||
cancellationSource.Cancel(); | ||
|
||
Assert.That(() => adminClient.CreateAlertConfigurationAsync(config, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); | ||
Assert.That(() => adminClient.CreateAlertConfiguration(config, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); | ||
} | ||
|
||
[Test] | ||
public void UpdateAlertConfigurationValidatesArguments() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
var metricConfigs = new List<MetricAnomalyAlertConfiguration>() | ||
{ | ||
new MetricAnomalyAlertConfiguration(FakeGuid, MetricAnomalyAlertScope.GetScopeForWholeSeries()) | ||
}; | ||
var config = new AnomalyAlertConfiguration("configName", new List<string>(), metricConfigs); | ||
|
||
Assert.That(() => adminClient.UpdateAlertConfigurationAsync(null, config), Throws.InstanceOf<ArgumentNullException>()); | ||
Assert.That(() => adminClient.UpdateAlertConfigurationAsync("", config), Throws.InstanceOf<ArgumentException>()); | ||
Assert.That(() => adminClient.UpdateAlertConfigurationAsync("configId", config), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); | ||
Assert.That(() => adminClient.UpdateAlertConfigurationAsync(FakeGuid, null), Throws.InstanceOf<ArgumentNullException>()); | ||
|
||
Assert.That(() => adminClient.UpdateAlertConfiguration(null, config), Throws.InstanceOf<ArgumentNullException>()); | ||
Assert.That(() => adminClient.UpdateAlertConfiguration("", config), Throws.InstanceOf<ArgumentException>()); | ||
Assert.That(() => adminClient.UpdateAlertConfiguration("configId", config), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); | ||
Assert.That(() => adminClient.UpdateAlertConfiguration(FakeGuid, null), Throws.InstanceOf<ArgumentNullException>()); | ||
} | ||
|
||
[Test] | ||
public void UpdateAlertConfigurationRespectsTheCancellationToken() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
var metricConfigs = new List<MetricAnomalyAlertConfiguration>() | ||
{ | ||
new MetricAnomalyAlertConfiguration(FakeGuid, MetricAnomalyAlertScope.GetScopeForWholeSeries()) | ||
}; | ||
var config = new AnomalyAlertConfiguration("configName", new List<string>(), metricConfigs); | ||
|
||
using var cancellationSource = new CancellationTokenSource(); | ||
cancellationSource.Cancel(); | ||
|
||
Assert.That(() => adminClient.UpdateAlertConfigurationAsync(FakeGuid, config, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); | ||
Assert.That(() => adminClient.UpdateAlertConfiguration(FakeGuid, config, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); | ||
} | ||
|
||
[Test] | ||
public void GetAlertConfigurationValidatesArguments() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
Assert.That(() => adminClient.GetAlertConfigurationAsync(null), Throws.InstanceOf<ArgumentNullException>()); | ||
Assert.That(() => adminClient.GetAlertConfigurationAsync(""), Throws.InstanceOf<ArgumentException>()); | ||
Assert.That(() => adminClient.GetAlertConfigurationAsync("configId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); | ||
|
||
Assert.That(() => adminClient.GetAlertConfiguration(null), Throws.InstanceOf<ArgumentNullException>()); | ||
Assert.That(() => adminClient.GetAlertConfiguration(""), Throws.InstanceOf<ArgumentException>()); | ||
Assert.That(() => adminClient.GetAlertConfiguration("configId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); | ||
} | ||
|
||
[Test] | ||
public void GetAlertConfigurationRespectsTheCancellationToken() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
using var cancellationSource = new CancellationTokenSource(); | ||
cancellationSource.Cancel(); | ||
|
||
Assert.That(() => adminClient.GetAlertConfigurationAsync(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); | ||
Assert.That(() => adminClient.GetAlertConfiguration(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); | ||
} | ||
|
||
[Test] | ||
public void GetAlertConfigurationsValidatesArguments() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
Assert.That(() => adminClient.GetAlertConfigurationsAsync(null), Throws.InstanceOf<ArgumentNullException>()); | ||
Assert.That(() => adminClient.GetAlertConfigurationsAsync(""), Throws.InstanceOf<ArgumentException>()); | ||
Assert.That(() => adminClient.GetAlertConfigurationsAsync("metricId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); | ||
|
||
Assert.That(() => adminClient.GetAlertConfigurations(null), Throws.InstanceOf<ArgumentNullException>()); | ||
Assert.That(() => adminClient.GetAlertConfigurations(""), Throws.InstanceOf<ArgumentException>()); | ||
Assert.That(() => adminClient.GetAlertConfigurations("metricId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); | ||
} | ||
|
||
[Test] | ||
public void GetAlertConfigurationsRespectsTheCancellationToken() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
using var cancellationSource = new CancellationTokenSource(); | ||
cancellationSource.Cancel(); | ||
|
||
IAsyncEnumerator<AnomalyAlertConfiguration> asyncEnumerator = adminClient.GetAlertConfigurationsAsync(FakeGuid, cancellationSource.Token).GetAsyncEnumerator(); | ||
Assert.That(async () => await asyncEnumerator.MoveNextAsync(), Throws.InstanceOf<OperationCanceledException>()); | ||
|
||
IEnumerator<AnomalyAlertConfiguration> enumerator = adminClient.GetAlertConfigurations(FakeGuid, cancellationSource.Token).GetEnumerator(); | ||
Assert.That(() => enumerator.MoveNext(), Throws.InstanceOf<OperationCanceledException>()); | ||
} | ||
|
||
[Test] | ||
public void DeleteAlertConfigurationValidatesArguments() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
Assert.That(() => adminClient.DeleteAlertConfigurationAsync(null), Throws.InstanceOf<ArgumentNullException>()); | ||
Assert.That(() => adminClient.DeleteAlertConfigurationAsync(""), Throws.InstanceOf<ArgumentException>()); | ||
Assert.That(() => adminClient.DeleteAlertConfigurationAsync("configId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); | ||
|
||
Assert.That(() => adminClient.DeleteAlertConfiguration(null), Throws.InstanceOf<ArgumentNullException>()); | ||
Assert.That(() => adminClient.DeleteAlertConfiguration(""), Throws.InstanceOf<ArgumentException>()); | ||
Assert.That(() => adminClient.DeleteAlertConfiguration("configId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); | ||
} | ||
|
||
[Test] | ||
public void DeleteAlertConfigurationRespectsTheCancellationToken() | ||
{ | ||
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); | ||
|
||
using var cancellationSource = new CancellationTokenSource(); | ||
cancellationSource.Cancel(); | ||
|
||
Assert.That(() => adminClient.DeleteAlertConfigurationAsync(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); | ||
Assert.That(() => adminClient.DeleteAlertConfiguration(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); | ||
} | ||
|
||
private MetricsAdvisorAdministrationClient GetMetricsAdvisorAdministrationClient() | ||
{ | ||
var fakeEndpoint = new Uri("http://notreal.azure.com"); | ||
var fakeCredential = new MetricsAdvisorKeyCredential("fakeSubscriptionKey", "fakeApiKey"); | ||
|
||
return new MetricsAdvisorAdministrationClient(fakeEndpoint, fakeCredential); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.