Skip to content

Commit fb35018

Browse files
authored
[MetricsAdvisor] Added tests for the AnomalyAlertConfiguration CRUD operations (Azure#17671)
1 parent 1aabcea commit fb35018

File tree

28 files changed

+6828
-1351
lines changed

28 files changed

+6828
-1351
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Azure.AI.MetricsAdvisor.Administration;
7+
using Azure.AI.MetricsAdvisor.Models;
8+
using NUnit.Framework;
9+
10+
namespace Azure.AI.MetricsAdvisor.Tests
11+
{
12+
/// <summary>
13+
/// Represents an <see cref="AnomalyAlertConfiguration"/> that has been created for testing purposes.
14+
/// In order to create a new instance of this class, the <see cref="CreateAlertConfigurationAsync"/>
15+
/// static method must be invoked. The created configuration will be deleted upon disposal.
16+
/// </summary>
17+
public class DisposableAlertConfiguration : IAsyncDisposable
18+
{
19+
/// <summary>
20+
/// The client to use for deleting the configuration upon disposal.
21+
/// </summary>
22+
private readonly MetricsAdvisorAdministrationClient _adminClient;
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="DisposableAlertConfiguration"/> class.
26+
/// </summary>
27+
/// <param name="adminClient">The client to use for deleting the configuration upon disposal.</param>
28+
/// <param name="id">The identifier of the alert configuration this instance is associated with.</param>
29+
private DisposableAlertConfiguration(MetricsAdvisorAdministrationClient adminClient, string id)
30+
{
31+
_adminClient = adminClient;
32+
Id = id;
33+
}
34+
35+
/// <summary>
36+
/// The identifier of the alert configuration this instance is associated with.
37+
/// </summary>
38+
public string Id { get; }
39+
40+
/// <summary>
41+
/// Creates an alert configuration using the specified <see cref="MetricsAdvisorAdministrationClient"/>.
42+
/// A <see cref="DisposableAlertConfiguration"/> instance is returned, from which the ID of the created
43+
/// configuration can be obtained. Upon disposal, the associated configuration will be deleted.
44+
/// </summary>
45+
/// <param name="adminClient">The client to use for creating and for deleting the configuration.</param>
46+
/// <param name="hook">Specifies how the created <see cref="AnomalyAlertConfiguration"/> should be configured.</param>
47+
/// <returns>A <see cref="DisposableAlertConfiguration"/> instance from which the ID of the created configuration can be obtained.</returns>
48+
public static async Task<DisposableAlertConfiguration> CreateAlertConfigurationAsync(MetricsAdvisorAdministrationClient adminClient, AnomalyAlertConfiguration alertConfiguration)
49+
{
50+
string configId = await adminClient.CreateAlertConfigurationAsync(alertConfiguration);
51+
52+
Assert.That(configId, Is.Not.Null.And.Not.Empty);
53+
54+
return new DisposableAlertConfiguration(adminClient, configId);
55+
}
56+
57+
/// <summary>
58+
/// Deletes the configuration this instance is associated with.
59+
/// </summary>
60+
public async ValueTask DisposeAsync() => await _adminClient.DeleteAlertConfigurationAsync(Id);
61+
}
62+
}

sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/MetricsAdvisorAdministrationClient/AnomalyAlertConfigurationLiveTests.cs

Lines changed: 987 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading;
7+
using Azure.AI.MetricsAdvisor.Administration;
8+
using Azure.AI.MetricsAdvisor.Models;
9+
using Azure.Core.TestFramework;
10+
using NUnit.Framework;
11+
12+
namespace Azure.AI.MetricsAdvisor.Tests
13+
{
14+
public class AnomalyAlertConfigurationTests : ClientTestBase
15+
{
16+
public AnomalyAlertConfigurationTests(bool isAsync) : base(isAsync)
17+
{
18+
}
19+
20+
private string FakeGuid => "00000000-0000-0000-0000-000000000000";
21+
22+
[Test]
23+
public void CreateAlertConfigurationValidatesArguments()
24+
{
25+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
26+
27+
Assert.That(() => adminClient.CreateAlertConfigurationAsync(null), Throws.InstanceOf<ArgumentNullException>());
28+
29+
Assert.That(() => adminClient.CreateAlertConfiguration(null), Throws.InstanceOf<ArgumentNullException>());
30+
}
31+
32+
[Test]
33+
public void CreateAlertConfigurationRespectsTheCancellationToken()
34+
{
35+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
36+
37+
var metricConfigs = new List<MetricAnomalyAlertConfiguration>()
38+
{
39+
new MetricAnomalyAlertConfiguration(FakeGuid, MetricAnomalyAlertScope.GetScopeForWholeSeries())
40+
};
41+
var config = new AnomalyAlertConfiguration("configName", new List<string>(), metricConfigs);
42+
43+
using var cancellationSource = new CancellationTokenSource();
44+
cancellationSource.Cancel();
45+
46+
Assert.That(() => adminClient.CreateAlertConfigurationAsync(config, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
47+
Assert.That(() => adminClient.CreateAlertConfiguration(config, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
48+
}
49+
50+
[Test]
51+
public void UpdateAlertConfigurationValidatesArguments()
52+
{
53+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
54+
55+
var metricConfigs = new List<MetricAnomalyAlertConfiguration>()
56+
{
57+
new MetricAnomalyAlertConfiguration(FakeGuid, MetricAnomalyAlertScope.GetScopeForWholeSeries())
58+
};
59+
var config = new AnomalyAlertConfiguration("configName", new List<string>(), metricConfigs);
60+
61+
Assert.That(() => adminClient.UpdateAlertConfigurationAsync(null, config), Throws.InstanceOf<ArgumentNullException>());
62+
Assert.That(() => adminClient.UpdateAlertConfigurationAsync("", config), Throws.InstanceOf<ArgumentException>());
63+
Assert.That(() => adminClient.UpdateAlertConfigurationAsync("configId", config), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
64+
Assert.That(() => adminClient.UpdateAlertConfigurationAsync(FakeGuid, null), Throws.InstanceOf<ArgumentNullException>());
65+
66+
Assert.That(() => adminClient.UpdateAlertConfiguration(null, config), Throws.InstanceOf<ArgumentNullException>());
67+
Assert.That(() => adminClient.UpdateAlertConfiguration("", config), Throws.InstanceOf<ArgumentException>());
68+
Assert.That(() => adminClient.UpdateAlertConfiguration("configId", config), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
69+
Assert.That(() => adminClient.UpdateAlertConfiguration(FakeGuid, null), Throws.InstanceOf<ArgumentNullException>());
70+
}
71+
72+
[Test]
73+
public void UpdateAlertConfigurationRespectsTheCancellationToken()
74+
{
75+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
76+
77+
var metricConfigs = new List<MetricAnomalyAlertConfiguration>()
78+
{
79+
new MetricAnomalyAlertConfiguration(FakeGuid, MetricAnomalyAlertScope.GetScopeForWholeSeries())
80+
};
81+
var config = new AnomalyAlertConfiguration("configName", new List<string>(), metricConfigs);
82+
83+
using var cancellationSource = new CancellationTokenSource();
84+
cancellationSource.Cancel();
85+
86+
Assert.That(() => adminClient.UpdateAlertConfigurationAsync(FakeGuid, config, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
87+
Assert.That(() => adminClient.UpdateAlertConfiguration(FakeGuid, config, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
88+
}
89+
90+
[Test]
91+
public void GetAlertConfigurationValidatesArguments()
92+
{
93+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
94+
95+
Assert.That(() => adminClient.GetAlertConfigurationAsync(null), Throws.InstanceOf<ArgumentNullException>());
96+
Assert.That(() => adminClient.GetAlertConfigurationAsync(""), Throws.InstanceOf<ArgumentException>());
97+
Assert.That(() => adminClient.GetAlertConfigurationAsync("configId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
98+
99+
Assert.That(() => adminClient.GetAlertConfiguration(null), Throws.InstanceOf<ArgumentNullException>());
100+
Assert.That(() => adminClient.GetAlertConfiguration(""), Throws.InstanceOf<ArgumentException>());
101+
Assert.That(() => adminClient.GetAlertConfiguration("configId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
102+
}
103+
104+
[Test]
105+
public void GetAlertConfigurationRespectsTheCancellationToken()
106+
{
107+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
108+
109+
using var cancellationSource = new CancellationTokenSource();
110+
cancellationSource.Cancel();
111+
112+
Assert.That(() => adminClient.GetAlertConfigurationAsync(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
113+
Assert.That(() => adminClient.GetAlertConfiguration(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
114+
}
115+
116+
[Test]
117+
public void GetAlertConfigurationsValidatesArguments()
118+
{
119+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
120+
121+
Assert.That(() => adminClient.GetAlertConfigurationsAsync(null), Throws.InstanceOf<ArgumentNullException>());
122+
Assert.That(() => adminClient.GetAlertConfigurationsAsync(""), Throws.InstanceOf<ArgumentException>());
123+
Assert.That(() => adminClient.GetAlertConfigurationsAsync("metricId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
124+
125+
Assert.That(() => adminClient.GetAlertConfigurations(null), Throws.InstanceOf<ArgumentNullException>());
126+
Assert.That(() => adminClient.GetAlertConfigurations(""), Throws.InstanceOf<ArgumentException>());
127+
Assert.That(() => adminClient.GetAlertConfigurations("metricId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
128+
}
129+
130+
[Test]
131+
public void GetAlertConfigurationsRespectsTheCancellationToken()
132+
{
133+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
134+
135+
using var cancellationSource = new CancellationTokenSource();
136+
cancellationSource.Cancel();
137+
138+
IAsyncEnumerator<AnomalyAlertConfiguration> asyncEnumerator = adminClient.GetAlertConfigurationsAsync(FakeGuid, cancellationSource.Token).GetAsyncEnumerator();
139+
Assert.That(async () => await asyncEnumerator.MoveNextAsync(), Throws.InstanceOf<OperationCanceledException>());
140+
141+
IEnumerator<AnomalyAlertConfiguration> enumerator = adminClient.GetAlertConfigurations(FakeGuid, cancellationSource.Token).GetEnumerator();
142+
Assert.That(() => enumerator.MoveNext(), Throws.InstanceOf<OperationCanceledException>());
143+
}
144+
145+
[Test]
146+
public void DeleteAlertConfigurationValidatesArguments()
147+
{
148+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
149+
150+
Assert.That(() => adminClient.DeleteAlertConfigurationAsync(null), Throws.InstanceOf<ArgumentNullException>());
151+
Assert.That(() => adminClient.DeleteAlertConfigurationAsync(""), Throws.InstanceOf<ArgumentException>());
152+
Assert.That(() => adminClient.DeleteAlertConfigurationAsync("configId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
153+
154+
Assert.That(() => adminClient.DeleteAlertConfiguration(null), Throws.InstanceOf<ArgumentNullException>());
155+
Assert.That(() => adminClient.DeleteAlertConfiguration(""), Throws.InstanceOf<ArgumentException>());
156+
Assert.That(() => adminClient.DeleteAlertConfiguration("configId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException)));
157+
}
158+
159+
[Test]
160+
public void DeleteAlertConfigurationRespectsTheCancellationToken()
161+
{
162+
MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient();
163+
164+
using var cancellationSource = new CancellationTokenSource();
165+
cancellationSource.Cancel();
166+
167+
Assert.That(() => adminClient.DeleteAlertConfigurationAsync(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
168+
Assert.That(() => adminClient.DeleteAlertConfiguration(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>());
169+
}
170+
171+
private MetricsAdvisorAdministrationClient GetMetricsAdvisorAdministrationClient()
172+
{
173+
var fakeEndpoint = new Uri("http://notreal.azure.com");
174+
var fakeCredential = new MetricsAdvisorKeyCredential("fakeSubscriptionKey", "fakeApiKey");
175+
176+
return new MetricsAdvisorAdministrationClient(fakeEndpoint, fakeCredential);
177+
}
178+
}
179+
}

sdk/metricsadvisor/Azure.AI.MetricsAdvisor/tests/MetricsAdvisorAdministrationClientLiveTests.cs

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using System.Threading.Tasks;
87
using Azure.AI.MetricsAdvisor.Models;
98
using Azure.Core.TestFramework;
@@ -153,72 +152,6 @@ public async Task GetDataFeedIngestionProgress()
153152
Assert.That(progress, Is.Not.Null);
154153
}
155154

156-
[RecordedTest]
157-
public async Task AnomalyAlertConfigurationOperations()
158-
{
159-
var adminClient = GetMetricsAdvisorAdministrationClient();
160-
161-
// Create a Detection Configuration
162-
DataFeed feed = await GetFirstDataFeed(adminClient);
163-
string createdAnomalyDetectionConfigurationId = await CreateDetectionConfiguration(adminClient).ConfigureAwait(false);
164-
165-
var alertConfigToCreate = new AnomalyAlertConfiguration(
166-
Recording.GenerateAlphaNumericId("test"),
167-
new List<string>(),
168-
new List<MetricAnomalyAlertConfiguration>
169-
{
170-
new MetricAnomalyAlertConfiguration(
171-
createdAnomalyDetectionConfigurationId,
172-
new MetricAnomalyAlertScope(
173-
MetricAnomalyAlertScopeType.TopN,
174-
new DimensionKey(new List<KeyValuePair<string, string>>
175-
{
176-
new KeyValuePair<string, string>("test", "test2")
177-
}),
178-
new TopNGroupScope(8, 4, 2)))
179-
});
180-
181-
string createdAlertConfigId = await adminClient.CreateAlertConfigurationAsync(alertConfigToCreate).ConfigureAwait(false);
182-
183-
Assert.That(createdAlertConfigId, Is.Not.Null);
184-
185-
// Validate that we can Get the newly created config
186-
AnomalyAlertConfiguration getAlertConfig = await adminClient.GetAlertConfigurationAsync(createdAlertConfigId).ConfigureAwait(false);
187-
188-
List<AnomalyAlertConfiguration> getAlertConfigs = new List<AnomalyAlertConfiguration>();
189-
190-
await foreach (var config in adminClient.GetAlertConfigurationsAsync(createdAnomalyDetectionConfigurationId))
191-
{
192-
getAlertConfigs.Add(config);
193-
}
194-
195-
Assert.That(getAlertConfig.Id, Is.EqualTo(createdAlertConfigId));
196-
Assert.That(getAlertConfigs.Any(c => c.Id == createdAlertConfigId));
197-
198-
getAlertConfig.Description = "Updated";
199-
getAlertConfig.CrossMetricsOperator = MetricAnomalyAlertConfigurationsOperator.And;
200-
201-
await adminClient.UpdateAlertConfigurationAsync(getAlertConfig.Id, getAlertConfig).ConfigureAwait(false);
202-
203-
// Validate that the update succeeded.
204-
getAlertConfig = await adminClient.GetAlertConfigurationAsync(createdAlertConfigId).ConfigureAwait(false);
205-
206-
Assert.That(getAlertConfig.Description, Is.EqualTo(getAlertConfig.Description));
207-
208-
// Update again starting with our locally created model.
209-
alertConfigToCreate.Description = "updated again!";
210-
alertConfigToCreate.CrossMetricsOperator = MetricAnomalyAlertConfigurationsOperator.And;
211-
await adminClient.UpdateAlertConfigurationAsync(getAlertConfig.Id, alertConfigToCreate).ConfigureAwait(false);
212-
213-
// Validate that the update succeeded.
214-
getAlertConfig = await adminClient.GetAlertConfigurationAsync(createdAlertConfigId).ConfigureAwait(false);
215-
216-
Assert.That(getAlertConfig.Description, Is.EqualTo(alertConfigToCreate.Description));
217-
218-
// Cleanup
219-
await adminClient.DeleteAlertConfigurationAsync(createdAlertConfigId).ConfigureAwait(false);
220-
}
221-
222155
[RecordedTest]
223156
public async Task ResetDataFeedIngestionStatus()
224157
{

0 commit comments

Comments
 (0)