PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ResourceManager/HDInsight/Commands.HDInsight.Test/UnitTests/JobTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 383 lines | 326 code | 37 blank | 20 comment | 55 complexity | c65fb90315fff9247d05a6ba91a5594c 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.Commands.HDInsight.Models;
  15. using Microsoft.Azure.Management.HDInsight.Job.Models;
  16. using Microsoft.WindowsAzure.Commands.Common;
  17. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  18. using Moq;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Management.Automation;
  22. using System.Net;
  23. using Xunit;
  24. namespace Microsoft.Azure.Commands.HDInsight.Test
  25. {
  26. public class JobTests : HDInsightTestBase
  27. {
  28. public JobTests(Xunit.Abstractions.ITestOutputHelper output)
  29. {
  30. ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
  31. base.SetupTestsForData();
  32. }
  33. [Fact]
  34. [Trait(Category.AcceptanceType, Category.CheckIn)]
  35. public void CreateHiveJob()
  36. {
  37. var args = new[] { "arg1", "arg2" };
  38. var defines = new Dictionary<string, string>
  39. {
  40. {"hive.1", "val1"},
  41. {"hive.2", "val2"}
  42. };
  43. const string query = "show tables;";
  44. const string name = "hivejob";
  45. const string file = "file";
  46. const string status = "folder";
  47. var files = new[] { "file1", "file2" };
  48. var cmdlet = new NewAzureHDInsightHiveJobDefinitionCommand
  49. {
  50. CommandRuntime = commandRuntimeMock.Object,
  51. HDInsightJobClient = hdinsightJobManagementMock.Object,
  52. Arguments = args,
  53. JobName = name,
  54. Query = query,
  55. File = file,
  56. Files = files,
  57. RunAsFileJob = false,
  58. StatusFolder = status
  59. };
  60. foreach (var define in defines)
  61. {
  62. cmdlet.Defines.Add(define.Key, define.Value);
  63. }
  64. cmdlet.ExecuteCmdlet();
  65. commandRuntimeMock.VerifyAll();
  66. commandRuntimeMock.Verify(
  67. f =>
  68. f.WriteObject(
  69. It.Is<AzureHDInsightHiveJobDefinition>(
  70. job =>
  71. job.Defines.Count == defines.Count && job.Query == query && job.JobName == name &&
  72. job.RunAsFileJob == false && job.File == file && job.Files.Count == files.Length &&
  73. job.StatusFolder == status)));
  74. }
  75. [Fact]
  76. [Trait(Category.AcceptanceType, Category.CheckIn)]
  77. public void CreatePigJob()
  78. {
  79. var args = new[] { "arg1", "arg2" };
  80. const string file = "file";
  81. const string status = "folder";
  82. const string query = "pigquery";
  83. var files = new[] { "file1", "file2" };
  84. var cmdlet = new NewAzureHDInsightPigJobDefinitionCommand
  85. {
  86. CommandRuntime = commandRuntimeMock.Object,
  87. HDInsightJobClient = hdinsightJobManagementMock.Object,
  88. Arguments = args,
  89. Query = query,
  90. File = file,
  91. Files = files,
  92. StatusFolder = status
  93. };
  94. cmdlet.ExecuteCmdlet();
  95. commandRuntimeMock.VerifyAll();
  96. commandRuntimeMock.Verify(
  97. f =>
  98. f.WriteObject(
  99. It.Is<AzureHDInsightPigJobDefinition>(
  100. job =>
  101. job.Query == query && job.Arguments.Count == args.Length &&
  102. job.Files.Count == files.Length && job.File == file && job.StatusFolder == status)));
  103. }
  104. [Fact]
  105. [Trait(Category.AcceptanceType, Category.CheckIn)]
  106. public void CreateMRJob()
  107. {
  108. var args = new[] { "arg1", "arg2" };
  109. var defines = new Dictionary<string, string>
  110. {
  111. {"hive.1", "val1"},
  112. {"hive.2", "val2"}
  113. };
  114. const string name = "mrjob";
  115. const string status = "folder";
  116. const string classname = "class";
  117. const string jar = "jar";
  118. var jars = new[] { "jar1" };
  119. var files = new[] { "file1", "file2" };
  120. var cmdlet = new NewAzureHDInsightMapReduceJobDefinitionCommand
  121. {
  122. CommandRuntime = commandRuntimeMock.Object,
  123. HDInsightJobClient = hdinsightJobManagementMock.Object,
  124. Arguments = args,
  125. Files = files,
  126. JobName = name,
  127. ClassName = classname,
  128. JarFile = jar,
  129. LibJars = jars,
  130. StatusFolder = status
  131. };
  132. foreach (var define in defines)
  133. {
  134. cmdlet.Defines.Add(define.Key, define.Value);
  135. }
  136. cmdlet.ExecuteCmdlet();
  137. commandRuntimeMock.VerifyAll();
  138. commandRuntimeMock.Verify(
  139. f =>
  140. f.WriteObject(
  141. It.Is<AzureHDInsightMapReduceJobDefinition>(
  142. job =>
  143. job.Arguments.Count == args.Length && job.Files.Count == files.Length &&
  144. job.JobName == name && job.ClassName == classname && job.JarFile == jar &&
  145. job.LibJars.Count == jars.Length && job.StatusFolder == status)));
  146. }
  147. [Fact]
  148. [Trait(Category.AcceptanceType, Category.CheckIn)]
  149. public void CreateStreamingJob()
  150. {
  151. var args = new[] { "arg1", "arg2" };
  152. var defines = new Dictionary<string, string>
  153. {
  154. {"hive.1", "val1"},
  155. {"hive.2", "val2"}
  156. };
  157. const string status = "folder";
  158. const string inputpath = "input";
  159. const string outputpath = "output";
  160. const string mapper = "mapper.exe";
  161. const string reducer = "reducer.exe";
  162. const string file = "file";
  163. var cmdlet = new NewAzureHDInsightStreamingMapReduceJobDefinitionCommand
  164. {
  165. CommandRuntime = commandRuntimeMock.Object,
  166. HDInsightJobClient = hdinsightJobManagementMock.Object,
  167. Arguments = args,
  168. File = file,
  169. StatusFolder = status,
  170. InputPath = inputpath,
  171. OutputPath = outputpath,
  172. Mapper = mapper,
  173. Reducer = reducer
  174. };
  175. foreach (var define in defines)
  176. {
  177. cmdlet.Defines.Add(define.Key, define.Value);
  178. }
  179. cmdlet.ExecuteCmdlet();
  180. commandRuntimeMock.VerifyAll();
  181. commandRuntimeMock.Verify(
  182. f =>
  183. f.WriteObject(
  184. It.Is<AzureHDInsightStreamingMapReduceJobDefinition>(
  185. job =>
  186. job.Arguments.Count == args.Length && job.File == file && job.StatusFolder == status &&
  187. job.Input == inputpath && job.Output == outputpath && job.Mapper == mapper &&
  188. job.Reducer == reducer && job.Defines.Count == defines.Count)));
  189. }
  190. [Fact]
  191. [Trait(Category.AcceptanceType, Category.CheckIn)]
  192. public void GetJobWithIdProvided()
  193. {
  194. // Update HDInsight Management properties for Job.
  195. SetupManagementClientForJobTests();
  196. var jobId = "jobid_1984120_001";
  197. var cmdlet = GetJobCommandDefinition();
  198. cmdlet.JobId = jobId;
  199. // Setup Job Management mocks
  200. var jobResponse = new JobGetResponse
  201. {
  202. JobDetail = new JobDetailRootJsonObject { Id = jobId, Status = new Status(), Userargs = new Userargs() }
  203. };
  204. hdinsightJobManagementMock.Setup(c => c.GetJob(It.IsAny<string>()))
  205. .Returns(jobResponse)
  206. .Verifiable();
  207. cmdlet.ExecuteCmdlet();
  208. commandRuntimeMock.VerifyAll();
  209. commandRuntimeMock.Verify(
  210. f =>
  211. f.WriteObject(It.Is<AzureHDInsightJob>(job => job.JobId.Equals(jobId))));
  212. }
  213. [Fact]
  214. [Trait(Category.AcceptanceType, Category.CheckIn)]
  215. public void ListJobs()
  216. {
  217. // Update HDInsight Management properties for Job.
  218. SetupManagementClientForJobTests();
  219. var cmdlet = GetJobCommandDefinition();
  220. // Setup Job Management mocks
  221. var jobListResponse = GetJobListResponse();
  222. hdinsightJobManagementMock.Setup(c => c.ListJobs())
  223. .Returns(jobListResponse)
  224. .Verifiable();
  225. cmdlet.ExecuteCmdlet();
  226. commandRuntimeMock.VerifyAll();
  227. commandRuntimeMock.Verify(
  228. f =>
  229. f.WriteObject(It.Is<IEnumerable<string>>(job => job.ElementAt(0).Equals(jobListResponse.ElementAt(0).Detail.Id) && job.ElementAt(1).Equals(jobListResponse.ElementAt(1).Detail.Id)), true));
  230. }
  231. [Fact]
  232. [Trait(Category.AcceptanceType, Category.CheckIn)]
  233. public void ListJobsAfterJobId()
  234. {
  235. // Update HDInsight Management properties for Job.
  236. SetupManagementClientForJobTests();
  237. var cmdlet = GetJobCommandDefinition();
  238. cmdlet.NumOfJobs = 2;
  239. // Setup Job Management mocks
  240. var jobListResponse = GetJobListResponse();
  241. hdinsightJobManagementMock.Setup(c => c.ListJobsAfterJobId(It.IsAny<string>(), It.IsAny<int>()))
  242. .Returns(jobListResponse)
  243. .Verifiable();
  244. cmdlet.ExecuteCmdlet();
  245. commandRuntimeMock.VerifyAll();
  246. commandRuntimeMock.Verify(
  247. f =>
  248. f.WriteObject(It.Is<IEnumerable<AzureHDInsightJob>>(job => job.ElementAt(0).JobId.Equals(jobListResponse.ElementAt(0).Detail.Id) && job.ElementAt(1).JobId.Equals(jobListResponse.ElementAt(1).Detail.Id)), true));
  249. }
  250. [Fact]
  251. [Trait(Category.AcceptanceType, Category.CheckIn)]
  252. public void StartJob()
  253. {
  254. // Update HDInsight Management properties for Job.
  255. SetupManagementClientForJobTests();
  256. var cmdlet = new StartAzureHDInsightJobCommand
  257. {
  258. CommandRuntime = commandRuntimeMock.Object,
  259. HDInsightJobClient = hdinsightJobManagementMock.Object,
  260. HDInsightManagementClient = hdinsightManagementMock.Object,
  261. HttpCredential = new PSCredential("httpuser", string.Format("Password1!").ConvertToSecureString()),
  262. ClusterName = ClusterName
  263. };
  264. var args = new[] { "arg1", "arg2" };
  265. const string query = "show tables;";
  266. const string name = "hivejob";
  267. var hivedef = new AzureHDInsightHiveJobDefinition
  268. {
  269. JobName = name,
  270. Query = query,
  271. };
  272. foreach (var arg in args)
  273. {
  274. hivedef.Arguments.Add(arg);
  275. }
  276. cmdlet.JobDefinition = hivedef;
  277. const string jobid = "jobid_1984120_001";
  278. var jobsub = new JobSubmissionResponse
  279. {
  280. JobSubmissionJsonResponse = new JobSubmissionJsonResponse
  281. {
  282. Id = jobid
  283. },
  284. StatusCode = HttpStatusCode.OK
  285. };
  286. hdinsightJobManagementMock.Setup(
  287. c =>
  288. c.SubmitHiveJob(
  289. It.Is<AzureHDInsightHiveJobDefinition>(
  290. def => def.JobName == name && def.Query == query && def.Arguments.Count == args.Length)))
  291. .Returns(jobsub)
  292. .Verifiable();
  293. var getresponse = new JobGetResponse
  294. {
  295. StatusCode = HttpStatusCode.OK,
  296. JobDetail = new JobDetailRootJsonObject
  297. {
  298. Completed = "false",
  299. User = cmdlet.HttpCredential.UserName,
  300. Id = jobid,
  301. Status = new Status(),
  302. Userargs = new Userargs()
  303. }
  304. };
  305. hdinsightJobManagementMock.Setup(c => c.GetJob(jobsub.JobSubmissionJsonResponse.Id)).Returns(getresponse).Verifiable();
  306. cmdlet.ExecuteCmdlet();
  307. commandRuntimeMock.VerifyAll();
  308. commandRuntimeMock.Verify(
  309. f =>
  310. f.WriteObject(
  311. It.Is<AzureHDInsightJob>(
  312. job => job.Cluster == ClusterName && job.JobId == jobid && job.Completed == "false")));
  313. }
  314. public JobListResponse GetJobListResponse()
  315. {
  316. var jobListobject1 = new JobListJsonObject
  317. {
  318. Detail = new JobDetailRootJsonObject { Id = "jobid_1984120_001", Status = new Status(), Userargs = new Userargs() },
  319. Id = "jobid_1984120_001"
  320. };
  321. var jobListobject2 = new JobListJsonObject
  322. {
  323. Detail = new JobDetailRootJsonObject { Id = "jobid_1984120_002", Status = new Status(), Userargs = new Userargs() },
  324. Id = "jobid_1984120_002"
  325. };
  326. var jobListResponse = new JobListResponse
  327. {
  328. JobList = new List<JobListJsonObject> { jobListobject1, jobListobject2 },
  329. StatusCode = HttpStatusCode.OK
  330. };
  331. return jobListResponse;
  332. }
  333. public GetAzureHDInsightJobCommand GetJobCommandDefinition()
  334. {
  335. var cmdlet = new GetAzureHDInsightJobCommand
  336. {
  337. CommandRuntime = commandRuntimeMock.Object,
  338. HDInsightJobClient = hdinsightJobManagementMock.Object,
  339. HDInsightManagementClient = hdinsightManagementMock.Object,
  340. HttpCredential = new PSCredential("httpuser", string.Format("Password1!").ConvertToSecureString()),
  341. ClusterName = ClusterName
  342. };
  343. return cmdlet;
  344. }
  345. }
  346. }