Skip to content

Commit

Permalink
Fix for issue Azure#5747
Browse files Browse the repository at this point in the history
Add/update tests for Azure#7522 and Azure#5747
  • Loading branch information
mentat9 committed Jan 16, 2019
1 parent b37dc22 commit 89d9b9a
Show file tree
Hide file tree
Showing 11 changed files with 22,231 additions and 471 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -110,15 +111,35 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()

if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)))
{
var resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
ResponseWithContinuation<JObject[]> retVal;
return resource.TryConvertTo(out retVal) && retVal.Value != null
JObject resource;
try
{
resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
}
catch (ErrorResponseMessageException ex)
{
if (!ex.Message.StartsWith("PolicyDefinitionNotFound", StringComparison.OrdinalIgnoreCase))
{
throw;
}

resourceId = this.GetBuiltinResourceId();
resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
}

return resource.TryConvertTo(out ResponseWithContinuation<JObject[]> retVal) && retVal.Value != null
? retVal
: new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
}
Expand All @@ -141,5 +162,13 @@ private string GetResourceId()
{
return this.Id ?? this.MakePolicyDefinitionId(this.ManagementGroupName, this.SubscriptionId, this.Name);
}

/// <summary>
/// Gets the resource Id assuming the name is for a builtin
/// </summary>
private string GetBuiltinResourceId()
{
return $"/{Constants.Providers}/{Constants.MicrosoftAuthorizationPolicyDefinitionType}/{this.Name}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -110,15 +111,35 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()

if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)))
{
var resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
ResponseWithContinuation<JObject[]> retVal;
return resource.TryConvertTo(out retVal) && retVal.Value != null
JObject resource;
try
{
resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
}
catch (ErrorResponseMessageException ex)
{
if (!ex.Message.StartsWith("PolicySetDefinitionNotFound", StringComparison.OrdinalIgnoreCase))
{
throw;
}

resourceId = this.GetBuiltinResourceId();
resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
}

return resource.TryConvertTo(out ResponseWithContinuation<JObject[]> retVal) && retVal.Value != null
? retVal
: new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
}
Expand All @@ -141,5 +162,13 @@ private string GetResourceId()
{
return this.Id ?? this.MakePolicySetDefinitionId(this.ManagementGroupName, this.SubscriptionId, this.Name);
}

