PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/jslee1/azure-powershell
C# | 529 lines | 411 code | 88 blank | 30 comment | 0 complexity | 55af33456b97ba474e321d504870fb47 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.IO;
  17. using System.Net;
  18. using System.Threading;
  19. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  20. using Xunit;
  21. using Microsoft.Azure.Commands.Common.Authentication.Models;
  22. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  23. using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
  24. using Microsoft.WindowsAzure.Commands.Utilities.CloudService;
  25. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  26. using Microsoft.WindowsAzure.Management.Compute.Models;
  27. using Microsoft.WindowsAzure.Management.Models;
  28. using Microsoft.WindowsAzure.Management.Storage;
  29. using Microsoft.WindowsAzure.Management.Storage.Models;
  30. using Microsoft.WindowsAzure.Storage.Blob;
  31. using Moq;
  32. using MockStorageService = Microsoft.WindowsAzure.Commands.Test.Utilities.Common.MockStorageService;
  33. using Microsoft.WindowsAzure.Commands.Common;
  34. using Microsoft.Azure.Commands.Common.Authentication;
  35. using Microsoft.Azure;
  36. namespace Microsoft.WindowsAzure.Commands.Test.CloudService.Utilities
  37. {
  38. public class CloudServiceClientTests : SMTestBase
  39. {
  40. private AzureSubscription subscription;
  41. private ClientMocks clientMocks;
  42. private Mock<CloudBlobUtility> cloudBlobUtilityMock;
  43. private ICloudServiceClient client;
  44. private const string serviceName = "cloudService";
  45. private const string storageName = "storagename";
  46. private MockServicesHost services;
  47. private MockStorageService storageService;
  48. private void ExecuteInTempCurrentDirectory(string path, Action action)
  49. {
  50. string currentDirectory = System.Environment.CurrentDirectory;
  51. try
  52. {
  53. System.Environment.CurrentDirectory = path;
  54. action();
  55. }
  56. catch
  57. {
  58. System.Environment.CurrentDirectory = currentDirectory;
  59. throw;
  60. }
  61. }
  62. private void SetupStorage(string name, MockStorageService.StorageAccountData a)
  63. {
  64. a.Name = name;
  65. a.BlobEndpoint = "http://awesome.blob.core.windows.net/";
  66. a.QueueEndpoint = "http://awesome.queue.core.windows.net/";
  67. a.TableEndpoint = "http://awesome.table.core.windows.net/";
  68. a.PrimaryKey =
  69. "MNao3bm7t7B/x+g2/ssh9HnG0mEh1QV5EHpcna8CetYn+TSRoA8/SBoH6B3Ufwtnz3jZLSw9GEUuCTr3VooBWq==";
  70. a.SecondaryKey = "secondaryKey";
  71. }
  72. private void RemoveDeployments()
  73. {
  74. services.Clear()
  75. .Add(s => { s.Name = serviceName; });
  76. }
  77. public CloudServiceClientTests()
  78. {
  79. AzurePowerShell.ProfileDirectory = Test.Utilities.Common.Data.AzureSdkAppDir;
  80. storageService = new MockStorageService()
  81. .Add(a => SetupStorage(serviceName.ToLowerInvariant(), a))
  82. .Add(a => SetupStorage(storageName.ToLowerInvariant(), a));
  83. services = new MockServicesHost()
  84. .Add(s =>
  85. {
  86. s.Name = serviceName;
  87. s.AddDeployment(d =>
  88. {
  89. d.Slot = DeploymentSlot.Production;
  90. d.Name = "mydeployment";
  91. });
  92. });
  93. subscription = new AzureSubscription
  94. {
  95. Properties = new Dictionary<AzureSubscription.Property,string> {{AzureSubscription.Property.Default, "True"}},
  96. Id = Guid.NewGuid(),
  97. Name = Test.Utilities.Common.Data.Subscription1,
  98. };
  99. cloudBlobUtilityMock = new Mock<CloudBlobUtility>();
  100. cloudBlobUtilityMock.Setup(f => f.UploadPackageToBlob(
  101. It.IsAny<StorageManagementClient>(),
  102. It.IsAny<string>(),
  103. It.IsAny<string>(),
  104. It.IsAny<BlobRequestOptions>())).Returns(new Uri("http://www.packageurl.azure.com"));
  105. clientMocks = new ClientMocks(subscription.Id);
  106. services.InitializeMocks(clientMocks.ComputeManagementClientMock);
  107. storageService.InitializeMocks(clientMocks.StorageManagementClientMock);
  108. client = new CloudServiceClient(subscription,
  109. clientMocks.ManagementClientMock.Object,
  110. clientMocks.StorageManagementClientMock.Object,
  111. clientMocks.ComputeManagementClientMock.Object
  112. )
  113. {
  114. CloudBlobUtility = cloudBlobUtilityMock.Object
  115. };
  116. }
  117. [Fact]
  118. [Trait(Category.AcceptanceType, Category.CheckIn)]
  119. public void TestStartCloudService()
  120. {
  121. client.StartCloudService(serviceName);
  122. Assert.True(services.LastDeploymentStatusUpdate.HasValue);
  123. Assert.Equal(UpdatedDeploymentStatus.Running, services.LastDeploymentStatusUpdate.Value);
  124. }
  125. [Fact]
  126. [Trait(Category.AcceptanceType, Category.CheckIn)]
  127. public void TestStopCloudService()
  128. {
  129. client.StopCloudService(serviceName);
  130. Assert.True(services.LastDeploymentStatusUpdate.HasValue);
  131. Assert.Equal(UpdatedDeploymentStatus.Suspended, services.LastDeploymentStatusUpdate.Value);
  132. }
  133. [Fact]
  134. [Trait(Category.AcceptanceType, Category.CheckIn)]
  135. public void TestRemoveCloudService()
  136. {
  137. clientMocks.ComputeManagementClientMock.Setup(
  138. c => c.Deployments.DeleteByNameAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()))
  139. .Returns((string s, string deploymentName, bool deleteAll, CancellationToken cancellationToken) => Tasks.FromResult(
  140. CreateComputeOperationResponse("req0")));
  141. clientMocks.ComputeManagementClientMock.Setup(
  142. c => c.HostedServices.DeleteAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
  143. .Returns(Tasks.FromResult(new AzureOperationResponse
  144. {
  145. RequestId = "request000",
  146. StatusCode = HttpStatusCode.OK
  147. }));
  148. // Test
  149. client.RemoveCloudService(serviceName);
  150. // Assert
  151. clientMocks.ComputeManagementClientMock.Verify(
  152. c => c.Deployments.DeleteByNameAsync(serviceName, "mydeployment", false, It.IsAny<CancellationToken>()), Times.Once);
  153. clientMocks.ComputeManagementClientMock.Verify(
  154. c => c.HostedServices.DeleteAsync(serviceName, It.IsAny<CancellationToken>()), Times.Once);
  155. }
  156. [Fact]
  157. [Trait(Category.AcceptanceType, Category.CheckIn)]
  158. public void TestRemoveCloudServiceWithStaging()
  159. {
  160. services.Clear()
  161. .Add(s =>
  162. {
  163. s.Name = serviceName;
  164. s.AddDeployment(d =>
  165. {
  166. d.Name = "myStagingdeployment";
  167. d.Slot = DeploymentSlot.Staging;
  168. });
  169. });
  170. clientMocks.ComputeManagementClientMock.Setup(
  171. c => c.Deployments.DeleteByNameAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()))
  172. .Returns((string s, string deploymentName, bool deleteAll, CancellationToken cancellationToken) => Tasks.FromResult(
  173. CreateComputeOperationResponse("request001")));
  174. clientMocks.ComputeManagementClientMock.Setup(
  175. c => c.HostedServices.DeleteAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
  176. .Returns(Tasks.FromResult(new AzureOperationResponse
  177. {
  178. RequestId = "request000",
  179. StatusCode = HttpStatusCode.OK
  180. }));
  181. // Test
  182. client.RemoveCloudService(serviceName);
  183. // Assert
  184. clientMocks.ComputeManagementClientMock.Verify(
  185. c => c.Deployments.DeleteByNameAsync(serviceName, "myStagingdeployment", false, It.IsAny<CancellationToken>()), Times.Once);
  186. clientMocks.ComputeManagementClientMock.Verify(
  187. c => c.HostedServices.DeleteAsync(serviceName, It.IsAny<CancellationToken>()), Times.Once);
  188. }
  189. [Fact]
  190. [Trait(Category.AcceptanceType, Category.CheckIn)]
  191. public void TestRemoveCloudServiceWithoutDeployments()
  192. {
  193. RemoveDeployments();
  194. clientMocks.ComputeManagementClientMock.Setup(
  195. c => c.Deployments.BeginDeletingBySlotAsync(It.IsAny<string>(), DeploymentSlot.Production, It.IsAny<CancellationToken>()))
  196. .Returns((string s, DeploymentSlot slot, CancellationToken cancellationToken) => Tasks.FromResult(new AzureOperationResponse
  197. {
  198. RequestId = "req0",
  199. StatusCode = HttpStatusCode.OK
  200. }));
  201. clientMocks.ComputeManagementClientMock.Setup(
  202. c => c.HostedServices.DeleteAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
  203. .Returns(Tasks.FromResult(new AzureOperationResponse
  204. {
  205. RequestId = "request000",
  206. StatusCode = HttpStatusCode.OK
  207. }));
  208. // Test
  209. client.RemoveCloudService(serviceName);
  210. // Assert
  211. clientMocks.ComputeManagementClientMock.Verify(
  212. c => c.Deployments.BeginDeletingBySlotAsync(serviceName, DeploymentSlot.Production, It.IsAny<CancellationToken>()), Times.Never);
  213. clientMocks.ComputeManagementClientMock.Verify(
  214. c => c.HostedServices.DeleteAsync(serviceName, It.IsAny<CancellationToken>()), Times.Once);
  215. }
  216. [Fact (Skip = "Ignore")]
  217. public void TestPublishNewCloudService()
  218. {
  219. RemoveDeployments();
  220. clientMocks.ComputeManagementClientMock.Setup(
  221. c =>
  222. c.HostedServices.CreateAsync(It.IsAny<HostedServiceCreateParameters>(), It.IsAny<CancellationToken>()))
  223. .Returns(Tasks.FromResult(new AzureOperationResponse
  224. {
  225. RequestId = "request001",
  226. StatusCode = HttpStatusCode.OK
  227. }));
  228. using (var files = new FileSystemHelper(this) { EnableMonitoring = true })
  229. {
  230. // Setup
  231. string rootPath = files.CreateNewService(serviceName);
  232. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  233. var cloudServiceProject = new CloudServiceProject(rootPath, FileUtilities.GetContentFilePath(@"..\..\..\..\..\Package\Debug\ServiceManagement\Azure\Services"));
  234. cloudServiceProject.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  235. ExecuteInTempCurrentDirectory(rootPath, () => client.PublishCloudService(location: "West US"));
  236. clientMocks.ComputeManagementClientMock.Verify(c => c.Deployments.CreateAsync(
  237. serviceName, DeploymentSlot.Production, It.IsAny<DeploymentCreateParameters>(), It.IsAny<CancellationToken>()), Times.Once);
  238. }
  239. }
  240. [Fact(Skip = "Ignore")]
  241. public void TestUpgradeCloudService()
  242. {
  243. clientMocks.ComputeManagementClientMock.Setup(
  244. c =>
  245. c.HostedServices.CreateAsync(It.IsAny<HostedServiceCreateParameters>(), It.IsAny<CancellationToken>()))
  246. .Returns(Tasks.FromResult(new AzureOperationResponse
  247. {
  248. RequestId = "request001",
  249. StatusCode = HttpStatusCode.OK
  250. }));
  251. clientMocks.ComputeManagementClientMock.Setup(
  252. c =>
  253. c.Deployments.UpgradeBySlotAsync(It.IsAny<string>(), DeploymentSlot.Production,
  254. It.IsAny<DeploymentUpgradeParameters>(),
  255. It.IsAny<CancellationToken>()))
  256. .Returns(Tasks.FromResult(CreateComputeOperationResponse("req002")));
  257. using (var files = new FileSystemHelper(this) { EnableMonitoring = true })
  258. {
  259. // Setup
  260. string rootPath = files.CreateNewService(serviceName);
  261. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  262. var cloudServiceProject = new CloudServiceProject(rootPath, FileUtilities.GetContentFilePath(@"..\..\..\..\..\Package\Debug\ServiceManagement\Azure\Services"));
  263. cloudServiceProject.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  264. ExecuteInTempCurrentDirectory(rootPath, () => client.PublishCloudService(location: "West US"));
  265. clientMocks.ComputeManagementClientMock.Verify(c => c.Deployments.UpgradeBySlotAsync(serviceName, DeploymentSlot.Production, It.IsAny<DeploymentUpgradeParameters>(), It.IsAny<CancellationToken>()), Times.Once);
  266. }
  267. }
  268. [Fact(Skip = "Ignore")]
  269. public void TestCreateStorageServiceWithPublish()
  270. {
  271. RemoveDeployments();
  272. clientMocks.ComputeManagementClientMock.Setup(
  273. c =>
  274. c.HostedServices.CreateAsync(It.IsAny<HostedServiceCreateParameters>(), It.IsAny<CancellationToken>()))
  275. .Returns(Tasks.FromResult(new AzureOperationResponse
  276. {
  277. RequestId = "request001",
  278. StatusCode = HttpStatusCode.OK
  279. }));
  280. storageService.Clear();
  281. using (var files = new FileSystemHelper(this) { EnableMonitoring = true })
  282. {
  283. // Setup
  284. string rootPath = files.CreateNewService(serviceName);
  285. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  286. var cloudServiceProject = new CloudServiceProject(rootPath, FileUtilities.GetContentFilePath(@"..\..\..\..\..\Package\Debug\ServiceManagement\Azure\Services"));
  287. cloudServiceProject.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  288. ExecuteInTempCurrentDirectory(rootPath, () => client.PublishCloudService(location: "West US"));
  289. clientMocks.StorageManagementClientMock.Verify(c => c.StorageAccounts.CreateAsync(It.IsAny<StorageAccountCreateParameters>(), It.IsAny<CancellationToken>()), Times.Once);
  290. }
  291. }
  292. [Fact(Skip = "Ignore")]
  293. public void TestPublishWithCurrentStorageAccount()
  294. {
  295. RemoveDeployments();
  296. clientMocks.ComputeManagementClientMock.Setup(
  297. c =>
  298. c.HostedServices.CreateAsync(It.IsAny<HostedServiceCreateParameters>(), It.IsAny<CancellationToken>()))
  299. .Returns(Tasks.FromResult(new AzureOperationResponse
  300. {
  301. RequestId = "request001",
  302. StatusCode = HttpStatusCode.OK
  303. }));
  304. using (var files = new FileSystemHelper(this) { EnableMonitoring = true })
  305. {
  306. // Setup
  307. string rootPath = files.CreateNewService(serviceName);
  308. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  309. var cloudServiceProject = new CloudServiceProject(rootPath, FileUtilities.GetContentFilePath(@"..\..\..\..\..\Package\Debug\ServiceManagement\Azure\Services"));
  310. cloudServiceProject.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  311. subscription.Properties[AzureSubscription.Property.StorageAccount] = storageName;
  312. ExecuteInTempCurrentDirectory(rootPath, () => client.PublishCloudService(location: "West US"));
  313. cloudBlobUtilityMock.Verify(f => f.UploadPackageToBlob(
  314. clientMocks.StorageManagementClientMock.Object,
  315. subscription.GetProperty(AzureSubscription.Property.StorageAccount),
  316. It.IsAny<string>(),
  317. It.IsAny<BlobRequestOptions>()), Times.Once());
  318. }
  319. }
  320. [Fact(Skip = "Ignore")]
  321. public void TestPublishWithDefaultLocation()
  322. {
  323. RemoveDeployments();
  324. clientMocks.ComputeManagementClientMock.Setup(
  325. c =>
  326. c.HostedServices.CreateAsync(It.IsAny<HostedServiceCreateParameters>(), It.IsAny<CancellationToken>()))
  327. .Returns(Tasks.FromResult(new AzureOperationResponse
  328. {
  329. RequestId = "request001",
  330. StatusCode = HttpStatusCode.OK
  331. }));
  332. clientMocks.ManagementClientMock.Setup(c => c.Locations.ListAsync(It.IsAny<CancellationToken>()))
  333. .Returns(Tasks.FromResult(new LocationsListResponse
  334. {
  335. Locations =
  336. {
  337. new LocationsListResponse.Location {DisplayName = "East US", Name = "EastUS"}
  338. }
  339. }));
  340. using (var files = new FileSystemHelper(this) { EnableMonitoring = true })
  341. {
  342. // Setup
  343. string rootPath = files.CreateNewService(serviceName);
  344. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  345. var cloudServiceProject = new CloudServiceProject(rootPath, FileUtilities.GetContentFilePath(@"..\..\..\..\..\Package\Debug\ServiceManagement\Azure\Services"));
  346. cloudServiceProject.AddWebRole(Test.Utilities.Common.Data.NodeWebRoleScaffoldingPath);
  347. ExecuteInTempCurrentDirectory(rootPath, () => client.PublishCloudService());
  348. clientMocks.ManagementClientMock.Verify(c => c.Locations.ListAsync(It.IsAny<CancellationToken>()), Times.Once);
  349. }
  350. }
  351. [Fact]
  352. [Trait(Category.AcceptanceType, Category.CheckIn)]
  353. public void TestPublishFromPackageUsingDefaultLocation()
  354. {
  355. RemoveDeployments();
  356. clientMocks.ComputeManagementClientMock.Setup(
  357. c =>
  358. c.HostedServices.CreateAsync(It.IsAny<HostedServiceCreateParameters>(), It.IsAny<CancellationToken>()))
  359. .Returns(Tasks.FromResult(new AzureOperationResponse
  360. {
  361. RequestId = "request001",
  362. StatusCode = HttpStatusCode.OK
  363. }));
  364. clientMocks.ManagementClientMock.Setup(c => c.Locations.ListAsync(It.IsAny<CancellationToken>()))
  365. .Returns(Tasks.FromResult(new LocationsListResponse
  366. {
  367. Locations =
  368. {
  369. new LocationsListResponse.Location {DisplayName = "East US", Name = "EastUS"}
  370. }
  371. }));
  372. using (var files = new FileSystemHelper(this) { EnableMonitoring = false })
  373. {
  374. // Setup
  375. string packageName = serviceName;
  376. string package, configuration;
  377. files.CreateDirectoryWithPrebuiltPackage(packageName, out package, out configuration);
  378. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  379. // Execute
  380. ExecuteInTempCurrentDirectory(Path.GetDirectoryName(package),
  381. () => client.PublishCloudService(package, configuration, null, null, null, null, null, false, false));
  382. // Verify
  383. clientMocks.ComputeManagementClientMock.Verify(c => c.Deployments.CreateAsync(
  384. serviceName, DeploymentSlot.Production, It.IsAny<DeploymentCreateParameters>(), It.IsAny<CancellationToken>()), Times.Once);
  385. }
  386. }
  387. [Fact]
  388. [Trait(Category.AcceptanceType, Category.CheckIn)]
  389. public void TestUpgradeCloudServiceFromAPackage()
  390. {
  391. clientMocks.ComputeManagementClientMock.Setup(
  392. c =>
  393. c.HostedServices.CreateAsync(It.IsAny<HostedServiceCreateParameters>(), It.IsAny<CancellationToken>()))
  394. .Returns(Tasks.FromResult(new AzureOperationResponse
  395. {
  396. RequestId = "request001",
  397. StatusCode = HttpStatusCode.OK
  398. }));
  399. clientMocks.ComputeManagementClientMock.Setup(
  400. c =>
  401. c.Deployments.UpgradeBySlotAsync(It.IsAny<string>(), DeploymentSlot.Production,
  402. It.IsAny<DeploymentUpgradeParameters>(),
  403. It.IsAny<CancellationToken>()))
  404. .Returns(Tasks.FromResult(CreateComputeOperationResponse("req002")));
  405. clientMocks.ManagementClientMock.Setup(c => c.Locations.ListAsync(It.IsAny<CancellationToken>()))
  406. .Returns(Tasks.FromResult(new LocationsListResponse
  407. {
  408. Locations =
  409. {
  410. new LocationsListResponse.Location {DisplayName = "East US", Name = "EastUS"}
  411. }
  412. }));
  413. using (var files = new FileSystemHelper(this) { EnableMonitoring = true })
  414. {
  415. // Setup
  416. string packageName = serviceName;
  417. string package, configuration;
  418. files.CreateDirectoryWithPrebuiltPackage(packageName, out package, out configuration);
  419. files.CreateAzureSdkDirectoryAndImportPublishSettings();
  420. // Execute
  421. ExecuteInTempCurrentDirectory(Path.GetDirectoryName(package),
  422. () => client.PublishCloudService(package, configuration, null, null, null, null, null, false, false));
  423. // Verify
  424. clientMocks.ComputeManagementClientMock.Verify(c => c.Deployments.UpgradeBySlotAsync(serviceName,
  425. DeploymentSlot.Production, It.IsAny<DeploymentUpgradeParameters>(), It.IsAny<CancellationToken>()), Times.Once);
  426. }
  427. }
  428. private OperationStatusResponse CreateComputeOperationResponse(string requestId, OperationStatus status = OperationStatus.Succeeded)
  429. {
  430. return new OperationStatusResponse
  431. {
  432. Error = null,
  433. HttpStatusCode = HttpStatusCode.OK,
  434. Id = "id",
  435. RequestId = requestId,
  436. Status = status,
  437. StatusCode = HttpStatusCode.OK
  438. };
  439. }
  440. }
  441. }