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

/src/ResourceManager/AzureBatch/Commands.Batch.Test/Files/GetBatchNodeFileCommandTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 453 lines | 320 code | 77 blank | 56 comment | 0 complexity | fe43f4e27bfa66aa8deee63e7c91dc69 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 Xunit;
  25. using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
  26. using ProxyModels = Microsoft.Azure.Batch.Protocol.Models;
  27. namespace Microsoft.Azure.Commands.Batch.Test.Files
  28. {
  29. public class GetBatchNodeFileCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
  30. {
  31. private GetBatchNodeFileCommand cmdlet;
  32. private Mock<BatchClient> batchClientMock;
  33. private Mock<ICommandRuntime> commandRuntimeMock;
  34. public GetBatchNodeFileCommandTests(Xunit.Abstractions.ITestOutputHelper output)
  35. {
  36. ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
  37. batchClientMock = new Mock<BatchClient>();
  38. commandRuntimeMock = new Mock<ICommandRuntime>();
  39. cmdlet = new GetBatchNodeFileCommand()
  40. {
  41. CommandRuntime = commandRuntimeMock.Object,
  42. BatchClient = batchClientMock.Object,
  43. };
  44. }
  45. [Fact]
  46. [Trait(Category.AcceptanceType, Category.CheckIn)]
  47. public void GetBatchNodeFileByTaskParametersTest()
  48. {
  49. // Setup cmdlet without required parameters
  50. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  51. cmdlet.BatchContext = context;
  52. cmdlet.JobId = null;
  53. cmdlet.TaskId = null;
  54. cmdlet.Name = null;
  55. cmdlet.Task = null;
  56. cmdlet.Filter = null;
  57. // Build a NodeFile instead of querying the service on a List NodeFile call
  58. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromTaskHeaders> response = BatchTestHelpers.CreateNodeFileListByTaskResponse(new string[] { });
  59. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  60. bool?,
  61. ProxyModels.FileListFromTaskOptions,
  62. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromTaskHeaders>>(response);
  63. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  64. Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
  65. cmdlet.JobId = "job-1";
  66. cmdlet.TaskId = "task";
  67. // Verify no exceptions occur
  68. cmdlet.ExecuteCmdlet();
  69. cmdlet.JobId = null;
  70. cmdlet.TaskId = null;
  71. cmdlet.Task = new PSCloudTask("task", "cmd /c dir /s");
  72. // Verify that we don't get an argument exception. We should get an InvalidOperationException though since the task is unbound
  73. Assert.Throws<InvalidOperationException>(() => cmdlet.ExecuteCmdlet());
  74. }
  75. [Fact]
  76. [Trait(Category.AcceptanceType, Category.CheckIn)]
  77. public void GetBatchNodeFileByTaskTest()
  78. {
  79. // Setup cmdlet to get a Task file by name
  80. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  81. cmdlet.BatchContext = context;
  82. cmdlet.JobId = "job-1";
  83. cmdlet.TaskId = "task";
  84. cmdlet.Name = "stdout.txt";
  85. cmdlet.Filter = null;
  86. // Build a NodeFile instead of querying the service on a Get NodeFile Properties call
  87. AzureOperationHeaderResponse<ProxyModels.FileGetNodeFilePropertiesFromTaskHeaders> response = BatchTestHelpers.CreateNodeFileGetPropertiesByTaskResponse();
  88. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  89. ProxyModels.FileGetNodeFilePropertiesFromTaskOptions,
  90. AzureOperationHeaderResponse<ProxyModels.FileGetNodeFilePropertiesFromTaskHeaders>>(response);
  91. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  92. // Setup the cmdlet to write pipeline output to a list that can be examined later
  93. List<PSNodeFile> pipeline = new List<PSNodeFile>();
  94. commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSNodeFile>())).Callback<object>(f => pipeline.Add((PSNodeFile)f));
  95. cmdlet.ExecuteCmdlet();
  96. // Verify that the cmdlet wrote the node file returned from the OM to the pipeline
  97. Assert.Equal(1, pipeline.Count);
  98. Assert.Equal(cmdlet.Name, pipeline[0].Name);
  99. }
  100. [Fact]
  101. [Trait(Category.AcceptanceType, Category.CheckIn)]
  102. public void ListBatchNodeFilesByTaskByODataFilterTest()
  103. {
  104. // Setup cmdlet to list node files using an OData filter.
  105. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  106. cmdlet.BatchContext = context;
  107. cmdlet.JobId = "job-1";
  108. cmdlet.TaskId = "task";
  109. cmdlet.Name = null;
  110. cmdlet.Filter = "startswith(name,'std')";
  111. string[] namesOfConstructedNodeFiles = new[] { "stdout.txt", "stderr.txt" };
  112. // Build some NodeFiles instead of querying the service on a List NodeFiles call
  113. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromTaskHeaders> response =
  114. BatchTestHelpers.CreateNodeFileListByTaskResponse(namesOfConstructedNodeFiles);
  115. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  116. bool?,
  117. ProxyModels.FileListFromTaskOptions,
  118. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromTaskHeaders>>(response);
  119. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  120. // Setup the cmdlet to write pipeline output to a list that can be examined later
  121. List<PSNodeFile> pipeline = new List<PSNodeFile>();
  122. commandRuntimeMock.Setup(r =>
  123. r.WriteObject(It.IsAny<PSNodeFile>()))
  124. .Callback<object>(f => pipeline.Add((PSNodeFile)f));
  125. cmdlet.ExecuteCmdlet();
  126. // Verify that the cmdlet wrote the constructed node files to the pipeline
  127. Assert.Equal(2, pipeline.Count);
  128. int taskCount = 0;
  129. foreach (PSNodeFile f in pipeline)
  130. {
  131. Assert.True(namesOfConstructedNodeFiles.Contains(f.Name));
  132. taskCount++;
  133. }
  134. Assert.Equal(namesOfConstructedNodeFiles.Length, taskCount);
  135. }
  136. [Fact]
  137. [Trait(Category.AcceptanceType, Category.CheckIn)]
  138. public void ListBatchNodeFilesByTaskWithoutFiltersTest()
  139. {
  140. // Setup cmdlet to list Task files without filters.
  141. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  142. cmdlet.BatchContext = context;
  143. cmdlet.JobId = "job-1";
  144. cmdlet.TaskId = "task";
  145. cmdlet.Name = null;
  146. cmdlet.Filter = null;
  147. string[] namesOfConstructedNodeFiles = new[] { "stdout.txt", "stderr.txt", "wd" };
  148. // Build some NodeFiles instead of querying the service on a List NodeFiles call
  149. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromTaskHeaders> response =
  150. BatchTestHelpers.CreateNodeFileListByTaskResponse(namesOfConstructedNodeFiles);
  151. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  152. bool?,
  153. ProxyModels.FileListFromTaskOptions,
  154. AzureOperationResponse<IPage<ProxyModels.NodeFile>,
  155. ProxyModels.FileListFromTaskHeaders>>(response);
  156. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  157. // Setup the cmdlet to write pipeline output to a list that can be examined later
  158. List<PSNodeFile> pipeline = new List<PSNodeFile>();
  159. commandRuntimeMock.Setup(r =>
  160. r.WriteObject(It.IsAny<PSNodeFile>()))
  161. .Callback<object>(f => pipeline.Add((PSNodeFile)f));
  162. cmdlet.ExecuteCmdlet();
  163. // Verify that the cmdlet wrote the constructed node files to the pipeline
  164. Assert.Equal(3, pipeline.Count);
  165. int taskCount = 0;
  166. foreach (PSNodeFile f in pipeline)
  167. {
  168. Assert.True(namesOfConstructedNodeFiles.Contains(f.Name));
  169. taskCount++;
  170. }
  171. Assert.Equal(namesOfConstructedNodeFiles.Length, taskCount);
  172. }
  173. [Fact]
  174. [Trait(Category.AcceptanceType, Category.CheckIn)]
  175. public void ListNodeFilesByTaskMaxCountTest()
  176. {
  177. // Verify default max count
  178. Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);
  179. // Setup cmdlet to list node files and a max count.
  180. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  181. cmdlet.BatchContext = context;
  182. cmdlet.JobId = "job-1";
  183. cmdlet.TaskId = "task";
  184. cmdlet.Name = null;
  185. cmdlet.Filter = null;
  186. int maxCount = 2;
  187. cmdlet.MaxCount = maxCount;
  188. string[] namesOfConstructedNodeFiles = new[] { "stdout.txt", "stderr.txt", "wd" };
  189. // Build some NodeFiles instead of querying the service on a List NodeFiles call
  190. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromTaskHeaders> response =
  191. BatchTestHelpers.CreateNodeFileListByTaskResponse(namesOfConstructedNodeFiles);
  192. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  193. bool?,
  194. ProxyModels.FileListFromTaskOptions,
  195. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromTaskHeaders>>(response);
  196. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  197. // Setup the cmdlet to write pipeline output to a list that can be examined later
  198. List<PSNodeFile> pipeline = new List<PSNodeFile>();
  199. commandRuntimeMock.Setup(r =>
  200. r.WriteObject(It.IsAny<PSNodeFile>()))
  201. .Callback<object>(f => pipeline.Add((PSNodeFile)f));
  202. cmdlet.ExecuteCmdlet();
  203. // Verify that the max count was respected
  204. Assert.Equal(maxCount, pipeline.Count);
  205. // Verify setting max count <= 0 doesn't return nothing
  206. cmdlet.MaxCount = -5;
  207. pipeline.Clear();
  208. cmdlet.ExecuteCmdlet();
  209. Assert.Equal(namesOfConstructedNodeFiles.Length, pipeline.Count);
  210. }
  211. [Fact]
  212. [Trait(Category.AcceptanceType, Category.CheckIn)]
  213. public void GetBatchNodeFileByComputeNodeParametersTest()
  214. {
  215. // Setup cmdlet without required parameters
  216. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  217. cmdlet.BatchContext = context;
  218. cmdlet.PoolId = null;
  219. cmdlet.ComputeNodeId = null;
  220. cmdlet.Name = null;
  221. cmdlet.ComputeNode = null;
  222. cmdlet.Filter = null;
  223. // Build a NodeFile instead of querying the service on a List NodeFile call
  224. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromComputeNodeHeaders> response =
  225. BatchTestHelpers.CreateNodeFileListByComputeNodeResponse(new string[] { });
  226. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  227. bool?,
  228. ProxyModels.FileListFromComputeNodeOptions,
  229. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromComputeNodeHeaders>>(response);
  230. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  231. Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
  232. cmdlet.PoolId = "pool";
  233. cmdlet.ComputeNodeId = "computeNode1";
  234. // Verify no exceptions occur
  235. cmdlet.ExecuteCmdlet();
  236. }
  237. [Fact]
  238. [Trait(Category.AcceptanceType, Category.CheckIn)]
  239. public void GetBatchNodeFileByComputeNodeTest()
  240. {
  241. // Setup cmdlet to get a Task file by name
  242. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  243. cmdlet.BatchContext = context;
  244. cmdlet.PoolId = "pool";
  245. cmdlet.ComputeNodeId = "vm1";
  246. cmdlet.Name = "startup\\stdout.txt";
  247. cmdlet.Filter = null;
  248. // Build a NodeFile instead of querying the service on a Get NodeFile Properties call
  249. AzureOperationHeaderResponse<ProxyModels.FileGetNodeFilePropertiesFromComputeNodeHeaders> response = BatchTestHelpers.CreateNodeFileGetPropertiesByComputeNodeResponse();
  250. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  251. ProxyModels.FileGetNodeFilePropertiesFromComputeNodeOptions,
  252. AzureOperationHeaderResponse<ProxyModels.FileGetNodeFilePropertiesFromComputeNodeHeaders>>(response);
  253. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  254. // Setup the cmdlet to write pipeline output to a list that can be examined later
  255. List<PSNodeFile> pipeline = new List<PSNodeFile>();
  256. commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSNodeFile>())).Callback<object>(f => pipeline.Add((PSNodeFile)f));
  257. cmdlet.ExecuteCmdlet();
  258. // Verify that the cmdlet wrote the node file returned from the OM to the pipeline
  259. Assert.Equal(1, pipeline.Count);
  260. Assert.Equal(cmdlet.Name, pipeline[0].Name);
  261. }
  262. [Fact]
  263. [Trait(Category.AcceptanceType, Category.CheckIn)]
  264. public void ListBatchNodeFilesByComputeNodeByODataFilterTest()
  265. {
  266. // Setup cmdlet to list vm files using an OData filter.
  267. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  268. cmdlet.BatchContext = context;
  269. cmdlet.PoolId = "pool";
  270. cmdlet.ComputeNodeId = "computeNode1";
  271. cmdlet.Name = null;
  272. cmdlet.Filter = "startswith(name,'startup')";
  273. string[] namesOfConstructedNodeFiles = new[] { "startup\\stdout.txt", "startup\\stderr.txt" };
  274. // Build some NodeFiles instead of querying the service on a List NodeFiles call
  275. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromComputeNodeHeaders> response =
  276. BatchTestHelpers.CreateNodeFileListByComputeNodeResponse(namesOfConstructedNodeFiles);
  277. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  278. bool?,
  279. ProxyModels.FileListFromComputeNodeOptions,
  280. AzureOperationResponse<IPage<ProxyModels.NodeFile>,
  281. ProxyModels.FileListFromComputeNodeHeaders>>(response);
  282. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  283. // Setup the cmdlet to write pipeline output to a list that can be examined later
  284. List<PSNodeFile> pipeline = new List<PSNodeFile>();
  285. commandRuntimeMock.Setup(r =>
  286. r.WriteObject(It.IsAny<PSNodeFile>()))
  287. .Callback<object>(f => pipeline.Add((PSNodeFile)f));
  288. cmdlet.ExecuteCmdlet();
  289. // Verify that the cmdlet wrote the constructed node files to the pipeline
  290. Assert.Equal(2, pipeline.Count);
  291. int taskCount = 0;
  292. foreach (PSNodeFile f in pipeline)
  293. {
  294. Assert.True(namesOfConstructedNodeFiles.Contains(f.Name));
  295. taskCount++;
  296. }
  297. Assert.Equal(namesOfConstructedNodeFiles.Length, taskCount);
  298. }
  299. [Fact]
  300. [Trait(Category.AcceptanceType, Category.CheckIn)]
  301. public void ListBatchNodeFilesByComputeNodeWithoutFiltersTest()
  302. {
  303. // Setup cmdlet to list vm files without filters.
  304. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  305. cmdlet.BatchContext = context;
  306. cmdlet.PoolId = "pool";
  307. cmdlet.ComputeNodeId = "computeNode1";
  308. cmdlet.Name = null;
  309. cmdlet.Filter = null;
  310. string[] namesOfConstructedNodeFiles = new[] { "startup", "workitems", "shared" };
  311. // Build some NodeFiles instead of querying the service on a List NodeFiles call
  312. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromComputeNodeHeaders> response =
  313. BatchTestHelpers.CreateNodeFileListByComputeNodeResponse(namesOfConstructedNodeFiles);
  314. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  315. bool?,
  316. ProxyModels.FileListFromComputeNodeOptions,
  317. AzureOperationResponse<IPage<ProxyModels.NodeFile>,
  318. ProxyModels.FileListFromComputeNodeHeaders>>(response);
  319. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  320. // Setup the cmdlet to write pipeline output to a list that can be examined later
  321. List<PSNodeFile> pipeline = new List<PSNodeFile>();
  322. commandRuntimeMock.Setup(r =>
  323. r.WriteObject(It.IsAny<PSNodeFile>()))
  324. .Callback<object>(f => pipeline.Add((PSNodeFile)f));
  325. cmdlet.ExecuteCmdlet();
  326. // Verify that the cmdlet wrote the constructed node files to the pipeline
  327. Assert.Equal(3, pipeline.Count);
  328. int taskCount = 0;
  329. foreach (PSNodeFile f in pipeline)
  330. {
  331. Assert.True(namesOfConstructedNodeFiles.Contains(f.Name));
  332. taskCount++;
  333. }
  334. Assert.Equal(namesOfConstructedNodeFiles.Length, taskCount);
  335. }
  336. [Fact]
  337. [Trait(Category.AcceptanceType, Category.CheckIn)]
  338. public void ListNodeFilesByComputeNodeMaxCountTest()
  339. {
  340. // Verify default max count
  341. Assert.Equal(Microsoft.Azure.Commands.Batch.Utils.Constants.DefaultMaxCount, cmdlet.MaxCount);
  342. // Setup cmdlet to list vm files and a max count.
  343. BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
  344. cmdlet.BatchContext = context;
  345. cmdlet.PoolId = "pool";
  346. cmdlet.ComputeNodeId = "computeNode1";
  347. cmdlet.Name = null;
  348. cmdlet.Filter = null;
  349. int maxCount = 2;
  350. cmdlet.MaxCount = maxCount;
  351. string[] namesOfConstructedNodeFiles = new[] { "startup", "workitems", "shared" };
  352. // Build some NodeFiles instead of querying the service on a List NodeFiles call
  353. AzureOperationResponse<IPage<ProxyModels.NodeFile>, ProxyModels.FileListFromComputeNodeHeaders> response =
  354. BatchTestHelpers.CreateNodeFileListByComputeNodeResponse(namesOfConstructedNodeFiles);
  355. RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
  356. bool?,
  357. ProxyModels.FileListFromComputeNodeOptions,
  358. AzureOperationResponse<IPage<ProxyModels.NodeFile>,
  359. ProxyModels.FileListFromComputeNodeHeaders>>(response);
  360. cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
  361. // Setup the cmdlet to write pipeline output to a list that can be examined later
  362. List<PSNodeFile> pipeline = new List<PSNodeFile>();
  363. commandRuntimeMock.Setup(r =>
  364. r.WriteObject(It.IsAny<PSNodeFile>()))
  365. .Callback<object>(f => pipeline.Add((PSNodeFile)f));
  366. cmdlet.ExecuteCmdlet();
  367. // Verify that the max count was respected
  368. Assert.Equal(maxCount, pipeline.Count);
  369. // Verify setting max count <= 0 doesn't return nothing
  370. cmdlet.MaxCount = -5;
  371. pipeline.Clear();
  372. cmdlet.ExecuteCmdlet();
  373. Assert.Equal(namesOfConstructedNodeFiles.Length, pipeline.Count);
  374. }
  375. }
  376. }