Skip to content

Commit c912cd5

Browse files
authored
mgmt, add Live test for batch (Azure#27671)
* enable live tests on batch * update batch pool test * add jupiter engine to pom * make test class public * delete resources after test * change resource delete sequence * reduce pool size * add matrix filters to test.mgmt * nit, add comments
1 parent 7cbeecb commit c912cd5

File tree

4 files changed

+292
-21
lines changed

4 files changed

+292
-21
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
<artifactId>azure-core-management</artifactId>
5252
<version>1.5.3</version> <!-- {x-version-update;com.azure:azure-core-management;dependency} -->
5353
</dependency>
54+
<dependency>
55+
<groupId>org.junit.jupiter</groupId>
56+
<artifactId>junit-jupiter-engine</artifactId>
57+
<version>5.8.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} -->
58+
<scope>test</scope>
59+
</dependency>
5460
<dependency>
5561
<groupId>com.azure</groupId>
5662
<artifactId>azure-identity</artifactId>

sdk/batch/azure-resourcemanager-batch/src/test/java/com/azure/resourcemanager/batch/BatchTests.java

Lines changed: 243 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,94 @@
1010
import com.azure.core.management.profile.AzureProfile;
1111
import com.azure.core.test.TestBase;
1212
import com.azure.core.test.annotation.DoNotRecord;
13+
import com.azure.core.util.Configuration;
14+
import com.azure.core.util.CoreUtils;
1315
import com.azure.identity.DefaultAzureCredentialBuilder;
16+
import com.azure.resourcemanager.batch.models.AccountKeyType;
17+
import com.azure.resourcemanager.batch.models.Application;
18+
import com.azure.resourcemanager.batch.models.ApplicationPackage;
1419
import com.azure.resourcemanager.batch.models.AutoStorageBaseProperties;
1520
import com.azure.resourcemanager.batch.models.BatchAccount;
21+
import com.azure.resourcemanager.batch.models.BatchAccountKeys;
22+
import com.azure.resourcemanager.batch.models.BatchAccountRegenerateKeyParameters;
23+
import com.azure.resourcemanager.batch.models.CloudServiceConfiguration;
24+
import com.azure.resourcemanager.batch.models.ComputeNodeDeallocationOption;
25+
import com.azure.resourcemanager.batch.models.DeploymentConfiguration;
26+
import com.azure.resourcemanager.batch.models.FixedScaleSettings;
27+
import com.azure.resourcemanager.batch.models.Pool;
28+
import com.azure.resourcemanager.batch.models.ScaleSettings;
1629
import com.azure.resourcemanager.storage.StorageManager;
1730
import com.azure.resourcemanager.storage.models.StorageAccount;
31+
import org.junit.jupiter.api.Assertions;
1832
import org.junit.jupiter.api.Test;
1933

34+
import java.time.Duration;
35+
import java.time.OffsetDateTime;
2036
import java.util.Random;
2137

2238
import static org.junit.jupiter.api.Assertions.assertEquals;
2339
import static org.junit.jupiter.api.Assertions.assertNotNull;
2440

2541

