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

/redistributable/rackspace-net-sdk/test/Rackspace.UnitTests/RackConnect/v3/PublicIPTests.cs

https://gitlab.com/rekby-archive/onlyoffice-CommunityServer
C# | 312 lines | 262 code | 50 blank | 0 comment | 0 complexity | 8c70acf0e69399f29818124b1a2a57ba MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using Newtonsoft.Json.Linq;
  5. using Rackspace.Synchronous;
  6. using Rackspace.Testing;
  7. using Xunit;
  8. namespace Rackspace.RackConnect.v3
  9. {
  10. public class PublicIPTests
  11. {
  12. private readonly RackConnectService _rackConnectService;
  13. public PublicIPTests()
  14. {
  15. _rackConnectService = new RackConnectService(Stubs.IdentityService, "region");
  16. }
  17. [Fact]
  18. public void ListPublicIPs()
  19. {
  20. using (var httpTest = new HttpTest())
  21. {
  22. Identifier id = Guid.NewGuid();
  23. httpTest.RespondWithJson(new[] {new PublicIP {Id = id}});
  24. var results = _rackConnectService.ListPublicIPs();
  25. httpTest.ShouldHaveCalled($"*/public_ips");
  26. Assert.NotNull(results);
  27. Assert.Equal(1, results.Count());
  28. Assert.Equal(id, results.First().Id);
  29. Assert.All(results.OfType<IServiceResource<RackConnectService>>(), ip => Assert.NotNull(ip.Owner));
  30. }
  31. }
  32. [Fact]
  33. public void ListPublicIPsForServer()
  34. {
  35. using (var httpTest = new HttpTest())
  36. {
  37. string serverId = Guid.NewGuid().ToString();
  38. Identifier id = Guid.NewGuid();
  39. httpTest.RespondWithJson(new[] {new PublicIP {Id = id}});
  40. var results = _rackConnectService.ListPublicIPs(new ListPublicIPsFilter {ServerId = serverId});
  41. httpTest.ShouldHaveCalled($"*/public_ips?cloud_server_id={serverId}");
  42. Assert.NotNull(results);
  43. Assert.Equal(1, results.Count());
  44. Assert.Equal(id, results.First().Id);
  45. }
  46. }
  47. [Fact]
  48. public void ListPersistentPublicIPs()
  49. {
  50. using (var httpTest = new HttpTest())
  51. {
  52. Identifier id = Guid.NewGuid();
  53. httpTest.RespondWithJson(new[] { new PublicIP { Id = id } });
  54. var results = _rackConnectService.ListPublicIPs(new ListPublicIPsFilter { IsRetained = true });
  55. httpTest.ShouldHaveCalled($"*/public_ips?retain=True");
  56. Assert.NotNull(results);
  57. Assert.Equal(1, results.Count());
  58. Assert.Equal(id, results.First().Id);
  59. }
  60. }
  61. [Fact]
  62. public void GetPublicIP()
  63. {
  64. using (var httpTest = new HttpTest())
  65. {
  66. Identifier id = Guid.NewGuid();
  67. httpTest.RespondWithJson(new PublicIP { Id = id });
  68. var result = _rackConnectService.GetPublicIP(id);
  69. httpTest.ShouldHaveCalled($"*/public_ips/{id}");
  70. Assert.NotNull(result);
  71. Assert.Equal(id, result.Id);
  72. Assert.NotNull(((IServiceResource<RackConnectService>)result).Owner);
  73. }
  74. }
  75. [Fact]
  76. public void CreatePublicIP()
  77. {
  78. using (var httpTest = new HttpTest())
  79. {
  80. string serverId = Guid.NewGuid().ToString();
  81. Identifier id = Guid.NewGuid();
  82. httpTest.RespondWithJson(new PublicIP { Id = id });
  83. var ipRequest = new PublicIPCreateDefinition {ServerId = serverId};
  84. var result = _rackConnectService.CreatePublicIP(ipRequest);
  85. httpTest.ShouldHaveCalled($"*/public_ips");
  86. Assert.NotNull(result);
  87. Assert.Equal(id, result.Id);
  88. Assert.NotNull(((IServiceResource<RackConnectService>)result).Owner);
  89. }
  90. }
  91. [Fact]
  92. public void CreatePublicIP_RetriesWhenTheServerIsNotFound()
  93. {
  94. using (var httpTest = new HttpTest())
  95. {
  96. string serverId = Guid.NewGuid().ToString();
  97. Identifier id = Guid.NewGuid();
  98. httpTest.RespondWith((int)HttpStatusCode.Conflict, $"Cloud Server {serverId} does not exist");
  99. httpTest.RespondWithJson(new PublicIP { Id = id });
  100. var ipRequest = new PublicIPCreateDefinition { ServerId = serverId };
  101. var result = _rackConnectService.CreatePublicIP(ipRequest);
  102. httpTest.ShouldHaveCalled($"*/public_ips");
  103. Assert.NotNull(result);
  104. Assert.Equal(id, result.Id);
  105. Assert.NotNull(((IServiceResource<RackConnectService>)result).Owner);
  106. }
  107. }
  108. [Fact]
  109. public void UpdatePublicIP()
  110. {
  111. using (var httpTest = new HttpTest())
  112. {
  113. Identifier id = Guid.NewGuid();
  114. httpTest.RespondWithJson(new PublicIP { Id = id });
  115. var result = _rackConnectService.UpdatePublicIP(id, new PublicIPUpdateDefinition());
  116. httpTest.ShouldHaveCalled($"*/public_ips/{id}");
  117. Assert.NotNull(result);
  118. Assert.Equal(id, result.Id);
  119. Assert.NotNull(((IServiceResource<RackConnectService>)result).Owner);
  120. }
  121. }
  122. [Fact]
  123. public void AssignPublicIP()
  124. {
  125. using (var httpTest = new HttpTest())
  126. {
  127. Identifier serverId = Guid.NewGuid();
  128. Identifier id = Guid.NewGuid();
  129. httpTest.RespondWithJson(new PublicIP {Id = id});
  130. httpTest.RespondWithJson(new PublicIP {Id = id, Server = new PublicIPServerAssociation {ServerId = serverId}});
  131. var ip = _rackConnectService.GetPublicIP(id);
  132. ip.Assign(serverId);
  133. httpTest.ShouldHaveCalled($"*/public_ips/{id}");
  134. Assert.NotNull(ip.Server);
  135. Assert.Equal(serverId, ip.Server.ServerId);
  136. }
  137. }
  138. [Fact]
  139. public void AssignPublicIP_RetriesWhenTheServerIsNotFound()
  140. {
  141. using (var httpTest = new HttpTest())
  142. {
  143. Identifier serverId = Guid.NewGuid();
  144. Identifier id = Guid.NewGuid();
  145. httpTest.RespondWithJson(new PublicIP { Id = id });
  146. httpTest.RespondWith((int)HttpStatusCode.Conflict, $"Cloud Server {serverId} does not exist");
  147. httpTest.RespondWithJson(new PublicIP { Id = id, Server = new PublicIPServerAssociation { ServerId = serverId } });
  148. var ip = _rackConnectService.GetPublicIP(id);
  149. ip.Assign(serverId);
  150. httpTest.ShouldHaveCalled($"*/public_ips/{id}");
  151. Assert.NotNull(ip.Server);
  152. Assert.Equal(serverId, ip.Server.ServerId);
  153. }
  154. }
  155. [Fact]
  156. public void UnassignPublicIP()
  157. {
  158. using (var httpTest = new HttpTest())
  159. {
  160. Identifier id = Guid.NewGuid();
  161. httpTest.RespondWithJson(new PublicIP {Id = id, Server = new PublicIPServerAssociation()});
  162. httpTest.RespondWithJson(new PublicIP {Id = id });
  163. var ip = _rackConnectService.GetPublicIP(id);
  164. ip.Unassign();
  165. httpTest.ShouldHaveCalled($"*/public_ips/{id}");
  166. Assert.Null(ip.Server);
  167. }
  168. }
  169. [Fact]
  170. public void WaitUntilActive()
  171. {
  172. using (var httpTest = new HttpTest())
  173. {
  174. Identifier id = Guid.NewGuid();
  175. httpTest.RespondWithJson(new PublicIP {Id = id, Status = PublicIPStatus.Creating});
  176. httpTest.RespondWithJson(new PublicIP {Id = id, Status = PublicIPStatus.Active, PublicIPv4Address = "10.0.0.1"});
  177. var ip = _rackConnectService.GetPublicIP(id);
  178. ip.WaitUntilActive();
  179. Assert.NotNull(ip);
  180. Assert.Equal(id, ip.Id);
  181. Assert.NotNull(ip.PublicIPv4Address);
  182. }
  183. }
  184. [Fact]
  185. public void WaitUntilActive_ThrowsException_WhenAddFails()
  186. {
  187. using (var httpTest = new HttpTest())
  188. {
  189. Identifier id = Guid.NewGuid();
  190. httpTest.RespondWithJson(new PublicIP { Id = id, Status = PublicIPStatus.Creating });
  191. httpTest.RespondWithJson(new PublicIP { Id = id, Status = PublicIPStatus.CreateFailed, StatusDetails = "No IP for you!"});
  192. var ip = _rackConnectService.GetPublicIP(id);
  193. Assert.Throws<ServiceOperationFailedException>(() => ip.WaitUntilActive());
  194. }
  195. }
  196. [Fact]
  197. public void WaitUntilActive_ThrowsException_WhenCalledOnManuallyCreatedInstance()
  198. {
  199. var ip = new PublicIP();
  200. Assert.Throws<InvalidOperationException>(() => ip.WaitUntilActive());
  201. }
  202. [Fact]
  203. public void DeletePublicIP()
  204. {
  205. using (var httpTest = new HttpTest())
  206. {
  207. Identifier id = Guid.NewGuid();
  208. httpTest.RespondWithJson(new PublicIP { Id = id });
  209. _rackConnectService.DeletePublicIP(id);
  210. httpTest.ShouldHaveCalled($"*/public_ips/{id}");
  211. }
  212. }
  213. [Fact]
  214. public void WaitUntilDeleted()
  215. {
  216. using (var httpTest = new HttpTest())
  217. {
  218. Identifier id = Guid.NewGuid();
  219. httpTest.RespondWithJson(new PublicIP { Id = id, Status = PublicIPStatus.Active });
  220. httpTest.RespondWith((int) HttpStatusCode.NoContent, "All gone!");
  221. httpTest.RespondWithJson(new PublicIP { Id = id, Status = PublicIPStatus.Deleted });
  222. var ip = _rackConnectService.GetPublicIP(id);
  223. ip.Delete();
  224. ip.WaitUntilDeleted();
  225. }
  226. }
  227. [Fact]
  228. public void WaitUntilDeleted_ThrowsException_WhenDeleteFails()
  229. {
  230. using (var httpTest = new HttpTest())
  231. {
  232. Identifier id = Guid.NewGuid();
  233. httpTest.RespondWithJson(new PublicIP { Id = id, Status = PublicIPStatus.Active });
  234. httpTest.RespondWithJson(new PublicIP { Id = id, Status = PublicIPStatus.Active });
  235. httpTest.RespondWith(JObject.Parse(@"{'status':'REMOVE_FAILED'}").ToString());
  236. var ip = _rackConnectService.GetPublicIP(id);
  237. ip.Delete();
  238. Assert.Throws<ServiceOperationFailedException>(() =>ip.WaitUntilDeleted());
  239. }
  240. }
  241. [Fact]
  242. public void WaitUntilDeleted_AcceptsNotFoundExceptionAsSuccess()
  243. {
  244. using (var httpTest = new HttpTest())
  245. {
  246. Identifier id = Guid.NewGuid();
  247. httpTest.RespondWithJson(new PublicIP { Id = id, Status = PublicIPStatus.Active });
  248. httpTest.RespondWith((int) HttpStatusCode.NoContent, "All gone!");
  249. httpTest.RespondWith((int) HttpStatusCode.NotFound, "Not here, boss!");
  250. var ip = _rackConnectService.GetPublicIP(id);
  251. ip.Delete();
  252. ip.WaitUntilDeleted();
  253. }
  254. }
  255. [Fact]
  256. public void WaitUntilDeleted_ThrowsException_WhenCalledOnManuallyCreatedInstance()
  257. {
  258. var ip = new PublicIP();
  259. Assert.Throws<InvalidOperationException>(() => ip.WaitUntilDeleted());
  260. }
  261. }
  262. }