Skip to content

Commit 24c1757

Browse files
authored
[ChangeAnalysis] Add Tests (Azure#34029)
1 parent 0c7d99f commit 24c1757

File tree

8 files changed

+72333
-5
lines changed

8 files changed

+72333
-5
lines changed

sdk/changeanalysis/Azure.ResourceManager.ChangeAnalysis/tests/ChangeAnalysisManagementTestBase.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ namespace Azure.ResourceManager.ChangeAnalysis.Tests
1313
public class ChangeAnalysisManagementTestBase : ManagementRecordedTestBase<ChangeAnalysisManagementTestEnvironment>
1414
{
1515
protected ArmClient Client { get; private set; }
16+
protected SubscriptionResource DefaultSubscription { get; private set; }
17+
protected AzureLocation DefaultLocation = AzureLocation.EastUS;
18+
protected string ResourceGroupNamePrefix = "ChangeAnalysisRg";
1619

1720
protected ChangeAnalysisManagementTestBase(bool isAsync, RecordedTestMode mode)
1821
: base(isAsync, mode)
@@ -25,16 +28,17 @@ protected ChangeAnalysisManagementTestBase(bool isAsync)
2528
}
2629

2730
[SetUp]
28-
public void CreateCommonClient()
31+
public async Task CreateCommonClient()
2932
{
3033
Client = GetArmClient();
34+
DefaultSubscription = await Client.GetDefaultSubscriptionAsync();
3135
}
3236

33-
protected async Task<ResourceGroupResource> CreateResourceGroup(SubscriptionResource subscription, string rgNamePrefix, AzureLocation location)
37+
protected async Task<ResourceGroupResource> CreateResourceGroup()
3438
{
35-
string rgName = Recording.GenerateAssetName(rgNamePrefix);
36-
ResourceGroupData input = new ResourceGroupData(location);
37-
var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, input);
39+
string rgName = Recording.GenerateAssetName(ResourceGroupNamePrefix);
40+
ResourceGroupData input = new ResourceGroupData(DefaultLocation);
41+
var lro = await DefaultSubscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, input);
3842
return lro.Value;
3943
}
4044
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Azure.Core.TestFramework;
8+
using Azure.ResourceManager.ChangeAnalysis.Models;
9+
using NUnit.Framework;
10+
11+
namespace Azure.ResourceManager.ChangeAnalysis.Tests.Scenario
12+
{
13+
internal class ChangeAnalysisTests : ChangeAnalysisManagementTestBase
14+
{
15+
private DateTimeOffset _startTime;
16+
private DateTimeOffset _endTime;
17+
private long _totalSecondsInAWeek = 604800;
18+
19+
public ChangeAnalysisTests(bool isAsync) : base(isAsync)//, RecordedTestMode.Record)
20+
{
21+
}
22+
23+
[SetUp]
24+
public void SetUp()
25+
{
26+
_endTime = Recording.Now;
27+
_startTime = DateTimeOffset.FromUnixTimeSeconds(_endTime.ToUnixTimeSeconds() - _totalSecondsInAWeek);
28+
}
29+
30+
[RecordedTest]
31+
public async Task GetChangesBySubscription()
32+
{
33+
var list = await DefaultSubscription.GetChangesBySubscriptionAsync(_startTime, _endTime).ToEnumerableAsync();
34+
Assert.IsNotEmpty(list);
35+
ValidateDetectedChangeData(list.FirstOrDefault());
36+
Assert.AreEqual(list.FirstOrDefault().ResourceType, "Microsoft.ChangeAnalysis/changes");
37+
}
38+
39+
[RecordedTest]
40+
public async Task GetChangesByResourceGroup()
41+
{
42+
var resourceGroup = await CreateResourceGroup();
43+
var list = await resourceGroup.GetChangesByResourceGroupAsync(_startTime, _endTime).ToEnumerableAsync();
44+
Assert.IsEmpty(list);
45+
}
46+
47+
[RecordedTest]
48+
public async Task GetChangesByTenants()
49+
{
50+
var tenants = await Client.GetTenants().GetAllAsync().ToEnumerableAsync();
51+
var tenant = tenants.FirstOrDefault();
52+
53+
var subscriptionChanges = await DefaultSubscription.GetChangesBySubscriptionAsync(_startTime, _endTime).ToEnumerableAsync();
54+
string changeId = subscriptionChanges.FirstOrDefault().Id.ToString();
55+
string resourceId = changeId.Substring(0, changeId.IndexOf("/providers/Microsoft.ChangeAnalysis/"));
56+
var list = await tenant.GetResourceChangesAsync(resourceId, _startTime, _endTime).ToEnumerableAsync();
57+
Assert.IsNotEmpty(list);
58+
ValidateDetectedChangeData(list.FirstOrDefault());
59+
Assert.AreEqual(list.FirstOrDefault().ResourceType, "Microsoft.ChangeAnalysis/resourceChanges");
60+
}
61+
62+
private void ValidateDetectedChangeData(DetectedChangeData detectedChange)
63+
{
64+
Assert.IsNotNull(detectedChange.Id);
65+
Assert.IsNotNull(detectedChange.Name);
66+
}
67+
}
68+
}

sdk/changeanalysis/Azure.ResourceManager.ChangeAnalysis/tests/SessionRecords/ChangeAnalysisTests/GetChangesByResourceGroup.json

Lines changed: 147 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/changeanalysis/Azure.ResourceManager.ChangeAnalysis/tests/SessionRecords/ChangeAnalysisTests/GetChangesByResourceGroupAsync.json

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)