Skip to content

Commit 2e32322

Browse files
authored
Merge pull request #2464 from pattipaka/dev
Add new options to Wait-AzureRmHDInsightJob
2 parents b9d401d + 06e0af3 commit 2e32322

File tree

10 files changed

+7676
-7485
lines changed

10 files changed

+7676
-7485
lines changed

AzurePowershell.Test.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<XUnitTests Include=".\src\ResourceManager\RedisCache\Commands.RedisCache.Test\bin\Debug\Microsoft.Azure.Commands.RedisCache.Test.dll"/>
102102
<!--<XUnitTests Include=".\src\ResourceManager\ServerManagement\Commands.ServerManagement.Test\bin\Debug\Microsoft.Azure.Commands.ServerManagement.Test.dll"/>
103103
<XUnitTests Include=".\src\ResourceManager\Storage\Commands.Management.Storage.Test\bin\Debug\Microsoft.Azure.Commands.Management.Storage.Test.dll"/>-->
104+
<XUnitTests Include=".\src\ResourceManager\HDInsight\Commands.HDInsight.Test\bin\Debug\Commands.HDInsight.Test.dll"/>
104105
<XUnitTests Include=".\src\Common\Commands.Common.Authentication.Test\bin\Debug\Microsoft.Azure.Commands.Common.Authentication.Test.dll"/>
105106
<XUnitTests Include="@(AsmXUnitTests)"/>
106107
</ItemGroup>

src/ResourceManager/HDInsight/Commands.HDInsight.Test/Commands.HDInsight.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</Reference>
5757
<Reference Include="Microsoft.Azure.Management.HDInsight.Job">
5858
<SpecificVersion>False</SpecificVersion>
59-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.HDInsight.Job.2.0.0-preview\lib\net40\Microsoft.Azure.Management.HDInsight.Job.dll</HintPath>
59+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.HDInsight.Job.2.0.3\lib\net40\Microsoft.Azure.Management.HDInsight.Job.dll</HintPath>
6060
</Reference>
6161
<Reference Include="Microsoft.Azure.Management.Storage">
6262
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Storage.2.4.0-preview\lib\net40\Microsoft.Azure.Management.Storage.dll</HintPath>

src/ResourceManager/HDInsight/Commands.HDInsight.Test/ScenarioTests/HDInsightScenarioTestsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected void SetupManagementClients()
4444
{
4545
var hdinsightManagementClient = GetHdInsightManagementClient();
4646

47-
helper.SetupManagementClients(hdinsightManagementClient);
47+
helper.SetupSomeOfManagementClients(hdinsightManagementClient);
4848
}
4949

5050
protected HDInsightManagementClient GetHdInsightManagementClient()

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

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
using System.Collections.Generic;
17+
using System.Globalization;
18+
using System.Management.Automation;
19+
using System.Net;
20+
using System.Linq;
21+
using Hyak.Common;
1522
using Microsoft.Azure.Commands.HDInsight.Models;
1623
using Microsoft.Azure.Management.HDInsight.Job.Models;
1724
using Microsoft.WindowsAzure.Commands.Common;
1825
using Microsoft.WindowsAzure.Commands.ScenarioTest;
1926
using Moq;
20-
using System.Collections.Generic;
21-
using System.Linq;
22-
using System.Management.Automation;
23-
using System.Net;
2427
using Xunit;
2528

2629
namespace Microsoft.Azure.Commands.HDInsight.Test
@@ -379,5 +382,114 @@ public GetAzureHDInsightJobCommand GetJobCommandDefinition()
379382

