Skip to content

Commit

Permalink
Merge pull request Azure#2605 from jasper-schneider/batchMgmtRecorder
Browse files Browse the repository at this point in the history
Add some recorded tests to the Batch Management lib
  • Loading branch information
markcowl authored Dec 2, 2016
2 parents ec4bcad + b35b02f commit 6d924a5
Show file tree
Hide file tree
Showing 6 changed files with 1,204 additions and 0 deletions.
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);
}
}
}
}
}
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();
}
}
}
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);
}
}
}
}
Loading

0 comments on commit 6d924a5

Please sign in to comment.