Skip to content

Commit dc5014e

Browse files
authored
mgmt, sphere, add live tests (Azure#40443)
* mgmt, sphere, add live tests
1 parent 3ae37ed commit dc5014e

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@
6969
<version>1.12.1</version> <!-- {x-version-update;com.azure:azure-identity;dependency} -->
7070
<scope>test</scope>
7171
</dependency>
72+
<dependency>
73+
<groupId>com.azure.resourcemanager</groupId>
74+
<artifactId>azure-resourcemanager-resources</artifactId>
75+
<version>2.39.0</version> <!-- {x-version-update;com.azure.resourcemanager:azure-resourcemanager-resources;dependency} -->
76+
<scope>test</scope>
77+
</dependency>
7278
<dependency>
7379
<groupId>org.junit.jupiter</groupId>
7480
<artifactId>junit-jupiter-api</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.resourcemanager.sphere;
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.LiveOnly;
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.resources.ResourceManager;
18+
import com.azure.resourcemanager.sphere.models.Catalog;
19+
import org.junit.jupiter.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.Random;
23+
24+
public class AzureSphereManagerTests extends TestBase {
25+
private static final Random RANDOM = new Random();
26+
private static final Region REGION_USEAST = Region.US_EAST;
27+
private static final Region REGION_GLOBAL = Region.create("global", "Global");
28+
private String resourceGroupName = "rg" + randomPadding();
29+
private AzureSphereManager azureSphereManager = null;
30+
private ResourceManager resourceManager;
31+
private boolean testEnv;
32+
33+
@Override
34+
public void beforeTest() {
35+
final TokenCredential credential = new DefaultAzureCredentialBuilder().build();
36+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
37+
38+
azureSphereManager = AzureSphereManager
39+
.configure()
40+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
41+
.authenticate(credential, profile);
42+
43+
resourceManager = ResourceManager
44+
.configure()
45+
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
46+
.authenticate(credential, profile)
47+
.withDefaultSubscription();
48+
49+
// use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI
50+
String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME");
51+
testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup);
52+
if (testEnv) {
53+
resourceGroupName = testResourceGroup;
54+
} else {
55+
resourceManager.resourceGroups()
56+
.define(resourceGroupName)
57+
.withRegion(REGION_USEAST)
58+
.create();
59+
}
60+
}
61+
62+
@Override
63+
protected void afterTest() {
64+
if (!testEnv) {
65+
resourceManager.resourceGroups().beginDeleteByName(resourceGroupName);
66+
}
67+
}
68+
69+
@Test
70+
@LiveOnly
71+
public void testCreateCatalog() {
72+
Catalog catalog = null;
73+
try {
74+
String catalogName = "catalog" + randomPadding();
75+
// @embedStart
76+
catalog = azureSphereManager.catalogs()
77+
.define(catalogName)
78+
.withRegion(REGION_GLOBAL)
79+
.withExistingResourceGroup(resourceGroupName)
80+
.create();
81+
// @embedEnd
82+
catalog.refresh();
83+
Assertions.assertEquals(catalogName, catalog.name());
84+
Assertions.assertEquals(catalogName, azureSphereManager.catalogs().getById(catalog.id()).name());
85+
Assertions.assertTrue(azureSphereManager.catalogs().listByResourceGroup(resourceGroupName).stream().findAny().isPresent());
86+
} finally {
87+
if (catalog != null) {
88+
azureSphereManager.catalogs().deleteById(catalog.id());
89+
}
90+
}
91+
}
92+
93+
private static String randomPadding() {
94+
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000));
95+
}
96+
}

sdk/sphere/test-resources.bicep

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/sphere/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+
extends:
6+
template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml
7+
parameters:
8+
ServiceDirectory: sphere
9+
Artifacts:
10+
- name: azure-resourcemanager-sphere
11+
groupId: com.azure.resourcemanager
12+
safeName: azureresourcemanagersphere
13+
Clouds: 'Public'
14+
# Only run tests on Windows to save cost.
15+
MatrixFilters:
16+
- pool=.*(win).*

0 commit comments

Comments
 (0)