380383
return cmdlet;
381384
}
385+
386+
[Fact]
387+
[Trait(Category.AcceptanceType, Category.CheckIn)]
388+
public void WaitForJobCompletion_Sucess()
389+
{
390+
var cmdlet = GetWaitAzureHDInsightJobCommandDefinition(JobCompletionType.Success);
391+
cmdlet.ExecuteCmdlet();
392+
}
393+
394+
[Fact]
395+
[Trait(Category.AcceptanceType, Category.CheckIn)]
396+
public void WaitForJobCompletion_Fail()
397+
{
398+
var cmdlet = GetWaitAzureHDInsightJobCommandDefinition(JobCompletionType.Fail);
399+
400+
try
401+
{
402+
cmdlet.ExecuteCmdlet();
403+
throw new Exception("Operation didn't fail");
404+
}
405+
catch (Exception)
406+
{
407+
}
408+
}
409+
410+
[Fact]
411+
[Trait(Category.AcceptanceType, Category.CheckIn)]
412+
public void WaitForJobCompletion_TimeOut()
413+
{
414+
var expectedExceptionMessage = "HDInsight job with ID {0} has not completed in {1} seconds. Increase the value of -TimeoutInSeconds or check the job runtime.";
415+
WaitForJobCompletion_Internal(JobCompletionType.TimeOut, expectedExceptionMessage);
416+
}
417+
418+
public void WaitForJobCompletion_Internal(JobCompletionType completionType, string expectedMessageFormat)
419+
{
420+
var cmdlet = GetWaitAzureHDInsightJobCommandDefinition(completionType);
421+
422+
bool success = false;
423+
424+
try
425+
{
426+
cmdlet.ExecuteCmdlet();
427+
}
428+
catch (CloudException ex)
429+
{
430+
var expectedExceptionMessage = string.Format(CultureInfo.InvariantCulture, expectedMessageFormat, cmdlet.JobId, cmdlet.TimeoutInSeconds);
431+
if (ex.Message.Equals(expectedExceptionMessage))
432+
{
433+
success = true;
434+
}
435+
}
436+
437+
if (!success)
438+
{
439+
throw new Exception("Operation didn't fail");
440+
}
441+
}
442+
443+
public WaitAzureHDInsightJobCommand GetWaitAzureHDInsightJobCommandDefinition(JobCompletionType jobCompetionType)
444+
{
445+
// Update HDInsight Management properties for Job.
446+
SetupManagementClientForJobTests();
447+
448+
// Setup Job Management mocks
449+
const string jobId = "jobid_1984120_001";
450+
451+
var cmdlet = new WaitAzureHDInsightJobCommand
452+
{
453+
CommandRuntime = commandRuntimeMock.Object,
454+
HDInsightJobClient = hdinsightJobManagementMock.Object,
455+
HDInsightManagementClient = hdinsightManagementMock.Object,
456+
HttpCredential = new PSCredential("httpuser", string.Format("Password1!").ConvertToSecureString()),
457+
ClusterName = ClusterName,
458+
JobId = jobId,
459+
TimeoutInSeconds = 10
460+
};
461+
462+
JobGetResponse jobDetails = null;
463+
TimeSpan? jobDuration = TimeSpan.FromSeconds(10);
464+
hdinsightJobManagementMock.Setup(c => c.WaitForJobCompletion(It.IsAny<string>(), It.IsAny<TimeSpan?>(), null))
465+
.Callback((string id, TimeSpan? duration, TimeSpan? interval) => { jobDetails = MockJobCompletion(id, jobCompetionType); })
466+
.Returns(() => jobDetails);
467+
468+
return cmdlet;
469+
}
470+
471+
public enum JobCompletionType { Success, Fail, TimeOut };
472+
473+
public JobGetResponse MockJobCompletion(string jobId, JobCompletionType type)
474+
{
475+
var jobResponse = new JobGetResponse
476+
{
477+
JobDetail = new JobDetailRootJsonObject { Id = jobId, Status = new Status(), Userargs = new Userargs() }
478+
};
479+
480+
switch (type)
481+
{
482+
case JobCompletionType.Success:
483+
break;
484+
case JobCompletionType.Fail:
485+
throw new CloudException("Some Failure");
486+
case JobCompletionType.TimeOut:
487+
throw new TimeoutException("Some Failure");
488+
default:
489+
break;
490+
}
491+
492+
return jobResponse;
493+
}
382494
}
383495
}

src/ResourceManager/HDInsight/Commands.HDInsight.Test/packages.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
88
<package id="Microsoft.Azure.Management.Authorization" version="0.18.2-preview" targetFramework="net45" />
99
<package id="Microsoft.Azure.Management.HDInsight" version="1.0.14-preview" targetFramework="net45" />
10-
<package id="Microsoft.Azure.Management.HDInsight.Job" version="2.0.0-preview" targetFramework="net45" />
10+
<package id="Microsoft.Azure.Management.HDInsight.Job" version="2.0.3" targetFramework="net45" />
1111
<package id="Microsoft.Azure.Management.Resources" version="2.20.0-preview" targetFramework="net45" />
1212
<package id="Microsoft.Azure.Management.Storage" version="2.4.0-preview" targetFramework="net45" />
1313
<package id="Microsoft.Azure.Test.Framework" version="1.0.5945.28173-prerelease" targetFramework="net45" />
@@ -34,4 +34,4 @@
3434
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" />
3535
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net45" />
3636
<package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="net45" />
37-
</packages>
37+
</packages>

