Skip to content

Commit

Permalink
[KeyVault] Refactor tests (Azure#12797)
Browse files Browse the repository at this point in the history
* don't use mgmt clients

* remove unused code

* remove useless comments
  • Loading branch information
isra-fel authored Sep 3, 2020
1 parent 260f054 commit b964fcd
Show file tree
Hide file tree
Showing 4 changed files with 955 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ public void TestDeleteVault()
public void TestSetRemoveAccessPolicyByObjectId()
{
string upn = "";
_data.ResetPreCreatedVault();

KeyVaultManagementController controller = KeyVaultManagementController.NewInstance;
controller.RunPsTestWorkflow(
Expand All @@ -168,7 +167,6 @@ public void TestSetRemoveAccessPolicyByObjectId()
public void TestSetRemoveAccessPolicyByUPN()
{
string upn = "";
_data.ResetPreCreatedVault();
KeyVaultManagementController.NewInstance.RunPsTestWorkflow(
_logger,
() =>
Expand All @@ -187,7 +185,6 @@ public void TestSetRemoveAccessPolicyByCompoundId()
{
string upn = "";
Guid? appId = null;
_data.ResetPreCreatedVault();

KeyVaultManagementController controller = KeyVaultManagementController.NewInstance;
controller.RunPsTestWorkflow(
Expand All @@ -211,7 +208,6 @@ public void TestRemoveAccessPolicyWithCompoundIdPolicies()

Guid? appId1 = null;
Guid? appId2 = null;
_data.ResetPreCreatedVault();

KeyVaultManagementController controller = KeyVaultManagementController.NewInstance;
controller.RunPsTestWorkflow(
Expand All @@ -233,7 +229,6 @@ public void TestSetCompoundIdAccessPolicy()
{
string upn = "";
Guid? appId = null;
_data.ResetPreCreatedVault();

KeyVaultManagementController controller = KeyVaultManagementController.NewInstance;
controller.RunPsTestWorkflow(
Expand All @@ -257,7 +252,6 @@ public void TestSetRemoveAccessPolicyBySPN()
ServicePrincipal principal = null;

KeyVaultManagementController controller = KeyVaultManagementController.NewInstance;
_data.ResetPreCreatedVault();
controller.RunPsTestWorkflow(
_logger,
//script builder
Expand Down Expand Up @@ -288,7 +282,6 @@ public void TestModifyAccessPolicy()
string upn = "";

KeyVaultManagementController controller = KeyVaultManagementController.NewInstance;
_data.ResetPreCreatedVault();

controller.RunPsTestWorkflow(
_logger,
Expand All @@ -310,7 +303,6 @@ public void TestModifyAccessPolicyEnabledForDeployment()
{
string upn = "";

_data.ResetPreCreatedVault();
KeyVaultManagementController.NewInstance.RunPsTestWorkflow(
_logger,
() =>
Expand All @@ -330,7 +322,6 @@ public void TestModifyAccessPolicyEnabledForTemplateDeployment()
{
string upn = "";

_data.ResetPreCreatedVault();
KeyVaultManagementController.NewInstance.RunPsTestWorkflow(
_logger,
() =>
Expand All @@ -349,7 +340,6 @@ public void TestModifyAccessPolicyEnabledForDiskEncryption()
{
string upn = "";

_data.ResetPreCreatedVault();
KeyVaultManagementController.NewInstance.RunPsTestWorkflow(
_logger,
() =>
Expand All @@ -367,14 +357,11 @@ public void TestModifyAccessPolicyEnabledForDiskEncryption()
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestModifyAccessPolicyNegativeCases()
{
string upn = "";

_data.ResetPreCreatedVault();
KeyVaultManagementController.NewInstance.RunPsTestWorkflow(
_logger,
() =>
{
return new[] { string.Format("{0} {1} {2} {3}", "Test-ModifyAccessPolicyNegativeCases", _data.PreCreatedVault, _data.ResourceGroupName, upn) };
return new[] { "Test-ModifyAccessPolicyNegativeCases" };
},
null,
MethodBase.GetCurrentMethod().ReflectedType?.ToString(),
Expand All @@ -387,7 +374,6 @@ public void TestModifyAccessPolicyNegativeCases()
public void TestRemoveNonExistentAccessPolicyDoesNotThrow()
{
string upn = "";
_data.ResetPreCreatedVault();

KeyVaultManagementController controller = KeyVaultManagementController.NewInstance;
controller.RunPsTestWorkflow(
Expand Down
114 changes: 1 addition & 113 deletions src/KeyVault/KeyVault.Test/ScenarioTests/KeyVaultTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,144 +12,32 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Management.KeyVault;
using Microsoft.Azure.Management.KeyVault.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Azure.Management.Internal.Resources;
using Microsoft.Azure.Management.Internal.Resources.Models;
using Sku = Microsoft.Azure.Management.KeyVault.Models.Sku;

namespace Microsoft.Azure.Commands.KeyVault.Test.ScenarioTests
{
public class KeyVaultTestFixture : RMTestBase, IDisposable
{
private readonly HttpRecorderMode _mode;

public string TagName { get; set; } = "testtag";
public string TagValue { get; set; } = "testvalue";

public string ResourceGroupName { get; set; }
public string Location { get; set; }
public string PreCreatedVault { get; set; }

private bool _initialized;
public KeyVaultTestFixture()
{
// Initialize has bug which causes null reference exception
HttpMockServer.FileSystemUtilsObject = new FileSystemUtils();
_mode = HttpMockServer.GetCurrentMode();
}

public void Initialize(string className)
{
if (_initialized)
return;

if (_mode == HttpRecorderMode.Record)
{
using (MockContext context = MockContext.Start(new StackTrace().GetFrame(1).GetMethod().ReflectedType?.ToString(), new StackTrace().GetFrame(1).GetMethod().Name))
{
var resourcesClient = context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
var mgmtClient = context.GetServiceClient<KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
var tenantId = TestEnvironmentFactory.GetTestEnvironment().Tenant;

//Figure out which locations are available for Key Vault
Location = GetKeyVaultLocation(resourcesClient);

//Create a resource group in that location
PreCreatedVault = TestUtilities.GenerateName("pshtestvault");
ResourceGroupName = TestUtilities.GenerateName("pshtestrg");

resourcesClient.ResourceGroups.CreateOrUpdate(ResourceGroupName, new ResourceGroup { Location = Location });
CreateVault(mgmtClient, Location, tenantId);
}
}

_initialized = true;
}

private static string GetKeyVaultLocation(ResourceManagementClient resourcesClient)
{
var provider = resourcesClient.Providers.Get("Microsoft.KeyVault");
var location = provider.ResourceTypes.First(resType => resType.ResourceType.Contains("vaults")).Locations.FirstOrDefault();
return location?.ToLowerInvariant().Replace(" ", "");
// no op
}

private void CreateVault(KeyVaultManagementClient mgmtClient, string location, string tenantId)
{
mgmtClient.Vaults.CreateOrUpdate(
ResourceGroupName,
PreCreatedVault,
new VaultCreateOrUpdateParameters
{
Location = location,
Tags = new Dictionary<string, string> { { TagName, TagValue } },
Properties = new VaultProperties
{
EnabledForDeployment = false,
Sku = new Sku { Name = SkuName.Premium },
TenantId = Guid.Parse(tenantId),
VaultUri = "",
AccessPolicies = new AccessPolicyEntry[]{ }
}
});
}

public void ResetPreCreatedVault()
{
if (_mode == HttpRecorderMode.Record)
{
using (MockContext context = MockContext.Start(new StackTrace().GetFrame(1).GetMethod().ReflectedType?.ToString(), new StackTrace().GetFrame(1).GetMethod().Name))
{
var mgmtClient = context.GetServiceClient<KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
var tenantId = Guid.Parse(TestEnvironmentFactory.GetTestEnvironment().Tenant);

var policies = new AccessPolicyEntry[] { };

mgmtClient.Vaults.CreateOrUpdate(
ResourceGroupName,
PreCreatedVault,
new VaultCreateOrUpdateParameters
{
Location = Location,
Tags = new Dictionary<string, string> { { TagName, TagValue } },
Properties = new VaultProperties
{
EnabledForDeployment = false,
Sku = new Sku { Name = SkuName.Premium },
TenantId = tenantId,
VaultUri = "",
AccessPolicies = policies
}
});
}
}
}
public void Dispose()
{
Dispose(false);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_mode == HttpRecorderMode.Record && _initialized)
{
using (MockContext context = MockContext.Start(new StackTrace().GetFrame(1).GetMethod().ReflectedType?.ToString(), new StackTrace().GetFrame(1).GetMethod().Name))
{
var resourcesClient = context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
resourcesClient.ResourceGroups.Delete(ResourceGroupName);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -693,20 +693,32 @@ function Test-ModifyAccessPolicyEnabledForDiskEncryption {
}

function Test-ModifyAccessPolicyNegativeCases {
Param($existingVaultName, $rgName, $objId)
$objId = "" # INTENTIONAL
$rgName = getAssetName
$vaultName = getAssetName
$rgLocation = Get-Location "Microsoft.Resources" "resourceGroups" "West US"
$vaultLocation = Get-Location "Microsoft.KeyVault" "vault" "West US"
New-AzResourceGroup -Name $rgName -Location $rgLocation

# random string in perms
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -ObjectId $objId -PermissionsToSecrets blah, get }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -ObjectId $objId -PermissionsToCertificates blah, get }

# invalid set of params
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName }
Assert-Throws { Remove-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName }
Assert-Throws { Remove-AzKeyVaultAccessPolicy -VaultName $existingVaultName }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -UserPrincipalName $objId }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -SPN $objId }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -ObjectId $objId }
try {
New-AzKeyVault -Name $vaultName -ResourceGroupName $rgName -Location $vaultLocation

# random string in perms
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -ObjectId $objId -PermissionsToSecrets blah, get }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -ObjectId $objId -PermissionsToCertificates blah, get }

# invalid set of params
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName }
Assert-Throws { Remove-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName }
Assert-Throws { Remove-AzKeyVaultAccessPolicy -VaultName $existingVaultName }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -UserPrincipalName $objId }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -SPN $objId }
Assert-Throws { Set-AzKeyVaultAccessPolicy -VaultName $existingVaultName -ResourceGroupName $rgName -ObjectId $objId }
}
finally {
Remove-AzResourceGroup -Name $rgName -Force
}
}

function Test-RemoveNonExistentAccessPolicyDoesNotThrow {
Expand Down
Loading

0 comments on commit b964fcd

Please sign in to comment.