PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceManagement/Services/Commands.Test/CloudService/Utilities/ServiceComponentsTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 347 lines | 306 code | 28 blank | 13 comment | 1 complexity | 3c46c2812b5953603ed74079bcb218f1 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.Linq;
  17. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  18. using Xunit;
  19. using Microsoft.WindowsAzure.Commands.CloudService.Development.Scaffolding;
  20. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  21. using Microsoft.WindowsAzure.Commands.Test.Utilities.CloudService;
  22. using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
  23. using Microsoft.WindowsAzure.Commands.Utilities.CloudService;
  24. using Microsoft.WindowsAzure.Commands.Utilities.Properties;
  25. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  26. namespace Microsoft.WindowsAzure.Commands.Test.CloudService.Utilities
  27. {
  28. public class ServiceComponentsTests : SMTestBase, IDisposable
  29. {
  30. private const string serviceName = "NodeService";
  31. private NewAzureServiceProjectCommand newServiceCmdlet;
  32. private MockCommandRuntime mockCommandRuntime;
  33. public ServiceComponentsTests()
  34. {
  35. mockCommandRuntime = new MockCommandRuntime();
  36. newServiceCmdlet = new NewAzureServiceProjectCommand();
  37. newServiceCmdlet.CommandRuntime = mockCommandRuntime;
  38. }
  39. public void TestCleanup()
  40. {
  41. if (Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, serviceName)))
  42. {
  43. Directory.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, serviceName), true);
  44. }
  45. }
  46. public void Dispose()
  47. {
  48. TestCleanup();
  49. }
  50. [Fact]
  51. [Trait(Category.AcceptanceType, Category.CheckIn)]
  52. public void ServiceComponentsTest()
  53. {
  54. TestMockSupport.TestExecutionFolder = AppDomain.CurrentDomain.BaseDirectory;
  55. newServiceCmdlet.NewAzureServiceProcess(TestMockSupport.TestExecutionFolder, serviceName);
  56. ServiceComponents components = new ServiceComponents(
  57. new PowerShellProjectPathInfo(
  58. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, serviceName)));
  59. AzureAssert.AreEqualServiceComponents(components);
  60. }
  61. [Fact]
  62. [Trait(Category.AcceptanceType, Category.CheckIn)]
  63. public void ServiceComponentsTestNullPathsFail()
  64. {
  65. try
  66. {
  67. ServiceComponents components = new ServiceComponents(null as CloudProjectPathInfo);
  68. Assert.True(false, "No exception was thrown");
  69. }
  70. catch (Exception ex)
  71. {
  72. Assert.True(ex is ArgumentException);
  73. Assert.Equal<string>(ex.Message, string.Format(Resources.NullObjectMessage, "paths"));
  74. }
  75. }
  76. [Fact]
  77. [Trait(Category.AcceptanceType, Category.CheckIn)]
  78. public void ServiceComponentsTestCloudConfigDoesNotExistFail()
  79. {
  80. TestMockSupport.TestExecutionFolder = AppDomain.CurrentDomain.BaseDirectory;
  81. newServiceCmdlet.NewAzureServiceProcess(TestMockSupport.TestExecutionFolder, serviceName);
  82. PowerShellProjectPathInfo paths = new PowerShellProjectPathInfo(
  83. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, serviceName));
  84. try
  85. {
  86. File.Delete(paths.CloudConfiguration);
  87. ServiceComponents components = new ServiceComponents(paths);
  88. Assert.True(false, "No exception was thrown");
  89. }
  90. catch (Exception ex)
  91. {
  92. Assert.True(ex is FileNotFoundException);
  93. Assert.Equal<string>(ex.Message, string.Format(Resources.PathDoesNotExistForElement, Resources.ServiceConfiguration, paths.CloudConfiguration));
  94. }
  95. }
  96. [Fact]
  97. [Trait(Category.AcceptanceType, Category.CheckIn)]
  98. public void ServiceComponentsTestLocalConfigDoesNotExistFail()
  99. {
  100. TestMockSupport.TestExecutionFolder = AppDomain.CurrentDomain.BaseDirectory;
  101. newServiceCmdlet.NewAzureServiceProcess(TestMockSupport.TestExecutionFolder, serviceName);
  102. PowerShellProjectPathInfo paths = new PowerShellProjectPathInfo(
  103. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, serviceName));
  104. try
  105. {
  106. File.Delete(paths.LocalConfiguration);
  107. ServiceComponents components = new ServiceComponents(paths);
  108. Assert.True(false, "No exception was thrown");
  109. }
  110. catch (Exception ex)
  111. {
  112. Assert.True(ex is FileNotFoundException);
  113. Assert.Equal<string>(string.Format(Resources.PathDoesNotExistForElement, Resources.ServiceConfiguration, paths.LocalConfiguration), ex.Message);
  114. }
  115. }
  116. [Fact]
  117. [Trait(Category.AcceptanceType, Category.CheckIn)]
  118. public void ServiceComponentsTestSettingsDoesNotExistFail()
  119. {
  120. TestMockSupport.TestExecutionFolder = AppDomain.CurrentDomain.BaseDirectory;
  121. newServiceCmdlet.NewAzureServiceProcess(TestMockSupport.TestExecutionFolder, serviceName);
  122. PowerShellProjectPathInfo paths = new PowerShellProjectPathInfo(
  123. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, serviceName));
  124. try
  125. {
  126. File.Delete(paths.Definition);
  127. ServiceComponents components = new ServiceComponents(paths);
  128. Assert.True(false, "No exception was thrown");
  129. }
  130. catch (Exception ex)
  131. {
  132. Assert.True(ex is FileNotFoundException);
  133. Assert.Equal<string>(string.Format(Resources.PathDoesNotExistForElement, Resources.ServiceDefinition, paths.Definition), ex.Message);
  134. }
  135. }
  136. [Fact]
  137. [Trait(Category.AcceptanceType, Category.CheckIn)]
  138. public void ServiceComponentsTestDefinitionDoesNotExistFail()
  139. {
  140. TestMockSupport.TestExecutionFolder = AppDomain.CurrentDomain.BaseDirectory;
  141. newServiceCmdlet.NewAzureServiceProcess(TestMockSupport.TestExecutionFolder, serviceName);
  142. PowerShellProjectPathInfo paths = new PowerShellProjectPathInfo(
  143. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, serviceName));
  144. try
  145. {
  146. File.Delete(paths.Definition);
  147. ServiceComponents components = new ServiceComponents(paths);
  148. Assert.True(false, "No exception was thrown");
  149. }
  150. catch (Exception ex)
  151. {
  152. Assert.True(ex is FileNotFoundException);
  153. Assert.Equal<string>(string.Format(Resources.PathDoesNotExistForElement, Resources.ServiceDefinition, paths.Definition), ex.Message);
  154. }
  155. }
  156. [Fact]
  157. [Trait(Category.AcceptanceType, Category.CheckIn)]
  158. public void GetNextPortAllNull()
  159. {
  160. using (FileSystemHelper files = new FileSystemHelper(this))
  161. {
  162. int expectedPort = int.Parse(Resources.DefaultWebPort);
  163. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  164. int nextPort = service.Components.GetNextPort();
  165. Assert.Equal<int>(expectedPort, nextPort);
  166. }
  167. }
  168. [Fact]
  169. [Trait(Category.AcceptanceType, Category.CheckIn)]
  170. public void GetNextPortNodeWorkerRoleNull()
  171. {
  172. using (FileSystemHelper files = new FileSystemHelper(this))
  173. {
  174. int expectedPort = int.Parse(Resources.DefaultPort);
  175. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  176. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  177. service = new CloudServiceProject(service.Paths.RootPath, null);
  178. int nextPort = service.Components.GetNextPort();
  179. Assert.Equal<int>(expectedPort, nextPort);
  180. }
  181. }
  182. [Fact]
  183. [Trait(Category.AcceptanceType, Category.CheckIn)]
  184. public void GetNextPortPHPWorkerRoleNull()
  185. {
  186. using (FileSystemHelper files = new FileSystemHelper(this))
  187. {
  188. int expectedPort = int.Parse(Resources.DefaultPort);
  189. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  190. service.AddWebRole(Test.Utilities.Common.Data.PHPWebRoleScaffoldingPath);
  191. service = new CloudServiceProject(service.Paths.RootPath, null);
  192. int nextPort = service.Components.GetNextPort();
  193. Assert.Equal<int>(expectedPort, nextPort);
  194. }
  195. }
  196. [Fact]
  197. [Trait(Category.AcceptanceType, Category.CheckIn)]
  198. public void GetNextPortNodeWebRoleNull()
  199. {
  200. using (FileSystemHelper files = new FileSystemHelper(this))
  201. {
  202. int expectedPort = int.Parse(Resources.DefaultPort);
  203. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  204. service.AddWorkerRole(Test.Utilities.Common.Data.NodeWorkerRoleScaffoldingPath);
  205. service = new CloudServiceProject(service.Paths.RootPath, null);
  206. int nextPort = service.Components.GetNextPort();
  207. Assert.Equal<int>(expectedPort, nextPort);
  208. }
  209. }
  210. [Fact]
  211. [Trait(Category.AcceptanceType, Category.CheckIn)]
  212. public void GetNextPortPHPWebRoleNull()
  213. {
  214. using (FileSystemHelper files = new FileSystemHelper(this))
  215. {
  216. int expectedPort = int.Parse(Resources.DefaultPort);
  217. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  218. service.AddWorkerRole(Test.Utilities.Common.Data.PHPWorkerRoleScaffoldingPath);
  219. service = new CloudServiceProject(service.Paths.RootPath, null);
  220. int nextPort = service.Components.GetNextPort();
  221. Assert.Equal<int>(expectedPort, nextPort);
  222. }
  223. }
  224. [Fact]
  225. [Trait(Category.AcceptanceType, Category.CheckIn)]
  226. public void GetNextPortNullNodeWebEndpointAndNullWorkerRole()
  227. {
  228. using (FileSystemHelper files = new FileSystemHelper(this))
  229. {
  230. int expectedPort = int.Parse(Resources.DefaultWebPort);
  231. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  232. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  233. service = new CloudServiceProject(service.Paths.RootPath, null);
  234. service.Components.Definition.WebRole.ToList().ForEach(wr => wr.Endpoints = null);
  235. int nextPort = service.Components.GetNextPort();
  236. Assert.Equal<int>(expectedPort, nextPort);
  237. }
  238. }
  239. [Fact]
  240. [Trait(Category.AcceptanceType, Category.CheckIn)]
  241. public void GetNextPortNullPHPWebEndpointAndNullWorkerRole()
  242. {
  243. using (FileSystemHelper files = new FileSystemHelper(this))
  244. {
  245. int expectedPort = int.Parse(Resources.DefaultWebPort);
  246. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  247. service.AddWebRole(Test.Utilities.Common.Data.PHPWebRoleScaffoldingPath);
  248. service = new CloudServiceProject(service.Paths.RootPath, null);
  249. service.Components.Definition.WebRole.ToList().ForEach(wr => wr.Endpoints = null);
  250. int nextPort = service.Components.GetNextPort();
  251. Assert.Equal<int>(expectedPort, nextPort);
  252. }
  253. }
  254. [Fact]
  255. [Trait(Category.AcceptanceType, Category.CheckIn)]
  256. public void GetNextPortNullNodeWebEndpointAndWorkerRole()
  257. {
  258. using (FileSystemHelper files = new FileSystemHelper(this))
  259. {
  260. int expectedPort = int.Parse(Resources.DefaultPort);
  261. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  262. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  263. service.Components.Definition.WebRole.ToList().ForEach(wr => wr.Endpoints = null);
  264. service.AddWorkerRole(Test.Utilities.Common.Data.NodeWorkerRoleScaffoldingPath);
  265. service = new CloudServiceProject(service.Paths.RootPath, null);
  266. int nextPort = service.Components.GetNextPort();
  267. Assert.Equal<int>(expectedPort, nextPort);
  268. }
  269. }
  270. [Fact]
  271. [Trait(Category.AcceptanceType, Category.CheckIn)]
  272. public void GetNextPortNullPHPWebEndpointAndWorkerRole()
  273. {
  274. using (FileSystemHelper files = new FileSystemHelper(this))
  275. {
  276. int expectedPort = int.Parse(Resources.DefaultPort);
  277. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  278. service.AddWebRole(Test.Utilities.Common.Data.PHPWebRoleScaffoldingPath);
  279. service.Components.Definition.WebRole.ToList().ForEach(wr => wr.Endpoints = null);
  280. service.AddWorkerRole(Test.Utilities.Common.Data.PHPWorkerRoleScaffoldingPath);
  281. service = new CloudServiceProject(service.Paths.RootPath, null);
  282. int nextPort = service.Components.GetNextPort();
  283. Assert.Equal<int>(expectedPort, nextPort);
  284. }
  285. }
  286. [Fact]
  287. [Trait(Category.AcceptanceType, Category.CheckIn)]
  288. public void GetNextPortWithEmptyPortIndpoints()
  289. {
  290. using (FileSystemHelper files = new FileSystemHelper(this))
  291. {
  292. int expectedPort = int.Parse(Resources.DefaultPort);
  293. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  294. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  295. service.Components.Definition.WebRole[0].Endpoints.InputEndpoint = null;
  296. service.Components.Save(service.Paths);
  297. service.AddWebRole(Test.Utilities.Common.Data.PHPWebRoleScaffoldingPath);
  298. service = new AzureServiceWrapper(service.Paths.RootPath, null);
  299. int nextPort = service.Components.GetNextPort();
  300. Assert.Equal<int>(expectedPort, nextPort);
  301. }
  302. }
  303. [Fact]
  304. [Trait(Category.AcceptanceType, Category.CheckIn)]
  305. public void GetNextPortAddingThirdEndpoint()
  306. {
  307. using (FileSystemHelper files = new FileSystemHelper(this))
  308. {
  309. int expectedPort = int.Parse(Resources.DefaultPort) + 1;
  310. CloudServiceProject service = new CloudServiceProject(files.RootPath, serviceName, null);
  311. service.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  312. service.AddWebRole(Test.Utilities.Common.Data.PHPWebRoleScaffoldingPath);
  313. service = new AzureServiceWrapper(service.Paths.RootPath, null);
  314. int nextPort = service.Components.GetNextPort();
  315. Assert.Equal<int>(expectedPort, nextPort);
  316. }
  317. }
  318. }
  319. }