forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Azure#2605 from jasper-schneider/batchMgmtRecorder
Add some recorded tests to the Batch Management lib
- Loading branch information
Showing
6 changed files
with
1,204 additions
and
0 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
src/ResourceManagement/Batch/Batch.Tests/ScenarioTests/AccountTests.ScenarioTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// | ||
// 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 Microsoft.Azure.Management.Batch; | ||
using Microsoft.Azure.Management.Batch.Models; | ||
using Microsoft.Azure.Management.ResourceManager.Models; | ||
using Microsoft.Rest.Azure; | ||
using Microsoft.Rest.ClientRuntime.Azure.TestFramework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
|
||
namespace Batch.Tests.ScenarioTests | ||
{ | ||
public class AccountTests : BatchScenarioTestBase | ||
{ | ||
[Fact] | ||
public async Task BatchAccountEndToEndAsync() | ||
{ | ||
using (MockContext context = StartMockContextAndInitializeClients(this.GetType().FullName)) | ||
{ | ||
string resourceGroupName = TestUtilities.GenerateName(); | ||
string batchAccountName = TestUtilities.GenerateName(); | ||
ResourceGroup group = new ResourceGroup(this.Location); | ||
await this.ResourceManagementClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, group); | ||
|
||
try | ||
{ | ||
// Create an account | ||
BatchAccountCreateParameters createParams = new BatchAccountCreateParameters(this.Location); | ||
await this.BatchManagementClient.BatchAccount.CreateAsync(resourceGroupName, batchAccountName, createParams); | ||
|
||
// Get the account and verify some properties | ||
BatchAccount batchAccount = await this.BatchManagementClient.BatchAccount.GetAsync(resourceGroupName, batchAccountName); | ||
Assert.Equal(batchAccountName, batchAccount.Name); | ||
Assert.True(batchAccount.CoreQuota > 0); | ||
|
||
// Rotate a key | ||
BatchAccountKeys originalKeys = await this.BatchManagementClient.BatchAccount.GetKeysAsync(resourceGroupName, batchAccountName); | ||
BatchAccountKeys newKeys = await this.BatchManagementClient.BatchAccount.RegenerateKeyAsync(resourceGroupName, batchAccountName, AccountKeyType.Primary); | ||
Assert.NotEqual(originalKeys.Primary, newKeys.Primary); | ||
Assert.Equal(originalKeys.Secondary, newKeys.Secondary); | ||
|
||
// List accounts under the resource group | ||
IPage<BatchAccount> listResponse = await this.BatchManagementClient.BatchAccount.ListByResourceGroupAsync(resourceGroupName); | ||
List<BatchAccount> accounts = new List<BatchAccount>(listResponse); | ||
string nextLink = listResponse.NextPageLink; | ||
while (nextLink != null) | ||
{ | ||
listResponse = await this.BatchManagementClient.BatchAccount.ListByResourceGroupNextAsync(nextLink); | ||
accounts.AddRange(listResponse); | ||
nextLink = listResponse.NextPageLink; | ||
} | ||
|
||
Assert.Equal(1, accounts.Count); | ||
Assert.Equal(batchAccountName, accounts.First().Name); | ||
|
||
// Delete the account | ||
try | ||
{ | ||
await this.BatchManagementClient.BatchAccount.DeleteAsync(resourceGroupName, batchAccountName); | ||
} | ||
catch (CloudException ex) | ||
{ | ||
/* Account deletion is a long running operation. This .DeleteAsync() method will submit the account deletion request and | ||
* poll for the status of the long running operation until the account is deleted. Currently, querying for the operation | ||
* status after the account is deleted will return a 404 error, so we have to add this catch statement. This behavior will | ||
* be fixed in a future service release. | ||
*/ | ||
if (ex.Response.StatusCode != HttpStatusCode.NotFound) | ||
{ | ||
throw; | ||
} | ||
} | ||
// Verify account was deleted. A GET operation will return a 404 error and result in an exception | ||
try | ||
{ | ||
await this.BatchManagementClient.BatchAccount.GetAsync(resourceGroupName, batchAccountName); | ||
} | ||
catch (CloudException ex) | ||
{ | ||
Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); | ||
} | ||
} | ||
finally | ||
{ | ||
await this.ResourceManagementClient.ResourceGroups.DeleteWithHttpMessagesAsync(resourceGroupName); | ||
} | ||
} | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/ResourceManagement/Batch/Batch.Tests/ScenarioTests/BatchScenarioTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// | ||
// 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 Microsoft.Azure.Management.Batch; | ||
using Microsoft.Azure.Management.ResourceManager; | ||
using Microsoft.Azure.Management.ResourceManager.Models; | ||
using Microsoft.Rest.ClientRuntime.Azure.TestFramework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Batch.Tests.ScenarioTests | ||
{ | ||
public class BatchScenarioTestBase : TestBase | ||
{ | ||
public ResourceManagementClient ResourceManagementClient { get; private set; } | ||
|
||
public BatchManagementClient BatchManagementClient { get; private set; } | ||
|
||
public string Location { get; private set; } | ||
|
||
protected MockContext StartMockContextAndInitializeClients(string className, | ||
// Automatically populates the methodName parameter with the calling method, which | ||
// gets used to generate recorder file names. | ||
[System.Runtime.CompilerServices.CallerMemberName] | ||
string methodName = "") | ||
{ | ||
MockContext context = MockContext.Start(className, methodName); | ||
Initialize(context); | ||
|
||
return context; | ||
} | ||
|
||
private void Initialize(MockContext context) | ||
{ | ||
this.ResourceManagementClient = context.GetServiceClient<ResourceManagementClient>(); | ||
this.BatchManagementClient = context.GetServiceClient<BatchManagementClient>(); | ||
|
||
Provider provider = this.ResourceManagementClient.Providers.Get("Microsoft.Batch"); | ||
IList<string> locations = provider.ResourceTypes.Where((resType) => resType.ResourceType == "batchAccounts").First().Locations; | ||
this.Location = locations.DefaultIfEmpty("westus").First(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/ResourceManagement/Batch/Batch.Tests/ScenarioTests/LocationTests.ScenarioTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// | ||
// 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 Microsoft.Azure.Management.Batch; | ||
using Microsoft.Azure.Management.Batch.Models; | ||
using Microsoft.Rest.ClientRuntime.Azure.TestFramework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Batch.Tests.ScenarioTests | ||
{ | ||
public class LocationTests : BatchScenarioTestBase | ||
{ | ||
[Fact] | ||
public async Task GetLocationQuotasAsync() | ||
{ | ||
using (MockContext context = StartMockContextAndInitializeClients(this.GetType().FullName)) | ||
{ | ||
BatchLocationQuota quotas = await this.BatchManagementClient.Location.GetQuotasAsync(this.Location); | ||
|
||
Assert.NotNull(quotas.AccountQuota); | ||
Assert.True(quotas.AccountQuota.Value > 0); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.