Skip to content

Commit b77fdaa

Browse files
authored
mgmt, devtestlabs, add live tests (Azure#37434)
mgmt, devtestlabs, add live tests
1 parent 4fb95dc commit b77fdaa

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

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

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

sdk/devtestlabs/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/devtestlabs/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: devtestlabs
9+
Artifacts:
10+
- name: azure-resourcemanager-devtestlabs
11+
groupId: com.azure.resourcemanager
12+
safeName: azureresourcemanagerdevtestlabs
13+
Clouds: 'Public'
14+
# Only run tests on Windows to save cost.
15+
MatrixFilters:
16+
- pool=.*(win).*

0 commit comments

Comments
 (0)