Skip to content

Commit

Permalink
PSResourceRepository: Add property Reasons (#402)
Browse files Browse the repository at this point in the history
* PSResourceRepository: Fix property `Reasons`

* Fix/reasons psrepository integ test and unit tests (#3)

* Fix unit test for PowerShell and Windows PowerShell

* Remove Reasons class

* Fix correct type for Reasons

* Fix review comments

* Fix correct type

* Fix integration test

* Fix review comment
  • Loading branch information
johlju authored Mar 10, 2023
1 parent ff57072 commit fcff2e0
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The class-based resources are now re-using the module DscResource.Base - Fixes [Issue #404](https://github.com/dsccommunity/ComputerManagementDsc/issues/404).
- Removed the file `source/build.psd1` as it is no longer required for the
build pipeline.
- PSResourceRepository
- The resource now supports the read-only property `Reasons` that the
compliance part (audit via Azure Policy) of Azure AutoManage Machine
Configuration uses.

## [9.0.0] - 2023-02-22

Expand Down
21 changes: 21 additions & 0 deletions source/Classes/001.CMReason.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<#
.SYNOPSIS
The reason a property of a DSC resource is not in desired state.
.DESCRIPTION
A DSC resource can have a read-only property `Reasons` that the compliance
part (audit via Azure Policy) of Azure AutoManage Machine Configuration
uses. The property Reasons holds an array of CMReason. Each CMReason
explains why a property of a DSC resource is not in desired state.
#>

class CMReason
{
[DscProperty()]
[System.String]
$Code

[DscProperty()]
[System.String]
$Phrase
}
15 changes: 11 additions & 4 deletions source/Classes/020.PSResourceRepository.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
and PackageManagementProvider may not be used in conjunction with Default.
When the Default parameter is used, properties are not enforced when PSGallery properties are changed outside of Dsc.
.PARAMETER Reasons
Returns the reason a property is not in desired state.
.EXAMPLE
Invoke-DscResource -ModuleName ComputerManagementDsc -Name PSResourceRepository -Method Get -Property @{
Name = 'PSTestRepository'
Expand Down Expand Up @@ -96,7 +99,7 @@ class PSResourceRepository : ResourceBase
$Proxy

[DscProperty()]
[pscredential]
[PSCredential]
$ProxyCredential

[DscProperty()]
Expand All @@ -112,6 +115,10 @@ class PSResourceRepository : ResourceBase
[Nullable[System.Boolean]]
$Default

[DscProperty(NotConfigurable)]
[CMReason[]]
$Reasons

# Passing the module's base directory to the base constructor so it finds localization files.
PSResourceRepository () : base ($PSScriptRoot)
{
Expand All @@ -124,12 +131,12 @@ class PSResourceRepository : ResourceBase

[PSResourceRepository] Get()
{
return ([ResourceBase]$this).Get()
return ([ResourceBase] $this).Get()
}

[void] Set()
{
([ResourceBase]$this).Set()
([ResourceBase] $this).Set()
}

[Boolean] Test()
Expand Down Expand Up @@ -226,7 +233,7 @@ class PSResourceRepository : ResourceBase
$returnValue.PublishLocation = $repository.PublishLocation
$returnValue.ScriptPublishLocation = $repository.ScriptPublishLocation
$returnValue.Proxy = $repository.Proxy
$returnValue.ProxyCredential = $repository.ProxyCredental
$returnValue.ProxyCredential = $repository.ProxyCredential
$returnValue.InstallationPolicy = $repository.InstallationPolicy
$returnValue.PackageManagementProvider = $repository.PackageManagementProvider
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ try
$resourceCurrentState.PackageManagementProvider | Should -Be 'NuGet'
$resourceCurrentState.InstallationPolicy | Should -Be 'Untrusted'

# Read-only properties
$resourceCurrentState.Reasons | Should -BeNullOrEmpty
}

It 'Should return $true when Test-DscConfiguration is run' {
Expand Down Expand Up @@ -152,6 +154,9 @@ try

# Defaulted properties
$resourceCurrentState.Ensure | Should -Be $shouldBeData.Ensure

# Read-only properties
$resourceCurrentState.Reasons | Should -BeNullOrEmpty
}

It 'Should return $true when Test-DscConfiguration is run' {
Expand Down Expand Up @@ -218,6 +223,9 @@ try

# Ensure will be Absent
$resourceCurrentState.Ensure | Should -Be 'Absent'

# Read-only properties
$resourceCurrentState.Reasons | Should -BeNullOrEmpty
}

It 'Should return $true when Test-DscConfiguration is run' {
Expand All @@ -226,7 +234,72 @@ try
}

Wait-ForIdleLcm -Clear
}

Context 'When using Invoke-DscResource' {
BeforeAll {
$script:mockInvokeDscResourceParameters = @{
ModuleName = $script:dscModuleName
Name = $script:dscResourceFriendlyName
Verbose = $true
}
}

BeforeEach {
Wait-ForIdleLcm -Clear
}

Context 'When the configuration is not in desired state' {
Context 'When calling method Get()' {
It 'Should not throw and return the correct values for each property' {
{
$script:resourceCurrentState = Invoke-DscResource @mockInvokeDscResourceParameters -Method 'Get' -Property @{
Name = 'PSTestGallery'
Ensure = 'Present'
SourceLocation = 'https://www.nuget.org/api/v2'
PublishLocation = 'https://www.nuget.org/api/v2/package/'
ScriptSourceLocation = 'https://www.nuget.org/api/v2/items/psscript/'
ScriptPublishLocation = 'https://www.nuget.org/api/v2/package/'
InstallationPolicy = 'Trusted'
PackageManagementProvider = 'NuGet'
}
} | Should -Not -Throw

$resourceCurrentState.Name | Should -Be 'PSTestGallery'
$resourceCurrentState.Ensure | Should -Be 'Absent'
$resourceCurrentState.PackageManagementProvider | Should -BeNullOrEmpty
$resourceCurrentState.Proxy | Should -BeNullOrEmpty
$resourceCurrentState.ProxyCredential | Should -BeNullOrEmpty
$resourceCurrentState.PublishLocation | Should -BeNullOrEmpty
$resourceCurrentState.ScriptPublishLocation | Should -BeNullOrEmpty
$resourceCurrentState.ScriptSourceLocation | Should -BeNullOrEmpty
$resourceCurrentState.SourceLocation | Should -BeNullOrEmpty

$resourceCurrentState.Reasons | Should -HaveCount 7

$resourceCurrentState.Reasons.Code | Should -Contain 'PSResourceRepository:PSResourceRepository:ScriptPublishLocation'
$resourceCurrentState.Reasons.Phrase | Should -Contain 'The property ScriptPublishLocation should be "https://www.nuget.org/api/v2/package/", but was ""'

$resourceCurrentState.Reasons.Code | Should -Contain 'PSResourceRepository:PSResourceRepository:InstallationPolicy'
$resourceCurrentState.Reasons.Phrase | Should -Contain 'The property InstallationPolicy should be "Trusted", but was ""'

$resourceCurrentState.Reasons.Code | Should -Contain 'PSResourceRepository:PSResourceRepository:Ensure'
$resourceCurrentState.Reasons.Phrase | Should -Contain 'The property Ensure should be "Present", but was "Absent"'

$resourceCurrentState.Reasons.Code | Should -Contain 'PSResourceRepository:PSResourceRepository:PackageManagementProvider'
$resourceCurrentState.Reasons.Phrase | Should -Contain 'The property PackageManagementProvider should be "NuGet", but was ""'

$resourceCurrentState.Reasons.Code | Should -Contain 'PSResourceRepository:PSResourceRepository:ScriptSourceLocation'
$resourceCurrentState.Reasons.Phrase | Should -Contain 'The property ScriptSourceLocation should be "https://www.nuget.org/api/v2/items/psscript/", but was ""'

$resourceCurrentState.Reasons.Code | Should -Contain 'PSResourceRepository:PSResourceRepository:PublishLocation'
$resourceCurrentState.Reasons.Phrase | Should -Contain 'The property PublishLocation should be "https://www.nuget.org/api/v2/package/", but was ""'

$resourceCurrentState.Reasons.Code | Should -Contain 'PSResourceRepository:PSResourceRepository:SourceLocation'
$resourceCurrentState.Reasons.Phrase | Should -Contain 'The property SourceLocation should be "https://www.nuget.org/api/v2", but was ""'
}
}
}
}
#endregion
}
Expand Down
81 changes: 81 additions & 0 deletions tests/Unit/Classes/CMReason.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<#
.SYNOPSIS
Unit test for PSResourceRepository DSC resource.
#>

# Suppressing this rule because Script Analyzer does not understand Pester's syntax.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param ()

try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null
}

# If the dependencies has not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
}

