Skip to content

Commit 79a119d

Browse files
authored
Add live test case for storage actions (Azure#45208)
* Add live test case for storage actions * fix commits
1 parent 67b30e8 commit 79a119d

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,11 @@
8888
<version>1.15.5</version> <!-- {x-version-update;testdep_net.bytebuddy:byte-buddy-agent;external_dependency} -->
8989
<scope>test</scope>
9090
</dependency>
91+
<dependency>
92+
<groupId>com.azure.resourcemanager</groupId>
93+
<artifactId>azure-resourcemanager-resources</artifactId>
94+
<version>2.50.0</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;dependency} -->
95+
<scope>test</scope>
96+
</dependency>
9197
</dependencies>
9298
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.resourcemanager.storageactions;
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.TestProxyTestBase;
13+
import com.azure.core.test.annotation.LiveOnly;
14+
import com.azure.core.util.Configuration;
15+
import com.azure.core.util.CoreUtils;
16+
import com.azure.identity.AzurePowerShellCredentialBuilder;
17+
import com.azure.resourcemanager.resources.ResourceManager;
18+
import com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy;
19+
import com.azure.resourcemanager.storageactions.models.IfCondition;
20+
import com.azure.resourcemanager.storageactions.models.ManagedServiceIdentity;
21+
import com.azure.resourcemanager.storageactions.models.ManagedServiceIdentityType;
22+
import com.azure.resourcemanager.storageactions.models.OnFailure;
23+
import com.azure.resourcemanager.storageactions.models.OnSuccess;
24+
import com.azure.resourcemanager.storageactions.models.StorageTask;
25+
import com.azure.resourcemanager.storageactions.models.StorageTaskAction;
26+
import com.azure.resourcemanager.storageactions.models.StorageTaskOperation;
27+
import com.azure.resourcemanager.storageactions.models.StorageTaskOperationName;
28+
import com.azure.resourcemanager.storageactions.models.StorageTaskProperties;
29+
import org.junit.jupiter.api.Assertions;
30+
import org.junit.jupiter.api.Test;
31+
32+
import java.util.Arrays;
33+
import java.util.LinkedHashMap;
34+
import java.util.Map;
35+
import java.util.Random;
36+
37+
public class StorageActionsManagerTests extends TestProxyTestBase {
38+
private static final Random RANDOM = new Random();
39+
private static final Region REGION = Region.US_WEST3;
40+
private String resourceGroupName = "rg" + randomPadding();
41+
private StorageActionsManager storageActionsManager = null;
42+
private ResourceManager resourceManager;
43+
private boolean testEnv;
44+
45+
@Override
46+
public void beforeTest() {
47+
final TokenCredential credential = new AzurePowerShellCredentialBuilder().build();
48+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
49+
50+
resourceManager = ResourceManager.configure()
51+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
52+
.authenticate(credential, profile)
53+
.withDefaultSubscription();
54+
55+
storageActionsManager = StorageActionsManager.configure()
56+
.withPolicy(new ProviderRegistrationPolicy(resourceManager))
57+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
58+
.authenticate(credential, profile);
59+
60+
// use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI
61+
String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME");
62+
testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup);
63+
if (testEnv) {
64+
resourceGroupName = testResourceGroup;
65+
} else {
66+
resourceManager.resourceGroups().define(resourceGroupName).withRegion(REGION).create();
67+
}
68+
}
69+
70+
@Override
71+
protected void afterTest() {
72+
if (!testEnv) {
73+
resourceManager.resourceGroups().beginDeleteByName(resourceGroupName);
74+
}
75+
}
76+
77+
@Test
78+
@LiveOnly
79+
public void testCreateStorageTask() {
80+
StorageTask storageTask = null;
81+
try {
82+
String taskName = "task" + randomPadding();
83+
// @embedmeStart
84+
Map<String, String> operationMap = new LinkedHashMap<>();
85+
operationMap.put("tier", "Hot");
86+
storageTask = storageActionsManager.storageTasks()
87+
.define(taskName)
88+
.withRegion(REGION)
89+
.withExistingResourceGroup(resourceGroupName)
90+
.withIdentity(new ManagedServiceIdentity().withType(ManagedServiceIdentityType.SYSTEM_ASSIGNED))
91+
.withProperties(new StorageTaskProperties()
92+
.withAction(new StorageTaskAction()
93+
.withIfProperty(new IfCondition().withCondition("[[[equals(AccessTier, 'Cool')]]")
94+
.withOperations(Arrays
95+
.asList(new StorageTaskOperation().withName(StorageTaskOperationName.SET_BLOB_TIER)
96+
.withParameters(operationMap)
97+
.withOnSuccess(OnSuccess.CONTINUE)
98+
.withOnFailure(OnFailure.BREAK)))))
99+
.withDescription("Storage task")
100+
.withEnabled(true))
101+
.create();
102+
// @embedmeEnd
103+
storageTask.refresh();
104+
Assertions.assertEquals(taskName, storageTask.name());
105+
Assertions.assertEquals(taskName, storageActionsManager.storageTasks().getById(storageTask.id()).name());
106+
Assertions.assertTrue(storageActionsManager.storageTasks()
107+
.listByResourceGroup(resourceGroupName)
108+
.stream()
109+
.findAny()
110+
.isPresent());
111+
} finally {
112+
if (storageTask != null) {
113+
storageActionsManager.storageTasks().deleteById(storageTask.id());
114+
}
115+
}
116+
}
117+
118+
private static String randomPadding() {
119+
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000));
120+
}
121+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
var contributorRoleId = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c'
11+
12+
resource contributorRoleId_name 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
13+
name: guid('contributorRoleId${resourceGroup().name}')
14+
properties: {
15+
roleDefinitionId: contributorRoleId
16+
principalId: testApplicationOid
17+
}
18+
}
19+
20+
output AZURE_TENANT_ID string = tenantId
21+
output AZURE_SUBSCRIPTION_ID string = subscription().subscriptionId
22+
output AZURE_RESOURCE_GROUP_NAME string = resourceGroup().name

sdk/storageactions/tests.mgmt.yml

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

0 commit comments

Comments
 (0)