-
Notifications
You must be signed in to change notification settings - Fork 73
Use JSON Schema defaults in synthetic test get_diff #1670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Steve Lee (SteveL-MSFT)
wants to merge
5
commits into
main
Choose a base branch
from
stevel-msft-synthetic-test-schema-default
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6e5b53
Use JSON Schema defaults in synthetic test get_diff
cf3c11c
Address PR feedback: restrict visibility and avoid redundant serializ…
0dad5dd
Add FirewallRuleList Pester tests for schema default fix (#1666)
1d20bf7
Fix CI: skip firewall schema default tests when NetSecurity module un…
642e0e0
Apply suggestions from code review
SteveL-MSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,54 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| Describe 'Synthetic test uses schema defaults' { | ||
| It 'Property matching schema default is not reported as differing' { | ||
| $out = '{"name":"test","enabled":true}' | dsc resource test -r Test/SchemaDefault -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.inDesiredState | Should -Be $true | ||
| $out.differingProperties | Should -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'Property differing from schema default is reported as differing' { | ||
| $out = '{"name":"test","enabled":false}' | dsc resource test -r Test/SchemaDefault -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.inDesiredState | Should -Be $false | ||
| $out.differingProperties | Should -Contain 'enabled' | ||
| } | ||
|
|
||
| It 'Integer property matching schema default is not reported as differing' { | ||
| $out = '{"name":"test","count":5}' | dsc resource test -r Test/SchemaDefault -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.inDesiredState | Should -Be $true | ||
| $out.differingProperties | Should -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'Integer property differing from schema default is reported as differing' { | ||
| $out = '{"name":"test","count":10}' | dsc resource test -r Test/SchemaDefault -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.inDesiredState | Should -Be $false | ||
| $out.differingProperties | Should -Contain 'count' | ||
| } | ||
|
|
||
| It 'Multiple properties matching schema defaults are not reported as differing' { | ||
| $out = '{"name":"test","enabled":true,"count":5}' | dsc resource test -r Test/SchemaDefault -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.inDesiredState | Should -Be $true | ||
| $out.differingProperties | Should -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'Mix of matching and non-matching defaults reports only non-matching' { | ||
| $out = '{"name":"test","enabled":true,"count":10}' | dsc resource test -r Test/SchemaDefault -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.inDesiredState | Should -Be $false | ||
| $out.differingProperties | Should -Contain 'count' | ||
| $out.differingProperties | Should -Not -Contain 'enabled' | ||
| } | ||
|
|
||
| It 'Property present in both expected and actual is compared normally' { | ||
| $out = '{"name":"test"}' | dsc resource test -r Test/SchemaDefault -f - | ConvertFrom-Json | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $out.inDesiredState | Should -Be $true | ||
| $out.differingProperties | Should -BeNullOrEmpty | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
105 changes: 105 additions & 0 deletions
105
resources/windows_firewall/tests/windows_firewall_schema_default.tests.ps1
This file contains hidden or 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,105 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| Describe 'Microsoft.Windows/FirewallRuleList - synthetic test with schema defaults' -Skip:(!$canRunFirewallTests) { | ||
| BeforeDiscovery { | ||
| $canRunFirewallTests = $IsWindows -and | ||
| (Get-Command Get-NetFirewallRule -ErrorAction Ignore) -and | ||
| ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( | ||
| [Security.Principal.WindowsBuiltInRole]::Administrator) | ||
| } | ||
|
|
||
| BeforeAll { | ||
| $resourceType = 'Microsoft.Windows/FirewallRuleList' | ||
| $testRuleName = 'DSC-WindowsFirewall-SchemaDefault-Test' | ||
|
|
||
| # Ensure a known rule exists for testing | ||
| $existing = Get-NetFirewallRule -Name $testRuleName -ErrorAction Ignore | ||
| if (-not $existing) { | ||
| New-NetFirewallRule -Name $testRuleName -DisplayName $testRuleName ` | ||
| -Direction Inbound -Action Allow -Protocol TCP -LocalPort 32921 ` | ||
| -Enabled True -PolicyStore PersistentStore | Out-Null | ||
| } | ||
| } | ||
|
|
||
| AfterAll { | ||
| Remove-NetFirewallRule -Name $testRuleName -ErrorAction Ignore | ||
| } | ||
|
|
||
| It 'unspecifiedRulesAction set to default "ignore" does not report as differing' { | ||
| $json = @{ | ||
| unspecifiedRulesAction = 'ignore' | ||
| rules = @(@{ | ||
| name = $testRuleName | ||
| direction = 'Inbound' | ||
| action = 'Allow' | ||
| protocol = 6 | ||
| localPorts = '32921' | ||
| enabled = $true | ||
| }) | ||
| } | ConvertTo-Json -Compress -Depth 5 | ||
| $out = $json | dsc resource test -r $resourceType -f - 2>$testdrive/error.log | ||
| $LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw $testdrive/error.log) | ||
|
|
||
| $result = $out | ConvertFrom-Json | ||
| $result.inDesiredState | Should -Be $true | ||
| $result.differingProperties | Should -Not -Contain 'unspecifiedRulesAction' | ||
| } | ||
|
|
||
| It 'unspecifiedRulesAction omitted does not report as differing' { | ||
| $json = @{ | ||
| rules = @(@{ | ||
| name = $testRuleName | ||
| direction = 'Inbound' | ||
| action = 'Allow' | ||
| protocol = 6 | ||
| localPorts = '32921' | ||
| enabled = $true | ||
| }) | ||
| } | ConvertTo-Json -Compress -Depth 5 | ||
| $out = $json | dsc resource test -r $resourceType -f - 2>$testdrive/error.log | ||
| $LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw $testdrive/error.log) | ||
|
|
||
| $result = $out | ConvertFrom-Json | ||
| $result.inDesiredState | Should -Be $true | ||
| $result.differingProperties | Should -Not -Contain 'unspecifiedRulesAction' | ||
| } | ||
|
|
||
| It 'non-default unspecifiedRulesAction "disable" is reported as differing' { | ||
| $json = @{ | ||
| unspecifiedRulesAction = 'disable' | ||
| rules = @(@{ | ||
| name = $testRuleName | ||
| direction = 'Inbound' | ||
| action = 'Allow' | ||
| protocol = 6 | ||
| localPorts = '32921' | ||
| enabled = $true | ||
| }) | ||
| } | ConvertTo-Json -Compress -Depth 5 | ||
| $out = $json | dsc resource test -r $resourceType -f - 2>$testdrive/error.log | ||
| $LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw $testdrive/error.log) | ||
|
|
||
| $result = $out | ConvertFrom-Json | ||
| $result.differingProperties | Should -Contain 'unspecifiedRulesAction' | ||
| } | ||
|
|
||
| It 'non-default unspecifiedRulesAction "remove" is reported as differing' { | ||
| $json = @{ | ||
| unspecifiedRulesAction = 'remove' | ||
| rules = @(@{ | ||
| name = $testRuleName | ||
| direction = 'Inbound' | ||
| action = 'Allow' | ||
| protocol = 6 | ||
| localPorts = '32921' | ||
| enabled = $true | ||
| }) | ||
| } | ConvertTo-Json -Compress -Depth 5 | ||
| $out = $json | dsc resource test -r $resourceType -f - 2>$testdrive/error.log | ||
| $LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw $testdrive/error.log) | ||
|
|
||
| $result = $out | ConvertFrom-Json | ||
| $result.differingProperties | Should -Contain 'unspecifiedRulesAction' | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Out of scope for this PR, but I think this fits better as an extension method in
dsc-lib-jsonschema- probably something like the following signature:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree we can improve this outside of this PR