src/ResourceManager/HDInsight/Commands.HDInsight/Commands.HDInsight.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
</Reference>
129129
<Reference Include="Microsoft.Azure.Management.HDInsight.Job">
130130
<SpecificVersion>False</SpecificVersion>
131-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.HDInsight.Job.2.0.0-preview\lib\net40\Microsoft.Azure.Management.HDInsight.Job.dll</HintPath>
131+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.HDInsight.Job.2.0.3\lib\net40\Microsoft.Azure.Management.HDInsight.Job.dll</HintPath>
132132
</Reference>
133133
<Reference Include="Microsoft.Azure.ResourceManager, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
134134
<SpecificVersion>False</SpecificVersion>

src/ResourceManager/HDInsight/Commands.HDInsight/JobCommands/WaitAzureHDInsightJobCommand.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
using System.Globalization;
17+
using System.Management.Automation;
1518
using Hyak.Common;
1619
using Microsoft.Azure.Commands.HDInsight.Commands;
1720
using Microsoft.Azure.Commands.HDInsight.Models;
21+
using Microsoft.Azure.Management.HDInsight.Job.Models;
1822
using Microsoft.WindowsAzure.Commands.Common;
19-
using System.Management.Automation;
2023

2124
namespace Microsoft.Azure.Commands.HDInsight
2225
{
@@ -65,6 +68,12 @@ public PSCredential HttpCredential
6568
[Parameter(HelpMessage = "The name of the resource group.")]
6669
public string ResourceGroupName { get; set; }
6770

71+
[Parameter(HelpMessage = "The total time to wait for job completion, in seconds.")]
72+
public int TimeoutInSeconds { get; set; }
73+
74+
[Parameter(HelpMessage = "The time to wait between job status checks, in seconds.")]
75+
public int WaitIntervalInSeconds { get; set; }
76+
6877
#endregion
6978

7079
public override void ExecuteCmdlet()
@@ -75,17 +84,40 @@ public override void ExecuteCmdlet()
7584
}
7685

7786
var jobDetails = WaitJob();
87+
7888
WriteObject(jobDetails);
7989
}
8090

8191
public AzureHDInsightJob WaitJob()
8292
{
8393
_clusterName = GetClusterConnection(ResourceGroupName, ClusterName);
84-
var jobDetail = HDInsightJobClient.GetJob(JobId).JobDetail;
85-
while (!jobDetail.Status.JobComplete)
94+
95+
TimeSpan? duration = null;
96+
97+
if (TimeoutInSeconds > 0)
98+
{
99+
duration = TimeSpan.FromSeconds(TimeoutInSeconds);
100+
}
101+
102+
TimeSpan? waitInterval = null;
103+
104+
if (WaitIntervalInSeconds > 0)
86105
{
87-
jobDetail = HDInsightJobClient.GetJob(JobId).JobDetail;
106+
waitInterval = TimeSpan.FromSeconds(WaitIntervalInSeconds);
88107
}
108+
109+
JobDetailRootJsonObject jobDetail = null;
110+
111+
try
112+
{
113+
jobDetail = HDInsightJobClient.WaitForJobCompletion(JobId, duration, waitInterval).JobDetail;
114+
}
115+
catch (TimeoutException)
116+
{
117+
var exceptionMessage = string.Format(CultureInfo.InvariantCulture, "HDInsight job with ID {0} has not completed in {1} seconds. Increase the value of -TimeoutInSeconds or check the job runtime.", JobId, TimeoutInSeconds);
118+
throw new CloudException(exceptionMessage);
119+
}
120+
89121
var jobDetails = new AzureHDInsightJob(jobDetail, HDInsightJobClient.ClusterName);
90122
return jobDetails;
91123
}

0 commit comments

Comments
 (0)