PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceManagement/Services/Commands.Test/WAPackIaaS/Operations/VMRoleOperationsTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 212 lines | 163 code | 30 blank | 19 comment | 2 complexity | c25d0c082cf498fc7c0f873e43c69ab6 MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Net;
  18. using System.Net.Http;
  19. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  20. using Xunit;
  21. using Microsoft.WindowsAzure.Commands.Test.WAPackIaaS.Mocks;
  22. using Microsoft.WindowsAzure.Commands.Utilities.WAPackIaaS;
  23. using Microsoft.WindowsAzure.Commands.Utilities.WAPackIaaS.DataContract;
  24. using Microsoft.WindowsAzure.Commands.Utilities.WAPackIaaS.Operations;
  25. using Microsoft.WindowsAzure.Commands.Utilities.WAPackIaaS.WebClient;
  26. namespace Microsoft.WindowsAzure.Commands.Test.WAPackIaaS.Operations
  27. {
  28. public class VMRoleOperationsTests
  29. {
  30. private const string genericBaseUri = "/CloudServices/{0}/Resources/MicrosoftCompute/VMRoles";
  31. private const string specificBaseUri = "/CloudServices/{0}/Resources/MicrosoftCompute/VMRoles/{1}";
  32. private const string vmsUri = "/CloudServices/{0}/Resources/MicrosoftCompute/VMRoles/{1}/VMs";
  33. private const string vmRoleName = "VMRole01";
  34. private const string vmRoleLabel = "VMRole01-Label";
  35. private const string cloudServiceName = "CloudService01";
  36. [Fact]
  37. [Trait(Category.AcceptanceType, Category.CheckIn)]
  38. [Trait("Type", "WAPackIaaS-All")]
  39. [Trait("Type", "WAPackIaaS-Unit")]
  40. public void ShouldCreateOneVMRole()
  41. {
  42. var mockChannel = new MockRequestChannel();
  43. var vmRoleToCreate = new VMRole
  44. {
  45. Name = vmRoleName,
  46. Label = vmRoleLabel
  47. };
  48. var vmRoleToReturn = new VMRole
  49. {
  50. Name = vmRoleName,
  51. Label = vmRoleLabel,
  52. };
  53. mockChannel.AddReturnObject(vmRoleToReturn, new WebHeaderCollection { "x-ms-request-id:" + Guid.NewGuid() });
  54. Guid? jobOut;
  55. var vmRoleOperations = new VMRoleOperations(new WebClientFactory(new Subscription(), mockChannel));
  56. var createdVMRole = vmRoleOperations.Create(cloudServiceName, vmRoleToCreate, out jobOut);
  57. Assert.NotNull(createdVMRole);
  58. Assert.Equal(vmRoleToReturn.Name, createdVMRole.Name);
  59. Assert.Equal(vmRoleToReturn.Label, createdVMRole.Label);
  60. var requestList = mockChannel.ClientRequests;
  61. Assert.Equal(1, requestList.Count);
  62. Assert.Equal(HttpMethod.Post.ToString(), requestList[0].Item1.Method);
  63. // Check the URI (for Azure consistency)
  64. Assert.Equal(String.Format(genericBaseUri,cloudServiceName), mockChannel.ClientRequests[0].Item1.Address.AbsolutePath.Substring(1));
  65. }
  66. [Fact]
  67. [Trait(Category.AcceptanceType, Category.CheckIn)]
  68. [Trait("Type", "WAPackIaaS-All")]
  69. [Trait("Type", "WAPackIaaS-Unit")]
  70. public void ShouldReturnOneVMRole()
  71. {
  72. var mockChannel = new MockRequestChannel();
  73. mockChannel.AddReturnObject(new VMRole { Name = vmRoleName, Label = vmRoleLabel });
  74. var vmRoleOperations = new VMRoleOperations(new WebClientFactory(new Subscription(), mockChannel));
  75. Assert.Equal(1, vmRoleOperations.Read(cloudServiceName).Count);
  76. // Check the URI (for Azure consistency)
  77. var requestList = mockChannel.ClientRequests;
  78. Assert.Equal(2, requestList.Count);
  79. Assert.Equal(String.Format(genericBaseUri, cloudServiceName), mockChannel.ClientRequests[0].Item1.Address.AbsolutePath.Substring(1));
  80. Assert.Equal(String.Format(vmsUri, cloudServiceName, vmRoleName), mockChannel.ClientRequests[1].Item1.Address.AbsolutePath.Substring(1));
  81. }
  82. [Fact]
  83. [Trait(Category.AcceptanceType, Category.CheckIn)]
  84. [Trait("Type", "WAPackIaaS-All")]
  85. [Trait("Type", "WAPackIaaS-Unit")]
  86. public void ShouldReturnOneVMRoleByName()
  87. {
  88. var mockChannel = new MockRequestChannel();
  89. mockChannel.AddReturnObject(new VMRole { Name = vmRoleName, Label = vmRoleLabel });
  90. var vmRoleOperations = new VMRoleOperations(new WebClientFactory(new Subscription(), mockChannel));
  91. Assert.Equal(vmRoleName, vmRoleOperations.Read(cloudServiceName, vmRoleName).Name);
  92. // Check the URI (for Azure consistency)
  93. var requestList = mockChannel.ClientRequests;
  94. Assert.Equal(2, requestList.Count);
  95. Assert.Equal(String.Format(specificBaseUri, cloudServiceName, vmRoleName), mockChannel.ClientRequests[0].Item1.Address.AbsolutePath.Substring(1));
  96. Assert.Equal(String.Format(vmsUri, cloudServiceName, vmRoleName), mockChannel.ClientRequests[1].Item1.Address.AbsolutePath.Substring(1));
  97. }
  98. [Fact]
  99. [Trait(Category.AcceptanceType, Category.CheckIn)]
  100. [Trait("Type", "WAPackIaaS-All")]
  101. [Trait("Type", "WAPackIaaS-Unit")]
  102. public void ShouldReturnMultipleVMRole()
  103. {
  104. const string vmRoleName01 = "VMRole01";
  105. const string vmRoleName02 = "VMRole02";
  106. const string vmRoleLabel01 = "VMRole01-Label";
  107. const string vmRoleLabel02 = "VMRole02-Label";
  108. const string cloudServiceName = "CloudService01";
  109. var mockChannel = new MockRequestChannel();
  110. var vmRoles = new List<object>
  111. {
  112. new VMRole { Name = vmRoleName01, Label = vmRoleLabel01 },
  113. new VMRole { Name = vmRoleName02, Label = vmRoleLabel02 }
  114. };
  115. mockChannel.AddReturnObject(vmRoles);
  116. var vmRoleOperations = new VMRoleOperations(new WebClientFactory(new Subscription(), mockChannel));
  117. var vmRoleList = vmRoleOperations.Read(cloudServiceName);
  118. Assert.Equal(vmRoles.Count, vmRoleList.Count);
  119. Assert.True(vmRoleList[0].Name == vmRoleName01);
  120. Assert.True(vmRoleList[1].Name == vmRoleName02);
  121. // Check the URI (for Azure consistency)
  122. var requestList = mockChannel.ClientRequests;
  123. Assert.Equal(3, requestList.Count);
  124. Assert.Equal(String.Format(genericBaseUri, cloudServiceName), mockChannel.ClientRequests[0].Item1.Address.AbsolutePath.Substring(1));
  125. Assert.Equal(String.Format(vmsUri, cloudServiceName, vmRoleName01), mockChannel.ClientRequests[1].Item1.Address.AbsolutePath.Substring(1));
  126. Assert.Equal(String.Format(vmsUri, cloudServiceName, vmRoleName02), mockChannel.ClientRequests[2].Item1.Address.AbsolutePath.Substring(1));
  127. }
  128. [Fact]
  129. [Trait(Category.AcceptanceType, Category.CheckIn)]
  130. [Trait("Type", "WAPackIaaS-All")]
  131. [Trait("Type", "WAPackIaaS-Unit")]
  132. public void ShouldReturnMultipleVMRoleVMs()
  133. {
  134. var mockChannel = new MockRequestChannel();
  135. var vmRole = new VMRole
  136. {
  137. Name = vmRoleName,
  138. Label = vmRoleLabel
  139. };
  140. var vmList = new List<VM> { new VM() { Id = Guid.Empty }, new VM() { Id = Guid.Empty } };
  141. vmRole.VMs.Load(vmList);
  142. mockChannel.AddReturnObject(vmRole);
  143. var vmRoleOperations = new VMRoleOperations(new WebClientFactory(new Subscription(), mockChannel));
  144. var readVMRole = vmRoleOperations.Read(cloudServiceName, vmRoleName);
  145. Assert.Equal(vmRoleName, readVMRole.Name);
  146. Assert.Equal(vmList.Count, readVMRole.VMs.Count);
  147. // Check the URI (for Azure consistency)
  148. var requestList = mockChannel.ClientRequests;
  149. Assert.Equal(2, requestList.Count);
  150. Assert.Equal(String.Format(specificBaseUri, cloudServiceName, vmRoleName), mockChannel.ClientRequests[0].Item1.Address.AbsolutePath.Substring(1));
  151. Assert.Equal(String.Format(vmsUri, cloudServiceName, vmRoleName), mockChannel.ClientRequests[1].Item1.Address.AbsolutePath.Substring(1));
  152. }
  153. [Fact]
  154. [Trait(Category.AcceptanceType, Category.CheckIn)]
  155. [Trait("Type", "WAPackIaaS-All")]
  156. [Trait("Type", "WAPackIaaS-Unit")]
  157. public void ShouldDeleteVMRole()
  158. {
  159. var mockChannel = new MockRequestChannel();
  160. mockChannel.AddReturnObject(new VMRole { Name = vmRoleName, Label = vmRoleLabel }, new WebHeaderCollection { "x-ms-request-id:" + Guid.NewGuid() });
  161. Guid? jobOut;
  162. var vmRoleOperations = new VMRoleOperations(new WebClientFactory(new Subscription(), mockChannel));
  163. vmRoleOperations.Delete(cloudServiceName, vmRoleName, out jobOut);
  164. Assert.Equal(mockChannel.ClientRequests.Count, 1);
  165. Assert.Equal(HttpMethod.Delete.ToString(), mockChannel.ClientRequests[0].Item1.Method);
  166. // Check the URI (for Azure consistency)
  167. var requestList = mockChannel.ClientRequests;
  168. Assert.Equal(1, requestList.Count);
  169. Assert.Equal(String.Format(specificBaseUri, cloudServiceName, vmRoleName), mockChannel.ClientRequests[0].Item1.Address.AbsolutePath.Substring(1));
  170. }
  171. [Fact]
  172. [Trait(Category.AcceptanceType, Category.CheckIn)]
  173. [Trait("Type", "WAPackIaaS-All")]
  174. [Trait("Type", "WAPackIaaS-Unit")]
  175. [Trait("Type", "WAPackIaaS-Negative")]
  176. public void ShouldReturnEmptyOnNoResult()
  177. {
  178. var vmRoleOperations = new VMRoleOperations(new WebClientFactory(new Subscription(), MockRequestChannel.Create()));
  179. Assert.False(vmRoleOperations.Read().Any());
  180. }
  181. }
  182. }