1010import com .azure .core .management .profile .AzureProfile ;
1111import com .azure .core .test .TestBase ;
1212import com .azure .core .test .annotation .DoNotRecord ;
13+ import com .azure .core .util .Configuration ;
14+ import com .azure .core .util .CoreUtils ;
1315import 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 ;
1419import com .azure .resourcemanager .batch .models .AutoStorageBaseProperties ;
1520import 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 ;
1629import com .azure .resourcemanager .storage .StorageManager ;
1730import com .azure .resourcemanager .storage .models .StorageAccount ;
31+ import org .junit .jupiter .api .Assertions ;
1832import org .junit .jupiter .api .Test ;
1933
34+ import java .time .Duration ;
35+ import java .time .OffsetDateTime ;
2036import java .util .Random ;
2137
2238import static org .junit .jupiter .api .Assertions .assertEquals ;
2339import 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 () {
0 commit comments