-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DeploymentManager - SDK preview (#5649)
* Port from -pr repo * UT framework; ArtifactSource CRUD tests * Unit Tests * Enhance unit tests * Fix for Playback mode * Update Tests to fetch SAS from storage * Update TargetFramework * Update tests, session records * PR Feedback * Autogenerate using generate.ps1 * PR Feedback * Regerate sdk using generate.ps1
- Loading branch information
1 parent
87bf0fe
commit 149dd1b
Showing
81 changed files
with
17,126 additions
and
0 deletions.
There are no files selected for viewing
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,7 @@ | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<!--This file and it's contents are updated at build time moving or editing might result in build failure. Take due deligence while editing this file--> | ||
<PropertyGroup> | ||
<AzureApiTag>DeploymentManager_2018-09-01-preview;</AzureApiTag> | ||
<PackageTags>$(PackageTags);$(CommonTags);$(AzureApiTag);</PackageTags> | ||
</PropertyGroup> | ||
</Project> |
46 changes: 46 additions & 0 deletions
46
src/SDKs/DeploymentManager/DeploymentManager.Tests/DeploymentManager.Tests.csproj
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,46 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0"> | ||
<Import Project="$([MSBuild]::GetPathOfFileAbove('AzSdk.test.reference.props'))" /> | ||
<PropertyGroup> | ||
<PackageId>Microsoft.Azure.Management.DeploymentManager.Tests</PackageId> | ||
<Description>DeploymentManager.Tests Class Library</Description> | ||
<AssemblyName>Microsoft.Azure.Management.DeploymentManager.Tests</AssemblyName> | ||
<Version>1.0.0</Version> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<ProjectGuid>{23CD2878-1A27-4ECD-80F9-8B9BB1539F7F}</ProjectGuid> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Update="SessionRecords\**\*.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
<!--Do not remove until VS Test Tools fixes #472--> | ||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="4.2.0-preview" /> | ||
<PackageReference Include="WindowsAzure.Storage" Version="8.7.0" /> | ||
<PackageReference Include="Microsoft.Azure.Management.Authorization" Version="2.11.0-preview" /> | ||
<PackageReference Include="Microsoft.Azure.Management.ManagedServiceIdentity" Version="0.10.0-preview" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Management.DeploymentManager\Microsoft.Azure.Management.DeploymentManager.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="SessionRecords\" /> | ||
</ItemGroup> | ||
|
||
<!--Do not remove until VS Test Tools fixes #472--> | ||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Update="Tests\ArtifactRoot\**\*.*"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
233 changes: 233 additions & 0 deletions
233
src/SDKs/DeploymentManager/DeploymentManager.Tests/Helpers/DeploymentManagerClientHelper.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,233 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
namespace Management.DeploymentManager.Tests | ||
{ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.Azure.Management.Authorization; | ||
using Microsoft.Azure.Management.ManagedServiceIdentity; | ||
using Microsoft.Azure.Management.Resources; | ||
using Microsoft.Azure.Management.Resources.Models; | ||
using Microsoft.Azure.Management.Storage; | ||
using Microsoft.Azure.Test.HttpRecorder; | ||
using Microsoft.Rest.ClientRuntime.Azure.TestFramework; | ||
using Microsoft.WindowsAzure.Storage; | ||
using Microsoft.WindowsAzure.Storage.Auth; | ||
using Microsoft.WindowsAzure.Storage.Blob; | ||
using Xunit; | ||
|
||
public class DeploymentManagerClientHelper | ||
{ | ||
private ResourceManagementClient resourceManagementClient; | ||
private StorageManagementClient storageManagementClient; | ||
private ManagedServiceIdentityClient managedServiceIdentityClient; | ||
private AuthorizationManagementClient authorizationClient; | ||
|
||
private MockContext _context; | ||
private TestBase _testBase; | ||
|
||
public DeploymentManagerClientHelper(TestBase testBase, MockContext context) : this( | ||
testBase, | ||
context, | ||
new RecordedDelegatingHandler() { StatusCodeToReturn = System.Net.HttpStatusCode.OK }) | ||
{ | ||
this.ResourceGroupName = TestUtilities.GenerateName("admsdknet"); | ||
} | ||
|
||
public DeploymentManagerClientHelper(TestBase testBase, MockContext context, RecordedDelegatingHandler handler) | ||
{ | ||
_testBase = testBase; | ||
_context = context; | ||
|
||
resourceManagementClient = DeploymentManagerTestUtilities.GetResourceManagementClient(context, handler); | ||
storageManagementClient = DeploymentManagerTestUtilities.GetStorageManagementClient(context, handler); | ||
managedServiceIdentityClient = DeploymentManagerTestUtilities.GetManagedServiceIdentityClient(context, handler); | ||
authorizationClient = DeploymentManagerTestUtilities.GetAuthorizationManagementClient(context, handler); | ||
} | ||
|
||
public string ResourceGroupName { get; private set; } | ||
|
||
public void TryCreateResourceGroup(string location) | ||
{ | ||
ResourceGroup result = resourceManagementClient.ResourceGroups.CreateOrUpdate(this.ResourceGroupName, new ResourceGroup { Location = location }); | ||
var newlyCreatedGroup = resourceManagementClient.ResourceGroups.Get(this.ResourceGroupName); | ||
ThrowIfTrue(newlyCreatedGroup == null, "_client.ResourceGroups.Get returned null."); | ||
ThrowIfTrue(!this.ResourceGroupName.Equals(newlyCreatedGroup.Name), string.Format("resourceGroupName is not equal to {0}", this.ResourceGroupName)); | ||
} | ||
|
||
public string GetProviderLocation(string providerName, string resourceType) | ||
{ | ||
string defaultLocation = "Central US"; | ||
string location = defaultLocation; | ||
|
||
if (HttpMockServer.Mode == HttpRecorderMode.Record) | ||
{ | ||
var providerType = resourceManagementClient.Providers.Get(providerName).ResourceTypes.ToList() | ||
.FirstOrDefault(t => t.ResourceType.Equals(resourceType, StringComparison.OrdinalIgnoreCase)); | ||
|
||
location = providerType?.Locations?.FirstOrDefault() ?? defaultLocation; | ||
} | ||
|
||
return location; | ||
} | ||
|
||
public void DeleteResourceGroup(string resourceGroupName = null) | ||
{ | ||
if (string.IsNullOrEmpty(resourceGroupName)) | ||
{ | ||
resourceManagementClient.ResourceGroups.Delete(this.ResourceGroupName); | ||
} | ||
else | ||
{ | ||
resourceManagementClient.ResourceGroups.Delete(resourceGroupName); | ||
} | ||
} | ||
|
||
public string CreateManagedIdentity( | ||
string subscriptionId, | ||
string identityName) | ||
{ | ||
if (HttpMockServer.Mode == HttpRecorderMode.Record) | ||
{ | ||
var parameters = new Microsoft.Azure.Management.ManagedServiceIdentity.Models.Identity() | ||
{ | ||
Location = this.GetProviderLocation("Microsoft.ManagedIdentity", "userAssignedIdentities") | ||
}; | ||
|
||
var identity = this.managedServiceIdentityClient.UserAssignedIdentities.CreateOrUpdate( | ||
this.ResourceGroupName, | ||
identityName, | ||
parameters); | ||
|
||
Assert.NotNull(identity); | ||
|
||
// Give a couple minutes for the MSI to propagate. Observed failures of principalId not being found in the tenant | ||
// when there is no wait time between MSI creation and role assignment. | ||
DeploymentManagerTestUtilities.Sleep(TimeSpan.FromMinutes(2)); | ||
|
||
var scope = "/subscriptions/" + subscriptionId; | ||
var roleDefinitionList = this.authorizationClient.RoleDefinitions.List( | ||
scope, | ||
new Microsoft.Rest.Azure.OData.ODataQuery<Microsoft.Azure.Management.Authorization.Models.RoleDefinitionFilter>("roleName eq 'Contributor'")); | ||
|
||
var roleAssignmentName = Guid.NewGuid().ToString(); | ||
var roleAssignmentParameters = new Microsoft.Azure.Management.Authorization.Models.RoleAssignmentCreateParameters() | ||
{ | ||
PrincipalId = identity.PrincipalId.ToString(), | ||
RoleDefinitionId = roleDefinitionList.First().Id, | ||
CanDelegate = false | ||
}; | ||
|
||
var roleAssignment = this.authorizationClient.RoleAssignments.Create( | ||
scope, | ||
roleAssignmentName, | ||
roleAssignmentParameters); | ||
Assert.NotNull(roleAssignment); | ||
|
||
// Add additional wait time after role assignment to propagate permissions. Observed | ||
// no permissions to modify resource group errors without wait time. | ||
DeploymentManagerTestUtilities.Sleep(TimeSpan.FromMinutes(1)); | ||
|
||
roleAssignment = this.authorizationClient.RoleAssignments.Get( | ||
scope, | ||
roleAssignmentName); | ||
Assert.NotNull(roleAssignment); | ||
|
||
return identity.Id; | ||
} | ||
|
||
return "dummyIdentity"; | ||
} | ||
|
||
public void CreateStorageAccount(string storageAccountName) | ||
{ | ||
if (HttpMockServer.Mode == HttpRecorderMode.Record) | ||
{ | ||
var parameters = new Microsoft.Azure.Management.Storage.Models.StorageAccountCreateParameters() | ||
{ | ||
Location = this.GetProviderLocation("Microsoft.Storage", "storageAccounts"), | ||
AccountType = Microsoft.Azure.Management.Storage.Models.AccountType.StandardLRS | ||
}; | ||
|
||
var storageAccount = this.storageManagementClient.StorageAccounts.Create( | ||
this.ResourceGroupName, | ||
storageAccountName, | ||
parameters); | ||
} | ||
} | ||
|
||
public void UploadBlob( | ||
string storageAccountName, | ||
string containerName, | ||
string filePath, | ||
string blobName) | ||
{ | ||
if (HttpMockServer.Mode == HttpRecorderMode.Record) | ||
{ | ||
var accountKeyResult = this.storageManagementClient.StorageAccounts.ListKeysWithHttpMessagesAsync( | ||
this.ResourceGroupName, | ||
storageAccountName).Result; | ||
var storageAccount = new CloudStorageAccount( | ||
new StorageCredentials( | ||
storageAccountName, | ||
accountKeyResult.Body.Key1), | ||
useHttps: true); | ||
|
||
var blobClient = storageAccount.CreateCloudBlobClient(); | ||
var container = blobClient.GetContainerReference(containerName); | ||
|
||
container.CreateIfNotExistsAsync().Wait(); | ||
|
||
var blob = container.GetBlockBlobReference(blobName); | ||
blob.UploadFromFileAsync(filePath).Wait(); | ||
} | ||
} | ||
|
||
public string GetBlobContainerSasUri(string resourceGroupName, string storageAccountName, string containerName) | ||
{ | ||
string sasUri = "foobar"; | ||
|
||
if (HttpMockServer.Mode == HttpRecorderMode.Record) | ||
{ | ||
var accountKeyResult = this.storageManagementClient.StorageAccounts.ListKeysWithHttpMessagesAsync( | ||
resourceGroupName, | ||
storageAccountName).Result; | ||
var storageAccount = new CloudStorageAccount( | ||
new StorageCredentials( | ||
storageAccountName, | ||
accountKeyResult.Body.Key1), | ||
useHttps: true); | ||
|
||
var blobClient = storageAccount.CreateCloudBlobClient(); | ||
var container = blobClient.GetContainerReference(containerName); | ||
container.CreateIfNotExistsAsync(); | ||
sasUri = this.GetContainerSasUri(container); | ||
} | ||
|
||
return sasUri; | ||
} | ||
|
||
private string GetContainerSasUri(CloudBlobContainer container) | ||
{ | ||
var sasConstraints = new SharedAccessBlobPolicy(); | ||
sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddDays(-1); | ||
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddDays(2); | ||
sasConstraints.Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List; | ||
|
||
// Generate the shared access signature on the blob, setting the constraints directly on the signature. | ||
string sasContainerToken = container.GetSharedAccessSignature(sasConstraints); | ||
|
||
// Return the URI string for the container, including the SAS token. | ||
return container.Uri + sasContainerToken; | ||
} | ||
|
||
private void ThrowIfTrue(bool condition, string message) | ||
{ | ||
if (condition) | ||
{ | ||
throw new Exception(message); | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/SDKs/DeploymentManager/DeploymentManager.Tests/Helpers/DeploymentManagerTestUtilities.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,61 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
namespace Management.DeploymentManager.Tests | ||
{ | ||
using Microsoft.Azure.Management.Authorization; | ||
using Microsoft.Azure.Management.DeploymentManager; | ||
using Microsoft.Azure.Management.ManagedServiceIdentity; | ||
using Microsoft.Azure.Management.Resources; | ||
using Microsoft.Azure.Management.Storage; | ||
using Microsoft.Azure.Test.HttpRecorder; | ||
using Microsoft.Rest.ClientRuntime.Azure.TestFramework; | ||
using System; | ||
using System.Threading; | ||
|
||
public static class DeploymentManagerTestUtilities | ||
{ | ||
public static ResourceManagementClient GetResourceManagementClient(MockContext context, RecordedDelegatingHandler handler) | ||
{ | ||
handler.IsPassThrough = true; | ||
var client = context.GetServiceClient<ResourceManagementClient>(handlers: handler); | ||
return client; | ||
} | ||
|
||
public static AuthorizationManagementClient GetAuthorizationManagementClient(MockContext context, RecordedDelegatingHandler handler) | ||
{ | ||
handler.IsPassThrough = true; | ||
var client = context.GetServiceClient<AuthorizationManagementClient>(handlers: handler); | ||
return client; | ||
} | ||
|
||
public static ManagedServiceIdentityClient GetManagedServiceIdentityClient(MockContext context, RecordedDelegatingHandler handler) | ||
{ | ||
handler.IsPassThrough = true; | ||
var client = context.GetServiceClient<ManagedServiceIdentityClient>(handlers: handler); | ||
return client; | ||
} | ||
|
||
public static AzureDeploymentManagerClient GetDeploymentManagerClient(MockContext context, RecordedDelegatingHandler handler) | ||
{ | ||
handler.IsPassThrough = true; | ||
var client = context.GetServiceClient<AzureDeploymentManagerClient>(handlers: handler); | ||
return client; | ||
} | ||
|
||
public static StorageManagementClient GetStorageManagementClient(MockContext context, RecordedDelegatingHandler handler) | ||
{ | ||
handler.IsPassThrough = true; | ||
var client = context.GetServiceClient<StorageManagementClient>(handlers: handler); | ||
return client; | ||
} | ||
|
||
public static void Sleep(TimeSpan duration) | ||
{ | ||
if (HttpMockServer.Mode == HttpRecorderMode.Record) | ||
{ | ||
Thread.Sleep(duration); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.