PageRenderTime 30ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceManagement/Services/Commands.Test/CloudService/Development/SetAzureInstancesTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 294 lines | 244 code | 37 blank | 13 comment | 3 complexity | 343530040d717c70361b853673baa827 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.IO;
  16. using System.Management.Automation;
  17. using Microsoft.WindowsAzure.Commands.CloudService.Development;
  18. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  19. using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
  20. using Microsoft.WindowsAzure.Commands.Utilities.CloudService;
  21. using Microsoft.WindowsAzure.Commands.Utilities.Common.XmlSchema.ServiceConfigurationSchema;
  22. using Microsoft.WindowsAzure.Commands.Utilities.Properties;
  23. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  24. using Xunit;
  25. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  26. namespace Microsoft.WindowsAzure.Commands.Test.CloudService.Development.Tests.Cmdlet
  27. {
  28. public class SetAzureInstancesTests : SMTestBase
  29. {
  30. private const string serviceName = "AzureService";
  31. private MockCommandRuntime mockCommandRuntime;
  32. private SetAzureServiceProjectRoleCommand cmdlet;
  33. public SetAzureInstancesTests()
  34. {
  35. mockCommandRuntime = new MockCommandRuntime();
  36. cmdlet = new SetAzureServiceProjectRoleCommand();
  37. cmdlet.CommandRuntime = mockCommandRuntime;
  38. cmdlet.PassThru = true;
  39. }
  40. [Fact]
  41. [Trait(Category.AcceptanceType, Category.CheckIn)]
  42. public void SetAzureInstancesProcessTestsNode()
  43. {
  44. int newRoleInstances = 10;
  45. using (FileSystemHelper files = new FileSystemHelper(this))
  46. {
  47. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  48. string roleName = "WebRole1";
  49. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  50. cmdlet.PassThru = false;
  51. RoleSettings roleSettings = cmdlet.SetAzureInstancesProcess("WebRole1", newRoleInstances, service.Paths.RootPath);
  52. service = new CloudServiceProject(service.Paths.RootPath, null);
  53. Assert.Equal<int>(newRoleInstances, service.Components.CloudConfig.Role[0].Instances.count);
  54. Assert.Equal<int>(newRoleInstances, service.Components.LocalConfig.Role[0].Instances.count);
  55. Assert.Equal<int>(0, mockCommandRuntime.OutputPipeline.Count);
  56. Assert.Equal<int>(newRoleInstances, roleSettings.Instances.count);
  57. Assert.Equal<string>(roleName, roleSettings.name);
  58. }
  59. }
  60. [Fact]
  61. [Trait(Category.AcceptanceType, Category.CheckIn)]
  62. public void SetAzureInstancesProcessTestsPHP()
  63. {
  64. int newRoleInstances = 10;
  65. using (FileSystemHelper files = new FileSystemHelper(this))
  66. {
  67. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  68. string roleName = "WebRole1";
  69. service.AddWebRole(Test.Utilities.Common.Data.PHPWebRoleScaffoldingPath);
  70. RoleSettings roleSettings = cmdlet.SetAzureInstancesProcess("WebRole1", newRoleInstances, service.Paths.RootPath);
  71. service = new CloudServiceProject(service.Paths.RootPath, null);
  72. Assert.Equal<int>(newRoleInstances, service.Components.CloudConfig.Role[0].Instances.count);
  73. Assert.Equal<int>(newRoleInstances, service.Components.LocalConfig.Role[0].Instances.count);
  74. Assert.Equal<string>(roleName, ((PSObject)mockCommandRuntime.OutputPipeline[0]).Members[Parameters.RoleName].Value.ToString());
  75. Assert.True(((PSObject)mockCommandRuntime.OutputPipeline[0]).TypeNames.Contains(typeof(RoleSettings).FullName));
  76. Assert.Equal<int>(newRoleInstances, roleSettings.Instances.count);
  77. Assert.Equal<string>(roleName, roleSettings.name);
  78. }
  79. }
  80. [Fact]
  81. [Trait(Category.AcceptanceType, Category.CheckIn)]
  82. public void SetAzureInstancesProcessTestsRoleNameDoesNotExistFail()
  83. {
  84. string roleName = "WebRole1";
  85. using (FileSystemHelper files = new FileSystemHelper(this))
  86. {
  87. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  88. Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, roleName, 10), string.Format(Resources.RoleNotFoundMessage, roleName));
  89. }
  90. }
  91. [Fact]
  92. [Trait(Category.AcceptanceType, Category.CheckIn)]
  93. public void SetAzureInstancesProcessTestsNodeRoleNameDoesNotExistServiceContainsWebRoleFail()
  94. {
  95. string roleName = "WebRole1";
  96. string invalidRoleName = "foo";
  97. using (FileSystemHelper files = new FileSystemHelper(this))
  98. {
  99. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  100. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath, roleName, 1);
  101. Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, invalidRoleName, 10), string.Format(Resources.RoleNotFoundMessage, invalidRoleName));
  102. }
  103. }
  104. [Fact]
  105. [Trait(Category.AcceptanceType, Category.CheckIn)]
  106. public void SetAzureInstancesProcessTestsPHPRoleNameDoesNotExistServiceContainsWebRoleFail()
  107. {
  108. string roleName = "WebRole1";
  109. string invalidRoleName = "foo";
  110. using (FileSystemHelper files = new FileSystemHelper(this))
  111. {
  112. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  113. service.AddWebRole(Test.Utilities.Common.Data.PHPWebRoleScaffoldingPath, roleName, 1);
  114. Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, invalidRoleName, 10), string.Format(Resources.RoleNotFoundMessage, invalidRoleName));
  115. }
  116. }
  117. [Fact]
  118. [Trait(Category.AcceptanceType, Category.CheckIn)]
  119. public void SetAzureInstancesProcessTestsNodeRoleNameDoesNotExistServiceContainsWorkerRoleFail()
  120. {
  121. string roleName = "WorkerRole1";
  122. string invalidRoleName = "foo";
  123. using (FileSystemHelper files = new FileSystemHelper(this))
  124. {
  125. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  126. service.AddWorkerRole(Test.Utilities.Common.Data.NodeWorkerRoleScaffoldingPath, roleName, 1);
  127. Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, invalidRoleName, 10), string.Format(Resources.RoleNotFoundMessage, invalidRoleName));
  128. }
  129. }
  130. [Fact]
  131. [Trait(Category.AcceptanceType, Category.CheckIn)]
  132. public void SetAzureInstancesProcessTestsPHPRoleNameDoesNotExistServiceContainsWorkerRoleFail()
  133. {
  134. string roleName = "WorkerRole1";
  135. string invalidRoleName = "foo";
  136. using (FileSystemHelper files = new FileSystemHelper(this))
  137. {
  138. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  139. service.AddWorkerRole(Test.Utilities.Common.Data.PHPWorkerRoleScaffoldingPath, roleName, 1);
  140. Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, invalidRoleName, 10), string.Format(Resources.RoleNotFoundMessage, invalidRoleName));
  141. }
  142. }
  143. [Fact]
  144. [Trait(Category.AcceptanceType, Category.CheckIn)]
  145. public void SetAzureInstancesProcessTestsEmptyRoleNameFail()
  146. {
  147. using (FileSystemHelper files = new FileSystemHelper(this))
  148. {
  149. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  150. Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, string.Empty, 10), string.Format(Resources.InvalidOrEmptyArgumentMessage, Resources.RoleName));
  151. }
  152. }
  153. [Fact]
  154. [Trait(Category.AcceptanceType, Category.CheckIn)]
  155. public void SetAzureInstancesProcessTestsNullRoleNameFail()
  156. {
  157. using (FileSystemHelper files = new FileSystemHelper(this))
  158. {
  159. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  160. Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, null, 10), string.Format(Resources.InvalidOrEmptyArgumentMessage, Resources.RoleName));
  161. }
  162. }
  163. [Fact]
  164. [Trait(Category.AcceptanceType, Category.CheckIn)]
  165. public void SetAzureInstancesProcessTestsLargeRoleInstanceFail()
  166. {
  167. string roleName = "WebRole1";
  168. using (FileSystemHelper files = new FileSystemHelper(this))
  169. {
  170. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  171. Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, roleName, 2000), string.Format(Resources.InvalidInstancesCount, roleName));
  172. }
  173. }
  174. [Fact]
  175. [Trait(Category.AcceptanceType, Category.CheckIn)]
  176. public void SetAzureInstancesProcessNegativeRoleInstanceFail()
  177. {
  178. string roleName = "WebRole1";
  179. using (FileSystemHelper files = new FileSystemHelper(this))
  180. {
  181. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  182. Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, roleName, -1), string.Format(Resources.InvalidInstancesCount, roleName));
  183. }
  184. }
  185. [Fact]
  186. [Trait(Category.AcceptanceType, Category.CheckIn)]
  187. public void SetAzureInstancesProcessTestsCaseInsensitive()
  188. {
  189. int newRoleInstances = 10;
  190. using (FileSystemHelper files = new FileSystemHelper(this))
  191. {
  192. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  193. string roleName = "WebRole1";
  194. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  195. cmdlet.PassThru = false;
  196. RoleSettings roleSettings = cmdlet.SetAzureInstancesProcess("WeBrolE1", newRoleInstances, service.Paths.RootPath);
  197. service = new CloudServiceProject(service.Paths.RootPath, null);
  198. Assert.Equal<int>(newRoleInstances, service.Components.CloudConfig.Role[0].Instances.count);
  199. Assert.Equal<int>(newRoleInstances, service.Components.LocalConfig.Role[0].Instances.count);
  200. Assert.Equal<int>(0, mockCommandRuntime.OutputPipeline.Count);
  201. Assert.Equal<int>(newRoleInstances, roleSettings.Instances.count);
  202. Assert.Equal<string>(roleName, roleSettings.name);
  203. }
  204. }
  205. [Fact]
  206. [Trait(Category.AcceptanceType, Category.CheckIn)]
  207. public void SetAzureServiceProjectRoleWithoutPassingRoleName()
  208. {
  209. TestMockSupport.TestExecutionFolder = AppDomain.CurrentDomain.BaseDirectory;
  210. string serviceName = "AzureService1";
  211. if (Directory.Exists(Path.Combine(TestMockSupport.TestExecutionFolder,serviceName)))
  212. {
  213. Directory.Delete(Path.Combine(TestMockSupport.TestExecutionFolder, serviceName), true);
  214. }
  215. CloudServiceProject service = new CloudServiceProject(TestMockSupport.TestExecutionFolder, serviceName, null);
  216. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  217. TestMockSupport.TestExecutionFolder = Path.Combine(service.Paths.RootPath, "WebRole1");
  218. cmdlet.RoleName = string.Empty;
  219. cmdlet.ExecuteCmdlet();
  220. service = new CloudServiceProject(service.Paths.RootPath, null);
  221. Assert.Equal<string>("WebRole1", cmdlet.RoleName);
  222. }
  223. [Fact]
  224. [Trait(Category.AcceptanceType, Category.CheckIn)]
  225. public void SetAzureServiceProjectRoleInDeepDirectory()
  226. {
  227. TestMockSupport.TestExecutionFolder = AppDomain.CurrentDomain.BaseDirectory;
  228. string serviceName = "AzureService2";
  229. if (Directory.Exists(Path.Combine(TestMockSupport.TestExecutionFolder, serviceName)))
  230. {
  231. Directory.Delete(Path.Combine(TestMockSupport.TestExecutionFolder, serviceName), true);
  232. }
  233. CloudServiceProject service = new CloudServiceProject(TestMockSupport.TestExecutionFolder, serviceName, null);
  234. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  235. TestMockSupport.TestExecutionFolder = Path.Combine(service.Paths.RootPath, "WebRole1", "bin");
  236. cmdlet.RoleName = string.Empty;
  237. cmdlet.ExecuteCmdlet();
  238. service = new CloudServiceProject(service.Paths.RootPath, null);
  239. Assert.Equal<string>("WebRole1", cmdlet.RoleName);
  240. }
  241. [Fact]
  242. [Trait(Category.AcceptanceType, Category.CheckIn)]
  243. public void SetAzureServiceProjectRoleInServiecRootDirectoryFail()
  244. {
  245. TestMockSupport.TestExecutionFolder = AppDomain.CurrentDomain.BaseDirectory;
  246. string serviceName = "AzureService3";
  247. if (Directory.Exists(Path.Combine(TestMockSupport.TestExecutionFolder, serviceName)))
  248. {
  249. Directory.Delete(Path.Combine(TestMockSupport.TestExecutionFolder, serviceName), true);
  250. }
  251. CloudServiceProject service = new CloudServiceProject(TestMockSupport.TestExecutionFolder, serviceName, null);
  252. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  253. cmdlet.RoleName = string.Empty;
  254. Testing.AssertThrows<InvalidOperationException>(() => cmdlet.ExecuteCmdlet(), Resources.CannotFindServiceRoot);
  255. }
  256. }
  257. }