From 206f10a866d3d2139023a1bf347570dfec7c89b3 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sun, 26 Feb 2023 16:03:34 +0100 Subject: [PATCH 1/4] SqlServerDsc: Change type for property `Reasons` --- CHANGELOG.md | 6 ++++++ source/Classes/011.SqlResourceBase.ps1 | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81b31a60f..3440190e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -143,6 +143,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 output a warning to install any of the dependent modules. - Add empty constructor to classes to be able to use Pester's new code coverage method. See more information can be found in [pester/Pester#2306](https://github.com/pester/Pester/issues/2306). + - The type of the property `Reasons` was changed in the class-based resources. + This resolves a problem when using two DSC resource modules that was + using the same class-type for the property `Reasons`. Resolves the issues + [issue #1831](https://github.com/dsccommunity/SqlServerDsc/issues/1831), + [issue #1832](https://github.com/dsccommunity/SqlServerDsc/issues/1832), + and [issue #1833](https://github.com/dsccommunity/SqlServerDsc/issues/1833). - `Install-SqlServerDsc` - No longer throws an exception when parameter `AgtSvcAccount` is not specified. - SqlAgReplica diff --git a/source/Classes/011.SqlResourceBase.ps1 b/source/Classes/011.SqlResourceBase.ps1 index 369757d46..695ce5e3d 100644 --- a/source/Classes/011.SqlResourceBase.ps1 +++ b/source/Classes/011.SqlResourceBase.ps1 @@ -43,7 +43,7 @@ class SqlResourceBase : ResourceBase $Credential [DscProperty(NotConfigurable)] - [Reason[]] + [System.Collections.Hashtable[]] $Reasons # Passing the module's base directory to the base constructor. From 2f84e7c3545c802efe273b98c025c66ad4b2d22d Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Mon, 27 Feb 2023 17:16:14 +0100 Subject: [PATCH 2/4] Fix audit --- source/Classes/020.SqlAudit.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/Classes/020.SqlAudit.ps1 b/source/Classes/020.SqlAudit.ps1 index 48c7790ea..25b5afd4e 100644 --- a/source/Classes/020.SqlAudit.ps1 +++ b/source/Classes/020.SqlAudit.ps1 @@ -269,7 +269,7 @@ class SqlAudit : SqlResourceBase if ($auditObject.DestinationType -in @('ApplicationLog', 'SecurityLog')) { - $currentState.LogType = $auditObject.DestinationType + $currentState.LogType = $auditObject.DestinationType.ToString() } if ($auditObject.FilePath) From 803cb4f36a21a0e71dfedc1a9366dc88fa7cd9f5 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Tue, 28 Feb 2023 11:10:50 +0100 Subject: [PATCH 3/4] Fix a problem using hashtable --- CHANGELOG.md | 2 ++ source/Classes/001.SqlReason.ps1 | 21 +++++++++++++++++++++ source/Classes/011.SqlResourceBase.ps1 | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 source/Classes/001.SqlReason.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 3440190e8..86df8f67f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Set-SqlDscStartupParameter` - Added class `StartupParameters` which can parse the startup parameters of a manged computer service object. + - Added class `SqlReason` to be used as the type of the DSC property `Reasons` + for class-based resources. - New GitHub issue templates for proposing new public commands, proposing an enhancement to an existing command, or having a problem with an existing command. diff --git a/source/Classes/001.SqlReason.ps1 b/source/Classes/001.SqlReason.ps1 new file mode 100644 index 000000000..cbe97fbd6 --- /dev/null +++ b/source/Classes/001.SqlReason.ps1 @@ -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 SqlReason. Each SqlReason + explains why a property of a DSC resource is not in desired state. +#> + +class SqlReason +{ + [DscProperty()] + [System.String] + $Code + + [DscProperty()] + [System.String] + $Phrase +} diff --git a/source/Classes/011.SqlResourceBase.ps1 b/source/Classes/011.SqlResourceBase.ps1 index 695ce5e3d..bcd589217 100644 --- a/source/Classes/011.SqlResourceBase.ps1 +++ b/source/Classes/011.SqlResourceBase.ps1 @@ -43,7 +43,7 @@ class SqlResourceBase : ResourceBase $Credential [DscProperty(NotConfigurable)] - [System.Collections.Hashtable[]] + [SqlReason[]] $Reasons # Passing the module's base directory to the base constructor. From c6b7d8db4155a7eb23cf0e634db4c27fecc93362 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Tue, 28 Feb 2023 12:41:51 +0100 Subject: [PATCH 4/4] Fix unit test --- CHANGELOG.md | 2 + tests/Unit/Classes/SqlReason.Tests.ps1 | 80 ++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tests/Unit/Classes/SqlReason.Tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 86df8f67f..3afa24932 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -238,6 +238,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 current state ([issue #1834](https://github.com/dsccommunity/SqlServerDsc/issues/1834)). - `Set-TargetResource` was updated to correctly include or exclude a single flag ([issue #1834](https://github.com/dsccommunity/SqlServerDsc/issues/1834)). +- SqlAudit + - Return the correct type for parameter `LogType` when calling method `Get()`. ## [16.0.0] - 2022-09-09 diff --git a/tests/Unit/Classes/SqlReason.Tests.ps1 b/tests/Unit/Classes/SqlReason.Tests.ps1 new file mode 100644 index 000000000..d0c9ca061 --- /dev/null +++ b/tests/Unit/Classes/SqlReason.Tests.ps1 @@ -0,0 +1,80 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +param () + +BeforeDiscovery { + 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.' + } +} + +BeforeAll { + $script:dscModuleName = 'SqlServerDsc' + + $env:SqlServerDscCI = $true + + Import-Module -Name $script:dscModuleName + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName +} + +AfterAll { + $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 + + Remove-Item -Path 'env:SqlServerDscCI' +} + +Describe 'SqlReason' -Tag 'SqlReason' { + Context 'When instantiating the class' { + It 'Should not throw an error' { + $script:mockSqlReasonInstance = InModuleScope -ScriptBlock { + [SqlReason]::new() + } + } + + It 'Should be of the correct type' { + $mockSqlReasonInstance | Should -Not -BeNullOrEmpty + $mockSqlReasonInstance.GetType().Name | Should -Be 'SqlReason' + } + } + + Context 'When setting an reading values' { + It 'Should be able to set value in instance' { + $script:mockSqlReasonInstance = InModuleScope -ScriptBlock { + $sqlReasonInstance = [SqlReason]::new() + + $sqlReasonInstance.Code = 'SqlAudit:SqlAudit:Ensure' + $sqlReasonInstance.Phrase = 'The property Ensure should be "Present", but was "Absent"' + + return $sqlReasonInstance + } + } + + It 'Should be able read the values from instance' { + $mockSqlReasonInstance.Code | Should -Be 'SqlAudit:SqlAudit:Ensure' + $mockSqlReasonInstance.Phrase = 'The property Ensure should be "Present", but was "Absent"' + } + } +}