Skip to content

Commit 3ddcf0c

Browse files
authored
mgmt, dataprotection, add live tests (Azure#36410)
1 parent c383a02 commit 3ddcf0c

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

sdk/dataprotection/azure-resourcemanager-dataprotection/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,11 @@
9393
<version>1.7.36</version> <!-- {x-version-update;org.slf4j:slf4j-simple;external_dependency} -->
9494
<scope>test</scope>
9595
</dependency>
96+
<dependency>
97+
<groupId>com.azure.resourcemanager</groupId>
98+
<artifactId>azure-resourcemanager-resources</artifactId>
99+
<version>2.29.0</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;dependency} -->
100+
<scope>test</scope>
101+
</dependency>
96102
</dependencies>
97103
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.resourcemanager.dataprotection;
5+
6+
import com.azure.core.credential.TokenCredential;
7+
import com.azure.core.http.policy.HttpLogDetailLevel;
8+
import com.azure.core.http.policy.HttpLogOptions;
9+
import com.azure.core.management.AzureEnvironment;
10+
import com.azure.core.management.Region;
11+
import com.azure.core.management.profile.AzureProfile;
12+
import com.azure.core.test.TestBase;
13+
import com.azure.core.test.annotation.DoNotRecord;
14+
import com.azure.core.util.Configuration;
15+
import com.azure.core.util.CoreUtils;
16+
import com.azure.identity.DefaultAzureCredentialBuilder;
17+
import com.azure.resourcemanager.dataprotection.models.AlertsState;
18+
import com.azure.resourcemanager.dataprotection.models.AzureMonitorAlertSettings;
19+
import com.azure.resourcemanager.dataprotection.models.BackupVault;
20+
import com.azure.resourcemanager.dataprotection.models.BackupVaultResource;
21+
import com.azure.resourcemanager.dataprotection.models.CrossSubscriptionRestoreSettings;
22+
import com.azure.resourcemanager.dataprotection.models.CrossSubscriptionRestoreState;
23+
import com.azure.resourcemanager.dataprotection.models.DppIdentityDetails;
24+
import com.azure.resourcemanager.dataprotection.models.FeatureSettings;
25+
import com.azure.resourcemanager.dataprotection.models.ImmutabilitySettings;
26+
import com.azure.resourcemanager.dataprotection.models.ImmutabilityState;
27+
import com.azure.resourcemanager.dataprotection.models.MonitoringSettings;
28+
import com.azure.resourcemanager.dataprotection.models.SecuritySettings;
29+
import com.azure.resourcemanager.dataprotection.models.SoftDeleteSettings;
30+
import com.azure.resourcemanager.dataprotection.models.SoftDeleteState;
31+
import com.azure.resourcemanager.dataprotection.models.StorageSetting;
32+
import com.azure.resourcemanager.dataprotection.models.StorageSettingStoreTypes;
33+
import com.azure.resourcemanager.dataprotection.models.StorageSettingTypes;
34+
import com.azure.resourcemanager.resources.ResourceManager;
35+
import org.junit.jupiter.api.Assertions;
36+
import org.junit.jupiter.api.Test;
37+
38+
import java.util.Collections;
39+
import java.util.Random;
40+
41+
public class DataProtectionManagerTest extends TestBase {
42+
private static final Random RANDOM = new Random();
43+
private static final Region REGION = Region.US_WEST2;
44+
private String resourceGroupName = "rg" + randomPadding();
45+
private DataProtectionManager dataProtectionManager;
46+
private ResourceManager resourceManager;
47+
private boolean testEnv;
48+
49+
@Override
50+
public void beforeTest() {
51+
final TokenCredential credential = new DefaultAzureCredentialBuilder().build();
52+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
53+
54+
dataProtectionManager = DataProtectionManager
55+
.configure()
56+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
57+
.authenticate(credential, profile);
58+
59+
resourceManager = ResourceManager
60+
.configure()
61+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
62+
.authenticate(credential, profile)
63+
.withDefaultSubscription();
64+
65+
// use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI
66+
String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME");
67+
testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup);
68+
if (testEnv) {
69+
resourceGroupName = testResourceGroup;
70+
} else {
71+
resourceManager.resourceGroups()
72+
.define(resourceGroupName)
73+
.withRegion(REGION)
74+
.create();
75+
}
76+
}
77+
78+
@Override
79+
protected void afterTest() {
80+
if (!testEnv) {
81+
resourceManager.resourceGroups().beginDeleteByName(resourceGroupName);
82+
}
83+
}
84+
85+
@Test
86+
@DoNotRecord(skipInPlayback = true)
87+
public void testCreateBackupVault() {
88+
BackupVaultResource resource = null;
89+
try {
90+
String vaultName = "vault" + randomPadding();
91+
// @embedmeStart
92+
resource = dataProtectionManager
93+
.backupVaults()
94+
.define(vaultName)
95+
.withRegion(REGION)
96+
.withExistingResourceGroup(resourceGroupName)
97+
.withProperties(
98+
new BackupVault()
99+
.withMonitoringSettings(
100+
new MonitoringSettings()
101+
.withAzureMonitorAlertSettings(
102+
new AzureMonitorAlertSettings()
103+
.withAlertsForAllJobFailures(AlertsState.ENABLED)))
104+
.withSecuritySettings(
105+
new SecuritySettings()
106+
.withSoftDeleteSettings(
107+
new SoftDeleteSettings()
108+
.withState(SoftDeleteState.ALWAYS_ON)
109+
.withRetentionDurationInDays(14.0D))
110+
.withImmutabilitySettings(
111+
new ImmutabilitySettings()
112+
.withState(ImmutabilityState.LOCKED)))
113+
.withStorageSettings(
114+
Collections.singletonList(
115+
new StorageSetting()
116+
.withDatastoreType(StorageSettingStoreTypes.VAULT_STORE)
117+
.withType(StorageSettingTypes.LOCALLY_REDUNDANT)))
118+
.withFeatureSettings(
119+
new FeatureSettings()
120+
.withCrossSubscriptionRestoreSettings(
121+
new CrossSubscriptionRestoreSettings()
122+
.withState(CrossSubscriptionRestoreState.ENABLED))))
123+
.withIdentity(new DppIdentityDetails().withType("systemAssigned"))
124+
.create();
125+
// @embedmeEnd
126+
resource.refresh();
127+
Assertions.assertEquals(resource.name(), vaultName);
128+
Assertions.assertEquals(resource.name(), dataProtectionManager.backupVaults().getById(resource.id()).name());
129+
Assertions.assertTrue(dataProtectionManager.backupVaults().list().stream().count() > 0);
130+
} finally {
131+
if (resource != null) {
132+
dataProtectionManager.backupVaults().deleteById(resource.id());
133+
}
134+
}
135+
}
136+
137+
private static String randomPadding() {
138+
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000));
139+
}
140+
141+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@description('The tenant id to which the application and resources belong.')
2+
param tenantId string = '72f988bf-86f1-41af-91ab-2d7cd011db47'
3+
4+
@description('The client id of the service principal used to run tests.')
5+
param testApplicationId string
6+
7+
@description('This is the object id of the service principal used to run tests.')
8+
param testApplicationOid string
9+
10+
@description('The application client secret used to run tests.')
11+
param testApplicationSecret string
12+
13+
var contributorRoleId = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c'
14+
15+
resource contributorRoleId_name 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
16+
name: guid('contributorRoleId${resourceGroup().name}')
17+
properties: {
18+
roleDefinitionId: contributorRoleId
19+
principalId: testApplicationOid
20+
}
21+
}
22+
23+
output AZURE_TENANT_ID string = tenantId
24+
output AZURE_CLIENT_ID string = testApplicationId
25+
output AZURE_CLIENT_SECRET string = testApplicationSecret
26+
output AZURE_SUBSCRIPTION_ID string = subscription().subscriptionId
27+
output AZURE_RESOURCE_GROUP_NAME string = resourceGroup().name

sdk/dataprotection/tests.mgmt.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trigger: none
2+
3+
pr: none
4+
5+
stages:
6+
- template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml
7+
parameters:
8+
ServiceDirectory: dataprotection
9+
Artifacts:
10+
- name: azure-resourcemanager-dataprotection
11+
groupId: com.azure.resourcemanager
12+
safeName: azureresourcemanagerdataprotection
13+
Clouds: 'Public'
14+
# Only run tests on Windows to save cost.
15+
MatrixFilters:
16+
- pool=.*(win).*

0 commit comments

Comments
 (0)