Skip to content

Commit b66a02d

Browse files
authored
Add Data protection test (Azure#35757)
1 parent 1a29975 commit b66a02d

File tree

9 files changed

+5974
-6
lines changed

9 files changed

+5974
-6
lines changed

sdk/dataprotection/Azure.ResourceManager.DataProtectionBackup/tests/DataProtectionBackupManagementTestBase.cs

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

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

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

33-
protected async Task<ResourceGroupResource> CreateResourceGroup(SubscriptionResource subscription, string rgNamePrefix, AzureLocation location)
37+
protected async Task<ResourceGroupResource> CreateResourceGroupAsync()
3438
{
35-
string rgName = Recording.GenerateAssetName(rgNamePrefix);
36-
ResourceGroupData input = new ResourceGroupData(location);
37-
var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, input);
38-
return lro.Value;
39+
var resourceGroupName = Recording.GenerateAssetName("testRG-");
40+
var rgOp = await DefaultSubscription.GetResourceGroups().CreateOrUpdateAsync(
41+
WaitUntil.Completed,
42+
resourceGroupName,
43+
new ResourceGroupData(DefaultLocation)
44+
{
45+
Tags =
46+
{
47+
{ "test", "env" }
48+
}
49+
});
50+
if (Mode == RecordedTestMode.Playback)
51+
{
52+
groupName = resourceGroupName;
53+
}
54+
else
55+
{
56+
using (Recording.DisableRecording())
57+
{
58+
groupName = rgOp.Value.Data.Name;
59+
}
60+
}
61+
return rgOp.Value;
3962
}
4063
}
4164
}
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using Azure.ResourceManager.DataProtectionBackup.Models;
6+
using Azure.ResourceManager.Models;
7+
using Azure.ResourceManager.Resources.Models;
8+
using NUnit.Framework;
9+
using Azure.Core;
10+
using System;
11+
using System.Text;
12+
using Azure.ResourceManager.Resources;
13+
using System.Threading.Tasks;
14+
using NUnit.Framework.Internal;
15+
using System.IO;
16+
using System.Xml;
17+
18+
namespace Azure.ResourceManager.DataProtectionBackup.Tests.Helpers
19+
{
20+
public static class ResourceDataHelpers
21+
{
22+
public static IDictionary<string, string> ReplaceWith(this IDictionary<string, string> dest, IDictionary<string, string> src)
23+
{
24+
dest.Clear();
25+
foreach (var kv in src)
26+
{
27+
dest.Add(kv);
28+
}
29+
30+
return dest;
31+
}
32+
33+
public static void AssertResource(ResourceData r1, ResourceData r2)
34+
{
35+
Assert.AreEqual(r1.Name, r2.Name);
36+
Assert.AreEqual(r1.Id, r2.Id);
37+
Assert.AreEqual(r1.ResourceType, r2.ResourceType);
38+
}
39+
40+
#region policy
41+
public static DataProtectionBackupPolicyData GetPolicyData()
42+
{
43+
List<string> dataSource = new() { "Microsoft.Storage/storageAccounts/blobServices" };
44+
List<DataProtectionBasePolicyRule> rules = new()
45+
{
46+
new DataProtectionBackupRule(
47+
"BackupDaily",
48+
new DataStoreInfoBase(DataStoreType.VaultStore, "DataStoreInfoBase"),
49+
new ScheduleBasedBackupTriggerContext(
50+
new DataProtectionBackupSchedule( new List<string>{ "R/2023-04-17T16:00:00\u002B00:00/P1D"} ){ TimeZone = "UTC" },
51+
new List<DataProtectionBackupTaggingCriteria>{ new DataProtectionBackupTaggingCriteria(true, 99, new DataProtectionBackupRetentionTag("Default")) }
52+
))
53+
{
54+
BackupParameters = new DataProtectionBackupSettings("Discrete")
55+
},
56+
new DataProtectionRetentionRule(
57+
"Default",
58+
new List<SourceLifeCycle>
59+
{
60+
new SourceLifeCycle(
61+
new DataProtectionBackupAbsoluteDeleteSetting(XmlConvert.ToTimeSpan("P30D")),
62+
new DataStoreInfoBase(DataStoreType.OperationalStore, "DataStoreInfoBase")
63+
),
64+
new SourceLifeCycle(
65+
new DataProtectionBackupAbsoluteDeleteSetting(XmlConvert.ToTimeSpan("P7D")),
66+
new DataStoreInfoBase(DataStoreType.VaultStore, "DataStoreInfoBase")
67+
)
68+
}
69+
)
70+
{
71+
IsDefault = true
72+
}
73+
};
74+
var policyData = new DataProtectionBackupPolicyData()
75+
{
76+
Properties = new RuleBasedBackupPolicy(dataSource, rules)
77+
};
78+
return policyData;
79+
}
80+
81+
public static DataProtectionBackupPolicyData GetDiskPolicyData()
82+
{
83+
List<string> dataSource = new() { "Microsoft.Compute/disks" };
84+
List<DataProtectionBasePolicyRule> rules = new()
85+
{
86+
new DataProtectionBackupRule(
87+
"Default",
88+
new DataStoreInfoBase(DataStoreType.OperationalStore, "DataStoreInfoBase"),
89+
new ScheduleBasedBackupTriggerContext(
90+
new DataProtectionBackupSchedule( new List<string>{ "R/2023-04-27T15:30:00+00:00/P1D" } ){ TimeZone = "UTC" },
91+
new List<DataProtectionBackupTaggingCriteria>{ new DataProtectionBackupTaggingCriteria(true, 99, new DataProtectionBackupRetentionTag("Default")) }
92+
))
93+
{
94+
BackupParameters = new DataProtectionBackupSettings("Incremental")
95+
{
96+
ObjectType = "AzureBackupParams"
97+
}
98+
},
99+
new DataProtectionRetentionRule(
100+
"Default",
101+
new List<SourceLifeCycle>
102+
{
103+
new SourceLifeCycle(
104+
new DataProtectionBackupAbsoluteDeleteSetting(XmlConvert.ToTimeSpan("P30D")),
105+
new DataStoreInfoBase(DataStoreType.OperationalStore, "DataStoreInfoBase")
106+
),
107+
new SourceLifeCycle(
108+
new DataProtectionBackupAbsoluteDeleteSetting(XmlConvert.ToTimeSpan("P7D"))
109+
{
110+
ObjectType = "AbsoluteDeleteOption"
111+
},
112+
new DataStoreInfoBase(DataStoreType.OperationalStore, "DataStoreInfoBase")
113+
)
114+
}
115+
)
116+
{
117+
IsDefault = true
118+
}
119+
};
120+
var policyData = new DataProtectionBackupPolicyData()
121+
{
122+
Properties = new RuleBasedBackupPolicy(dataSource, rules)
123+
};
124+
return policyData;
125+
}
126+
127+
public static void AssertpolicyData(DataProtectionBackupPolicyData data1, DataProtectionBackupPolicyData data2)
128+
{
129+
AssertResource(data1, data2);
130+
Assert.AreEqual(data1.Properties.DataSourceTypes, data2.Properties.DataSourceTypes);
131+
Assert.AreEqual(data1.Properties.ObjectType, data2.Properties.ObjectType);
132+
}
133+
#endregion
134+
135+
#region Instance
136+
public static DataProtectionBackupInstanceData GetInstanceData(ResourceIdentifier policyId, String instanceName)
137+
{
138+
var instanceData = new DataProtectionBackupInstanceData()
139+
{
140+
Properties = new DataProtectionBackupInstanceProperties(
141+
new DataSourceInfo(new ResourceIdentifier("/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c/resourcegroups/deleteme0427/providers/Microsoft.Compute/disks/sdktestdisk"))
142+
{
143+
ResourceUriString = "/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c/resourcegroups/deleteme0427/providers/Microsoft.Compute/disks/sdktestdisk",
144+
DataSourceType = "Microsoft.Compute/disks",
145+
ResourceName = "sdktestdisk",
146+
ResourceType = new ResourceType("Microsoft.Compute/disks"),
147+
ResourceLocation = AzureLocation.EastUS,
148+
ObjectType = "Datasource"
149+
},
150+
new BackupInstancePolicyInfo(policyId)
151+
{
152+
PolicyParameters = new BackupInstancePolicySettings()
153+
{
154+
BackupDataSourceParametersList = { new UnknownBackupDatasourceParameters("AzureOperationalStoreParameters") }
155+
}
156+
},
157+
"BackupInstance"
158+
)
159+
{
160+
FriendlyName = instanceName,
161+
DataSourceSetInfo = new DataSourceSetInfo(new ResourceIdentifier("/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c/resourcegroups/deleteme0427/providers/Microsoft.Compute/disks/sdktestdisk"))
162+
{
163+
ResourceUriString = "/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c/resourcegroups/deleteme0427/providers/Microsoft.Compute/disks/sdktestdisk",
164+
DataSourceType = "Microsoft.Compute/disks",
165+
ResourceName = "sdktestdisk",
166+
ResourceType = new ResourceType("Microsoft.Compute/disks"),
167+
ResourceLocation = AzureLocation.EastUS,
168+
ObjectType = "Datasource"
169+
}
170+
}
171+
};
172+
return instanceData;
173+
}
174+
public static void AssertInstanceData(DataProtectionBackupInstanceData data1, DataProtectionBackupInstanceData data2)
175+
{
176+
AssertResource(data1, data2);
177+
Assert.AreEqual(data1.Properties.ProtectionErrorDetails, data2.Properties.ProtectionErrorDetails);
178+
Assert.AreEqual(data1.Properties.ObjectType, data2.Properties.ObjectType);
179+
Assert.AreEqual(data1.Properties.ValidationType, data2.Properties.ValidationType);
180+
Assert.AreEqual(data1.Properties.ProtectionStatus, data2.Properties.ProtectionStatus);
181+
}
182+
#endregion
183+
184+
#region Vault
185+
public static DataProtectionBackupVaultData GetVaultData()
186+
{
187+
IEnumerable<DataProtectionBackupStorageSetting> setting = new List<DataProtectionBackupStorageSetting>() { new DataProtectionBackupStorageSetting()
188+
{
189+
DataStoreType = StorageSettingStoreType.VaultStore,
190+
StorageSettingType = StorageSettingType.ZoneRedundant
191+
},
192+
new DataProtectionBackupStorageSetting()
193+
{
194+
DataStoreType = StorageSettingStoreType.OperationalStore,
195+
StorageSettingType = StorageSettingType.ZoneRedundant
196+
}
197+
};
198+
var data = new DataProtectionBackupVaultData(AzureLocation.EastUS, new Models.DataProtectionBackupVaultProperties(setting))
199+
{
200+
Identity = new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAssigned)
201+
};
202+
return data;
203+
}
204+
205+
public static void AssertVaultData(DataProtectionBackupVaultData data1, DataProtectionBackupVaultData data2)
206+
{
207+
AssertResource(data1, data2);
208+
Assert.AreEqual(data1.Properties.ProvisioningState, data2.Properties.ProvisioningState);
209+
Assert.AreEqual(data1.Properties.FeatureSettings, data2.Properties.FeatureSettings);
210+
Assert.AreEqual(data1.Properties.ProvisioningState, data2.Properties.ProvisioningState);
211+
}
212+
#endregion
213+
}
214+
}

0 commit comments

Comments
 (0)