Skip to content

Batch cmdlets for terminating job schedules, jobs, and tasks #815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 2015.09.03 version 0.9.8
## 2015.09.03 version 0.9.8
* Azure Compute (ARM) Cmdlets
* Add -Launch parameter for Get-AzureRemoteDesktopFile cmdlet
* Azure Backup - added the following cmdlets
Expand All @@ -18,6 +18,14 @@
* Set-AzureRMBackupProtectionPolicy
* Get-AzureRMBackupRecoveryPoint
* Restore-AzureRMBackupItem
* Azure Batch - added the following cmdlets
* Enable-AzureBatchJob
* Disable-AzureBatchJob
* Enable-AzureBatchJobSchedule
* Disable-AzureBatchJobSchedule
* Stop-AzureBatchJob
* Stop-AzureBatchJobSchedule
* Stop-AzureBatchTask

## 2015.08.17 version 0.9.7
* Azure Profile cmdlets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,13 @@
<Compile Include="Files\GetBatchNodeFileContentCommandTests.cs" />
<Compile Include="JobSchedules\DisableBatchJobScheduleCommandTests.cs" />
<Compile Include="JobSchedules\EnableBatchJobScheduleCommandTests.cs" />
<Compile Include="JobSchedules\StopBatchJobScheduleCommandTests.cs" />
<Compile Include="Jobs\DisableBatchJobCommandTests.cs" />
<Compile Include="Jobs\EnableBatchJobCommandTests.cs" />
<Compile Include="Jobs\GetBatchJobCommandTests.cs" />
<Compile Include="Jobs\NewBatchJobCommandTests.cs" />
<Compile Include="Jobs\RemoveBatchJobCommandTests.cs" />
<Compile Include="Jobs\StopBatchJobCommandTests.cs" />
<Compile Include="Models\BatchAccountContextTest.cs" />
<Compile Include="Pools\DisableBatchAutoScaleCommandTests.cs" />
<Compile Include="Pools\EnableBatchAutoScaleCommandTests.cs" />
Expand Down Expand Up @@ -204,6 +206,7 @@
<Compile Include="JobSchedules\GetBatchJobScheduleCommandTests.cs" />
<Compile Include="JobSchedules\NewBatchJobScheduleCommandTests.cs" />
<Compile Include="JobSchedules\RemoveBatchJobScheduleCommandTests.cs" />
<Compile Include="Tasks\StopBatchTaskCommandTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="MSSharedLibKey.snk" />
Expand Down Expand Up @@ -372,6 +375,12 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobScheduleTests\TestNewJobSchedule.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobScheduleTests\TestTerminateJobScheduleById.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobScheduleTests\TestTerminateJobSchedulePipeline.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestDeleteJob.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -399,6 +408,12 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestNewJob.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestTerminateJobById.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.JobTests\TestTerminateJobPipeline.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestChangeOSVersionById.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -480,6 +495,9 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestListTasksWithMaxCount.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestTerminateTask.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Common\Commands.Common.Test\Commands.Common.Test.csproj">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using Microsoft.Azure.Batch;
using Microsoft.Azure.Batch.Protocol;
using Microsoft.Azure.Batch.Protocol.Models;
using Microsoft.Azure.Commands.Batch.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading.Tasks;
using Xunit;
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;

