Skip to content

Commit 293a28e

Browse files
committed
Merge pull request Azure#81 from jasper-schneider/vmuser
New/Remove Azure Batch VM User
2 parents 732f80f + be1c3af commit 293a28e

34 files changed

+2897
-145
lines changed

ChangeLog.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2015.04.28 version 0.8.17
2+
* Azure Batch
3+
* Added cmdlets
4+
* New-AzureBatchVMUser
5+
* Remove-AzureBatchVMUser
6+
17
2015.03.31 version 0.8.16
28
* Azure Data Factory:
39
* Fixes for clean install and subscription registration issues

src/ResourceManager/Batch/Commands.Batch.Test/Commands.Batch.Test.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,14 @@
170170
<Compile Include="ScenarioTests\PoolTests.cs" />
171171
<Compile Include="ScenarioTests\ScenarioTestHelpers.cs" />
172172
<Compile Include="ScenarioTests\TaskTests.cs" />
173+
<Compile Include="ScenarioTests\VMUserTests.cs" />
173174
<Compile Include="ScenarioTests\VMTests.cs" />
174175
<Compile Include="ScenarioTests\WorkItemTests.cs" />
175176
<Compile Include="Tasks\GetBatchTaskCommandTests.cs" />
176177
<Compile Include="Tasks\NewBatchTaskCommandTests.cs" />
177178
<Compile Include="Tasks\RemoveBatchTaskCommandTests.cs" />
179+
<Compile Include="VMUsers\NewBatchVMUserCommandTests.cs" />
180+
<Compile Include="VMUsers\RemoveBatchVMUserCommandTests.cs" />
178181
<Compile Include="VMs\GetBatchVMCommandTests.cs" />
179182
<Compile Include="WorkItems\GetBatchWorkItemCommandTests.cs" />
180183
<Compile Include="WorkItems\NewBatchWorkItemCommandTests.cs" />
@@ -203,6 +206,9 @@
203206
<None Include="ScenarioTests\TaskTests.ps1">
204207
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
205208
</None>
209+
<None Include="ScenarioTests\VMUserTests.ps1">
210+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
211+
</None>
206212
<None Include="ScenarioTests\VMTests.ps1">
207213
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
208214
</None>
@@ -344,6 +350,15 @@
344350
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestListTasksWithMaxCount.json">
345351
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
346352
</None>
353+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.VMUserTests\TestCreateUser.json">
354+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
355+
</None>
356+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.VMUserTests\TestCreateUserPipeline.json">
357+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
358+
</None>
359+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.VMUserTests\TestDeleteUser.json">
360+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
361+
</None>
347362
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.VMTests\TestGetVMByName.json">
348363
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
349364
</None>
@@ -405,6 +420,7 @@
405420
<ItemGroup>
406421
<Content Include="about.txt" />
407422
</ItemGroup>
423+
<ItemGroup />
408424
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
409425
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
410426
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

src/ResourceManager/Batch/Commands.Batch.Test/ScenarioTests/ScenarioTestHelpers.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,48 @@ public static void TerminateJob(BatchAccountContext context, string workItemName
263263
}
264264
}
265265

