private static void DisposeOfTestResource(Action disposalCall) { try { disposalCall(); } catch (CloudException cloudEx) { Trace.TraceWarning("Caught unexpected exception during resource cleanup: "); Trace.TraceWarning(WebServiceTests.GenerateCloudExceptionReport(cloudEx)); } catch (Exception ex) { Trace.TraceWarning("Caught unexpected exception during resource cleanup: {0}", ex.Message); } }
private void RunAMLWebServiceTestScenario(AMLWebServiceTestDelegate actualTest, [System.Runtime.CompilerServices.CallerMemberName] string methodName = "testframework_failed") { using (var context = MockContext.Start(this.GetType().FullName, methodName)) { bool testIsSuccessfull = true; string cpRpApiVersion = string.Empty; ResourceManagementClient resourcesClient = null; StorageManagementClient storageManagementClient = null; var amlServiceName = TestUtilities.GenerateName(WebServiceTests.TestServiceNamePrefix); var resourceGroupName = TestUtilities.GenerateName(WebServiceTests.TestResourceGroupNamePrefix); var commitmentPlanName = TestUtilities.GenerateName(WebServiceTests.TestCommitmentPlanNamePrefix); var cpDeploymentName = "depl" + commitmentPlanName; var storageAccountName = TestUtilities.GenerateName(WebServiceTests.TestStorageAccountPrefix); try { // Create a resource group for the AML service resourcesClient = context.GetServiceClient <ResourceManagementClient>(); var resourceGroupDefinition = new ResourceGroup { Location = WebServiceTests.DefaultLocation }; resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroupDefinition); // Create a support storage account for the service in this resource group storageManagementClient = context.GetServiceClient <StorageManagementClient>(); var accountParameters = new StorageAccountCreateParameters { AccountType = AccountType.StandardLRS, Location = WebServiceTests.DefaultLocation }; storageManagementClient.StorageAccounts.Create(resourceGroupName, storageAccountName, accountParameters); StorageAccountKeys accountKeys = storageManagementClient.StorageAccounts.ListKeys(resourceGroupName, storageAccountName); var storageAccountInfo = new StorageAccount(storageAccountName, accountKeys.Key1); // Create an AML commitment plan resource to associate with the services cpRpApiVersion = ResourceProvidersHelper.GetRPApiVersion(resourcesClient, WebServiceTests.MLResourceProviderNamespace, WebServiceTests.CPResourceType); var cpDeploymentItems = WebServiceTests.CreateCommitmentPlanResource(resourceGroupName, commitmentPlanName, cpDeploymentName, resourcesClient, cpRpApiVersion); var cpResource = cpDeploymentItems.Item2; // Create a client for the AML RP and run the actual test var webServicesClient = context.GetServiceClient <AzureMLWebServicesManagementClient>(); webServicesClient.LongRunningOperationRetryTimeout = WebServiceTests.AsyncOperationPollingIntervalSeconds; // Run the actual test actualTest(amlServiceName, resourceGroupName, resourcesClient, webServicesClient, cpResource.Id, storageAccountInfo); } catch (CloudException cloudEx) { Trace.TraceError("Caught unexpected exception: "); Trace.TraceError(WebServiceTests.GenerateCloudExceptionReport(cloudEx)); testIsSuccessfull = false; } finally { if (resourcesClient != null) { // Delete the deployment with the commitment plan if (cpRpApiVersion != string.Empty) { WebServiceTests.DisposeOfTestResource(() => resourcesClient.Resources.Delete(resourceGroupName, WebServiceTests.MLResourceProviderNamespace, string.Empty, WebServiceTests.CPResourceType, commitmentPlanName, cpRpApiVersion)); WebServiceTests.DisposeOfTestResource(() => resourcesClient.Deployments.Delete(resourceGroupName, cpDeploymentName)); } // Delete the created storage account WebServiceTests.DisposeOfTestResource(() => storageManagementClient.StorageAccounts.Delete(resourceGroupName, storageAccountName)); // Delete the created resource group WebServiceTests.DisposeOfTestResource(() => resourcesClient.ResourceGroups.Delete(resourceGroupName)); } } Assert.True(testIsSuccessfull); } }