namespace Microsoft.Azure.Commands.Batch.Test.Pools
{
public class StopBatchJobScheduleCommandTests
{
private StopBatchJobScheduleCommand cmdlet;
private Mock<BatchClient> batchClientMock;
private Mock<ICommandRuntime> commandRuntimeMock;

public StopBatchJobScheduleCommandTests()
{
batchClientMock = new Mock<BatchClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new StopBatchJobScheduleCommand()
{
CommandRuntime = commandRuntimeMock.Object,
BatchClient = batchClientMock.Object,
};
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void StopJobScheduleParametersTest()
{
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;
cmdlet.Id = null;

Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());

cmdlet.Id = "testJobSchedule";

// Don't go to the service on a Terminate CloudJobSchedule call
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
{
BatchRequest<CloudJobScheduleTerminateParameters, CloudJobScheduleTerminateResponse> request =
(BatchRequest<CloudJobScheduleTerminateParameters, CloudJobScheduleTerminateResponse>)baseRequest;

request.ServiceRequestFunc = (cancellationToken) =>
{
CloudJobScheduleTerminateResponse response = new CloudJobScheduleTerminateResponse();
Task<CloudJobScheduleTerminateResponse> task = Task.FromResult(response);
return task;
};
});
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Verify no exceptions when required parameter is set
cmdlet.ExecuteCmdlet();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using Microsoft.Azure.Batch;
using Microsoft.Azure.Batch.Common;
using Microsoft.Azure.Batch.Protocol;
using Microsoft.Azure.Batch.Protocol.Models;
using Microsoft.Azure.Commands.Batch.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading.Tasks;
using Xunit;
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;

namespace Microsoft.Azure.Commands.Batch.Test.Pools
{
public class StopBatchJobCommandTests
{
private StopBatchJobCommand cmdlet;
private Mock<BatchClient> batchClientMock;
private Mock<ICommandRuntime> commandRuntimeMock;

public StopBatchJobCommandTests()
{
batchClientMock = new Mock<BatchClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new StopBatchJobCommand()
{
CommandRuntime = commandRuntimeMock.Object,
BatchClient = batchClientMock.Object,
};
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void StopJobParametersTest()
{
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;
cmdlet.Id = null;

Assert.Throws<ArgumentNullException>(() => cmdlet.ExecuteCmdlet());

cmdlet.Id = "testJob";

// Don't go to the service on a Terminate CloudJob call
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
{
BatchRequest<CloudJobTerminateParameters, CloudJobTerminateResponse> request =
(BatchRequest<CloudJobTerminateParameters, CloudJobTerminateResponse>)baseRequest;

request.ServiceRequestFunc = (cancellationToken) =>
{
CloudJobTerminateResponse response = new CloudJobTerminateResponse();
Task<CloudJobTerminateResponse> task = Task.FromResult(response);
return task;
};
});
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Verify no exceptions when required parameter is set
cmdlet.ExecuteCmdlet();
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void StopJobRequestTest()
{
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;

string terminateReason = "testing";
string requestTerminateReason = null;

cmdlet.Id = "testJob";
cmdlet.TerminateReason = terminateReason;

// Don't go to the service on a Terminate CloudJob call
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
{
BatchRequest<CloudJobTerminateParameters, CloudJobTerminateResponse> request =
(BatchRequest<CloudJobTerminateParameters, CloudJobTerminateResponse>)baseRequest;

requestTerminateReason = request.TypedParameters.TerminateReason;

request.ServiceRequestFunc = (cancellationToken) =>
{
CloudJobTerminateResponse response = new CloudJobTerminateResponse();
Task<CloudJobTerminateResponse> task = Task.FromResult(response);
return task;
};
});
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

cmdlet.ExecuteCmdlet();

// Verify that the job terminate reason was properly set on the outgoing request
Assert.Equal(terminateReason, requestTerminateReason);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,40 @@ public void TestDisableAndEnableJobSchedule()
TestUtilities.GetCallingClass(),
TestUtilities.GetCurrentMethodName());
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestTerminateJobScheduleById()
{
TestTerminateJobSchedule(false);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestTerminateJobSchedulePipeline()
{
TestTerminateJobSchedule(true);
}

private void TestTerminateJobSchedule(bool usePipeline)
{
BatchController controller = BatchController.NewInstance;
BatchAccountContext context = null;
string jobScheduleId = "testTerminateJobSchedule" + (usePipeline ? "Pipeline" : "Id");
controller.RunPsTestWorkflow(
() => { return new string[] { string.Format("Test-TerminateJobSchedule '{0}' '{1}' '{2}'", accountName, jobScheduleId, usePipeline ? 1 : 0) }; },
() =>
{
context = ScenarioTestHelpers.GetBatchAccountContextWithKeys(controller, accountName);
ScenarioTestHelpers.CreateTestJobSchedule(controller, context, jobScheduleId, null);
},
() =>
{
ScenarioTestHelpers.DeleteJobSchedule(controller, context, jobScheduleId);
},
TestUtilities.GetCallingClass(),
usePipeline ? "TestTerminateJobSchedulePipeline" : "TestTerminateJobScheduleById");
}
}

// Cmdlets that use the HTTP Recorder interceptor for use with scenario tests
Expand Down Expand Up @@ -258,4 +292,14 @@ public override void ExecuteCmdlet()
base.ExecuteCmdlet();
}
}

[Cmdlet(VerbsLifecycle.Stop, "AzureBatchJobSchedule_ST")]
public class StopBatchJobScheduleScenarioTestCommand : StopBatchJobScheduleCommand
{
public override void ExecuteCmdlet()
{
AdditionalBehaviors = new List<BatchClientBehavior>() { ScenarioTestHelpers.CreateHttpRecordingInterceptor() };
base.ExecuteCmdlet();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,28 @@ function Test-DisableAndEnableJobSchedule
$jobSchedule | Enable-AzureBatchJobSchedule_ST -BatchContext $context
$jobSchedule = Get-AzureBatchJobSchedule_ST -Filter "id eq '$jobScheduleId'" -BatchContext $context
Assert-AreEqual 'Active' $jobSchedule.State
}

<#
.SYNOPSIS
Tests terminating a job schedule
#>
function Test-TerminateJobSchedule
{
param([string]$accountName, [string]$jobScheduleId, [string]$usePipeline)

$context = Get-AzureBatchAccountKeys -Name $accountName

if ($usePipeline -eq '1')
{
Get-AzureBatchJobSchedule_ST -Id $jobScheduleId -BatchContext $context | Stop-AzureBatchJobSchedule_ST -BatchContext $context
}
else
{
Stop-AzureBatchJobSchedule_ST $jobScheduleId -BatchContext $context
}

# Verify the job schedule was terminated
$jobSchedule = Get-AzureBatchJobSchedule_ST $jobScheduleId -BatchContext $context
Assert-True { ($jobSchedule.State.ToString().ToLower() -eq 'terminating') -or ($jobSchedule.State.ToString().ToLower() -eq 'completed') }
}
Loading