266+
/// <summary>
267+
/// Creates a test user for use in Scenario tests.
268+
/// </summary>
269+
public static void CreateTestUser(BatchController controller, BatchAccountContext context, string poolName, string vmName, string userName)
270+
{
271+
YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor();
272+
BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor };
273+
BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient);
274+
275+
NewVMUserParameters parameters = new NewVMUserParameters()
276+
{
277+
Context = context,
278+
PoolName = poolName,
279+
VMName = vmName,
280+
UserName = userName,
281+
Password = "Password1234!",
282+
AdditionalBehaviors = behaviors
283+
};
284+
285+
client.CreateVMUser(parameters);
286+
}
287+
288+
/// <summary>
289+
/// Deletes a user used in a Scenario test.
290+
/// </summary>
291+
public static void DeleteUser(BatchController controller, BatchAccountContext context, string poolName, string vmName, string userName)
292+
{
293+
YieldInjectionInterceptor interceptor = CreateHttpRecordingInterceptor();
294+
BatchClientBehavior[] behaviors = new BatchClientBehavior[] { interceptor };
295+
BatchClient client = new BatchClient(controller.BatchManagementClient, controller.ResourceManagementClient);
296+
297+
RemoveVMUserParameters parameters = new RemoveVMUserParameters()
298+
{
299+
Context = context,
300+
PoolName = poolName,
301+
VMName = vmName,
302+
UserName = userName,
303+
AdditionalBehaviors = behaviors
304+
};
305+
client.DeleteVMUser(parameters);
306+
}
307+
266308
/// <summary>
267309
/// Creates an interceptor that can be used to support the HTTP recorder scenario tests.
268310
/// This behavior grabs the outgoing Protocol request, converts it to an HttpRequestMessage compatible with the
@@ -424,6 +466,15 @@ private static object GenerateBatchResponse(BatchRequest batchRequest, HttpWebRe
424466
if (createCommandMethod != null)
425467
{
426468
object command = createCommandMethod.Invoke(batchRequest, null);
469+
FieldInfo preProcessField = command.GetType().GetField("PreProcessResponse", BindingFlags.Public | BindingFlags.Instance);
470+
if (preProcessField != null)
471+
{
472+
Delegate preProcessDelegate = preProcessField.GetValue(command) as Delegate;
473+
if (preProcessDelegate != null)
474+
{
475+
preProcessDelegate.DynamicInvoke(command, webResponse, null, null);
476+
}
477+
}
427478

428479
FieldInfo postProcessField = command.GetType().GetField("PostProcessResponse", BindingFlags.Public | BindingFlags.Instance);
429480
if (postProcessField != null)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
15+
using System;
16+
using Microsoft.Azure.Batch;
17+
using Microsoft.Azure.Batch.Protocol.Entities;
18+
using Microsoft.Azure.Commands.Batch.Models;
19+
using Microsoft.Azure.Test;
20+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
21+
using System.Collections.Generic;
22+
using System.Management.Automation;
23+
using Xunit;
24+
using Constants = Microsoft.Azure.Commands.Batch.Utils.Constants;
25+
26+
namespace Microsoft.Azure.Commands.Batch.Test.ScenarioTests
27+
{
28+
public class VMUserTests
29+
{
30+
// NOTE: To save time on VM allocation when recording, these tests assume the following:
31+
// - A Batch account named 'usertests' exists under the subscription being used for recording.
32+
// - There is a pool named 'testPool' that has at least 1 vm allocated to it.
33+
34+
private const string accountName = "usertests";
35+
private const string poolName = "testPool";
36+
private const string vmName = "tvm-1900272697_1-20150407t180708z"; // Run the following to get a vm name: (Get-AzureBatchVM "testPool" -BatchContext $context)[0].Name
37+
38+
[Fact]
39+
[Trait(Category.AcceptanceType, Category.CheckIn)]
40+
public void TestCreateUser()
41+
{
42+
BatchController controller = BatchController.NewInstance;
43+
string userName = "testCreateUser";
44+
controller.RunPsTestWorkflow(
45+
() => { return new string[] { string.Format("Test-CreateUser '{0}' '{1}' '{2}' '{3}' 0", accountName, poolName, vmName, userName) }; },
46+
null,
47+
() =>
48+
{
49+
BatchAccountContext context = ScenarioTestHelpers.GetBatchAccountContextWithKeys(controller, accountName);
50+
ScenarioTestHelpers.DeleteUser(controller, context, poolName, vmName, userName);
51+
},
52+
TestUtilities.GetCallingClass(),
53+
TestUtilities.GetCurrentMethodName());
54+
55+
}
56+
57+
[Fact]
58+
[Trait(Category.AcceptanceType, Category.CheckIn)]
59+
public void TestCreateUserPipeline()
60+
{
61+
BatchController controller = BatchController.NewInstance;
62+
string userName = "testCreateUserPipe";
63+
controller.RunPsTestWorkflow(
64+
() => { return new string[] { string.Format("Test-CreateUser '{0}' '{1}' '{2}' '{3}' 1", accountName, poolName, vmName, userName) }; },
65+
null,
66+
() =>
67+
{
68+
BatchAccountContext context = ScenarioTestHelpers.GetBatchAccountContextWithKeys(controller, accountName);
69+
ScenarioTestHelpers.DeleteUser(controller, context, poolName, vmName, userName);
70+
},
71+
TestUtilities.GetCallingClass(),
72+
TestUtilities.GetCurrentMethodName());
73+
74+
}
75+
76+
[Fact]
77+
[Trait(Category.AcceptanceType, Category.CheckIn)]
78+
public void TestDeleteUser()
79+
{
80+
BatchController controller = BatchController.NewInstance;
81+
BatchAccountContext context = null;
82+
string userName = "testDeleteUser";
83+
controller.RunPsTestWorkflow(
84+
() => { return new string[] { string.Format("Test-DeleteUser '{0}' '{1}' '{2}' '{3}'", accountName, poolName, vmName, userName) }; },
85+
() =>
86+
{
87+
context = ScenarioTestHelpers.GetBatchAccountContextWithKeys(controller, accountName);
88+
ScenarioTestHelpers.CreateTestUser(controller, context, poolName, vmName, userName);
89+
},
90+
null,
91+
TestUtilities.GetCallingClass(),
92+
TestUtilities.GetCurrentMethodName());
93+
}
94+
}
95+
96+
// Cmdlets that use the HTTP Recorder interceptor for use with scenario tests
97+
[Cmdlet(VerbsCommon.New, "AzureBatchVMUser_ST")]
98+
public class NewBatchVMUserScenarioTestCommand : NewBatchVMUserCommand
99+
{
100+
public override void ExecuteCmdlet()
101+
{
102+
AdditionalBehaviors = new List<BatchClientBehavior>() { ScenarioTestHelpers.CreateHttpRecordingInterceptor() };
103+
base.ExecuteCmdlet();
104+
}
105+
}
106+
107+
[Cmdlet(VerbsCommon.Remove, "AzureBatchVMUser_ST")]
108+
public class RemoveBatchVMUserScenarioTestCommand : RemoveBatchVMUserCommand
109+
{
110+
public override void ExecuteCmdlet()
111+
{
112+
AdditionalBehaviors = new List<BatchClientBehavior>() { ScenarioTestHelpers.CreateHttpRecordingInterceptor() };
113+
base.ExecuteCmdlet();
114+
}
115+
}
116+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
15+
<#
16+
.SYNOPSIS
17+
Tests creating a User
18+
#>
19+
function Test-CreateUser
20+
{
21+
param([string]$accountName, [string]$poolName, [string]$vmName, [string]$userName, [string]$usePipeline)
22+
23+
$context = Get-AzureBatchAccountKeys -Name $accountName
24+
$password = "Password1234!"
25+
26+
# Create a user
27+
if ($usePipeline -eq '1')
28+
{
29+
$expiryTime = [DateTime]::Now.AddDays(5)
30+
$vm = Get-AzureBatchVM_ST $poolName $vmName -BatchContext $context
31+
$vm | New-AzureBatchVMUser_ST -Name $userName -Password $password -ExpiryTime $expiryTime -IsAdmin -BatchContext $context
32+
}
33+
else
34+
{
35+
New-AzureBatchVMUser_ST -PoolName $poolName -VMName $vmName -Name $userName -Password $password -BatchContext $context
36+
}
37+
38+
# Verify that a user was created
39+
# There is currently no Get/List user API, so try to create the user again and verify that it fails.
40+
Assert-Throws { New-AzureBatchVMUser_ST -PoolName $poolName -VMName $vmName -Name $userName -Password $password -BatchContext $context }
41+
}
42+
43+
<#
44+
.SYNOPSIS
45+
Tests deleting a user
46+
#>
47+
function Test-DeleteUser
48+
{
49+
param([string]$accountName, [string]$poolName, [string]$vmName, [string]$userName)
50+
51+
$context = Get-AzureBatchAccountKeys -Name $accountName
52+
53+
Remove-AzureBatchVMUser_ST -PoolName $poolName -VMName $vmName -Name $userName -Force -BatchContext $context
54+
55+
# Verify the user was deleted
56+
# There is currently no Get/List user API, so try to delete the user again and verify that it fails.
57+
Assert-Throws { Remove-AzureBatchUser_ST -PoolName $poolName -VMName $vmName -Name $userName -Force -BatchContext $context }
58+
}

src/ResourceManager/Batch/Commands.Batch.Test/ScenarioTests/WorkItemTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ public void TestDeleteWorkItem()
193193
},
194194
() =>
195195
{
196-
ScenarioTestHelpers.DeleteWorkItem(controller, context, workItemName);
197196
ScenarioTestHelpers.CleanupTestAccount(controller, resourceGroupName, accountName);
198197
},
199198
TestUtilities.GetCallingClass(),
@@ -220,7 +219,6 @@ public void TestDeleteWorkItemPipeline()
220219
},
221220
() =>
222221
{
223-
ScenarioTestHelpers.DeleteWorkItem(controller, context, workItemName);
224222
ScenarioTestHelpers.CleanupTestAccount(controller, resourceGroupName, accountName);
225223
},
226224
TestUtilities.GetCallingClass(),

0 commit comments

Comments
 (0)