PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/redistributable/openstack.net/src/testing/unit/ContentDeliveryNetworks/v1/ServiceTests.cs

https://gitlab.com/rekby-archive/onlyoffice-CommunityServer
C# | 302 lines | 258 code | 44 blank | 0 comment | 4 complexity | 3150a1e38c1f3c3df5584f5d3abcb29d MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Net.Http;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Flurl;
  8. using Flurl.Extensions;
  9. using Marvin.JsonPatch;
  10. using Newtonsoft.Json;
  11. using OpenStack.Serialization;
  12. using OpenStack.Synchronous;
  13. using OpenStack.Testing;
  14. using Xunit;
  15. namespace OpenStack.ContentDeliveryNetworks.v1
  16. {
  17. public class ServiceTests
  18. {
  19. private readonly ContentDeliveryNetworkService _cdnService;
  20. public ServiceTests()
  21. {
  22. _cdnService = new ContentDeliveryNetworkService(Stubs.AuthenticationProvider, "DFW");
  23. }
  24. [Fact]
  25. public void SerializeServiceCollection()
  26. {
  27. var services = new ServiceCollection
  28. {
  29. Items = {new Service {Id = "service-id"}},
  30. Links = {new PageLink("next", "http://api.com/next")}
  31. };
  32. string json = OpenStackNet.Serialize(services);
  33. Assert.Contains("\"services\"", json);
  34. Assert.DoesNotContain("\"service\"", json);
  35. var result = OpenStackNet.Deserialize<ServiceCollection>(json);
  36. Assert.NotNull(result);
  37. Assert.Single(result);
  38. Assert.Single(result.Items);
  39. Assert.Single(result.Links);
  40. Assert.True(result.HasNextPage);
  41. }
  42. [Fact]
  43. public void SerializePageLink()
  44. {
  45. var link = new PageLink("next", "http://api.com/next");
  46. string json = OpenStackNet.Serialize(link);
  47. var result = OpenStackNet.Deserialize<PageLink>(json);
  48. Assert.NotNull(result);
  49. Assert.True(result.IsNextPage);
  50. }
  51. [Fact]
  52. public void FindServiceOnAPage()
  53. {
  54. using (var httpTest = new HttpTest())
  55. {
  56. httpTest.RespondWithJson(new ServiceCollection
  57. {
  58. Items = {new Service()},
  59. Links = {new PageLink("next", "http://api.com/next")}
  60. });
  61. httpTest.RespondWithJson(new ServiceCollection
  62. {
  63. Items = {new Service {Name = "MyService"}}
  64. });
  65. var currentPage = _cdnService.ListServices();
  66. Service myService;
  67. do
  68. {
  69. myService = currentPage.FirstOrDefault(x => x.Name == "MyService");
  70. if (myService != null)
  71. break;
  72. currentPage = currentPage.GetNextPage();
  73. } while (currentPage.Any());
  74. Assert.NotNull(myService);
  75. }
  76. }
  77. [Fact]
  78. public void GetService()
  79. {
  80. using (var httpTest = new HttpTest())
  81. {
  82. httpTest.RespondWithJson(new Service{Id = "service-id"});
  83. var service = _cdnService.GetService("service-id");
  84. Assert.NotNull(service);
  85. Assert.Equal("service-id", service.Id);
  86. }
  87. }
  88. [Fact]
  89. public void CreateService()
  90. {
  91. using (var httpTest = new HttpTest())
  92. {
  93. var response = new HttpResponseMessage(HttpStatusCode.Created);
  94. response.Headers.Location = "http://api.com".AppendPathSegments("services", "service-id").ToUri();
  95. httpTest.ResponseQueue.Enqueue(response);
  96. var service = new ServiceDefinition("service-name", "flavor-id", "www.example.com", "example.com")
  97. {
  98. Caches =
  99. {
  100. new ServiceCache("keep-one-day", TimeSpan.FromDays(1))
  101. },
  102. Restrictions =
  103. {
  104. new ServiceRestriction("internal-users-only", new[]
  105. {
  106. new ServiceRestrictionRule("intranet", "intranet.example.com")
  107. })
  108. }
  109. };
  110. var serviceId = _cdnService.CreateService(service);
  111. Assert.Equal("service-id", serviceId);
  112. }
  113. }
  114. [Fact]
  115. public void DeleteService()
  116. {
  117. using (var httpTest = new HttpTest())
  118. {
  119. httpTest.RespondWith((int)HttpStatusCode.NoContent, "All gone!");
  120. _cdnService.DeleteService("service-id");
  121. }
  122. }
  123. [Fact]
  124. public void WaitForServiceDeployed()
  125. {
  126. using (var httpTest = new HttpTest())
  127. {
  128. httpTest.RespondWithJson(new Service { Status = ServiceStatus.CreateInProgress });
  129. httpTest.RespondWithJson(new Service { Status = ServiceStatus.UpdateInProgress });
  130. httpTest.RespondWithJson(new Service { Status = ServiceStatus.Deployed });
  131. var service = _cdnService.WaitForServiceDeployed("service-id", TimeSpan.FromMilliseconds(1));
  132. Assert.NotNull(service);
  133. }
  134. }
  135. [Fact]
  136. public void WaitForServiceDeploy_ThrowsAnException_WhenTheOperationFailed()
  137. {
  138. using (var httpTest = new HttpTest())
  139. {
  140. var failedServiceDeployment = new Service
  141. {
  142. Status = ServiceStatus.Failed,
  143. Errors = new[] { new ServiceError { Message = "The domain is already in use." } }
  144. };
  145. httpTest.RespondWithJson(failedServiceDeployment);
  146. var ex = Assert.Throws<ServiceOperationFailedException>(() => _cdnService.WaitForServiceDeployed("failed-service-id"));
  147. Assert.NotEmpty(ex.Errors);
  148. }
  149. }
  150. [Fact]
  151. public async Task WaitForServiceDeployed_StopsWhenUserTokenIsCancelled()
  152. {
  153. using (var httpTest = new HttpTest())
  154. {
  155. httpTest.RespondWithJson(new Service {Status = ServiceStatus.CreateInProgress});
  156. var timedToken = new CancellationTokenSource(TimeSpan.FromSeconds(1));
  157. await Assert.ThrowsAsync<TaskCanceledException>(() => _cdnService.WaitForServiceDeployedAsync("service-id", Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan, null, timedToken.Token));
  158. }
  159. }
  160. [Fact]
  161. public async Task WaitForServiceDeployed_StopsWhenTimeoutIsReached()
  162. {
  163. using (var httpTest = new HttpTest())
  164. {
  165. httpTest.RespondWithJson(new Service { Status = ServiceStatus.CreateInProgress });
  166. await Assert.ThrowsAsync<TimeoutException>(() => _cdnService.WaitForServiceDeployedAsync("service-id", TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1)));
  167. }
  168. }
  169. [Fact]
  170. public void WaitForServiceDeleted()
  171. {
  172. using (var httpTest = new HttpTest())
  173. {
  174. httpTest.RespondWithJson(new Service { Status = ServiceStatus.DeleteInProgress });
  175. httpTest.RespondWith((int)HttpStatusCode.NotFound, "All gone!");
  176. _cdnService.WaitForServiceDeleted("service-id", TimeSpan.FromMilliseconds(1));
  177. }
  178. }
  179. [Fact]
  180. public async Task WaitForServiceDeleted_StopsWhenUserTokenIsCancelled()
  181. {
  182. using (var httpTest = new HttpTest())
  183. {
  184. httpTest.RespondWithJson(new Service { Status = ServiceStatus.DeleteInProgress });
  185. var timedToken = new CancellationTokenSource(TimeSpan.FromSeconds(1));
  186. await Assert.ThrowsAsync<TaskCanceledException>(() => _cdnService.WaitForServiceDeletedAsync("service-id", Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan, null, timedToken.Token));
  187. }
  188. }
  189. [Fact]
  190. public async Task WaitForServiceDeleted_StopsWhenTimeoutIsReached()
  191. {
  192. using (var httpTest = new HttpTest())
  193. {
  194. httpTest.RespondWithJson(new Service { Status = ServiceStatus.CreateInProgress });
  195. await Assert.ThrowsAsync<TimeoutException>(() => _cdnService.WaitForServiceDeletedAsync("service-id", TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1)));
  196. }
  197. }
  198. [Fact]
  199. public void WaitForServiceDeleted_ThrowsAnException_WhenTheOperationFailed()
  200. {
  201. using (var httpTest = new HttpTest())
  202. {
  203. var failedServiceDeployment = new Service
  204. {
  205. Status = ServiceStatus.Failed,
  206. Errors = new[] { new ServiceError { Message = "Random error." } }
  207. };
  208. httpTest.RespondWithJson(failedServiceDeployment);
  209. var ex = Assert.Throws<ServiceOperationFailedException>(() => _cdnService.WaitForServiceDeleted("failed-service-id"));
  210. Assert.NotEmpty(ex.Errors);
  211. }
  212. }
  213. [Fact]
  214. public void WhenIDeleteANonExistingService_TheOperationShouldBeConsideredSuccessful()
  215. {
  216. using (var httpTest = new HttpTest())
  217. {
  218. httpTest.RespondWith((int)HttpStatusCode.NotFound, "Nothing to see here!");
  219. _cdnService.DeleteService("bad-service-id");
  220. }
  221. }
  222. [Fact]
  223. public void UpdateService()
  224. {
  225. using (new HttpTest())
  226. {
  227. var patch = new JsonPatchDocument<ServiceDefinition>();
  228. patch.Replace(x => x.Name, "My Cool New Name");
  229. _cdnService.UpdateService("service-id", patch);
  230. }
  231. }
  232. [Fact]
  233. public void PurgeAsset()
  234. {
  235. using (new HttpTest())
  236. {
  237. _cdnService.PurgeCachedAsset("service-id", "asset-url");
  238. }
  239. }
  240. [Fact]
  241. public void PurgeAssets()
  242. {
  243. using (new HttpTest())
  244. {
  245. _cdnService.PurgeCachedAssets("service-id");
  246. }
  247. }
  248. [Fact]
  249. public void WhenIPurgeANonExistingAsset_TheOperationShouldBeConsideredSuccessful()
  250. {
  251. using (var httpTest = new HttpTest())
  252. {
  253. httpTest.RespondWith((int)HttpStatusCode.NotFound, "Nothing to see here!");
  254. _cdnService.PurgeCachedAsset("service-id", "missing-asset");
  255. }
  256. }
  257. }
  258. }