Skip to content

Commit c9d8d2a

Browse files
authored
Merge pull request Azure#2463 from jasper-schneider/testCleanup
Batch test cleanup
2 parents 516a1f0 + be176bb commit c9d8d2a

24 files changed

+11836
-5652
lines changed

src/ResourceManager/AzureBatch/Commands.Batch.Test/BatchTestHelpers.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ namespace Microsoft.Azure.Commands.Batch.Test
3737
/// </summary>
3838
public static class BatchTestHelpers
3939
{
40-
internal static readonly string TestCertificateFileName1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\BatchTestCert01.cer");
41-
internal static readonly string TestCertificateFileName2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\BatchTestCert02.cer");
40+
internal static readonly string TestCertificateFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\BatchTestCert01.cer");
4241
internal const string TestCertificateAlgorithm = "sha1";
4342
internal const string TestCertificatePassword = "Passw0rd";
4443
internal static readonly int DefaultQuotaCount = 20;

src/ResourceManager/AzureBatch/Commands.Batch.Test/Certificates/NewBatchCertificateCommandTests.cs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Microsoft.Azure.Batch;
1616
using Microsoft.Azure.Batch.Protocol;
17+
using Microsoft.Azure.Batch.Protocol.BatchRequests;
1718
using Microsoft.Azure.Batch.Protocol.Models;
1819
using Microsoft.Rest.Azure;
1920
using Microsoft.WindowsAzure.Commands.ScenarioTest;
@@ -22,6 +23,7 @@
2223
using System.Collections.Generic;
2324
using System.Management.Automation;
2425
using System.Security.Cryptography.X509Certificates;
26+
using System.Threading.Tasks;
2527
using Xunit;
2628
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
2729

@@ -47,15 +49,15 @@ public NewBatchCertificateCommandTests(Xunit.Abstractions.ITestOutputHelper outp
4749

4850
[Fact]
4951
[Trait(Category.AcceptanceType, Category.CheckIn)]
50-
public void NewBatchCertificateParametersTest()
52+
public void NewBatchCertificateRequiredParametersTest()
5153
{
5254
// Setup cmdlet without the required parameters
5355
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
5456
cmdlet.BatchContext = context;
5557

5658
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
5759

58-
cmdlet.FilePath = BatchTestHelpers.TestCertificateFileName1;
60+
cmdlet.FilePath = BatchTestHelpers.TestCertificateFileName;
5961

6062
// Don't go to the service on an Add Certificate call
6163
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
@@ -70,11 +72,80 @@ public void NewBatchCertificateParametersTest()
7072

7173
// Use the RawData parameter set next
7274
cmdlet.FilePath = null;
73-
X509Certificate2 cert = new X509Certificate2(BatchTestHelpers.TestCertificateFileName1);
75+
X509Certificate2 cert = new X509Certificate2(BatchTestHelpers.TestCertificateFileName);
7476
cmdlet.RawData = cert.RawData;
7577

7678
// Verify no exceptions when required parameters are set
7779
cmdlet.ExecuteCmdlet();
7880
}
81+
82+
[Fact]
83+
[Trait(Category.AcceptanceType, Category.CheckIn)]
84+
public void NewBatchCertificateRequestBodyTest()
85+
{
86+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
87+
cmdlet.BatchContext = context;
88+
89+
X509Certificate2 cert = new X509Certificate2(BatchTestHelpers.TestCertificateFileName);
90+
string certDataBase64String = Convert.ToBase64String(cert.RawData);
91+
92+
CertificateAddParameter requestParameters = null;
93+
94+
// Don't go to the service on an Add Certificate call
95+
RequestInterceptor interceptor = new RequestInterceptor((baseRequest) =>
96+
{
97+
CertificateAddBatchRequest request = (CertificateAddBatchRequest)baseRequest;
98+
99+
request.ServiceRequestFunc = (cancellationToken) =>
100+
{
101+
requestParameters = request.Parameters;
102+
103+
var response = new AzureOperationHeaderResponse<CertificateAddHeaders>();
104+
Task<AzureOperationHeaderResponse<CertificateAddHeaders>> task = Task.FromResult(response);
105+
return task;
106+
};
107+
});
108+
109+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };
110+
111+
// Verify that when just the raw data is specified, the request body matches expectations
112+
cmdlet.RawData = cert.RawData;
113+
cmdlet.ExecuteCmdlet();
114+
Assert.Equal(CertificateFormat.Cer, requestParameters.CertificateFormat);
115+
Assert.Equal(BatchTestHelpers.TestCertificateAlgorithm, requestParameters.ThumbprintAlgorithm);
116+
Assert.Equal(cert.Thumbprint.ToLowerInvariant(), requestParameters.Thumbprint.ToLowerInvariant());
117+
Assert.True(string.IsNullOrEmpty(requestParameters.Password));
118+
Assert.Equal(certDataBase64String, requestParameters.Data);
119+
120+
// Verify that when the raw data is specified with a password, the request body matches expectations
121+
cmdlet.RawData = cert.RawData;
122+
cmdlet.Password = BatchTestHelpers.TestCertificatePassword;
123+
cmdlet.ExecuteCmdlet();
124+
Assert.Equal(CertificateFormat.Pfx, requestParameters.CertificateFormat);
125+
Assert.Equal(BatchTestHelpers.TestCertificateAlgorithm, requestParameters.ThumbprintAlgorithm);
126+
Assert.Equal(cert.Thumbprint.ToLowerInvariant(), requestParameters.Thumbprint.ToLowerInvariant());
127+
Assert.Equal(BatchTestHelpers.TestCertificatePassword, requestParameters.Password);
128+
Assert.Equal(certDataBase64String, requestParameters.Data);
129+
130+
// Verify that when just a file path is specified, the request body matches expectations
131+
cmdlet.RawData = null;
132+
cmdlet.Password = null;
133+
cmdlet.FilePath = BatchTestHelpers.TestCertificateFileName;
134+
cmdlet.ExecuteCmdlet();
135+
Assert.Equal(CertificateFormat.Cer, requestParameters.CertificateFormat);
136+
Assert.Equal(BatchTestHelpers.TestCertificateAlgorithm, requestParameters.ThumbprintAlgorithm);
137+
Assert.Equal(cert.Thumbprint.ToLowerInvariant(), requestParameters.Thumbprint.ToLowerInvariant());
138+
Assert.True(string.IsNullOrEmpty(requestParameters.Password));
139+
Assert.Equal(certDataBase64String, requestParameters.Data);
140+
141+
// Verify that when a file path is specified with a password, the request body matches expectations
142+
cmdlet.Password = BatchTestHelpers.TestCertificatePassword;
143+
cmdlet.ExecuteCmdlet();
144+
Assert.Equal(CertificateFormat.Pfx, requestParameters.CertificateFormat);
145+
Assert.Equal(BatchTestHelpers.TestCertificateAlgorithm, requestParameters.ThumbprintAlgorithm);
146+
Assert.Equal(cert.Thumbprint.ToLowerInvariant(), requestParameters.Thumbprint.ToLowerInvariant());
147+
Assert.Equal(BatchTestHelpers.TestCertificatePassword, requestParameters.Password);
148+
Assert.Equal(certDataBase64String, requestParameters.Data);
149+
}
79150
}
80151
}

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,6 @@
284284
<None Include="Resources\BatchTestCert01.cer">
285285
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
286286
</None>
287-
<None Include="Resources\BatchTestCert02.cer">
288-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
289-
</None>
290-
<None Include="Resources\BatchTestCert03.cer">
291-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
292-
</None>
293-
<None Include="Resources\BatchTestCert04.pfx">
294-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
295-
</None>
296-
<None Include="Resources\BatchTestCert05.pfx">
297-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
298-
</None>
299287
<Content Include="Resources\TestApplicationPackage.zip">
300288
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
301289
</Content>
@@ -371,29 +359,11 @@
371359
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.BatchApplicationTests\TestUploadApplicationPackage.json">
372360
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
373361
</None>
374-
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.CertificateTests\TestAddCertificate.json">
375-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
376-
</None>
377362
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.CertificateTests\TestCancelCertificateDelete.json">
378363
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
379364
</None>
380-
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.CertificateTests\TestDeleteCertificate.json">
381-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
382-
</None>
383-
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.CertificateTests\TestGetAndListCertificatesWithSelect.json">
384-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
385-
</None>
386-
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.CertificateTests\TestGetCertificateByThumbprint.json">
387-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
388-
</None>
389-
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.CertificateTests\TestListAllCertificates.json">
390-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
391-
</None>
392-
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.CertificateTests\TestListCertificatesByFilter.json">
393-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
394-
</None>
395-
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.CertificateTests\TestListCertificatesWithMaxCount.json">
396-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
365+
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.CertificateTests\TestCertificateCrudOperations.json">
366+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
397367
</None>
398368
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.ComputeNodeTests\TestDisableAndEnableComputeNodeSchedulingById.json">
399369
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@@ -695,9 +665,6 @@
695665
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestListAllTasks.json">
696666
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
697667
</None>
698-
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestListSubtasksWithMaxCount.json">
699-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
700-
</None>
701668
<None Include="SessionRecords\Microsoft.Azure.Commands.Batch.Test.ScenarioTests.TaskTests\TestListTaskPipeline.json">
702669
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
703670
</None>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/ResourceManager/AzureBatch/Commands.Batch.Test/ScenarioTests/BatchController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
using Microsoft.Azure.Management.Resources;
2020
using Microsoft.Azure.Test;
2121
using Microsoft.Azure.Test.HttpRecorder;
22+
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
2223
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2324
using System;
2425
using System.Collections.Generic;
2526
using System.IO;
2627
using System.Linq;
27-
using Microsoft.Azure.Commands.Common.Authentication;
28-
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
2928
using TestBase = Microsoft.Azure.Test.TestBase;
3029
using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory;
3130
using TestUtilities = Microsoft.Azure.Test.TestUtilities;