/// <summary>
/// Gets the resource Id assuming the name is for a builtin
/// </summary>
private string GetBuiltinResourceId()
{
return $"/{Constants.Providers}/{Constants.MicrosoftAuthorizationPolicySetDefinitionType}/{this.Name}";
}
}
}
14 changes: 14 additions & 0 deletions src/Resources/Resources.Test/ScenarioTests/PolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ public void TestPolicyDefinitionWithUri()
TestRunner.RunTestScript("Test-PolicyDefinitionWithUri");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetCmdletFilterParameter()
{
TestRunner.RunTestScript("Test-GetCmdletFilterParameter");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetBuiltinsByName()
{
TestRunner.RunTestScript("Test-GetBuiltinsByName");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetPolicyAssignmentParameters()
Expand Down
52 changes: 44 additions & 8 deletions src/Resources/Resources.Test/ScenarioTests/PolicyTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,6 @@ function Test-PolicyDefinitionCRUDAtManagementGroup
Assert-NotNull($actual.Properties.PolicyRule)
Assert-AreEqual $expected.Properties.Mode $actual.Properties.Mode

# make sure it can't be retrieved at default subscription level
Assert-ThrowsContains { Get-AzureRMPolicyDefinition -Name $policyName } "PolicyDefinitionNotFound : The policy definition '$policyName' could not be found."

# update the same policy definition, get it back and validate the new properties
$actual = Set-AzureRMPolicyDefinition -Name $policyName -ManagementGroupName $managementGroup -DisplayName testDisplay -Description $updatedDescription -Policy ".\SamplePolicyDefinition.json" -Metadata $metadata
$expected = Get-AzureRMPolicyDefinition -Name $policyName -ManagementGroupName $managementGroup
Expand Down Expand Up @@ -741,9 +738,6 @@ function Test-PolicySetDefinitionCRUDAtManagementGroup
Assert-AreEqual $expected.PolicySetDefinitionId $actual.PolicySetDefinitionId
Assert-NotNull($actual.Properties.PolicyDefinitions)

# make sure it can't be retrieved at default subscription level
Assert-ThrowsContains { Get-AzureRMPolicySetDefinition -Name $policySetDefName } "PolicySetDefinitionNotFound : The policy set definition '$policySetDefName' could not be found."

# update the policy set definition, get it back and validate
$expected = Set-AzureRMPolicySetDefinition -Name $policySetDefName -ManagementGroupName $managementGroup -DisplayName testDisplay -Description $updatedDescription
$actual = Get-AzureRMPolicySetDefinition -Name $policySetDefName -ManagementGroupName $managementGroup
Expand Down Expand Up @@ -808,6 +802,48 @@ function Test-PolicySetDefinitionCRUDAtSubscription
Assert-AreEqual True $remove
}

function Test-GetCmdletFilterParameter
{
# policy definitions
$builtins = Get-AzureRmPolicyDefinition -Builtin
$builtins | %{ Assert-AreEqual $_.Properties.PolicyType "Builtin" }

$custom = Get-AzureRmPolicyDefinition -Custom
$custom | %{ Assert-AreEqual $_.Properties.PolicyType "Custom" }

$all = Get-AzureRmPolicyDefinition
Assert-AreEqual ($builtins.Count + $custom.Count) $all.Count

# policy set definitions
$builtins = Get-AzureRmPolicySetDefinition -Builtin
$builtins | %{ Assert-AreEqual $_.Properties.PolicyType "Builtin" }

$custom = Get-AzureRmPolicySetDefinition -Custom
$custom | %{ Assert-AreEqual $_.Properties.PolicyType "Custom" }

$all = Get-AzureRmPolicySetDefinition
Assert-AreEqual ($builtins.Count + $custom.Count) $all.Count
}

function Test-GetBuiltinsByName
{
# policy definitions
$builtins = Get-AzureRmPolicyDefinition -Builtin
foreach ($builtin in $builtins)
{
$definition = Get-AzureRmPolicyDefinition -Name $builtin.Name
Assert-AreEqual $builtin.ResourceId $definition.ResourceId
}

# policy set definitions
$builtins = Get-AzureRmPolicySetDefinition -Builtin
foreach ($builtin in $builtins)
{
$setDefinition = Get-AzureRmPolicySetDefinition -Name $builtin.Name
Assert-AreEqual $builtin.ResourceId $setDefinition.ResourceId
}
}

<#
The following section contains tests for each cmdlet that validate as many combinations of
parameters as possible/reasonable. Tests for all combinations of parameters are present here
Expand Down Expand Up @@ -1015,7 +1051,7 @@ function Test-GetPolicyDefinitionParameters
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Name $someName -Id $someId -Custom } $parameterSetError

# validate remaining parameter combinations starting with -Id
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Id $goodId } $policyDefinitionNotFound
$ok = Get-AzureRmPolicyDefinition -Id $goodId
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Id $goodId -ManagementGroupName $someManagementGroup } $parameterSetError
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Id $goodId -SubscriptionId $subscriptionId } $parameterSetError
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Id $goodId -BuiltIn } $parameterSetError
Expand Down Expand Up @@ -1159,7 +1195,7 @@ function Test-GetPolicySetDefinitionParameters
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Name $someName -Id $someId -Custom } $parameterSetError

# validate remaining parameter combinations starting with -Id
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Id $goodId } $policySetDefinitionNotFound
$ok = Get-AzureRmPolicySetDefinition -Id $goodId
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Id $goodId -ManagementGroupName $someManagementGroup } $parameterSetError
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Id $goodId -SubscriptionId $subscriptionId } $parameterSetError
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Id $goodId -BuiltIn } $parameterSetError
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit 89d9b9a

Please sign in to comment.