PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/ServiceManagement/TrafficManager/Commands.TrafficManager.Test/Endpoints/SetTrafficManagerEndpointTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 357 lines | 238 code | 57 blank | 62 comment | 17 complexity | d0e8b474988267b0c1e0e328767847fe 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 Microsoft.VisualStudio.TestTools.UnitTesting;
  18. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  19. using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
  20. using Microsoft.WindowsAzure.Commands.TrafficManager.Endpoint;
  21. using Microsoft.WindowsAzure.Commands.TrafficManager.Models;
  22. using Microsoft.WindowsAzure.Management.TrafficManager.Models;
  23. namespace Microsoft.WindowsAzure.Commands.Test.TrafficManager.Endpoints
  24. {
  25. [TestClass]
  26. public class SetTrafficManagerEndpointTests : SMTestBase
  27. {
  28. private const string ProfileName = "my-profile";
  29. private const string ProfileDomainName = "my.profile.trafficmanager.net";
  30. private const LoadBalancingMethod DefaultLoadBalancingMethod = LoadBalancingMethod.Failover;
  31. private const string DomainName = "www.example.com";
  32. private const int Weight = 3;
  33. private const int MinChildEndpoints = 2;
  34. private const string Location = "West US";
  35. private MockCommandRuntime mockCommandRuntime;
  36. private SetAzureTrafficManagerEndpoint cmdlet;
  37. [TestInitialize]
  38. public void TestSetup()
  39. {
  40. mockCommandRuntime = new MockCommandRuntime();
  41. }
  42. [TestMethod]
  43. public void SetTrafficManagerEndpointSucceeds()
  44. {
  45. // Setup
  46. ProfileWithDefinition original = GetProfileWithDefinition();
  47. var existingEndpoint = new TrafficManagerEndpoint
  48. {
  49. DomainName = DomainName,
  50. Type = EndpointType.Any,
  51. Status = EndpointStatus.Enabled,
  52. Weight = 10,
  53. MinChildEndpoints = 2
  54. };
  55. original.Endpoints.Add(existingEndpoint);
  56. // Assert the endpoint exists
  57. Assert.IsTrue(original.Endpoints.Any(e => e.DomainName == DomainName));
  58. cmdlet = new SetAzureTrafficManagerEndpoint
  59. {
  60. DomainName = DomainName,
  61. TrafficManagerProfile = original,
  62. Weight = Weight,
  63. MinChildEndpoints = MinChildEndpoints,
  64. Location = Location,
  65. CommandRuntime = mockCommandRuntime
  66. };
  67. // Action
  68. cmdlet.ExecuteCmdlet();
  69. // Assert
  70. var actual = mockCommandRuntime.OutputPipeline[0] as ProfileWithDefinition;
  71. // All the properties stay the same except the endpoints
  72. AssertAllProfilePropertiesDontChangeExceptEndpoints(original, actual);
  73. // There is an endpoint with the domain name in "actual"
  74. Assert.IsNotNull(actual);
  75. Assert.IsTrue(actual.Endpoints.Any(e => e.DomainName == DomainName));
  76. TrafficManagerEndpoint updatedEndpoint = actual.Endpoints.First(e => e.DomainName == DomainName);
  77. // Unchanged properties
  78. Assert.AreEqual(EndpointType.Any, updatedEndpoint.Type);
  79. Assert.AreEqual(EndpointStatus.Enabled, updatedEndpoint.Status);
  80. // Updated properties
  81. Assert.AreEqual(Weight, updatedEndpoint.Weight);
  82. Assert.AreEqual(Location, updatedEndpoint.Location);
  83. Assert.AreEqual(MinChildEndpoints, updatedEndpoint.MinChildEndpoints);
  84. }
  85. [TestMethod]
  86. public void SetTrafficManagerNotOverrideWeight()
  87. {
  88. // Setup
  89. ProfileWithDefinition original = GetProfileWithDefinition();
  90. var existingEndpoint = new TrafficManagerEndpoint
  91. {
  92. DomainName = DomainName,
  93. Type = EndpointType.Any,
  94. Status = EndpointStatus.Enabled,
  95. Weight = Weight
  96. };
  97. original.Endpoints.Add(existingEndpoint);
  98. // Assert the endpoint exists
  99. Assert.IsTrue(original.Endpoints.Any(e => e.DomainName == DomainName));
  100. cmdlet = new SetAzureTrafficManagerEndpoint
  101. {
  102. DomainName = DomainName,
  103. TrafficManagerProfile = original,
  104. Location = Location,
  105. CommandRuntime = mockCommandRuntime
  106. };
  107. // Action
  108. cmdlet.ExecuteCmdlet();
  109. // Assert
  110. var actual = mockCommandRuntime.OutputPipeline[0] as ProfileWithDefinition;
  111. // All the properties stay the same except the endpoints
  112. AssertAllProfilePropertiesDontChangeExceptEndpoints(original, actual);
  113. // There is an endpoint with the domain name in "actual"
  114. Assert.IsNotNull(actual);
  115. Assert.IsTrue(actual.Endpoints.Any(e => e.DomainName == DomainName));
  116. TrafficManagerEndpoint updatedEndpoint = actual.Endpoints.First(e => e.DomainName == DomainName);
  117. // Unchanged properties
  118. Assert.AreEqual(EndpointType.Any, updatedEndpoint.Type);
  119. Assert.AreEqual(EndpointStatus.Enabled, updatedEndpoint.Status);
  120. // Updated properties
  121. Assert.AreEqual(Weight, updatedEndpoint.Weight);
  122. Assert.AreEqual(Location, updatedEndpoint.Location);
  123. }
  124. [TestMethod]
  125. public void SetTrafficManagerEndpointNotExisting()
  126. {
  127. // Setup
  128. ProfileWithDefinition original = GetProfileWithDefinition();
  129. TrafficManagerEndpoint expectedEndpoint = new TrafficManagerEndpoint()
  130. {
  131. DomainName = DomainName,
  132. Type = EndpointType.Any,
  133. Status = EndpointStatus.Enabled,
  134. Weight = Weight,
  135. MinChildEndpoints = MinChildEndpoints,
  136. Location = Location
  137. };
  138. cmdlet = new SetAzureTrafficManagerEndpoint
  139. {
  140. DomainName = DomainName,
  141. TrafficManagerProfile = original,
  142. Type = EndpointType.Any.ToString(),
  143. Weight = Weight,
  144. MinChildEndpoints = MinChildEndpoints,
  145. Location = Location,
  146. Status = EndpointStatus.Enabled.ToString(),
  147. CommandRuntime = mockCommandRuntime
  148. };
  149. // Assert the endpoint doesn't exist
  150. Assert.IsFalse(original.Endpoints.Any(e => e.DomainName == DomainName));
  151. // Action
  152. cmdlet.ExecuteCmdlet();
  153. var actual = mockCommandRuntime.OutputPipeline[0] as ProfileWithDefinition;
  154. // There is a new endpoint with the domain name in "actual"
  155. Assert.IsNotNull(actual);
  156. Assert.IsTrue(actual.Endpoints.Any(e => e.DomainName == DomainName));
  157. TrafficManagerEndpoint newEndpoint = actual.Endpoints.First(e => e.DomainName == DomainName);
  158. Assert.AreEqual(expectedEndpoint, newEndpoint);
  159. }
  160. /// <summary>
  161. /// The Type of the endpoint is a required field for new endpoints. Since it's not provided in the arguments
  162. /// to the cmdlet, the cmdlet fails.
  163. /// </summary>
  164. [TestMethod]
  165. public void SetTrafficManagerEndpointMissinTypeFails()
  166. {
  167. // Setup
  168. ProfileWithDefinition original = GetProfileWithDefinition();
  169. cmdlet = new SetAzureTrafficManagerEndpoint
  170. {
  171. DomainName = DomainName,
  172. TrafficManagerProfile = original,
  173. Weight = Weight,
  174. Location = Location,
  175. Status = EndpointStatus.Enabled.ToString(),
  176. CommandRuntime = mockCommandRuntime
  177. };
  178. // Assert the endpoint doesn't exist
  179. Assert.IsFalse(original.Endpoints.Any(e => e.DomainName == DomainName));
  180. // Action + Assert
  181. Testing.AssertThrows<Exception>(
  182. () => cmdlet.ExecuteCmdlet(),
  183. Microsoft.WindowsAzure.Commands.Common.Properties.Resources.SetTrafficManagerEndpointNeedsParameters);
  184. }
  185. /// <summary>
  186. /// The Type of the endpoint is a required field for new endpoints. Since it's not provided in the arguments
  187. /// to the cmdlet, the cmdlet fails.
  188. /// </summary>
  189. [TestMethod]
  190. public void SetTrafficManagerEndpointMissingStatusFails()
  191. {
  192. // Setup
  193. ProfileWithDefinition original = GetProfileWithDefinition();
  194. cmdlet = new SetAzureTrafficManagerEndpoint
  195. {
  196. DomainName = DomainName,
  197. TrafficManagerProfile = original,
  198. Weight = Weight,
  199. Location = Location,
  200. Type = EndpointType.Any.ToString(),
  201. CommandRuntime = mockCommandRuntime
  202. };
  203. // Assert the endpoint doesn't exist
  204. Assert.IsFalse(original.Endpoints.Any(e => e.DomainName == DomainName));
  205. // Action + Assert
  206. Testing.AssertThrows<Exception>(
  207. () => cmdlet.ExecuteCmdlet(),
  208. Microsoft.WindowsAzure.Commands.Common.Properties.Resources.SetTrafficManagerEndpointNeedsParameters);
  209. }
  210. /// <summary>
  211. /// The Type of the endpoint is a required field for new endpoints. Since it's not provided in the arguments
  212. /// to the cmdlet, the cmdlet fails.
  213. /// </summary>
  214. [TestMethod]
  215. public void SetTrafficManagerEndpointMissingWeightSucceeds()
  216. {
  217. // Setup
  218. ProfileWithDefinition original = GetProfileWithDefinition();
  219. cmdlet = new SetAzureTrafficManagerEndpoint
  220. {
  221. DomainName = DomainName,
  222. TrafficManagerProfile = original,
  223. Location = Location,
  224. Type = EndpointType.Any.ToString(),
  225. Status = EndpointStatus.Enabled.ToString(),
  226. CommandRuntime = mockCommandRuntime
  227. };
  228. // Assert the endpoint doesn't exist
  229. Assert.IsFalse(original.Endpoints.Any(e => e.DomainName == DomainName));
  230. // Action
  231. cmdlet.ExecuteCmdlet();
  232. // Assert
  233. var actual = mockCommandRuntime.OutputPipeline[0] as ProfileWithDefinition;
  234. // There is a new endpoint with the domain name in "actual"
  235. Assert.IsNotNull(actual);
  236. Assert.IsTrue(actual.Endpoints.Any(e => e.DomainName == DomainName));
  237. TrafficManagerEndpoint newEndpoint = actual.Endpoints.First(e => e.DomainName == DomainName);
  238. Assert.AreEqual(EndpointType.Any, newEndpoint.Type);
  239. Assert.AreEqual(EndpointStatus.Enabled, newEndpoint.Status);
  240. // Default weight value in PS is null
  241. Assert.AreEqual(null, newEndpoint.Weight);
  242. Assert.AreEqual(Location, newEndpoint.Location);
  243. }
  244. [TestMethod]
  245. public void SetTrafficManagerEndpointMissingLocationSucceeds()
  246. {
  247. // Setup
  248. ProfileWithDefinition original = GetProfileWithDefinition();
  249. cmdlet = new SetAzureTrafficManagerEndpoint
  250. {
  251. DomainName = DomainName,
  252. TrafficManagerProfile = original,
  253. Weight = Weight,
  254. Type = EndpointType.Any.ToString(),
  255. Status = EndpointStatus.Enabled.ToString(),
  256. CommandRuntime = mockCommandRuntime
  257. };
  258. // Assert the endpoint doesn't exist
  259. Assert.IsFalse(original.Endpoints.Any(e => e.DomainName == DomainName));
  260. // Action
  261. cmdlet.ExecuteCmdlet();
  262. // Assert
  263. var actual = mockCommandRuntime.OutputPipeline[0] as ProfileWithDefinition;
  264. // There is a new endpoint with the domain name in "actual"
  265. Assert.IsNotNull(actual);
  266. Assert.IsTrue(actual.Endpoints.Any(e => e.DomainName == DomainName));
  267. TrafficManagerEndpoint newEndpoint = actual.Endpoints.First(e => e.DomainName == DomainName);
  268. Assert.AreEqual(EndpointType.Any, newEndpoint.Type);
  269. Assert.AreEqual(EndpointStatus.Enabled, newEndpoint.Status);
  270. Assert.AreEqual(Weight, newEndpoint.Weight);
  271. Assert.AreEqual(null, newEndpoint.Location);
  272. }
  273. private ProfileWithDefinition GetProfileWithDefinition()
  274. {
  275. return new ProfileWithDefinition
  276. {
  277. DomainName = ProfileDomainName,
  278. Name = ProfileName,
  279. Endpoints = new List<TrafficManagerEndpoint>(),
  280. LoadBalancingMethod = DefaultLoadBalancingMethod,
  281. MonitorPort = 80,
  282. Status = ProfileDefinitionStatus.Enabled,
  283. MonitorRelativePath = "/",
  284. TimeToLiveInSeconds = 30
  285. };
  286. }
  287. private void AssertAllProfilePropertiesDontChangeExceptEndpoints(
  288. ProfileWithDefinition original,
  289. ProfileWithDefinition actual)
  290. {
  291. Assert.AreEqual(original.DomainName, actual.DomainName);
  292. Assert.AreEqual(original.Name, actual.Name);
  293. Assert.AreEqual(original.LoadBalancingMethod, actual.LoadBalancingMethod);
  294. Assert.AreEqual(original.MonitorPort, actual.MonitorPort);
  295. Assert.AreEqual(original.Status, actual.Status);
  296. Assert.AreEqual(original.MonitorRelativePath, actual.MonitorRelativePath);
  297. Assert.AreEqual(original.TimeToLiveInSeconds, actual.TimeToLiveInSeconds);
  298. }
  299. }
  300. }