/sdk/compute/mgmt/src/test/java/com/azure/management/compute/VirtualMachineBootDiagnosticsTests.java

http://github.com/WindowsAzure/azure-sdk-for-java · Java · 285 lines · 252 code · 27 blank · 6 comment · 0 complexity · 579a66d073bd80657fa2c42600cd7b4d MD5 · raw file

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. package com.azure.management.compute;
  4. import com.azure.core.http.HttpPipeline;
  5. import com.azure.management.resources.fluentcore.arm.Region;
  6. import com.azure.management.resources.fluentcore.model.Creatable;
  7. import com.azure.management.resources.fluentcore.profile.AzureProfile;
  8. import com.azure.management.storage.StorageAccount;
  9. import org.junit.jupiter.api.Assertions;
  10. import org.junit.jupiter.api.Test;
  11. public class VirtualMachineBootDiagnosticsTests extends ComputeManagementTest {
  12. private String rgName = "";
  13. private final Region region = Region.US_SOUTH_CENTRAL;
  14. private final String vmName = "javavm";
  15. @Override
  16. protected void initializeClients(HttpPipeline httpPipeline, AzureProfile profile) {
  17. rgName = generateRandomResourceName("javacsmrg", 15);
  18. super.initializeClients(httpPipeline, profile);
  19. }
  20. @Override
  21. protected void cleanUpResources() {
  22. resourceManager.resourceGroups().deleteByName(rgName);
  23. }
  24. @Test
  25. public void canEnableBootDiagnosticsWithImplicitStorageOnManagedVMCreation() {
  26. VirtualMachine virtualMachine =
  27. computeManager
  28. .virtualMachines()
  29. .define(vmName)
  30. .withRegion(region)
  31. .withNewResourceGroup(rgName)
  32. .withNewPrimaryNetwork("10.0.0.0/28")
  33. .withPrimaryPrivateIPAddressDynamic()
  34. .withoutPrimaryPublicIPAddress()
  35. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  36. .withRootUsername("Foo12")
  37. .withRootPassword("abc!@#F0orL")
  38. .withBootDiagnostics()
  39. .create();
  40. Assertions.assertNotNull(virtualMachine);
  41. Assertions.assertTrue(virtualMachine.isBootDiagnosticsEnabled());
  42. Assertions.assertNotNull(virtualMachine.bootDiagnosticsStorageUri());
  43. }
  44. @Test
  45. public void canEnableBootDiagnosticsWithCreatableStorageOnManagedVMCreation() {
  46. final String storageName = sdkContext.randomResourceName("st", 14);
  47. Creatable<StorageAccount> creatableStorageAccount =
  48. storageManager.storageAccounts().define(storageName).withRegion(region).withNewResourceGroup(rgName);
  49. VirtualMachine virtualMachine =
  50. computeManager
  51. .virtualMachines()
  52. .define(vmName)
  53. .withRegion(region)
  54. .withNewResourceGroup(rgName)
  55. .withNewPrimaryNetwork("10.0.0.0/28")
  56. .withPrimaryPrivateIPAddressDynamic()
  57. .withoutPrimaryPublicIPAddress()
  58. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  59. .withRootUsername("Foo12")
  60. .withRootPassword("abc!@#F0orL")
  61. .withBootDiagnostics(creatableStorageAccount)
  62. .create();
  63. Assertions.assertNotNull(virtualMachine);
  64. Assertions.assertTrue(virtualMachine.isBootDiagnosticsEnabled());
  65. Assertions.assertNotNull(virtualMachine.bootDiagnosticsStorageUri());
  66. Assertions.assertTrue(virtualMachine.bootDiagnosticsStorageUri().contains(storageName));
  67. }
  68. @Test
  69. public void canEnableBootDiagnosticsWithExplicitStorageOnManagedVMCreation() {
  70. final String storageName = sdkContext.randomResourceName("st", 14);
  71. StorageAccount storageAccount =
  72. storageManager
  73. .storageAccounts()
  74. .define(storageName)
  75. .withRegion(region)
  76. .withNewResourceGroup(rgName)
  77. .create();
  78. VirtualMachine virtualMachine =
  79. computeManager
  80. .virtualMachines()
  81. .define(vmName)
  82. .withRegion(region)
  83. .withNewResourceGroup(rgName)
  84. .withNewPrimaryNetwork("10.0.0.0/28")
  85. .withPrimaryPrivateIPAddressDynamic()
  86. .withoutPrimaryPublicIPAddress()
  87. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  88. .withRootUsername("Foo12")
  89. .withRootPassword("abc!@#F0orL")
  90. .withBootDiagnostics(storageAccount)
  91. .create();
  92. Assertions.assertNotNull(virtualMachine);
  93. Assertions.assertTrue(virtualMachine.isBootDiagnosticsEnabled());
  94. Assertions.assertNotNull(virtualMachine.bootDiagnosticsStorageUri());
  95. Assertions.assertTrue(virtualMachine.bootDiagnosticsStorageUri().contains(storageName));
  96. }
  97. @Test
  98. public void canDisableBootDiagnostics() {
  99. VirtualMachine virtualMachine =
  100. computeManager
  101. .virtualMachines()
  102. .define(vmName)
  103. .withRegion(region)
  104. .withNewResourceGroup(rgName)
  105. .withNewPrimaryNetwork("10.0.0.0/28")
  106. .withPrimaryPrivateIPAddressDynamic()
  107. .withoutPrimaryPublicIPAddress()
  108. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  109. .withRootUsername("Foo12")
  110. .withRootPassword("abc!@#F0orLX")
  111. .withBootDiagnostics()
  112. .create();
  113. Assertions.assertNotNull(virtualMachine);
  114. Assertions.assertTrue(virtualMachine.isBootDiagnosticsEnabled());
  115. Assertions.assertNotNull(virtualMachine.bootDiagnosticsStorageUri());
  116. virtualMachine.update().withoutBootDiagnostics().apply();
  117. Assertions.assertFalse(virtualMachine.isBootDiagnosticsEnabled());
  118. // Disabling boot diagnostics will not remove the storage uri from the vm payload.
  119. Assertions.assertNotNull(virtualMachine.bootDiagnosticsStorageUri());
  120. }
  121. @Test
  122. public void bootDiagnosticsShouldUsesOSUnManagedDiskImplicitStorage() {
  123. VirtualMachine virtualMachine =
  124. computeManager
  125. .virtualMachines()
  126. .define(vmName)
  127. .withRegion(region)
  128. .withNewResourceGroup(rgName)
  129. .withNewPrimaryNetwork("10.0.0.0/28")
  130. .withPrimaryPrivateIPAddressDynamic()
  131. .withoutPrimaryPublicIPAddress()
  132. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  133. .withRootUsername("Foo12")
  134. .withRootPassword("abc!@#F0orL")
  135. .withUnmanagedDisks() // The implicit storage account for OS disk should be used for boot diagnostics as
  136. // well
  137. .withBootDiagnostics()
  138. .create();
  139. Assertions.assertNotNull(virtualMachine);
  140. Assertions.assertTrue(virtualMachine.isBootDiagnosticsEnabled());
  141. Assertions.assertNotNull(virtualMachine.bootDiagnosticsStorageUri());
  142. Assertions.assertNotNull(virtualMachine.osUnmanagedDiskVhdUri());
  143. Assertions
  144. .assertTrue(
  145. virtualMachine
  146. .osUnmanagedDiskVhdUri()
  147. .toLowerCase()
  148. .startsWith(virtualMachine.bootDiagnosticsStorageUri().toLowerCase()));
  149. }
  150. @Test
  151. public void bootDiagnosticsShouldUseUnManagedDisksExplicitStorage() {
  152. final String storageName = sdkContext.randomResourceName("st", 14);
  153. StorageAccount storageAccount =
  154. storageManager
  155. .storageAccounts()
  156. .define(storageName)
  157. .withRegion(region)
  158. .withNewResourceGroup(rgName)
  159. .create();
  160. VirtualMachine virtualMachine =
  161. computeManager
  162. .virtualMachines()
  163. .define(vmName)
  164. .withRegion(region)
  165. .withNewResourceGroup(rgName)
  166. .withNewPrimaryNetwork("10.0.0.0/28")
  167. .withPrimaryPrivateIPAddressDynamic()
  168. .withoutPrimaryPublicIPAddress()
  169. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  170. .withRootUsername("Foo12")
  171. .withRootPassword("abc!@#F0orL")
  172. .withUnmanagedDisks()
  173. .withBootDiagnostics()
  174. .withExistingStorageAccount(
  175. storageAccount) // This storage account must be shared by disk and boot diagnostics
  176. .create();
  177. Assertions.assertNotNull(virtualMachine);
  178. Assertions.assertTrue(virtualMachine.isBootDiagnosticsEnabled());
  179. Assertions.assertNotNull(virtualMachine.bootDiagnosticsStorageUri());
  180. Assertions.assertTrue(virtualMachine.bootDiagnosticsStorageUri().contains(storageName));
  181. }
  182. @Test
  183. public void canEnableBootDiagnosticsWithImplicitStorageOnUnManagedVMCreation() {
  184. VirtualMachine virtualMachine1 =
  185. computeManager
  186. .virtualMachines()
  187. .define(vmName)
  188. .withRegion(region)
  189. .withNewResourceGroup(rgName)
  190. .withNewPrimaryNetwork("10.0.0.0/28")
  191. .withPrimaryPrivateIPAddressDynamic()
  192. .withoutPrimaryPublicIPAddress()
  193. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  194. .withRootUsername("Foo12")
  195. .withRootPassword("abc!@#F0orL")
  196. .withUnmanagedDisks()
  197. .create();
  198. String osDiskVhd = virtualMachine1.osUnmanagedDiskVhdUri();
  199. Assertions.assertNotNull(osDiskVhd);
  200. computeManager.virtualMachines().deleteById(virtualMachine1.id());
  201. VirtualMachine virtualMachine2 =
  202. computeManager
  203. .virtualMachines()
  204. .define(vmName)
  205. .withRegion(region)
  206. .withNewResourceGroup(rgName)
  207. .withNewPrimaryNetwork("10.0.0.0/28")
  208. .withPrimaryPrivateIPAddressDynamic()
  209. .withoutPrimaryPublicIPAddress()
  210. .withSpecializedOSUnmanagedDisk(osDiskVhd, OperatingSystemTypes.LINUX)
  211. .withBootDiagnostics() // A new storage account should be created and used
  212. .create();
  213. Assertions.assertNotNull(virtualMachine2);
  214. Assertions.assertTrue(virtualMachine2.isBootDiagnosticsEnabled());
  215. Assertions.assertNotNull(virtualMachine2.bootDiagnosticsStorageUri());
  216. Assertions.assertNotNull(virtualMachine2.osUnmanagedDiskVhdUri());
  217. Assertions
  218. .assertFalse(
  219. virtualMachine2
  220. .osUnmanagedDiskVhdUri()
  221. .toLowerCase()
  222. .startsWith(virtualMachine2.bootDiagnosticsStorageUri().toLowerCase()));
  223. }
  224. @Test
  225. public void canEnableBootDiagnosticsWithCreatableStorageOnUnManagedVMCreation() {
  226. final String storageName = sdkContext.randomResourceName("st", 14);
  227. Creatable<StorageAccount> creatableStorageAccount =
  228. storageManager.storageAccounts().define(storageName).withRegion(region).withNewResourceGroup(rgName);
  229. VirtualMachine virtualMachine =
  230. computeManager
  231. .virtualMachines()
  232. .define(vmName)
  233. .withRegion(region)
  234. .withNewResourceGroup(rgName)
  235. .withNewPrimaryNetwork("10.0.0.0/28")
  236. .withPrimaryPrivateIPAddressDynamic()
  237. .withoutPrimaryPublicIPAddress()
  238. .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
  239. .withRootUsername("Foo12")
  240. .withRootPassword("abc!@#F0orL")
  241. .withUnmanagedDisks()
  242. .withBootDiagnostics(
  243. creatableStorageAccount) // This storage account should be used for BDiagnostics not OS disk storage
  244. // account
  245. .create();
  246. Assertions.assertNotNull(virtualMachine);
  247. Assertions.assertTrue(virtualMachine.isBootDiagnosticsEnabled());
  248. Assertions.assertNotNull(virtualMachine.bootDiagnosticsStorageUri());
  249. Assertions.assertTrue(virtualMachine.bootDiagnosticsStorageUri().contains(storageName));
  250. // There should be a different storage account created for the OS Disk
  251. Assertions
  252. .assertFalse(
  253. virtualMachine
  254. .osUnmanagedDiskVhdUri()
  255. .toLowerCase()
  256. .startsWith(virtualMachine.bootDiagnosticsStorageUri().toLowerCase()));
  257. }
  258. }