PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ResourceManager/AzureBatch/Commands.Batch.Test/Tasks/GetBatchTaskCommandTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 271 lines | 190 code | 48 blank | 33 comment | 0 complexity | 879b8e70a5ca73a519cbbea0c6f73752 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.Tasks
  29. {
  30. public class GetBatchTaskCommandTests
  31. {
  32. private GetBatchTaskCommand cmdlet;
  33. private Mock<BatchClient> batchClientMock;
  34. private Mock<ICommandRuntime> commandRuntimeMock;
  35. public GetBatchTaskCommandTests(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 GetBatchTaskCommand()
  41. {
  42. CommandRuntime = commandRuntimeMock.Object,
  43. BatchClient = batchClientMock.Object,
  44. };
  45. }
  46. [Fact]
  47. [Trait(Category.AcceptanceType, Category.CheckIn)]
  48. public void GetBatchTaskParametersTest()
  49. {
  50. // Setup cmdlet without required parameters
  51. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  52. cmdlet.BatchContext = context;
  53. cmdlet.JobId = null;
  54. cmdlet.Job = null;
  55. cmdlet.Filter = null;
  56. AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders> response = BatchTestHelpers.CreateGenericAzureOperationListResponse<ProxyModels.CloudTask, ProxyModels.TaskListHeaders>();
  57. // Build a CloudTask instead of querying the service on a List CloudTask call
  58. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  59. ProxyModels.TaskListOptions,
  60. AzureOperationResponse<IPage<ProxyModels.CloudTask>,
  61. ProxyModels.TaskListHeaders>>(response);
  62. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  63. Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());
  64. cmdlet.JobId = "job-1";
  65. // Verify no exceptions occur
  66. cmdlet.ExecuteCmdlet();
  67. }
  68. [Fact]
  69. [Trait(Category.AcceptanceType, Category.CheckIn)]
  70. public void GetBatchTaskTest()
  71. {
  72. // Setup cmdlet to get a task by id
  73. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  74. cmdlet.BatchContext = context;
  75. cmdlet.JobId = "job-1";
  76. cmdlet.Id = "task1";
  77. cmdlet.Filter = null;
  78. // Build a CloudTask instead of querying the service on a Get CloudTask call
  79. AzureOperationResponse<ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> response = BatchTestHelpers.CreateCloudTaskGetResponse(cmdlet.Id);
  80. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  81. ProxyModels.TaskGetOptions,
  82. AzureOperationResponse<ProxyModels.CloudTask, ProxyModels.TaskGetHeaders>>(response);
  83. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  84. // Setup the cmdlet to write pipeline output to a list that can be examined later
  85. List<PSCloudTask> pipeline = new List<PSCloudTask>();
  86. commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSCloudTask>())).Callback<object>(t => pipeline.Add((PSCloudTask)t));
  87. cmdlet.ExecuteCmdlet();
  88. // Verify that the cmdlet wrote the task returned from the OM to the pipeline
  89. Assert.Equal(1, pipeline.Count);
  90. Assert.Equal(cmdlet.Id, pipeline[0].Id);
  91. }
  92. [Fact]
  93. [Trait(Category.AcceptanceType, Category.CheckIn)]
  94. public void GetBatchTaskODataTest()
  95. {
  96. // Setup cmdlet to get a single task
  97. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  98. cmdlet.BatchContext = context;
  99. cmdlet.JobId = "testJob";
  100. cmdlet.Id = "testTask1";
  101. cmdlet.Select = "id,state";
  102. cmdlet.Expand = "stats";
  103. string requestSelect = null;
  104. string requestExpand = null;
  105. // Fetch the OData clauses off the request. The OData clauses are applied after user provided RequestInterceptors, so a ResponseInterceptor is used.
  106. AzureOperationResponse<ProxyModels.CloudTask, ProxyModels.TaskGetHeaders> getResponse = BatchTestHelpers.CreateCloudTaskGetResponse(cmdlet.Id);
  107. RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  108. ProxyModels.TaskGetOptions,
  109. AzureOperationResponse<ProxyModels.CloudTask, ProxyModels.TaskGetHeaders>>(getResponse);
  110. ResponseInterceptor responseInterceptor = new ResponseInterceptor((response, request) =>
  111. {
  112. ProxyModels.TaskGetOptions options = (ProxyModels.TaskGetOptions)request.Options;
  113. requestSelect = options.Select;
  114. requestExpand = options.Expand;
  115. return Task.FromResult(response);
  116. });
  117. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor, responseInterceptor };
  118. cmdlet.ExecuteCmdlet();
  119. Assert.Equal(cmdlet.Select, requestSelect);
  120. Assert.Equal(cmdlet.Expand, requestExpand);
  121. }
  122. [Fact]
  123. [Trait(Category.AcceptanceType, Category.CheckIn)]
  124. public void ListBatchTasksODataTest()
  125. {
  126. // Setup cmdlet to list tasks using an OData filter
  127. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  128. cmdlet.BatchContext = context;
  129. cmdlet.JobId = "testJob";
  130. cmdlet.Id = null;
  131. cmdlet.Filter = "startswith(id,'test')";
  132. cmdlet.Select = "id,state";
  133. cmdlet.Expand = "stats";
  134. string requestFilter = null;
  135. string requestSelect = null;
  136. string requestExpand = null;
  137. AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders> response = BatchTestHelpers.CreateGenericAzureOperationListResponse<ProxyModels.CloudTask, ProxyModels.TaskListHeaders>();
  138. Action<BatchRequest<ProxyModels.TaskListOptions, AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders>>> extractTaskListAction =
  139. (request) =>
  140. {
  141. ProxyModels.TaskListOptions options = request.Options;
  142. requestFilter = options.Filter;
  143. requestSelect = options.Select;
  144. requestExpand = options.Expand;
  145. };
  146. RequestInterceptor requestInterceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor(responseToUse: response, requestAction: extractTaskListAction);
  147. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor };
  148. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { requestInterceptor };
  149. cmdlet.ExecuteCmdlet();
  150. Assert.Equal(cmdlet.Filter, requestFilter);
  151. Assert.Equal(cmdlet.Select, requestSelect);
  152. Assert.Equal(cmdlet.Expand, requestExpand);
  153. }
  154. [Fact]
  155. [Trait(Category.AcceptanceType, Category.CheckIn)]
  156. public void ListBatchTasksWithoutFiltersTest()
  157. {
  158. // Setup cmdlet to list tasks without filters. Use WorkItemName and JobName.
  159. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  160. cmdlet.BatchContext = context;
  161. cmdlet.JobId = "job-1";
  162. cmdlet.Id = null;
  163. cmdlet.Filter = null;
  164. string[] idsOfConstructedTasks = new[] { "testTask1", "testTask2", "testTask3" };
  165. // Build some CloudTasks instead of querying the service on a List CloudTasks call
  166. AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders> response = BatchTestHelpers.CreateCloudTaskListResponse(idsOfConstructedTasks);
  167. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  168. ProxyModels.TaskListOptions,
  169. AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders>>(response);
  170. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  171. // Setup the cmdlet to write pipeline output to a list that can be examined later
  172. List<PSCloudTask> pipeline = new List<PSCloudTask>();
  173. commandRuntimeMock.Setup(r =>
  174. r.WriteObject(It.IsAny<PSCloudTask>()))
  175. .Callback<object>(t => pipeline.Add((PSCloudTask)t));
  176. cmdlet.ExecuteCmdlet();
  177. // Verify that the cmdlet wrote the constructed tasks to the pipeline
  178. Assert.Equal(3, pipeline.Count);
  179. int taskCount = 0;
  180. foreach (PSCloudTask t in pipeline)
  181. {
  182. Assert.True(idsOfConstructedTasks.Contains(t.Id));
  183. taskCount++;
  184. }
  185. Assert.Equal(idsOfConstructedTasks.Length, taskCount);
  186. }
  187. [Fact]
  188. [Trait(Category.AcceptanceType, Category.CheckIn)]
  189. public void ListTasksMaxCountTest()
  190. {
  191. // Verify default max count
  192. Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);
  193. // Setup cmdlet to list tasks without filters and a max count
  194. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  195. cmdlet.BatchContext = context;
  196. cmdlet.JobId = "job-1";
  197. cmdlet.Id = null;
  198. cmdlet.Filter = null;
  199. int maxCount = 2;
  200. cmdlet.MaxCount = maxCount;
  201. string[] idsOfConstructedTasks = new[] { "testTask1", "testTask2", "testTask3" };
  202. // Build some CloudTasks instead of querying the service on a List CloudTasks call
  203. AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders> response = BatchTestHelpers.CreateCloudTaskListResponse(idsOfConstructedTasks);
  204. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  205. ProxyModels.TaskListOptions,
  206. AzureOperationResponse<IPage<ProxyModels.CloudTask>, ProxyModels.TaskListHeaders>>(response);
  207. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  208. // Setup the cmdlet to write pipeline output to a list that can be examined later
  209. List<PSCloudTask> pipeline = new List<PSCloudTask>();
  210. commandRuntimeMock.Setup(r =>
  211. r.WriteObject(It.IsAny<PSCloudTask>()))
  212. .Callback<object>(t => pipeline.Add((PSCloudTask)t));
  213. cmdlet.ExecuteCmdlet();
  214. // Verify that the max count was respected
  215. Assert.Equal(maxCount, pipeline.Count);
  216. // Verify setting max count <= 0 doesn't return nothing
  217. cmdlet.MaxCount = -5;
  218. pipeline.Clear();
  219. cmdlet.ExecuteCmdlet();
  220. Assert.Equal(idsOfConstructedTasks.Length, pipeline.Count);
  221. }
  222. }
  223. }