try
{
$script:dscModuleName = 'ComputerManagementDsc'

Import-Module -Name $script:dscModuleName

$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName
$PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName
$PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName

Describe 'CMReason' -Tag 'CMReason' {
Context 'When instantiating the class' {
It 'Should not throw an error' {
$script:mockCMReasonInstance = InModuleScope -ScriptBlock {
[CMReason]::new()
}
}

It 'Should be of the correct type' {
$mockCMReasonInstance | Should -Not -BeNullOrEmpty
$mockCMReasonInstance.GetType().Name | Should -Be 'CMReason'
}
}

Context 'When setting and reading values' {
It 'Should be able to set value in instance' {
$script:mockCMReasonInstance = InModuleScope -ScriptBlock {
$CMReasonInstance = [CMReason]::new()

$CMReasonInstance.Code = 'SqlAudit:SqlAudit:Ensure'
$CMReasonInstance.Phrase = 'The property Ensure should be "Present", but was "Absent"'

return $CMReasonInstance
}
}

It 'Should be able read the values from instance' {
$mockCMReasonInstance.Code | Should -Be 'SqlAudit:SqlAudit:Ensure'
$mockCMReasonInstance.Phrase = 'The property Ensure should be "Present", but was "Absent"'
}
}
}
}
finally
{
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
$PSDefaultParameterValues.Remove('Mock:ModuleName')
$PSDefaultParameterValues.Remove('Should:ModuleName')

# Unload the module being tested so that it doesn't impact any other tests.
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force
}
Loading

0 comments on commit fcff2e0

Please sign in to comment.