src/ResourceManager/AzureBatch/Commands.Batch.Test/ScenarioTests/CertificateTests.cs

Lines changed: 3 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -29,152 +29,9 @@ public CertificateTests(Xunit.Abstractions.ITestOutputHelper output)
2929

3030
[Fact]
3131
[Trait(Category.AcceptanceType, Category.CheckIn)]
32-
public void TestAddCertificate()
32+
public void TestCertificateCrudOperations()
3333
{
34-
BatchController.NewInstance.RunPsTest("Test-AddCertificate");
35-
}
36-
37-
[Fact]
38-
public void TestGetCertificateByThumbprint()
39-
{
40-
BatchController controller = BatchController.NewInstance;
41-
BatchAccountContext context = null;
42-
string thumbprint = null;
43-
controller.RunPsTestWorkflow(
44-
() => { return new string[] { string.Format("Test-GetCertificateByThumbprint '{0}' '{1}'", BatchTestHelpers.TestCertificateAlgorithm, thumbprint) }; },
45-
() =>
46-
{
47-
context = new ScenarioTestContext();
48-
thumbprint = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName1);
49-
},
50-
() =>
51-
{
52-
ScenarioTestHelpers.DeleteTestCertificate(controller, context, BatchTestHelpers.TestCertificateAlgorithm, thumbprint);
53-
},
54-
TestUtilities.GetCallingClass(),
55-
TestUtilities.GetCurrentMethodName());
56-
}
57-
58-
[Fact]
59-
public void TestListCertificatesByFilter()
60-
{
61-
BatchController controller = BatchController.NewInstance;
62-
BatchAccountContext context = null;
63-
string state = "active";
64-
string thumbprint1 = null;
65-
string toDeleteThumbprint = null;
66-
int matchCount = 1;
67-
controller.RunPsTestWorkflow(
68-
() => { return new string[] { string.Format("Test-ListCertificatesByFilter '{0}' '{1}' '{2}'", state, toDeleteThumbprint, matchCount) }; },
69-
() =>
70-
{
71-
context = new ScenarioTestContext();
72-
thumbprint1 = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName1);
73-
toDeleteThumbprint = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName2);
74-
},
75-
() =>
76-
{
77-
ScenarioTestHelpers.DeleteTestCertificate(controller, context, BatchTestHelpers.TestCertificateAlgorithm, thumbprint1);
78-
// Other cert is deleted as the first part of the PowerShell test script, but we ensure it's gone.
79-
try
80-
{
81-
ScenarioTestHelpers.DeleteTestCertificate(controller, context, BatchTestHelpers.TestCertificateAlgorithm, toDeleteThumbprint);
82-
}
83-
catch { }
84-
},
85-
TestUtilities.GetCallingClass(),
86-
TestUtilities.GetCurrentMethodName());
87-
}
88-
89-
[Fact]
90-
[Trait(Category.AcceptanceType, Category.CheckIn)]
91-
public void TestGetAndListCertificatesWithSelect()
92-
{
93-
BatchController controller = BatchController.NewInstance;
94-
BatchAccountContext context = null;
95-
string thumbprint = null;
96-
controller.RunPsTestWorkflow(
97-
() => { return new string[] { string.Format("Test-GetAndListCertificatesWithSelect '{0}' '{1}'", BatchTestHelpers.TestCertificateAlgorithm, thumbprint) }; },
98-
() =>
99-
{
100-
context = new ScenarioTestContext();
101-
thumbprint = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName1);
102-
},
103-
() =>
104-
{
105-
ScenarioTestHelpers.DeleteTestCertificate(controller, context, BatchTestHelpers.TestCertificateAlgorithm, thumbprint);
106-
},
107-
TestUtilities.GetCallingClass(),
108-
TestUtilities.GetCurrentMethodName());
109-
}
110-
111-
[Fact]
112-
public void TestListCertificatesWithMaxCount()
113-
{
114-
BatchController controller = BatchController.NewInstance;
115-
BatchAccountContext context = null;
116-
int maxCount = 1;
117-
string thumbprint1 = null;
118-
string thumbprint2 = null;
119-
controller.RunPsTestWorkflow(
120-
() => { return new string[] { string.Format("Test-ListCertificatesWithMaxCount '{0}'", maxCount) }; },
121-
() =>
122-
{
123-
context = new ScenarioTestContext();
124-
thumbprint1 = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName1);
125-
thumbprint2 = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName2);
126-
},
127-
() =>
128-
{
129-
ScenarioTestHelpers.DeleteTestCertificate(controller, context, BatchTestHelpers.TestCertificateAlgorithm, thumbprint1);
130-
ScenarioTestHelpers.DeleteTestCertificate(controller, context, BatchTestHelpers.TestCertificateAlgorithm, thumbprint2);
131-
},
132-
TestUtilities.GetCallingClass(),
133-
TestUtilities.GetCurrentMethodName());
134-
}
135-
136-
[Fact]
137-
public void TestListAllCertificates()
138-
{
139-
BatchController controller = BatchController.NewInstance;
140-
BatchAccountContext context = null;
141-
int count = 2;
142-
string thumbprint1 = null;
143-
string thumbprint2 = null;
144-
controller.RunPsTestWorkflow(
145-
() => { return new string[] { string.Format("Test-ListAllCertificates '{0}'", count) }; },
146-
() =>
147-
{
148-
context = new ScenarioTestContext();
149-
thumbprint1 = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName1);
150-
thumbprint2 = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName2);
151-
},
152-
() =>
153-
{
154-
ScenarioTestHelpers.DeleteTestCertificate(controller, context, BatchTestHelpers.TestCertificateAlgorithm, thumbprint1);
155-
ScenarioTestHelpers.DeleteTestCertificate(controller, context, BatchTestHelpers.TestCertificateAlgorithm, thumbprint2);
156-
},
157-
TestUtilities.GetCallingClass(),
158-
TestUtilities.GetCurrentMethodName());
159-
}
160-
161-
[Fact]
162-
[Trait(Category.AcceptanceType, Category.CheckIn)]
163-
public void TestDeleteCertificate()
164-
{
165-
BatchController controller = BatchController.NewInstance;
166-
BatchAccountContext context = null;
167-
string thumbprint = null;
168-
controller.RunPsTestWorkflow(
169-
() => { return new string[] { string.Format("Test-DeleteCertificate '{0}' '{1}'", BatchTestHelpers.TestCertificateAlgorithm, thumbprint) }; },
170-
() =>
171-
{
172-
context = new ScenarioTestContext();
173-
thumbprint = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName1);
174-
},
175-
null,
176-
TestUtilities.GetCallingClass(),
177-
TestUtilities.GetCurrentMethodName());
34+
BatchController.NewInstance.RunPsTest("Test-CertificateCrudOperations");
17835
}
17936

18037
[Fact]
@@ -189,7 +46,7 @@ public void TestCancelCertificateDelete()
18946
() =>
19047
{
19148
context = new ScenarioTestContext();
192-
thumbprint = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName1);
49+
thumbprint = ScenarioTestHelpers.AddTestCertificate(controller, context, BatchTestHelpers.TestCertificateFileName);
19350
CertificateReference certRef = new CertificateReference();
19451
certRef.StoreLocation = CertStoreLocation.CurrentUser;
19552
certRef.StoreName = "My";

0 commit comments

Comments
 (0)