26-
class BatchTests extends TestBase {
42+
public class BatchTests extends TestBase {
2743

2844
private static final Random RANDOM = new Random();
2945

3046
private static final Region REGION = Region.US_WEST2;
31-
private static final String RESOURCE_GROUP = "rg" + randomPadding();
32-
private static final String STORAGE_ACCOUNT = "sa" + randomPadding();
33-
private static final String BATCH_ACCOUNT = "ba" + randomPadding();
47+
private String resourceGroup = "rg" + randomPadding();
48+
private BatchManager batchManager;
49+
private StorageManager storageManager;
50+
private boolean testEnv;
3451

35-
@Test
36-
@DoNotRecord(skipInPlayback = true)
37-
public void testCreateBatchAccount() {
38-
39-
BatchManager batchManager = BatchManager
52+
@Override
53+
public void beforeTest() {
54+
batchManager = BatchManager
4055
.configure().withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
4156
.authenticate(new DefaultAzureCredentialBuilder().build(), new AzureProfile(AzureEnvironment.AZURE));
4257

43-
StorageManager storageManager = StorageManager
58+
storageManager = StorageManager
4459
.configure().withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
4560
.authenticate(new DefaultAzureCredentialBuilder().build(), new AzureProfile(AzureEnvironment.AZURE));
4661

47-
try {
48-
// resource group
49-
storageManager.resourceManager().resourceGroups().define(RESOURCE_GROUP)
62+
String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME");
63+
testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup);
64+
if (testEnv) {
65+
resourceGroup = testResourceGroup;
66+
} else {
67+
storageManager.resourceManager().resourceGroups().define(resourceGroup)
5068
.withRegion(REGION)
5169
.create();
70+
}
71+
}
72+
73+
@Override
74+
protected void afterTest() {
75+
if (!testEnv) {
76+
storageManager.resourceManager().resourceGroups().beginDeleteByName(resourceGroup);
77+
}
78+
}
5279

80+
@Test
81+
@DoNotRecord(skipInPlayback = true)
82+
public void testCreateBatchAccount() {
83+
StorageAccount storageAccount = null;
84+
BatchAccount account = null;
85+
try {
5386
// storage account
54-
StorageAccount storageAccount = storageManager.storageAccounts().define(STORAGE_ACCOUNT)
87+
final String storageAccountName = "sa" + randomPadding();
88+
89+
storageAccount = storageManager.storageAccounts().define(storageAccountName)
5590
.withRegion(REGION)
56-
.withExistingResourceGroup(RESOURCE_GROUP)
91+
.withExistingResourceGroup(resourceGroup)
5792
.create();
5893

5994
// batch account
60-
BatchAccount account = batchManager
95+
final String batchAccountName = "ba" + randomPadding();
96+
account = batchManager
6197
.batchAccounts()
62-
.define(BATCH_ACCOUNT)
98+
.define(batchAccountName)
6399
.withRegion(REGION)
64-
.withExistingResourceGroup(RESOURCE_GROUP)
100+
.withExistingResourceGroup(resourceGroup)
65101
.withAutoStorage(
66102
new AutoStorageBaseProperties()
67103
.withStorageAccountId(storageAccount.id()))
@@ -70,14 +106,200 @@ public void testCreateBatchAccount() {
70106

71107
assertNotNull(account);
72108

73-
BatchAccount batchAccount = batchManager.batchAccounts().getByResourceGroup(RESOURCE_GROUP, BATCH_ACCOUNT);
74-
assertEquals(BATCH_ACCOUNT, batchAccount.name());
109+
BatchAccount batchAccount = batchManager.batchAccounts().getByResourceGroup(resourceGroup, batchAccountName);
110+
assertEquals(batchAccountName, batchAccount.name());
75111
assertEquals(REGION.toString(), batchAccount.location());
112+
} finally {
113+
if (account != null) {
114+
batchManager.batchAccounts().deleteById(account.id());
115+
}
116+
if (storageAccount != null) {
117+
storageManager.storageAccounts().deleteById(storageAccount.id());
118+
}
119+
}
120+
}
121+
122+
@Test
123+
@DoNotRecord(skipInPlayback = true)
124+
public void testCRUDBatchAccount() {
125+
BatchAccount account = null;
126+
StorageAccount storageAccount = null;
127+
final String batchAccountName;
128+
try {
129+
// batch account
130+
batchAccountName = "sa" + randomPadding();
131+
account = batchManager
132+
.batchAccounts()
133+
.define(batchAccountName)
134+
.withRegion(REGION)
135+
.withExistingResourceGroup(resourceGroup)
136+
.create();
137+
Assertions.assertNull(account.autoStorage());
138+
139+
// batch account data plane access key
140+
BatchAccountKeys keys = account.getKeys();
141+
Assertions.assertNotNull(keys.primary());
142+
Assertions.assertNotNull(keys.secondary());
143+
144+
BatchAccountKeys regeneratedKeys = account.regenerateKey(new BatchAccountRegenerateKeyParameters().withKeyName(AccountKeyType.PRIMARY));
145+
Assertions.assertNotNull(regeneratedKeys.primary());
146+
Assertions.assertNotNull(regeneratedKeys.secondary());
147+
148+
// storage account
149+
final String storageAccountName = "sa" + randomPadding();
150+
storageAccount = storageManager
151+
.storageAccounts()
152+
.define(storageAccountName)
153+
.withRegion(REGION)
154+
.withExistingResourceGroup(resourceGroup)
155+
.create();
156+
account
157+
.update()
158+
.withAutoStorage(new AutoStorageBaseProperties().withStorageAccountId(storageAccount.id()))
159+
.apply();
160+
Assertions.assertNotNull(account.autoStorage().storageAccountId());
161+
OffsetDateTime lastKeySync = account.autoStorage().lastKeySync();
162+
Assertions.assertNotNull(lastKeySync);
76163

164+
account.synchronizeAutoStorageKeys();
165+
account.refresh();
166+
167+
Assertions.assertNotEquals(lastKeySync, account.autoStorage().lastKeySync());
77168
} finally {
78-
storageManager.resourceManager().resourceGroups().beginDeleteByName(RESOURCE_GROUP);
169+
if (account != null) {
170+
batchManager.batchAccounts().deleteById(account.id());
171+
}
172+
if (storageAccount != null) {
173+
storageManager.storageAccounts().deleteById(storageAccount.id());
174+
}
79175
}
176+
}
177+
178+
@Test
179+
@DoNotRecord(skipInPlayback = true)
180+
public void testCRUDBatchApplication() {
181+
StorageAccount storageAccount = null;
182+
BatchAccount account = null;
183+
Application application = null;
184+
ApplicationPackage applicationPackage = null;
185+
final String batchAccountName;
186+
final String applicationName;
187+
String packageVersion;
188+
try {
189+
// storage account
190+
final String storageAccountName = "sa" + randomPadding();
191+
storageAccount = storageManager
192+
.storageAccounts().
193+
define(storageAccountName)
194+
.withRegion(REGION)
195+
.withExistingResourceGroup(resourceGroup)
196+
.create();
197+
// batch account
198+
batchAccountName = "sa" + randomPadding();
199+
account = batchManager
200+
.batchAccounts()
201+
.define(batchAccountName)
202+
.withRegion(REGION)
203+
.withExistingResourceGroup(resourceGroup)
204+
.withAutoStorage(new AutoStorageBaseProperties().withStorageAccountId(storageAccount.id()))
205+
.create();
206+
207+
// create application with batch account
208+
applicationName = "ba" + randomPadding();
209+
String displayName = "badn" + randomPadding();
210+
application = batchManager
211+
.applications()
212+
.define(applicationName)
213+
.withExistingBatchAccount(resourceGroup, batchAccountName)
214+
.withDisplayName(displayName)
215+
.withAllowUpdates(true)
216+
.create();
217+
Assertions.assertEquals(application.displayName(), displayName);
218+
Assertions.assertEquals(application.name(), applicationName);
219+
Assertions.assertNull(application.defaultVersion());
220+
221+
// update application
222+
String newDisplayName = "newbadn" + randomPadding();
223+
application
224+
.update()
225+
.withDisplayName(newDisplayName)
226+
.apply();
227+
Assertions.assertNotEquals(displayName, application.displayName());
80228

229+
packageVersion = "version" + randomPadding();
230+
applicationPackage = batchManager
231+
.applicationPackages()
232+
.define(packageVersion)
233+
.withExistingApplication(resourceGroup, batchAccountName, applicationName)
234+
.create();
235+
Assertions.assertNotNull(applicationPackage);
236+
Assertions.assertNull(applicationPackage.lastActivationTime());
237+
} finally {
238+
// all application packages must be deleted before the application can be deleted
239+
if (applicationPackage != null) {
240+
batchManager.applicationPackages().deleteById(applicationPackage.id());
241+
}
242+
if (application != null) {
243+
batchManager.applications().deleteById(application.id());
244+
}
245+
if (account != null) {
246+
batchManager.batchAccounts().deleteById(account.id());
247+
}
248+
if (storageAccount != null) {
249+
storageManager.storageAccounts().deleteById(storageAccount.id());
250+
}
251+
}
252+
}
253+
254+
@Test
255+
@DoNotRecord(skipInPlayback = true)
256+
public void testCRUDBatchPool() {
257+
BatchAccount account = null;
258+
Pool pool = null;
259+
final String batchAccountName;
260+
String poolName;
261+
try {
262+
// batch account
263+
batchAccountName = "sa" + randomPadding();
264+
account = batchManager
265+
.batchAccounts()
266+
.define(batchAccountName)
267+
.withRegion(REGION)
268+
.withExistingResourceGroup(resourceGroup)
269+
.create();
270+
// batch pool create
271+
poolName = "bp" + randomPadding();
272+
String poolDisplayName = "bpdn" + randomPadding();
273+
pool = batchManager.pools()
274+
.define(poolName)
275+
.withExistingBatchAccount(resourceGroup, batchAccountName)
276+
.withDisplayName(poolDisplayName)
277+
.withDeploymentConfiguration(
278+
new DeploymentConfiguration()
279+
.withCloudServiceConfiguration(
280+
new CloudServiceConfiguration().withOsFamily("4")))
281+
.withScaleSettings(
282+
new ScaleSettings()
283+
.withFixedScale(
284+
new FixedScaleSettings()
285+
.withResizeTimeout(Duration.parse("PT8M"))
286+
.withTargetDedicatedNodes(1)
287+
.withTargetLowPriorityNodes(1)
288+
.withNodeDeallocationOption(ComputeNodeDeallocationOption.TASK_COMPLETION)))
289+
.withVmSize("Standard_D1")
290+
.create();
291+
Assertions.assertEquals(poolName, pool.name());
292+
Assertions.assertEquals(poolDisplayName, pool.displayName());
293+
Assertions.assertNull(pool.scaleSettings().autoScale());
294+
Assertions.assertEquals(pool.scaleSettings().fixedScale().nodeDeallocationOption(), ComputeNodeDeallocationOption.TASK_COMPLETION);
295+
} finally {
296+
if (pool != null) {
297+
batchManager.pools().deleteById(pool.id());
298+
}
299+
if (account != null) {
300+
batchManager.batchAccounts().deleteById(account.id());
301+
}
302+
}
81303
}
82304

83305
private static String randomPadding() {

sdk/batch/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@2020-08-01-preview' = {
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/batch/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: batch
9+
Artifacts:
10+
- name: azure-resourcemanager-batch
11+
groupId: com.azure.resourcemanager
12+
safeName: azureresourcemanagerbatch
13+
Clouds: 'Public'
14+
# Batch account has regional quota limit on test subscription. Only run tests on Windows to avoid exceeding.
15+
MatrixFilters:
16+
- pool=.*(win).*

0 commit comments

Comments
 (0)