1
1
# setup the Pester environment for policy backcompat tests
2
2
. (Join-Path $PSScriptRoot ' Common.ps1' ) ' Backcompat-PolicyAssignmentCRUD'
3
3
4
- Describe ' Backcompat-PolicyAssignmentCRUD' - Tag ' LiveOnly ' {
4
+ Describe ' Backcompat-PolicyAssignmentCRUD' {
5
5
6
6
BeforeAll {
7
7
# setup
8
- $rgname = Get-ResourceGroupName
8
+ $rgname = $env .rgname
9
9
$policyName = Get-ResourceName
10
10
11
11
$policySetDefName = Get-ResourceName
12
12
$policyDefName1 = Get-ResourceName
13
13
$policyDefName2 = Get-ResourceName
14
14
15
15
# make a new resource group and policy definition
16
- $rg = New-ResourceGroup - Name $rgname - Location " west us"
17
16
$policyDefinition1 = New-AzPolicyDefinition - Name $policyDefName1 - Policy " $testFilesFolder \SamplePolicyDefinition.json" - Description $description - BackwardCompatible
18
17
$policyDefinition2 = New-AzPolicyDefinition - Name $policyDefName2 - Policy " $testFilesFolder \SamplePolicyDefinition.json" - Description $description - BackwardCompatible
19
18
$policySetString = " [{"" policyDefinitionId"" :"" " + $policyDefinition1.PolicyDefinitionId + " "" }, {"" policyDefinitionId"" :"" " + $policyDefinition2.PolicyDefinitionId + " "" }]"
@@ -29,13 +28,13 @@ Describe 'Backcompat-PolicyAssignmentCRUD' -Tag 'LiveOnly' {
29
28
It ' make a policy assignment at RG scope' {
30
29
{
31
30
# assign the policy definition to the resource group, get the assignment back and validate
32
- $actual = New-AzPolicyAssignment - Name $test1 - PolicySetDefinition $policySet - Scope $rg .ResourceId - Description $description - NonComplianceMessage $nonComplianceMessages - BackwardCompatible
33
- $expected = Get-AzPolicyAssignment - Name $test1 - Scope $rg .ResourceId - BackwardCompatible
31
+ $actual = New-AzPolicyAssignment - Name $test1 - PolicySetDefinition $policySet - Scope $env .scope - Description $description - NonComplianceMessage $nonComplianceMessages - BackwardCompatible
32
+ $expected = Get-AzPolicyAssignment - Name $test1 - Scope $env .scope - BackwardCompatible
34
33
Assert-AreEqual $expected.Name $actual.Name
35
34
Assert-AreEqual Microsoft.Authorization/ policyAssignments $actual.ResourceType
36
35
Assert-AreEqual $expected.PolicyAssignmentId $actual.PolicyAssignmentId
37
36
Assert-AreEqual $expected.Properties.PolicyDefinitionId $policySet.ResourceId
38
- Assert-AreEqual $expected.Properties.Scope $rg .ResourceId
37
+ Assert-AreEqual $expected.Properties.Scope $env .scope
39
38
Assert-AreEqual 1 $expected.Properties.NonComplianceMessages.Length
40
39
Assert-AreEqual " General message" $expected.Properties.NonComplianceMessages [0 ].Message
41
40
} | Should -Not - Throw
@@ -44,7 +43,7 @@ Describe 'Backcompat-PolicyAssignmentCRUD' -Tag 'LiveOnly' {
44
43
It ' get policy assignment by Id' {
45
44
{
46
45
# get first assignment back by name
47
- $actual = Get-AzPolicyAssignment - Name $test1 - Scope $rg .ResourceId - BackwardCompatible
46
+ $actual = Get-AzPolicyAssignment - Name $test1 - Scope $env .scope - BackwardCompatible
48
47
49
48
# get it again by id and validate
50
49
$actualId = Get-AzPolicyAssignment - Id $actual.ResourceId - BackwardCompatible
@@ -57,7 +56,7 @@ Describe 'Backcompat-PolicyAssignmentCRUD' -Tag 'LiveOnly' {
57
56
It ' make a policy assignment with multiple noncompliance messages' {
58
57
{
59
58
# get first assignment back by name
60
- $get = Get-AzPolicyAssignment - Name $test1 - Scope $rg .ResourceId - BackwardCompatible
59
+ $get = Get-AzPolicyAssignment - Name $test1 - Scope $env .scope - BackwardCompatible
61
60
62
61
$nonComplianceMessages = $nonComplianceMessages + @ (@ {
63
62
Message = " Specific message 1"
@@ -68,7 +67,7 @@ Describe 'Backcompat-PolicyAssignmentCRUD' -Tag 'LiveOnly' {
68
67
Assert-AreEqual 2 $nonComplianceMessages.Length
69
68
70
69
# create it again with two non-compliance messages
71
- $new = New-AzPolicyAssignment - Name $test1 - PolicySetDefinition $policySet - Scope $rg .ResourceId - Description $description - NonComplianceMessage $nonComplianceMessages - BackwardCompatible
70
+ $new = New-AzPolicyAssignment - Name $test1 - PolicySetDefinition $policySet - Scope $env .scope - Description $description - NonComplianceMessage $nonComplianceMessages - BackwardCompatible
72
71
Assert-AreEqual $get.ResourceId $new.ResourceId
73
72
74
73
# get it again by id and validate non-compliance messages
@@ -96,7 +95,7 @@ Describe 'Backcompat-PolicyAssignmentCRUD' -Tag 'LiveOnly' {
96
95
It ' update policy assignment to a single noncompliance message' {
97
96
{
98
97
# get first assignment back again
99
- $actual = Get-AzPolicyAssignment - Name $test1 - Scope $rg .ResourceId - BackwardCompatible
98
+ $actual = Get-AzPolicyAssignment - Name $test1 - Scope $env .scope - BackwardCompatible
100
99
101
100
$nonComplianceMessages = @ (@ { Message = " General non-compliance message" })
102
101
@@ -111,7 +110,7 @@ Describe 'Backcompat-PolicyAssignmentCRUD' -Tag 'LiveOnly' {
111
110
It ' update policy assignment back to a multiple noncompliance message' {
112
111
{
113
112
# get first assignment back again
114
- $actual = Get-AzPolicyAssignment - Name $test1 - Scope $rg .ResourceId - BackwardCompatible
113
+ $actual = Get-AzPolicyAssignment - Name $test1 - Scope $env .scope - BackwardCompatible
115
114
116
115
$nonComplianceMessages = @ (
117
116
@ {
@@ -135,8 +134,8 @@ Describe 'Backcompat-PolicyAssignmentCRUD' -Tag 'LiveOnly' {
135
134
It ' list policy assignments' {
136
135
{
137
136
# make another policy assignment, ensure both are present in resource group scope listing
138
- $expected = New-AzPolicyAssignment - Name $test2 - Scope $rg .ResourceId - PolicyDefinition $policyDefinition1 - Description $description - BackwardCompatible
139
- $list1 = Get-AzPolicyAssignment - Scope $rg .ResourceId - BackwardCompatible | ? { $_.Name -in @ ($test1 , $test2 ) }
137
+ $expected = New-AzPolicyAssignment - Name $test2 - Scope $env .scope - PolicyDefinition $policyDefinition1 - Description $description - BackwardCompatible
138
+ $list1 = Get-AzPolicyAssignment - Scope $env .scope - BackwardCompatible | ? { $_.Name -in @ ($test1 , $test2 ) }
140
139
Assert-AreEqual 2 $list1.Count
141
140
142
141
# ensure both are present in full listing
@@ -151,12 +150,11 @@ Describe 'Backcompat-PolicyAssignmentCRUD' -Tag 'LiveOnly' {
151
150
152
151
AfterAll {
153
152
# clean up
154
- $remove = Remove-AzPolicyAssignment - Name $test1 - Scope $rg .ResourceId - BackwardCompatible
155
- $remove = (Remove-AzPolicyAssignment - Name $test2 - Scope $rg .ResourceId - BackwardCompatible) -and $remove
153
+ $remove = Remove-AzPolicyAssignment - Name $test1 - Scope $env .scope - BackwardCompatible
154
+ $remove = (Remove-AzPolicyAssignment - Name $test2 - Scope $env .scope - BackwardCompatible) -and $remove
156
155
$remove = (Remove-AzPolicySetDefinition - Name $policySetDefName - Force - BackwardCompatible) -and $remove
157
156
$remove = (Remove-AzPolicyDefinition - Name $policyDefName1 - Force - BackwardCompatible) -and $remove
158
157
$remove = (Remove-AzPolicyDefinition - Name $policyDefName2 - Force - BackwardCompatible) -and $remove
159
- $remove = (Remove-ResourceGroup - Name $rgname ) -and $remove
160
158
161
159
Assert-AreEqual True $remove
162
160
0 commit comments