Skip to content

Batch update cmdlets #960

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 6 commits into from
Sep 23, 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
126 changes: 111 additions & 15 deletions src/ResourceManager/AzureBatch/Commands.Batch.Test/BatchTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,16 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.Batch;
using Microsoft.Azure.Batch.Common;
using Microsoft.Azure.Batch.Protocol;
using Microsoft.Azure.Commands.Batch.Models;
using Microsoft.Azure.Commands.Batch.Test.ScenarioTests;
using Microsoft.Azure.Management.Batch;
using Microsoft.Azure.Management.Batch.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
using Xunit;
using ProxyModels = Microsoft.Azure.Batch.Protocol.Models;

Expand Down Expand Up @@ -114,16 +104,22 @@ public static void AssertBatchAccountContextsAreEqual(BatchAccountContext contex
/// Creates a RequestInterceptor that does not contact the Batch Service but instead uses the supplied response body.
/// </summary>
/// <param name="responseToUse">The response the interceptor should return. If none is specified, then a new instance of the response type is instantiated.</param>
/// <param name="requestAction">An action to perform on the request.</param>
/// <typeparam name="TParameters">The type of the request parameters.</typeparam>
/// <typeparam name="TResponse">The type of the expected response.</typeparam>
public static RequestInterceptor CreateNoOpInterceptor<TParameters, TResponse>(TResponse responseToUse = null)
public static RequestInterceptor CreateFakeServiceResponseInterceptor<TParameters, TResponse>(TResponse responseToUse = null,
Action<BatchRequest<TParameters, TResponse>> requestAction = null)
where TParameters : ProxyModels.BatchParameters
where TResponse : ProxyModels.BatchOperationResponse, new()
{
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
{
BatchRequest<TParameters, TResponse> request =
(BatchRequest<TParameters, TResponse>)baseRequest;
BatchRequest<TParameters, TResponse> request = (BatchRequest<TParameters, TResponse>)baseRequest;

if (requestAction != null)
{
requestAction(request);
}

request.ServiceRequestFunc = (cancellationToken) =>
{
Expand All @@ -140,12 +136,12 @@ public static RequestInterceptor CreateNoOpInterceptor<TParameters, TResponse>(T
/// The interceptor must handle both request types since it's possible for one OM node file method to perform both REST APIs.
/// </summary>
/// <param name="fileName">The name of the file to put in the response body.</param>
public static RequestInterceptor CreateNoOpGetFileAndPropertiesInterceptor(string fileName)
public static RequestInterceptor CreateFakGetFileAndPropertiesResponseInterceptor(string fileName)
{
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
{
BatchRequest<ProxyModels.NodeFileGetParameters, ProxyModels.NodeFileGetResponse> fileRequest = baseRequest as
BatchRequest<ProxyModels.NodeFileGetParameters, ProxyModels.NodeFileGetResponse>;
BatchRequest<ProxyModels.NodeFileGetParameters, ProxyModels.NodeFileGetResponse>;

if (fileRequest != null)
{
Expand Down Expand Up @@ -401,6 +397,106 @@ public static ProxyModels.NodeFileListResponse CreateNodeFileListResponse(IEnume
return response;
}

/// <summary>
/// Fabricates a CloudJob that's in the bound state
/// </summary>
public static CloudJob CreateFakeBoundJob(BatchAccountContext context)
{
string jobId = "testJob";

RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
{
BatchRequest<ProxyModels.CloudJobGetParameters, ProxyModels.CloudJobGetResponse> request =
(BatchRequest<ProxyModels.CloudJobGetParameters, ProxyModels.CloudJobGetResponse>)baseRequest;

request.ServiceRequestFunc = (cancellationToken) =>
{
ProxyModels.CloudJobGetResponse response = new ProxyModels.CloudJobGetResponse();
response.Job = new ProxyModels.CloudJob(jobId, new ProxyModels.PoolInformation());

Task<ProxyModels.CloudJobGetResponse> task = Task.FromResult(response);
return task;
};
});

return context.BatchOMClient.JobOperations.GetJob(jobId, additionalBehaviors: new BatchClientBehavior[] { interceptor });
}

/// <summary>
/// Fabricates a CloudJobSchedule that's in the bound state
/// </summary>
public static CloudJobSchedule CreateFakeBoundJobSchedule(BatchAccountContext context)
{
string jobScheduleId = "testJobSchedule";

RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
{
BatchRequest<ProxyModels.CloudJobScheduleGetParameters, ProxyModels.CloudJobScheduleGetResponse> request =
(BatchRequest<ProxyModels.CloudJobScheduleGetParameters, ProxyModels.CloudJobScheduleGetResponse>)baseRequest;

request.ServiceRequestFunc = (cancellationToken) =>
{
ProxyModels.CloudJobScheduleGetResponse response = new ProxyModels.CloudJobScheduleGetResponse();
response.JobSchedule = new ProxyModels.CloudJobSchedule(jobScheduleId, new ProxyModels.Schedule(), new ProxyModels.JobSpecification());

Task<ProxyModels.CloudJobScheduleGetResponse> task = Task.FromResult(response);
return task;
};
});

return context.BatchOMClient.JobScheduleOperations.GetJobSchedule(jobScheduleId, additionalBehaviors: new BatchClientBehavior[] { interceptor });
}

/// <summary>
/// Fabricates a CloudPool that's in the bound state
/// </summary>
public static CloudPool CreateFakeBoundPool(BatchAccountContext context)
{
string poolId = "testPool";

RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
{
BatchRequest<ProxyModels.CloudPoolGetParameters, ProxyModels.CloudPoolGetResponse> request =
(BatchRequest<ProxyModels.CloudPoolGetParameters, ProxyModels.CloudPoolGetResponse>)baseRequest;

request.ServiceRequestFunc = (cancellationToken) =>
{
ProxyModels.CloudPoolGetResponse response = new ProxyModels.CloudPoolGetResponse();
response.Pool = new ProxyModels.CloudPool(poolId, "small", "4");

Task<ProxyModels.CloudPoolGetResponse> task = Task.FromResult(response);
return task;
};
});

return context.BatchOMClient.PoolOperations.GetPool(poolId, additionalBehaviors: new BatchClientBehavior[] { interceptor });
}

/// <summary>
/// Fabricates a CloudTask that's in the bound state
/// </summary>
public static CloudTask CreateFakeBoundTask(BatchAccountContext context)
{
string taskId = "testTask";

RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
{
BatchRequest<ProxyModels.CloudTaskGetParameters, ProxyModels.CloudTaskGetResponse> request =
(BatchRequest<ProxyModels.CloudTaskGetParameters, ProxyModels.CloudTaskGetResponse>)baseRequest;

request.ServiceRequestFunc = (cancellationToken) =>
{
ProxyModels.CloudTaskGetResponse response = new ProxyModels.CloudTaskGetResponse();
response.Task = new ProxyModels.CloudTask(taskId, "cmd /c dir /s");

Task<ProxyModels.CloudTaskGetResponse> task = Task.FromResult(response);
return task;
};
});

return context.BatchOMClient.JobOperations.GetTask("jobId", taskId, additionalBehaviors: new BatchClientBehavior[] { interceptor });
}

/// <summary>
/// Uses Reflection to set a property value on an object. Can be used to bypass restricted set accessors.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,28 @@
<Compile Include="BatchTestHelpers.cs" />
<Compile Include="ComputeNodes\ResetBatchComputeNodeCommandTests.cs" />
<Compile Include="ComputeNodes\RestartBatchComputeNodeCommandTests.cs" />
<Compile Include="ComputeNodeUsers\SetBatchComputeNoderUserCommandTests.cs" />
<Compile Include="Files\GetBatchRemoteDesktopProtocolFileCommandTests.cs" />
<Compile Include="Files\GetBatchNodeFileCommandTests.cs" />
<Compile Include="Files\GetBatchNodeFileContentCommandTests.cs" />
<Compile Include="JobSchedules\DisableBatchJobScheduleCommandTests.cs" />
<Compile Include="JobSchedules\EnableBatchJobScheduleCommandTests.cs" />
<Compile Include="JobSchedules\SetBatchJobScheduleCommandTests.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\SetBatchJobCommandTests.cs" />
<Compile Include="Jobs\StopBatchJobCommandTests.cs" />
<Compile Include="Models\BatchAccountContextTest.cs" />
<Compile Include="Pools\DisableBatchAutoScaleCommandTests.cs" />
<Compile Include="Pools\EnableBatchAutoScaleCommandTests.cs" />
<Compile Include="Pools\GetBatchPoolCommandTests.cs" />
<Compile Include="Pools\NewBatchPoolCommandTests.cs" />
<Compile Include="Pools\RemoveBatchPoolCommandTests.cs" />
<Compile Include="Pools\SetBatchPoolCommandTests.cs" />
<Compile Include="Pools\SetBatchPoolOSVersionCommandTests.cs" />
<Compile Include="Pools\StartBatchPoolResizeCommandTests.cs" />
<Compile Include="Pools\StopBatchPoolResizeCommandTests.cs" />
Expand All @@ -220,6 +224,7 @@
<Compile Include="JobSchedules\GetBatchJobScheduleCommandTests.cs" />
<Compile Include="JobSchedules\NewBatchJobScheduleCommandTests.cs" />
<Compile Include="JobSchedules\RemoveBatchJobScheduleCommandTests.cs" />
<Compile Include="Tasks\SetBatchTaskCommandTests.cs" />
<Compile Include="Tasks\StopBatchTaskCommandTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -311,6 +316,9 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeUserTests\TestDeleteComputeNodeUser.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeUserTests\TestUpdateComputeNodeUser.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.FileTests\TestGetNodeFileByComputeNodeByName.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -395,6 +403,9 @@
<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.JobScheduleTests\TestUpdateJobSchedule.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 @@ -428,6 +439,9 @@
<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.JobTests\TestUpdateJob.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 @@ -485,6 +499,9 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestStopResizePoolByPipeline.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.PoolTests\TestUpdatePool.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestCreateTask.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -512,6 +529,9 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestTerminateTask.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestUpdateTask.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Common\Commands.ResourceManager.Common\Commands.ResourceManager.Common.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void NewBatchComputeNodeUserParametersTest()
cmdlet.Password = "Password1234";

// Don't go to the service on an Add ComputeNodeUser call
RequestInterceptor interceptor = BatchTestHelpers.CreateNoOpInterceptor<ComputeNodeAddUserParameters, ComputeNodeAddUserResponse>();
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeAddUserParameters, ComputeNodeAddUserResponse>();
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Verify no exceptions when required parameters are set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void RemoveBatchComputeNodeUserParametersTest()
cmdlet.Name = "testUser";

// Don't go to the service on a Delete ComputeNodeUser call
RequestInterceptor interceptor = BatchTestHelpers.CreateNoOpInterceptor<ComputeNodeDeleteUserParameters, ComputeNodeDeleteUserResponse>();
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeDeleteUserParameters, ComputeNodeDeleteUserResponse>();
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Verify no exceptions when required parameters are set
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// ----------------------------------------------------------------------------------
//
// 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 System.Threading.Tasks;
using Microsoft.Azure.Batch;
using Microsoft.Azure.Batch.Protocol;
using Microsoft.Azure.Batch.Protocol.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using System.Collections.Generic;
using System.Management.Automation;
using Xunit;
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;

namespace Microsoft.Azure.Commands.Batch.Test.ComputeNodeUsers
{
public class SetBatchComputeNodeUserCommandTests
{
private SetBatchComputeNodeUserCommand cmdlet;
private Mock<BatchClient> batchClientMock;
private Mock<ICommandRuntime> commandRuntimeMock;

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

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SetBatchComputeNodeUserParametersTest()
{
// Setup cmdlet without the required parameters
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;

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

cmdlet.PoolId = "testPool";
cmdlet.ComputeNodeId = "computeNode1";

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

cmdlet.Name = "testUser";

// Don't go to the service on an Update ComputeNodeUser call
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<ComputeNodeUpdateUserParameters, ComputeNodeUpdateUserResponse>();
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Verify no exceptions when required parameters are set
cmdlet.ExecuteCmdlet();
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SetBatchComputeNodeUserRequestTest()
{
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;
cmdlet.PoolId = "testPool";
cmdlet.ComputeNodeId = "computeNode1";
cmdlet.Name = "testUser";
cmdlet.Password = "Password1234";
cmdlet.ExpiryTime = DateTime.Now.AddDays(1);

string requestPassword = null;
DateTime requestExpiryTime = DateTime.Now;

// Don't go to the service on an Update ComputeNodeUser call
Action<BatchRequest<ComputeNodeUpdateUserParameters, ComputeNodeUpdateUserResponse>> extractUserUpdateParametersAction =
(request) =>
{
requestPassword = request.TypedParameters.Password;
requestExpiryTime = request.TypedParameters.ExpiryTime.Value;
};
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor(requestAction: extractUserUpdateParametersAction);
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

cmdlet.ExecuteCmdlet();

// Verify the request parameters match expectations
Assert.Equal(cmdlet.Password, requestPassword);
Assert.Equal(cmdlet.ExpiryTime, requestExpiryTime);
}
}
}
Loading