PageRenderTime 137ms CodeModel.GetById 37ms RepoModel.GetById 2ms app.codeStats 0ms

/src/ResourceManager/AzureBatch/Commands.Batch.Test/Pools/GetBatchPoolCommandTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 233 lines | 165 code | 38 blank | 30 comment | 0 complexity | 7324ec5b51d029c596e31c562833bf19 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 Microsoft.Azure.Batch;
  15. using Microsoft.Azure.Batch.Protocol;
  16. using Microsoft.Azure.Commands.Batch.Models;
  17. using Microsoft.Rest.Azure;
  18. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  19. using Moq;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Management.Automation;
  24. using System.Threading.Tasks;
  25. using Xunit;
  26. using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
  27. using ProxyModels = Microsoft.Azure.Batch.Protocol.Models;
  28. namespace Microsoft.Azure.Commands.Batch.Test.Pools
  29. {
  30. public class GetBatchPoolCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
  31. {
  32. private GetBatchPoolCommand cmdlet;
  33. private Mock<BatchClient> batchClientMock;
  34. private Mock<ICommandRuntime> commandRuntimeMock;
  35. public GetBatchPoolCommandTests(Xunit.Abstractions.ITestOutputHelper output)
  36. {
  37. ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
  38. batchClientMock = new Mock<BatchClient>();
  39. commandRuntimeMock = new Mock<ICommandRuntime>();
  40. cmdlet = new GetBatchPoolCommand()
  41. {
  42. CommandRuntime = commandRuntimeMock.Object,
  43. BatchClient = batchClientMock.Object,
  44. };
  45. }
  46. [Fact]
  47. [Trait(Category.AcceptanceType, Category.CheckIn)]
  48. public void GetBatchPoolTest()
  49. {
  50. // Setup cmdlet to get a pool by id
  51. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  52. cmdlet.BatchContext = context;
  53. cmdlet.Id = "testPool";
  54. cmdlet.Filter = null;
  55. // Build a CloudPool instead of querying the service on a Get CloudPool call
  56. AzureOperationResponse<ProxyModels.CloudPool, ProxyModels.PoolGetHeaders> response = BatchTestHelpers.CreateCloudPoolGetResponse(cmdlet.Id);
  57. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  58. ProxyModels.PoolGetOptions,
  59. AzureOperationResponse<ProxyModels.CloudPool, ProxyModels.PoolGetHeaders>>(response);
  60. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  61. // Setup the cmdlet to write pipeline output to a list that can be examined later
  62. List<PSCloudPool> pipeline = new List<PSCloudPool>();
  63. commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSCloudPool>())).Callback<object>(p => pipeline.Add((PSCloudPool)p));
  64. cmdlet.ExecuteCmdlet();
  65. // Verify that the cmdlet wrote the pool returned from the OM to the pipeline
  66. Assert.Equal(1, pipeline.Count);
  67. Assert.Equal(cmdlet.Id, pipeline[0].Id);
  68. }
  69. [Fact]
  70. [Trait(Category.AcceptanceType, Category.CheckIn)]
  71. public void GetBatchPoolODataTest()
  72. {
  73. // Setup cmdlet to get a single pool
  74. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  75. cmdlet.BatchContext = context;
  76. cmdlet.Id = "testPool";
  77. cmdlet.Select = "id,state";
  78. cmdlet.Expand = "stats";
  79. string requestSelect = null;
  80. string requestExpand = null;
  81. // Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
  82. AzureOperationResponse<ProxyModels.CloudPool, ProxyModels.PoolGetHeaders> getResponse = BatchTestHelpers.CreateCloudPoolGetResponse(cmdlet.Id);
  83. RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  84. ProxyModels.PoolGetOptions,
  85. AzureOperationResponse<ProxyModels.CloudPool, ProxyModels.PoolGetHeaders>>(getResponse);
  86. ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
  87. {
  88. ProxyModels.PoolGetOptions options = (ProxyModels.PoolGetOptions)request.Options;
  89. requestSelect = options.Select;
  90. requestExpand = options.Expand;
  91. return Task.FromResult(response);
  92. });
  93. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
  94. cmdlet.ExecuteCmdlet();
  95. Assert.Equal(cmdlet.Select, requestSelect);
  96. Assert.Equal(cmdlet.Expand, requestExpand);
  97. }
  98. [Fact]
  99. [Trait(Category.AcceptanceType, Category.CheckIn)]
  100. public void ListBatchPoolsODataTest()
  101. {
  102. // Setup cmdlet to list pools using an OData filter
  103. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  104. cmdlet.BatchContext = context;
  105. cmdlet.Id = null;
  106. cmdlet.Filter = "startswith(id,'test')";
  107. cmdlet.Select = "id,state";
  108. cmdlet.Expand = "stats";
  109. string requestFilter = null;
  110. string requestSelect = null;
  111. string requestExpand = null;
  112. AzureOperationResponse<IPage<ProxyModels.CloudPool>, ProxyModels.PoolListHeaders> response = BatchTestHelpers.CreateGenericAzureOperationListResponse<ProxyModels.CloudPool, ProxyModels.PoolListHeaders>();
  113. Action<BatchRequest<ProxyModels.PoolListOptions, AzureOperationResponse<IPage<ProxyModels.CloudPool>, ProxyModels.PoolListHeaders>>> extractPoolListAction =
  114. (request) =>
  115. {
  116. ProxyModels.PoolListOptions options = request.Options;
  117. requestFilter = options.Filter;
  118. requestSelect = options.Select;
  119. requestExpand = options.Expand;
  120. };
  121. RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor(responseToUse: response, requestAction: extractPoolListAction);
  122. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor };
  123. cmdlet.ExecuteCmdlet();
  124. Assert.Equal(cmdlet.Filter, requestFilter);
  125. Assert.Equal(cmdlet.Select, requestSelect);
  126. Assert.Equal(cmdlet.Expand, requestExpand);
  127. }
  128. [Fact]
  129. [Trait(Category.AcceptanceType, Category.CheckIn)]
  130. public void ListBatchPoolWithoutFiltersTest()
  131. {
  132. // Setup cmdlet to list pools without filters
  133. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  134. cmdlet.BatchContext = context;
  135. cmdlet.Id = null;
  136. cmdlet.Filter = null;
  137. string[] idsOfConstructedPools = new[] { "pool1", "pool2", "pool3" };
  138. // Build some CloudPools instead of querying the service on a List CloudPools call
  139. AzureOperationResponse<IPage<ProxyModels.CloudPool>, ProxyModels.PoolListHeaders> response = BatchTestHelpers.CreateCloudPoolListResponse(idsOfConstructedPools);
  140. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  141. ProxyModels.PoolListOptions,
  142. AzureOperationResponse<IPage<ProxyModels.CloudPool>, ProxyModels.PoolListHeaders>>(response);
  143. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  144. // Setup the cmdlet to write pipeline output to a list that can be examined later
  145. List<PSCloudPool> pipeline = new List<PSCloudPool>();
  146. commandRuntimeMock.Setup(r =>
  147. r.WriteObject(It.IsAny<PSCloudPool>()))
  148. .Callback<object>(p => pipeline.Add((PSCloudPool)p));
  149. cmdlet.ExecuteCmdlet();
  150. // Verify that the cmdlet wrote the constructed pools to the pipeline
  151. Assert.Equal(3, pipeline.Count);
  152. int poolCount = 0;
  153. foreach (PSCloudPool p in pipeline)
  154. {
  155. Assert.True(idsOfConstructedPools.Contains(p.Id));
  156. poolCount++;
  157. }
  158. Assert.Equal(idsOfConstructedPools.Length, poolCount);
  159. }
  160. [Fact]
  161. [Trait(Category.AcceptanceType, Category.CheckIn)]
  162. public void ListPoolsMaxCountTest()
  163. {
  164. // Verify default max count
  165. Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);
  166. // Setup cmdlet to list pools without filters and a max count
  167. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  168. cmdlet.BatchContext = context;
  169. cmdlet.Id = null;
  170. cmdlet.Filter = null;
  171. int maxCount = 2;
  172. cmdlet.MaxCount = maxCount;
  173. string[] idsOfConstructedPools = new[] { "pool1", "pool2", "pool3" };
  174. // Build some CloudPools instead of querying the service on a List CloudPools call
  175. AzureOperationResponse<IPage<ProxyModels.CloudPool>, ProxyModels.PoolListHeaders> response = BatchTestHelpers.CreateCloudPoolListResponse(idsOfConstructedPools);
  176. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  177. ProxyModels.PoolListOptions,
  178. AzureOperationResponse<IPage<ProxyModels.CloudPool>, ProxyModels.PoolListHeaders>>(response);
  179. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  180. // Setup the cmdlet to write pipeline output to a list that can be examined later
  181. List<PSCloudPool> pipeline = new List<PSCloudPool>();
  182. commandRuntimeMock.Setup(r =>
  183. r.WriteObject(It.IsAny<PSCloudPool>()))
  184. .Callback<object>(p => pipeline.Add((PSCloudPool)p));
  185. cmdlet.ExecuteCmdlet();
  186. // Verify that the max count was respected
  187. Assert.Equal(maxCount, pipeline.Count);
  188. // Verify setting max count <= 0 doesn't return nothing
  189. cmdlet.MaxCount = -5;
  190. pipeline.Clear();
  191. cmdlet.ExecuteCmdlet();
  192. Assert.Equal(idsOfConstructedPools.Length, pipeline.Count);
  193. }
  194. }
  195. }