From d5384f9255c5798bcbc5a730b00408aca4c9a8e7 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 10 Mar 2025 20:21:46 +0100 Subject: [PATCH 01/35] Add DnsClientNrptGlobal and DnsClientNrptRule resources --- README.md | 2 + .../DSC_DnsClientNrptGlobal.data.psd1 | 16 + .../DSC_DnsClientNrptGlobal.psm1 | 222 +++++++ .../DSC_DnsClientNrptGlobal.schema.mof | 8 + .../DSC_DnsClientNrptGlobal/README.MD | 3 + .../DSC_DnsClientNrptGlobal.strings.psd1 | 10 + .../DSC_DnsClientNrptRule.psm1 | 557 ++++++++++++++++++ .../DSC_DnsClientNrptRule.schema.mof | 22 + .../DSC_DnsClientNrptRule/README.MD | 3 + .../en-US/DSC_DnsClientNrptRule.strings.psd1 | 17 + source/NetworkingDsc.psd1 | 2 + 11 files changed, 862 insertions(+) create mode 100644 source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.data.psd1 create mode 100644 source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 create mode 100644 source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.schema.mof create mode 100644 source/DSCResources/DSC_DnsClientNrptGlobal/README.MD create mode 100644 source/DSCResources/DSC_DnsClientNrptGlobal/en-US/DSC_DnsClientNrptGlobal.strings.psd1 create mode 100644 source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 create mode 100644 source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof create mode 100644 source/DSCResources/DSC_DnsClientNrptRule/README.MD create mode 100644 source/DSCResources/DSC_DnsClientNrptRule/en-US/DSC_DnsClientNrptRule.strings.psd1 diff --git a/README.md b/README.md index fb2129bd..74273b44 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ The **NetworkingDsc** module contains the following resources: - **DefaultGatewayAddress**: Sets a node's default gateway address. - **DnsClientGlobalSetting**: Configure DNS client global settings. +- **DnsClientNrptGlobal**: Configure the global Name Resolution Policy Table (NRPT) settings. +- **DnsClientNrptRule**: Sets a NRPT rule on a node. - **DnsConnectionSuffix**: Sets a node's network interface connection-specific DNS suffix. - **DnsServerAddress**: Sets a node's DNS server address(s). diff --git a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.data.psd1 b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.data.psd1 new file mode 100644 index 00000000..363e78e3 --- /dev/null +++ b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.data.psd1 @@ -0,0 +1,16 @@ +@{ + ParameterList = @( + @{ + Name = 'EnableDAForAllNetworks' + Type = 'String' + }, + @{ + Name = 'QueryPolicy' + Type = 'String' + }, + @{ + Name = 'SecureNameQueryFallback' + Type = 'String' + } + ) +} diff --git a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 new file mode 100644 index 00000000..752dbe65 --- /dev/null +++ b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 @@ -0,0 +1,222 @@ +$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules' + +# Import the Networking Common Modules +Import-Module -Name (Join-Path -Path $modulePath ` + -ChildPath (Join-Path -Path 'NetworkingDsc.Common' ` + -ChildPath 'NetworkingDsc.Common.psm1')) + +Import-Module -Name (Join-Path -Path $modulePath -ChildPath 'DscResource.Common') + +# Import Localization Strings +$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' + +<# + This is an array of all the parameters used by this resource. +#> +$resourceData = Import-LocalizedData ` + -BaseDirectory $PSScriptRoot ` + -FileName 'DSC_DnsClientNrptGlobal.data.psd1' + +# This must be a script parameter so that it is accessible +$script:parameterList = $resourceData.ParameterList + +<# + .SYNOPSIS + Returns the current DNS Client Nrpt Global Settings. + + .PARAMETER IsSingleInstance + Specifies the resource is a single instance, the value must be 'Yes'. +#> +function Get-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param + ( + [Parameter(Mandatory = $true)] + [ValidateSet('Yes')] + [System.String] + $IsSingleInstance + ) + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.GettingDnsClientNrptGlobalMessage) + ) -join '' ) + + # Get the current Dns Client Global Settings + $DnsClientNrptGlobal = Get-DnsClientNrptGlobal ` + -ErrorAction Stop + + # Generate the return object. + $returnValue = @{ + IsSingleInstance = 'Yes' + } + + foreach ($parameter in $script:parameterList) + { + $returnValue += @{ + $parameter.Name = $DnsClientNrptGlobal.$($parameter.name) + } + } # foreach + + return $returnValue +} # Get-TargetResource + +<# + .SYNOPSIS + Sets the DNS Client NRPT Global Settings. + + .PARAMETER IsSingleInstance + Specifies the resource is a single instance, the value must be 'Yes'. + + .PARAMETER EnableDAForAllNetworks + Specifies DirectAccess (DA) settings. + + .PARAMETER QueryPolicy. + Specifies the DNS client query policy. + + .PARAMETER SecureNameQueryFallback + Specifies the DNS client name resolution fallback policy. +#> +function Set-TargetResource +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [ValidateSet('Yes')] + [System.String] + $IsSingleInstance, + + [Parameter()] + [System.String] + $EnableDAForAllNetworks, + + [Parameter()] + [System.String] + $QueryPolicy, + + [Parameter()] + [System.String] + $SecureNameQueryFallback + ) + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.SettingDnsClientNrptGlobalMessage) + ) -join '' ) + + # Get the current Dns Client Nrpt Global Settings + $DnsClientNrptGlobal = Get-DnsClientNrptGlobal ` + -ErrorAction Stop + + # Generate a list of parameters that will need to be changed. + $changeParameters = @{} + + foreach ($parameter in $script:parameterList) + { + $parameterSourceValue = $DnsClientNrptGlobal.$($parameter.name) + $parameterNewValue = (Get-Variable -Name ($parameter.name)).Value + + if ($PSBoundParameters.ContainsKey($parameter.Name) ` + -and (Compare-Object -ReferenceObject $parameterSourceValue -DifferenceObject $parameterNewValue -SyncWindow 0)) + { + $changeParameters += @{ + $($parameter.name) = $parameterNewValue + } + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.DnsClientNrptGlobalUpdateParameterMessage) ` + -f $parameter.Name,($parameterNewValue -join ',') + ) -join '' ) + } # if + } # foreach + + if ($changeParameters.Count -gt 0) + { + # Update any parameters that were identified as different + $null = Set-DnsClientNrptGlobal ` + @ChangeParameters ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.DnsClientNrptGlobalUpdatedMessage) + ) -join '' ) + } # if +} # Set-TargetResource + +<# + .SYNOPSIS + Tests the state of DNS Client Nrpt Global Settings. + + .PARAMETER IsSingleInstance + Specifies the resource is a single instance, the value must be 'Yes'. + + .PARAMETER EnableDAForAllNetworks + Specifies DirectAccess (DA) settings. + + .PARAMETER QueryPolicy. + Specifies the DNS client query policy. + + .PARAMETER SecureNameQueryFallback + Specifies the DNS client name resolution fallback policy. +#> +function Test-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Boolean])] + param + ( + [Parameter(Mandatory = $true)] + [ValidateSet('Yes')] + [System.String] + $IsSingleInstance, + + [Parameter()] + [System.String] + $EnableDAForAllNetworks, + + [Parameter()] + [System.String] + $QueryPolicy, + + [Parameter()] + [System.String] + $SecureNameQueryFallback + ) + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.TestingDnsClientNrptGlobalMessage) + ) -join '' ) + + # Flag to signal whether settings are correct + $desiredConfigurationMatch = $true + + # Get the current Dns Client Nrpt Global Settings + $DnsClientNrptGlobal = Get-DnsClientNrptGlobal ` + -ErrorAction Stop + + # Check each parameter + foreach ($parameter in $script:parameterList) + { + $parameterSourceValue = $DnsClientNrptGlobal.$($parameter.name) + $parameterNewValue = (Get-Variable -Name ($parameter.name)).Value + + if ($parameterNewValue -ne $parameterSourceValue) { + Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " + $($script:localizedData.DnsClientNrptGlobalParameterNeedsUpdateMessage) ` + -f $parameter.Name, ($parameterSourceValue -join ','), ($parameterNewValue -join ',') + ) -join '') + $desiredConfigurationMatch = $false + } + + } # foreach + + return $desiredConfigurationMatch +} # Test-TargetResource + +Export-ModuleMember -Function *-TargetResource diff --git a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.schema.mof b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.schema.mof new file mode 100644 index 00000000..c28c364f --- /dev/null +++ b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.schema.mof @@ -0,0 +1,8 @@ +[ClassVersion("1.0.0.0"), FriendlyName("DnsClientNrptGlobal")] +class DSC_DnsClientNrptGlobal : OMI_BaseResource +{ + [Key, Description("Specifies the resource is a single instance, the value must be 'Yes'."), ValueMap{"Yes"}, Values{"Yes"}] String IsSingleInstance; + [Write, Description("Specifies DirectAccess (DA) settings."), ValueMap{"EnableOnNetworkID", "EnableAlways", "Disable", "DisableDA"},Values{"EnableOnNetworkID", "EnableAlways", "Disable", "DisableDA"}] string EnableDAForAllNetworks; + [Write, Description("Specifies the DNS client query policy."), ValueMap{"Disable", "QueryIPv6Only", "QueryBoth"},Values{"Disable", "QueryIPv6Only", "QueryBoth"}] string QueryPolicy; + [Write, Description("SecureNameQueryFallback."), ValueMap{"Disable", "FallbackSecure", "FallbackUnsecure", "FallbackPrivate"},Values{"Disable", "FallbackSecure", "FallbackUnsecure", "FallbackPrivate"}] string SecureNameQueryFallback; +}; diff --git a/source/DSCResources/DSC_DnsClientNrptGlobal/README.MD b/source/DSCResources/DSC_DnsClientNrptGlobal/README.MD new file mode 100644 index 00000000..354ca235 --- /dev/null +++ b/source/DSCResources/DSC_DnsClientNrptGlobal/README.MD @@ -0,0 +1,3 @@ +# Description + +This resource is used to control DNS Client NRPT Global Settings for a node. diff --git a/source/DSCResources/DSC_DnsClientNrptGlobal/en-US/DSC_DnsClientNrptGlobal.strings.psd1 b/source/DSCResources/DSC_DnsClientNrptGlobal/en-US/DSC_DnsClientNrptGlobal.strings.psd1 new file mode 100644 index 00000000..7d1d1af7 --- /dev/null +++ b/source/DSCResources/DSC_DnsClientNrptGlobal/en-US/DSC_DnsClientNrptGlobal.strings.psd1 @@ -0,0 +1,10 @@ +# Localized resources for DSC_DnsClientNrptGlobal + +ConvertFrom-StringData @' + GettingDnsClientNrptGlobalMessage = Getting DNS Client NRPT Global Settings. + SettingDnsClientNrptGlobalMessage = Setting DNS Client NRPT Global Settings. + DnsClientNrptGlobalUpdateParameterMessage = Setting DNS Client NRPT Global Settings parameter {0} to "{1}". + DnsClientNrptGlobalUpdatedMessage = Setting DNS Client NRPT Global Settings updated. + TestingDnsClientNrptGlobalMessage = Testing DNS Client NRPT Global Settings. + DnsClientNrptGlobalParameterNeedsUpdateMessage = DNS Client NRPT Global Setting "{0}" is "{1}" but should be "{2}". Change required. +'@ diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 new file mode 100644 index 00000000..d41adc13 --- /dev/null +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 @@ -0,0 +1,557 @@ +$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules' + +# Import the Networking Common Modules +Import-Module -Name (Join-Path -Path $modulePath ` + -ChildPath (Join-Path -Path 'NetworkingDsc.Common' ` + -ChildPath 'NetworkingDsc.Common.psm1')) + +Import-Module -Name (Join-Path -Path $modulePath -ChildPath 'DscResource.Common') + +# Import Localization Strings +$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' + +<# + .SYNOPSIS + Returns the current state of a NRPT Rule. + + .PARAMETER Name + Specifies the name which uniquely identifies a rule. +#> +function Get-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $Name + ) + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.GettingNrptRuleMessage) ` + -f $Name ` + ) -join '' ) + + # Lookup the existing NrptRule + $NrptRule = Get-NrptRule -Name $Name + + $returnValue = @{ + Name = $Name + } + + if ($NrptRule) + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.NrptRuleExistsMessage) ` + -f $Name ` + ) -join '' ) + + $returnValue += @{ + Ensure = 'Present' + Comment = [System.String] $NrptRule.Comment + DAEnable = [System.Boolean] $NrptRule.DAEnable + DAIPsecEncryptionType = [System.String] $NrptRule.DAIPsecEncryptionType + DAIPsecRequired = [System.Boolean] $NrptRule.DAIPsecRequired + DANameServers = [System.String[]] $NrptRule.DANameServers + DAProxyServerName = [System.String] $NrptRule.DAProxyServerName + DAProxyType = [System.String] $NrptRule.DAProxyType + DisplayName = [System.String] $NrptRule.DisplayName + DnsSecEnable = [System.Boolean] $NrptRule.DnsSecEnable + DnsSecIPsecEncryptionType = [System.String] $NrptRule.DnsSecIPsecEncryptionType + DnsSecIPsecRequired = [System.Boolean] $NrptRule.DnsSecIPsecRequired + DnsSecValidationRequired = [System.Boolean] $NrptRule.DnsSecValidationRequired + IPsecTrustAuthority = [System.String] $NrptRule.IPsecTrustAuthority + NameEncoding = [System.String] $NrptRule.NameEncoding + NameServers = [System.String[]] $NrptRule.NameServers + Namespace = [System.String] $NrptRule.Namespace + } + } + else + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.NrptRuleDoesNotExistMessage) ` + -f $Name ` + ) -join '' ) + + $returnValue += @{ + Ensure = 'Absent' + } + } + + return $returnValue +} # Get-TargetResource + +<# + .SYNOPSIS + Sets a NRPT Rule. + + .PARAMETER Name + Specifies the name which uniquely identifies a rule. + + .PARAMETER Comment + Stores administrator notes. + + .PARAMETER DAEnable + Indicates the rule state for DirectAccess. + + .PARAMETER DAIPsecEncryptionType + Specifies the Internet Protocol security (IPsec) encryption setting for DirectAccess. + + .PARAMETER DAIPsecRequired + Indicates that IPsec is required for DirectAccess. + + .PARAMETER DANameServers + Specifies an array of DNS servers to query when DirectAccess is enabled. + + .PARAMETER DAProxyServerName + "Specifies the proxy server to use when connecting to the Internet. + This parameter is only applicable if the DAProxyType parameter is set to UseProxyName. + + .PARAMETER DAProxyType + Specifies the proxy server type to be used when connecting to the Internet. + + .PARAMETER DisplayName + Specifies an optional friendly name for the NRPT rule. + + .PARAMETER DnsSecEnable + Enables Domain Name System Security Extensions (DNSSEC) on the rule. + + .PARAMETER DnsSecIPsecEncryptionType + Specifies the IPsec tunnel encryption setting. + + .PARAMETER DnsSecIPsecRequired + Indicates the DNS client must set up an IPsec connection to the DNS server. + + .PARAMETER DnsSecValidationRequired + Indicates that DNSSEC validation is required. + + .PARAMETER IPsecTrustAuthority + Specifies the certification authority to validate the IPsec channel. + + .PARAMETER NameEncoding + Specifies the encoding format for host names in the DNS query. + + .PARAMETER NameServers + Specifies the DNS servers to which the DNS query is sent when DirectAccess is disabled. + + .PARAMETER Namespace + Specifies the DNS namespace. +#> +function Set-TargetResource +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $Name, + + [Parameter()] + [ValidateSet('Present', 'Absent')] + [System.String] + $Ensure = 'Present', + + [Parameter()] + [System.String] + $Comment, + + [Parameter()] + [System.Boolean] + $DAEnable, + + [Parameter()] + [ValidateSet('None', 'Low', 'Medium', 'High')] + [System.String] + $DAIPsecEncryptionType, + + [Parameter()] + [System.Boolean] + $DAIPsecRequired, + + [Parameter()] + [System.String[]] + $DANameServers, + + [Parameter()] + [System.String] + $DAProxyServerName, + + [Parameter()] + [ValidateSet('NoProxy', 'UseDefault', 'UseProxyName')] + [System.String] + $DAProxyType, + + [Parameter()] + [System.String] + $DisplayName, + + [Parameter()] + [System.Boolean] + $DnsSecEnable, + + [Parameter()] + [ValidateSet('None', 'Low', 'Medium', 'High')] + [System.String] + $DnsSecIPsecEncryptionType, + + [Parameter()] + [System.Boolean] + $DnsSecIPsecRequired, + + [Parameter()] + [System.Boolean] + $DnsSecValidationRequired, + + [Parameter()] + [System.String] + $IPsecTrustAuthority, + + [Parameter()] + [ValidateSet('Disable', 'Utf8WithMapping', 'Utf8WithoutMapping', 'Punycode')] + [System.String] + $NameEncoding, + + [Parameter()] + [System.String[]] + $NameServers, + + [Parameter()] + [System.String] + $Namespace + ) + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.SettingNrptRuleMessage) ` + -f $Name ` + ) -join '' ) + + # Remove any parameters that can't be splatted. + $null = $PSBoundParameters.Remove('Ensure') + + # Lookup the existing NrptRule + $NrptRule = Get-NrptRule -Name $Name + + if ($Ensure -eq 'Present') + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.EnsureNrptRuleExistsMessage) ` + -f $Name ` + ) -join '' ) + + if ($NrptRule) + { + # The NrptRule exists - update it + Set-DnsClientNrptRule @PSBoundParameters ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.NrptRuleUpdatedMessage) ` + -f $Name ` + ) -join '' ) + } + else + { + # Remove Name parameter as Add-DnsClientNrptRule cmdlet doesn't support it + $null = $PSBoundParameters.Remove("Name") + # The NrptRule does not exit - create it (PassThru to get the name of the rule created) + $NrptRuleName=(Add-DnsClientNrptRule @PSBoundParameters ` + -PassThru ` + -ErrorAction Stop).Name + + # If rule has been created, rename it by registry as Name cannot be provided in Add-DnsClientNrptRule cmdlet + if ($NrptRuleName -match "^\{[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}\}$") { + # Rename the registry key + Rename-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig\$($NrptRuleName)" -NewName $Name + } + + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.NrptRuleCreatedMessage) ` + -f $Name ` + ) -join '' ) + } + } + else + { + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.EnsureNrptRuleDoesNotExistMessage) ` + -f $Name ` + ) -join '' ) + + if ($NrptRule) + { + <# + The NrptRule exists - remove it + Use Force as confirm does not work in DnsClientNrptRule + #> + + Remove-DnsClientNrptRule -Name $Name ` + -Force ` + -ErrorAction Stop + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.NrptRuleRemovedMessage) ` + -f $Name ` + ) -join '' ) + } # if + } # if +} # Set-TargetResource + +<# + .SYNOPSIS + Tests the state of a NRPT Rule. + + .PARAMETER Name + Specifies the name which uniquely identifies a rule. + + .PARAMETER Comment + Stores administrator notes. + + .PARAMETER DAEnable + Indicates the rule state for DirectAccess. + + .PARAMETER DAIPsecEncryptionType + Specifies the Internet Protocol security (IPsec) encryption setting for DirectAccess. + + .PARAMETER DAIPsecRequired + Indicates that IPsec is required for DirectAccess. + + .PARAMETER DANameServers + Specifies an array of DNS servers to query when DirectAccess is enabled. + + .PARAMETER DAProxyServerName + "Specifies the proxy server to use when connecting to the Internet. + This parameter is only applicable if the DAProxyType parameter is set to UseProxyName. + + .PARAMETER DAProxyType + Specifies the proxy server type to be used when connecting to the Internet. + + .PARAMETER DisplayName + Specifies an optional friendly name for the NRPT rule. + + .PARAMETER DnsSecEnable + Enables Domain Name System Security Extensions (DNSSEC) on the rule. + + .PARAMETER DnsSecIPsecEncryptionType + Specifies the IPsec tunnel encryption setting. + + .PARAMETER DnsSecIPsecRequired + Indicates the DNS client must set up an IPsec connection to the DNS server. + + .PARAMETER DnsSecValidationRequired + Indicates that DNSSEC validation is required. + + .PARAMETER IPsecTrustAuthority + Specifies the certification authority to validate the IPsec channel. + + .PARAMETER NameEncoding + Specifies the encoding format for host names in the DNS query. + + .PARAMETER NameServers + Specifies the DNS servers to which the DNS query is sent when DirectAccess is disabled. + + .PARAMETER Namespace + Specifies the DNS namespace. +#> +function Test-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Boolean])] + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $Name, + + [Parameter()] + [ValidateSet('Present', 'Absent')] + [System.String] + $Ensure = 'Present', + + [Parameter()] + [System.String] + $Comment, + + [Parameter()] + [System.Boolean] + $DAEnable, + + [Parameter()] + [ValidateSet('None', 'Low', 'Medium', 'High')] + [System.String] + $DAIPsecEncryptionType, + + [Parameter()] + [System.Boolean] + $DAIPsecRequired, + + [Parameter()] + [System.String[]] + $DANameServers, + + [Parameter()] + [System.String] + $DAProxyServerName, + + [Parameter()] + [ValidateSet('NoProxy', 'UseDefault', 'UseProxyName')] + [System.String] + $DAProxyType, + + [Parameter()] + [System.String] + $DisplayName, + + [Parameter()] + [System.Boolean] + $DnsSecEnable, + + [Parameter()] + [ValidateSet('None', 'Low', 'Medium', 'High')] + [System.String] + $DnsSecIPsecEncryptionType, + + [Parameter()] + [System.Boolean] + $DnsSecIPsecRequired, + + [Parameter()] + [System.Boolean] + $DnsSecValidationRequired, + + [Parameter()] + [System.String] + $IPsecTrustAuthority, + + [Parameter()] + [ValidateSet('Disable', 'Utf8WithMapping', 'Utf8WithoutMapping', 'Punycode')] + [System.String] + $NameEncoding, + + [Parameter()] + [System.String[]] + $NameServers, + + [Parameter()] + [System.String] + $Namespace + ) + + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.TestingNrptRuleMessage) ` + -f $Name ` + ) -join '' ) + + # Flag to signal whether settings are correct + [System.Boolean] $desiredConfigurationMatch = $true + + # Remove any parameters that can't be splatted. + $null = $PSBoundParameters.Remove('Ensure') + + # Check the parameters + # Assert-ResourceProperty @PSBoundParameters + + # Lookup the existing NrptRule + $NrptRule = Get-NrptRule -Name $Name + + if ($Ensure -eq 'Present') + { + # The NrptRule should exist + if ($NrptRule) + { + # The route exists and does - but check the parameters + $currentState = Get-TargetResource -Name $Name + + return Test-DscParameterState -CurrentValues $currentState -DesiredValues $PSBoundParameters + } + else + { + # The NrptRule doesn't exist but should + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.NrptRuleDoesNotExistButShouldMessage) ` + -f $Name ` + ) -join '' ) + + $desiredConfigurationMatch = $false + } + } + else + { + # The NrptRule should not exist + if ($NrptRule) + { + # The NrptRule exists but should not + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.NrptRuleExistsButShouldNotMessage) ` + -f $Name ` + ) -join '' ) + + $desiredConfigurationMatch = $false + } + else + { + # The NrptRule does not exist and should not + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($script:localizedData.NrptRuleDoesNotExistAndShouldNotMessage) ` + -f $Name ` + ) -join '' ) + } + } # if + + return $desiredConfigurationMatch +} # Test-TargetResource + +<# + .SYNOPSIS + This function looks up NRPT Rule using the parameters and returns + it. If the rule is not found $null is returned. + + .PARAMETER Name + Specifies the name which uniquely identifies a rule. +#> + +function Get-NrptRule +{ + param + ( + [Parameter()] + [System.String] + $Name + ) + + try + { + $NrptRule = Get-DnsClientNrptRule ` + -Name $Name ` + -ErrorAction SilentlyContinue + } + catch [Microsoft.PowerShell.Cmdletization.Cim.CimJobException] + { + $NrptRule = $null + } + catch + { + throw $_ + } + + return $NrptRule +} + +Export-ModuleMember -Function *-TargetResource diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof new file mode 100644 index 00000000..e559bf49 --- /dev/null +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof @@ -0,0 +1,22 @@ +[ClassVersion("1.0.0.0"), FriendlyName("DnsClientNrptRule")] +class DSC_DnsClientNrptRule : OMI_BaseResource +{ + [Key, Description("Specifies the NRPT rule name.")] string Name; + [Write, Description("Specifies whether the NRPT rule should exist. Defaults to 'Present'."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; + [Write, Description("Stores administrator notes.")] string Comment; + [Write, Description("Indicates the rule state for DirectAccess.")] Boolean DAEnable; + [Write, Description("Specifies the Internet Protocol security (IPsec) encryption setting for DirectAccess."), ValueMap{"None", "Low", "Medium", "High"},Values{"None", "Low", "Medium", "High"}] string DAIPsecEncryptionType; + [Write, Description("Indicates that IPsec is required for DirectAccess.")] Boolean DAIPsecRequired; + [Write, Description("Specifies an array of DNS servers to query when DirectAccess is enabled.")] string DANameServers[]; + [Write, Description("Specifies the proxy server to use when connecting to the Internet. This parameter is only applicable if the DAProxyType parameter is set to UseProxyName.")] string DAProxyServerName; + [Write, Description("Specifies the proxy server type to be used when connecting to the Internet."), ValueMap{"NoProxy", "UseDefault", "UseProxyName"},Values{"NoProxy", "UseDefault", "UseProxyName"}] string DAProxyType; + [Write, Description("Specifies an optional friendly name for the NRPT rule.")] String DisplayName; + [Write, Description("Enables Domain Name System Security Extensions (DNSSEC) on the rule.")] Boolean DnsSecEnable; + [Write, Description("Specifies the IPsec tunnel encryption setting."), ValueMap{"None", "Low", "Medium", "High"},Values{"None", "Low", "Medium", "High"}] string DnsSecIPsecEncryptionType; + [Write, Description("Indicates the DNS client must set up an IPsec connection to the DNS server.")] Boolean DnsSecIPsecRequired; + [Write, Description("Indicates that DNSSEC validation is required.")] Boolean DnsSecValidationRequired; + [Write, Description("Specifies the certification authority to validate the IPsec channel.")] String IPsecTrustAuthority; + [Write, Description("Specifies the encoding format for host names in the DNS query."), ValueMap{"Disable", "Utf8WithMapping", "Utf8WithoutMapping", "Punycode"},Values{"Disable", "Utf8WithMapping", "Utf8WithoutMapping", "Punycode"}] string NameEncoding; + [Write, Description("Specifies the DNS servers to which the DNS query is sent when DirectAccess is disabled.")] string NameServers[]; + [Write, Description("Specifies the DNS namespace.")] string Namespace; +}; diff --git a/source/DSCResources/DSC_DnsClientNrptRule/README.MD b/source/DSCResources/DSC_DnsClientNrptRule/README.MD new file mode 100644 index 00000000..d3fbfcd1 --- /dev/null +++ b/source/DSCResources/DSC_DnsClientNrptRule/README.MD @@ -0,0 +1,3 @@ +# Description + +This resource is used to control NRPT rules for a node. diff --git a/source/DSCResources/DSC_DnsClientNrptRule/en-US/DSC_DnsClientNrptRule.strings.psd1 b/source/DSCResources/DSC_DnsClientNrptRule/en-US/DSC_DnsClientNrptRule.strings.psd1 new file mode 100644 index 00000000..5b913730 --- /dev/null +++ b/source/DSCResources/DSC_DnsClientNrptRule/en-US/DSC_DnsClientNrptRule.strings.psd1 @@ -0,0 +1,17 @@ +# Localized resources for DSC_DnsClientNrptRule + +ConvertFrom-StringData @' + GettingNrptRuleMessage = Getting '{0}' NRPT rule. + NrptRuleExistsMessage = '{0}' NRPT rule exists. + NrptRuleDoesNotExistMessage = '{0}' NRPT rule does not exist. + SettingNrptRuleMessage = Setting '{0}' NRPT rule. + EnsureNrptRuleExistsMessage = Ensuring '{0}' NRPT rule exists. + EnsureNrptRuleDoesNotExistMessage = Ensuring '{0}' NRPT rule does not exist. + NrptRuleCreatedMessage = '{0}' NRPT rule has been created. + NrptRuleUpdatedMessage = '{0}' NRPT rule has been updated. + NrptRuleRemovedMessage = '{0}' NRPT rule has been removed. + TestingNrptRuleMessage = Testing '{0}' NRPT rule. + NrptRuleDoesNotExistButShouldMessage = '{0}' NRPT rule does not exist but should. Change required. + NrptRuleExistsButShouldNotMessage = '{0}' NRPT rule exists but should not. Change required. + NrptRuleDoesNotExistAndShouldNotMessage = '{0}' NRPT rule does not exist and should not. Change not required. +'@ diff --git a/source/NetworkingDsc.psd1 b/source/NetworkingDsc.psd1 index 51c2971b..27ea1a1a 100644 --- a/source/NetworkingDsc.psd1 +++ b/source/NetworkingDsc.psd1 @@ -39,6 +39,8 @@ DscResourcesToExport = @( 'DefaultGatewayAddress', 'DnsClientGlobalSetting', + 'DnsClientNrptGlobal', + 'DnsClientNrptRule', 'DnsConnectionSuffix', 'DNSServerAddress', 'Firewall', From 956ad45c3a9bf2e1ef996c1fc31f59380a9fd3f9 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 10 Mar 2025 21:00:07 +0100 Subject: [PATCH 02/35] Update documentation --- CHANGELOG.md | 4 ++ .../DSC_DnsClientNrptRule.psm1 | 2 +- .../1-DnsClientNrptGlobal_Config.ps1 | 38 +++++++++++++++++++ .../1-DnsClientNrptRule_Server_Config.ps1 | 37 ++++++++++++++++++ .../2-DnsClientNrptRule_DNSSEC_Config.ps1 | 37 ++++++++++++++++++ .../3-DnsClientNrptRule_Punycode_Config.ps1 | 38 +++++++++++++++++++ 6 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 create mode 100644 source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 create mode 100644 source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 create mode 100644 source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 39401b0d..05a3c946 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Added the following resources: + - **DnsClientNrptGlobal**: Configure the global Name Resolution Policy Table (NRPT) settings. + - **DnsClientNrptRule**: Sets a NRPT rule on a node. + - Updated CHANGELOG.md - Renamed NetworkingDSc to NetworkingDsc in CHANGELOG.md - fixes [Issue #513](https://github.com/dsccommunity/NetworkingDsc/issues/513). - CI Pipeline diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 index d41adc13..17e47858 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 @@ -473,7 +473,7 @@ function Test-TargetResource # The NrptRule should exist if ($NrptRule) { - # The route exists and does - but check the parameters + # The NrptRule exists and does - but check the parameters $currentState = Get-TargetResource -Name $Name return Test-DscParameterState -CurrentValues $currentState -DesiredValues $PSBoundParameters diff --git a/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 b/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 new file mode 100644 index 00000000..8303af1a --- /dev/null +++ b/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 @@ -0,0 +1,38 @@ +<#PSScriptInfo +.VERSION 1.0.0 +.GUID ef9d0f9a-5a08-43c0-9645-48e103188971 +.AUTHOR DSC Community +.COMPANYNAME DSC Community +.COPYRIGHT Copyright the DSC Community contributors. All rights reserved. +.TAGS DSCConfiguration +.LICENSEURI https://github.com/dsccommunity/NetworkingDsc/blob/main/LICENSE +.PROJECTURI https://github.com/dsccommunity/NetworkingDsc +.ICONURI +.EXTERNALMODULEDEPENDENCIES +.REQUIREDSCRIPTS +.EXTERNALSCRIPTDEPENDENCIES +.RELEASENOTES First version. +.PRIVATEDATA 2016-Datacenter,2016-Datacenter-Server-Core +#> + +#Requires -module NetworkingDsc + +<# + .DESCRIPTION + Configure the NRPT global configuration. +#> +Configuration DnsClientNrptGlobal_Config +{ + Import-DscResource -Module NetworkingDsc + + Node localhost + { + DnsClientNrptGlobal DnsClientNrptGlobal + { + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + } +} diff --git a/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 new file mode 100644 index 00000000..e2bc361d --- /dev/null +++ b/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 @@ -0,0 +1,37 @@ +<#PSScriptInfo +.VERSION 1.0.0 +.GUID 9783741d-7f08-409d-8c93-d2e16a76e1ee +.AUTHOR DSC Community +.COMPANYNAME DSC Community +.COPYRIGHT Copyright the DSC Community contributors. All rights reserved. +.TAGS DSCConfiguration +.LICENSEURI https://github.com/dsccommunity/NetworkingDsc/blob/main/LICENSE +.PROJECTURI https://github.com/dsccommunity/NetworkingDsc +.ICONURI +.EXTERNALMODULEDEPENDENCIES +.REQUIREDSCRIPTS +.EXTERNALSCRIPTDEPENDENCIES +.RELEASENOTES First version. +.PRIVATEDATA 2016-Datacenter,2016-Datacenter-Server-Core +#> + +#Requires -module NetworkingDsc + +<# + .DESCRIPTION + Add an NRPT rule to configure a server. +#> +Configuration DnsClientNrptRule_Server_Config +{ + Import-DscResource -Module NetworkingDsc + + Node localhost + { + DnsClientNrptRule Server + { + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + } + } +} diff --git a/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 new file mode 100644 index 00000000..5e7eb175 --- /dev/null +++ b/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 @@ -0,0 +1,37 @@ +<#PSScriptInfo +.VERSION 1.0.0 +.GUID 87e17ee4-64b0-476a-8aa8-1018a5de6353 +.AUTHOR DSC Community +.COMPANYNAME DSC Community +.COPYRIGHT Copyright the DSC Community contributors. All rights reserved. +.TAGS DSCConfiguration +.LICENSEURI https://github.com/dsccommunity/NetworkingDsc/blob/main/LICENSE +.PROJECTURI https://github.com/dsccommunity/NetworkingDsc +.ICONURI +.EXTERNALMODULEDEPENDENCIES +.REQUIREDSCRIPTS +.EXTERNALSCRIPTDEPENDENCIES +.RELEASENOTES First version. +.PRIVATEDATA 2016-Datacenter,2016-Datacenter-Server-Core +#> + +#Requires -module NetworkingDsc + +<# + .DESCRIPTION + Add an NRPT rule to enable DNSSEC queries. +#> +Configuration DnsClientNrptRule_DNSSEC_Config +{ + Import-DscResource -Module NetworkingDsc + + Node localhost + { + DnsClientNrptRule DNSSEC + { + Name = 'DNSSEC' + Namespace = 'contoso.com' + DnsSecEnable = $true + } + } +} diff --git a/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 new file mode 100644 index 00000000..366d23ea --- /dev/null +++ b/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 @@ -0,0 +1,38 @@ +<#PSScriptInfo +.VERSION 1.0.0 +.GUID cc42de3d-7497-4179-8756-84154b528895 +.AUTHOR DSC Community +.COMPANYNAME DSC Community +.COPYRIGHT Copyright the DSC Community contributors. All rights reserved. +.TAGS DSCConfiguration +.LICENSEURI https://github.com/dsccommunity/NetworkingDsc/blob/main/LICENSE +.PROJECTURI https://github.com/dsccommunity/NetworkingDsc +.ICONURI +.EXTERNALMODULEDEPENDENCIES +.REQUIREDSCRIPTS +.EXTERNALSCRIPTDEPENDENCIES +.RELEASENOTES First version. +.PRIVATEDATA 2016-Datacenter,2016-Datacenter-Server-Core +#> + +#Requires -module NetworkingDsc + +<# + .DESCRIPTION + Add an NRPT rule to send Punycode DNS queries. +#> +Configuration DnsClientNrptRule_Punycode_Config +{ + Import-DscResource -Module NetworkingDsc + + Node localhost + { + DnsClientNrptRule Punycode + { + Name = 'Punycode' + Namespace = 'contoso.com' + NameEncoding = 'Punycode' + NameServers = ('192.168.1.1') + } + } +} From 24b207339d021aa7b423ffc2bd6169f44fd6c2cd Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 10 Mar 2025 22:25:44 +0100 Subject: [PATCH 03/35] Add DnsClientNrptGlobal unit testing --- ..._DnsClientNrptGlobal.Integration.Tests.ps1 | 100 +++++++++ .../DSC_DnsClientNrptGlobal.config.ps1 | 12 ++ tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 | 201 ++++++++++++++++++ 3 files changed, 313 insertions(+) create mode 100644 tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 create mode 100644 tests/Integration/DSC_DnsClientNrptGlobal.config.ps1 create mode 100644 tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 diff --git a/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 new file mode 100644 index 00000000..be494cf1 --- /dev/null +++ b/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 @@ -0,0 +1,100 @@ +$script:dscModuleName = 'NetworkingDsc' +$script:dscResourceName = 'DSC_DnsClientNrptGlobal' + +try +{ + Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' +} +catch [System.IO.FileNotFoundException] +{ + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' +} + +$script:testEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dscResourceName ` + -ResourceType 'Mof' ` + -TestType 'Integration' + +Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + +# Load the parameter List from the data file +$moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) +$resourceData = Import-LocalizedData ` + -BaseDirectory (Join-Path -Path $moduleRoot -ChildPath 'Source\DscResources\DSC_DnsClientNrptGlobal') ` + -FileName 'DSC_DnsClientNrptGlobal.data.psd1' + +$parameterList = $resourceData.ParameterList | Where-Object -Property IntTest -eq $True + +# Begin Testing +try +{ + Describe 'DnsClientNrptGlobal Integration Tests' { + # Backup the existing settings + $script:currentDnsClientNrptGlobal = Get-DnsClientNrptGlobal + + # Set the DNS Client Global settings to known values + Set-DnsClientNrptGlobal ` + -EnableDAForAllNetworks 'EnableAlways' ` + -QueryPolicy 'QueryIPv6Only' ` + -SecureNameQueryFallback 'FallbackSecure' + + $configData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + EnableDAForAllNetworks = 'DisableDA' + QueryPolicy = 'QueryBoth' + SecureNameQueryFallback = 'FallbackPrivate' + } + ) + } + + $configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1" + . $configFile + + Describe "$($script:dscResourceName)_Integration" { + It 'Should compile and apply the MOF without throwing' { + { + & "$($script:dscResourceName)_Config" ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force + } | Should -Not -Throw + } + + It 'Should be able to call Get-DscConfiguration without throwing' { + { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + } + + # Get the DNS Client Global Settings details + $DnsClientNrptGlobalNew = Get-DnsClientNrptGlobal + + # Use the Parameters List to perform these tests + foreach ($parameter in $parameterList) + { + $parameterCurrentValue = (Get-Variable -Name 'DnsClientNrptGlobalNew').value.$($parameter.name) + $parameterNewValue = (Get-Variable -Name configData).Value.AllNodes[0].$($parameter.Name) + + It "Should have set the '$parameterName' to '$parameterNewValue'" { + $parameterCurrentValue | Should -Be $parameterNewValue + } + } + } + } +} +finally +{ + # Clean up + Set-DnsClientNrptGlobal ` + -SuffixSearchList $script:currentDnsClientNrptGlobal.EnableDAForAllNetworks ` + -UseDevolution $script:currentDnsClientNrptGlobal.QueryPolicy ` + -DevolutionLevel $script:currentDnsClientNrptGlobal.SecureNameQueryFallback + + Restore-TestEnvironment -TestEnvironment $script:testEnvironment +} diff --git a/tests/Integration/DSC_DnsClientNrptGlobal.config.ps1 b/tests/Integration/DSC_DnsClientNrptGlobal.config.ps1 new file mode 100644 index 00000000..cdc72a11 --- /dev/null +++ b/tests/Integration/DSC_DnsClientNrptGlobal.config.ps1 @@ -0,0 +1,12 @@ +Configuration DSC_DnsClientNrptGlobal_Config { + Import-DscResource -ModuleName NetworkingDsc + + node localhost { + DnsClientNrptGlobal Integration_Test { + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = $Node.EnableDAForAllNetworks + QueryPolicy = $Node.QueryPolicy + SecureNameQueryFallback = $Node.SecureNameQueryFallback + } + } +} diff --git a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 new file mode 100644 index 00000000..ac9b9612 --- /dev/null +++ b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 @@ -0,0 +1,201 @@ +$script:dscModuleName = 'NetworkingDsc' +$script:dscResourceName = 'DSC_DnsClientNrptGlobal' + +function Invoke-TestSetup +{ + try + { + Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' + } + catch [System.IO.FileNotFoundException] + { + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' + } + + $script:testEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dscResourceName ` + -ResourceType 'Mof' ` + -TestType 'Unit' + + Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') +} + +function Invoke-TestCleanup +{ + Restore-TestEnvironment -TestEnvironment $script:testEnvironment +} + +Invoke-TestSetup + +# Begin Testing +try +{ + InModuleScope $script:dscResourceName { + # Create the Mock Objects that will be used for running tests + $DnsClientNrptGlobal = [PSObject] @{ + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + + $DnsClientNrptGlobalSplat = [PSObject]@{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = $DnsClientNrptGlobal.EnableDAForAllNetworks + QueryPolicy = $DnsClientNrptGlobal.QueryPolicy + SecureNameQueryFallback = $DnsClientNrptGlobal.SecureNameQueryFallback + } + + Describe 'DSC_DnsClientNrptGlobal\Get-TargetResource' -Tag 'Get' { + BeforeEach { + Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } + } + + Context 'DNS Client NRPT Global Settings Exists' { + It 'Should return correct DNS Client NRPT Global Settings values' { + $getTargetResourceParameters = Get-TargetResource -IsSingleInstance 'Yes' + $getTargetResourceParameters.EnableDAForAllNetworks | Should -Be $DnsClientNrptGlobal.EnableDAForAllNetworks + $getTargetResourceParameters.QueryPolicy | Should -Be $DnsClientNrptGlobal.QueryPolicy + $getTargetResourceParameters.SecureNameQueryFallback | Should -Be $DnsClientNrptGlobal.SecureNameQueryFallback + } + + It 'Should call the expected mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 + } + } + } + + Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { + BeforeEach { + Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } + } + + Context 'DNS Client NRPT Global Settings all parameters are the same' { + Mock -CommandName Set-DnsClientNrptGlobal + + It 'Should not throw error' { + { + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 0 + } + } + + Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { + Mock -CommandName Set-DnsClientNrptGlobal + + It 'Should not throw error' { + { + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 + } + } + + + Context 'DNS Client NRPT Global Settings QueryPolicy is different' { + Mock -CommandName Set-DnsClientNrptGlobal + + It 'Should not throw error' { + { + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters.QueryPolicy = 'QueryBoth' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 + } + } + + Context 'DNS Client NRPT Global Settings SecureNameQueryFallback is different' { + Mock -CommandName Set-DnsClientNrptGlobal + + It 'Should not throw error' { + { + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 + } + } + } + + Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { + Context 'DNS Client NRPT Global Settings configuration' { + BeforeEach { + Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } + } + + Context 'DNS Client NRPT Global Settings all parameters are the same' { + It 'Should return true' { + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + Test-TargetResource @testTargetResourceParameters | Should -Be $true + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + } + } + + Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { + It 'Should return false' { + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + } + } + + Context 'DNS Client NRPT Global Settings QueryPolicy is different' { + It 'Should return false' { + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters.QueryPolicy = 'QueryBoth' + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + } + } + + Context 'DNS Client NRPT Global Settings UseDevolution is different' { + It 'Should return false' { + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + } + } + } + } + } +} +finally +{ + Invoke-TestCleanup +} From 550555f0e247a602056d23375ba901bcdc66bcb0 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Tue, 11 Mar 2025 16:25:31 +0100 Subject: [PATCH 04/35] Add DnsClientNrptRule integration tests --- ...SC_DnsClientNrptRule.Integration.Tests.ps1 | 141 ++++++++++++++++++ .../DSC_DnsClientNrptRule.config.ps1 | 19 +++ 2 files changed, 160 insertions(+) create mode 100644 tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 create mode 100644 tests/Integration/DSC_DnsClientNrptRule.config.ps1 diff --git a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 new file mode 100644 index 00000000..c341a4d6 --- /dev/null +++ b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 @@ -0,0 +1,141 @@ +$script:dscModuleName = 'NetworkingDsc' +$script:dscResourceName = 'DSC_DnsClientNrptRule' + +try +{ + Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' +} +catch [System.IO.FileNotFoundException] +{ + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' +} + +$script:testEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dscResourceName ` + -ResourceType 'Mof' ` + -TestType 'Integration' + +Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + +# Begin Testing +try +{ + Describe 'NRPT Rule Integration Tests' { + $script:dummyRule = [PSObject] @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + } + + $configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1" + . $configFile -Verbose -ErrorAction Stop + + Describe "$($script:dscResourceName)_Add_Integration" { + $configData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + Name = $script:dummyRule.Name + Namespace = $script:dummyRule.Namespace + NameServers = $script:dummyRule.NameServers + Ensure = 'Present' + } + ) + } + + It 'Should compile and apply the MOF without throwing' { + { + & "$($script:dscResourceName)_Config" ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + } | Should -Not -Throw + } + + It 'Should be able to call Get-DscConfiguration without throwing' { + { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + } + + It 'Should have set the resource and all the parameters should match' { + $current = Get-DscConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq "$($script:dscResourceName)_Config" + } + + $current.Name | Should -Be $configData.AllNodes[0].Name + $current.Namespace | Should -Be $configData.AllNodes[0].Namespace + $current.NameServers | Should -Be $configData.AllNodes[0].NameServers + $current.Ensure | Should -Be $configData.AllNodes[0].Ensure + } + + It 'Should have created the NRPT rule' { + Get-DnsClientNrptRule -Name $script:dummyRule.Name -ErrorAction SilentlyContinue | Should -Not -BeNullOrEmpty + } + } + + Describe "$($script:dscResourceName)_Remove_Integration" { + $configData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + Name = $script:dummyRule.Name + Namespace = $script:dummyRule.Namespace + NameServers = $script:dummyRule.NameServers + Ensure = 'Absent' + } + ) + } + + It 'Should compile and apply the MOF without throwing' { + { + & "$($script:dscResourceName)_Config" ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + } | Should -Not -Throw + } + + It 'Should be able to call Get-DscConfiguration without throwing' { + { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + } + + It 'Should have set the resource and all the parameters should match' { + $current = Get-DscConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq "$($script:dscResourceName)_Config" + } + + $current.Name | Should -Be $configData.AllNodes[0].Name + $current.Namespace | Should -Be $configData.AllNodes[0].Namespace + $current.NameServers | Should -Be $configData.AllNodes[0].NameServers + $current.Ensure | Should -Be $configData.AllNodes[0].Ensure + } + + It 'Should have deleted the NRPT rule' { + Get-DnsClientNrptRule -Name $script:dummyRule.Name | Should -BeNullOrEmpty + } + } + } +} +finally +{ + # Clean up any created rules just in case the integration tests fail + $null = Remove-DnsClientNrptRule $script:dummyRule.Name ` + -Force` + -ErrorAction SilentlyContinue + + Restore-TestEnvironment -TestEnvironment $script:testEnvironment +} diff --git a/tests/Integration/DSC_DnsClientNrptRule.config.ps1 b/tests/Integration/DSC_DnsClientNrptRule.config.ps1 new file mode 100644 index 00000000..c5090f0f --- /dev/null +++ b/tests/Integration/DSC_DnsClientNrptRule.config.ps1 @@ -0,0 +1,19 @@ +configuration DSC_DnsClientNrptRule_Config { + param + ( + [Parameter()] + [System.String[]] + $NodeName = 'localhost' + ) + + Import-DscResource -ModuleName NetworkingDsc + + Node $NodeName { + DnsClientNrptRule Integration_Test { + Name = $Node.Name + Namespace = $Node.Namespace + NameEncoding = $Node.NameEncoding + NameServers = $Node.NameServers + } + } +} From 0fa9796e0ffbb4a55678d7a79a8cd9a4489bea7a Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Tue, 11 Mar 2025 16:58:18 +0100 Subject: [PATCH 05/35] Add DnsClientNrptRule unit testing --- tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 308 +++++++++++++++++++++ 1 file changed, 308 insertions(+) create mode 100644 tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 new file mode 100644 index 00000000..5eb2272b --- /dev/null +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -0,0 +1,308 @@ +$script:dscModuleName = 'NetworkingDsc' +$script:dscResourceName = 'DSC_DnsClientNrptRule' + +function Invoke-TestSetup +{ + try + { + Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' + } + catch [System.IO.FileNotFoundException] + { + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' + } + + $script:testEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dscResourceName ` + -ResourceType 'Mof' ` + -TestType 'Unit' + + Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') +} + +function Invoke-TestCleanup +{ + Restore-TestEnvironment -TestEnvironment $script:testEnvironment +} + +Invoke-TestSetup + +# Begin Testing +try +{ + InModuleScope $script:dscResourceName { + # Create the Mock Objects that will be used for running tests + $testNrptRule = [PSObject]@{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + + $testNrptRuleKeys = [PSObject]@{ + Name = $testNrptRule.Name + Namespace = $testNrptRule.Namespace + NameServers = $testNrptRule.NameServers + NextHop = $testNrptRule.NextHop + } + + $mockNrptRule = [PSObject]@{ + Name = $testNrptRule.Name + Namespace = $testNrptRule.Namespace + NameServers = $testNrptRule.NameServers + Ensure = $testNrptRule.Ensure + + } + + Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { + Context 'NRPT Rule does not exist' { + Mock -CommandName Get-DnsClientNrptRule + + It 'Should return absent NRPT Rule' { + $result = Get-TargetResource @testNrptRuleKeys + $result.Ensure | Should -Be 'Absent' + } + + It 'Should call the expected mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } + + Context 'NRPT Rule does exist' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + + It 'Should return correct NRPT Rule' { + $result = Get-TargetResource @testNrptRuleKeys + $result.Ensure | Should -Be 'Present' + $result.Namespace | Should -Be $testNrptRule.Namespace + $result.NameServers | Should -Be $testNrptRule.NameServers + } + + It 'Should call the expected mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } + } + + Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { + Context 'NRPT Rule does not exist but should' { + Mock -CommandName Get-DnsClientNrptRule + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + } + } + + Context 'NRPT Rule exists and should but has a different Namespace' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + $setTargetResourceParameters.Namespace = '.fabrikam.com' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + } + } + + Context 'NRPT Rule exists and should but has a different NameServers' { + Mock -CommandName Get-DnsClientNrptRule-MockWith { $mockNrptRule } + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + $setTargetResourceParameters.NameServers = ('192.168.0.1') + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + } + } + + + Context 'NRPT Rule exists and but should not' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule ` + -ParameterFilter { + ($Namespace -eq $testNrptRule.Namespace) -and ` + ($NameServers -eq $testNrptRule.NameServers) + } + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + $setTargetResourceParameters.Ensure = 'Absent' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected mocks and parameters' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule ` + -ParameterFilter { + ($Namespace -eq $testNrptRule.Namespace) -and ` + ($NameServers -eq $testNrptRule.NameServers) + } ` + -Exactly -Times 1 + } + } + + Context 'NRPT Rule does not exist and should not' { + Mock -CommandName Get-DnsClientNrptRule + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + $setTargetResourceParameters.Ensure = 'Absent' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + } + } + } + + Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { + Context 'NRPT Rule does not exist but should' { + Mock -CommandName Get-DnsClientNrptRule + + It 'Should return false' { + $testTargetResourceParameters = $testNrptRule.Clone() + Test-TargetResource @testTargetResourceParameters | Should -Be $False + + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } + + Context 'NRPT Rule exists and should but has a different Namespace' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + + It 'Should return false' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + $testTargetResourceParameters.Namespace = '.fabrikam.com' + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } + + Context 'NRPT Rule exists and should but has a different NameServers' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + + It 'Should return false' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + $testTargetResourceParameters.NameServers = ('192.168.0.1') + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } + + Context 'NRPT Rule exists and should and all parameters match' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + + It 'Should return true' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + Test-TargetResource @testTargetResourceParameters | Should -Be $True + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } + + Context 'NRPT Rule exists but should not' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + + It 'Should return false' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + $testTargetResourceParameters.Ensure = 'Absent' + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } + + Context 'NRPT Rule does not exist and should not' { + Mock -CommandName Get-DnsClientNrptRule + + It 'Should return true' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + $testTargetResourceParameters.Ensure = 'Absent' + Test-TargetResource @testTargetResourceParameters | Should -Be $True + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } + } + } +} +finally +{ + Invoke-TestCleanup +} From 2efc6e0cbd30678e5cff447b188d903635158242 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 11:19:30 +0100 Subject: [PATCH 06/35] Moving to Pester 5 tests --- tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 | 327 ++++++------ tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 501 ++++++++++--------- 2 files changed, 453 insertions(+), 375 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 index ac9b9612..97613155 100644 --- a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 @@ -1,16 +1,32 @@ -$script:dscModuleName = 'NetworkingDsc' -$script:dscResourceName = 'DSC_DnsClientNrptGlobal' +# Suppressing this rule because Script Analyzer does not understand Pester's syntax. +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +param () -function Invoke-TestSetup -{ +BeforeDiscovery { try { - Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' + 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' 3>&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 -Tasks build" first.' + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' } +} + +BeforeAll { + $script:dscModuleName = 'NetworkingDsc' + $script:dscResourceName = 'DSC_DnsClientNrptGlobal' $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` @@ -19,183 +35,202 @@ function Invoke-TestSetup -TestType 'Unit' Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscResourceName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscResourceName + $PSDefaultParameterValues['Should:ModuleName'] = $script:dscResourceName } -function Invoke-TestCleanup -{ +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + Restore-TestEnvironment -TestEnvironment $script:testEnvironment + + # Remove module common test helper. + Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + # Unload the module being tested so that it doesn't impact any other tests. + Get-Module -Name $script:dscResourceName -All | Remove-Module -Force } -Invoke-TestSetup -# Begin Testing -try -{ - InModuleScope $script:dscResourceName { +Describe 'DSC_DnsClientNrptGlobal\Get-TargetResource' -Tag 'Get' { + BeforeAll { # Create the Mock Objects that will be used for running tests $DnsClientNrptGlobal = [PSObject] @{ EnableDAForAllNetworks = 'Disable' QueryPolicy = 'Disable' SecureNameQueryFallback = 'Disable' } - + $DnsClientNrptGlobalSplat = [PSObject]@{ IsSingleInstance = 'Yes' EnableDAForAllNetworks = $DnsClientNrptGlobal.EnableDAForAllNetworks QueryPolicy = $DnsClientNrptGlobal.QueryPolicy SecureNameQueryFallback = $DnsClientNrptGlobal.SecureNameQueryFallback } + } - Describe 'DSC_DnsClientNrptGlobal\Get-TargetResource' -Tag 'Get' { - BeforeEach { - Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } - } + BeforeEach { + Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } + } - Context 'DNS Client NRPT Global Settings Exists' { - It 'Should return correct DNS Client NRPT Global Settings values' { - $getTargetResourceParameters = Get-TargetResource -IsSingleInstance 'Yes' - $getTargetResourceParameters.EnableDAForAllNetworks | Should -Be $DnsClientNrptGlobal.EnableDAForAllNetworks - $getTargetResourceParameters.QueryPolicy | Should -Be $DnsClientNrptGlobal.QueryPolicy - $getTargetResourceParameters.SecureNameQueryFallback | Should -Be $DnsClientNrptGlobal.SecureNameQueryFallback - } - - It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 - } - } + Context 'DNS Client NRPT Global Settings Exists' { + It 'Should return correct DNS Client NRPT Global Settings values' { + $getTargetResourceParameters = Get-TargetResource -IsSingleInstance 'Yes' + $getTargetResourceParameters.EnableDAForAllNetworks | Should -Be $DnsClientNrptGlobal.EnableDAForAllNetworks + $getTargetResourceParameters.QueryPolicy | Should -Be $DnsClientNrptGlobal.QueryPolicy + $getTargetResourceParameters.SecureNameQueryFallback | Should -Be $DnsClientNrptGlobal.SecureNameQueryFallback } - Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { - BeforeEach { - Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } - } + It 'Should call the expected mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 + } + } +} - Context 'DNS Client NRPT Global Settings all parameters are the same' { - Mock -CommandName Set-DnsClientNrptGlobal +Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { + BeforeEach { + Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } + } - It 'Should not throw error' { - { - $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw - } + Context 'DNS Client NRPT Global Settings all parameters are the same' { + Mock -CommandName Set-DnsClientNrptGlobal - It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 0 - } - } + It 'Should not throw error' { + { + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 0 + } + } + + Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { + Mock -CommandName Set-DnsClientNrptGlobal + + It 'Should not throw error' { + { + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 + } + } + + + Context 'DNS Client NRPT Global Settings QueryPolicy is different' { + Mock -CommandName Set-DnsClientNrptGlobal + + It 'Should not throw error' { + { + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters.QueryPolicy = 'QueryBoth' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } - Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { - Mock -CommandName Set-DnsClientNrptGlobal - - It 'Should not throw error' { - { - $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - $setTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw - } - - It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 - } + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 + } + } + + Context 'DNS Client NRPT Global Settings SecureNameQueryFallback is different' { + Mock -CommandName Set-DnsClientNrptGlobal + + It 'Should not throw error' { + { + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 + } + } +} + +Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { + BeforeAll { + # Create the Mock Objects that will be used for running tests + $DnsClientNrptGlobal = [PSObject] @{ + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + + $DnsClientNrptGlobalSplat = [PSObject]@{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = $DnsClientNrptGlobal.EnableDAForAllNetworks + QueryPolicy = $DnsClientNrptGlobal.QueryPolicy + SecureNameQueryFallback = $DnsClientNrptGlobal.SecureNameQueryFallback + } + } + Context 'DNS Client NRPT Global Settings configuration' { + BeforeEach { + Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } + } + + Context 'DNS Client NRPT Global Settings all parameters are the same' { + It 'Should return true' { + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + Test-TargetResource @testTargetResourceParameters | Should -Be $true } + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + } + } - Context 'DNS Client NRPT Global Settings QueryPolicy is different' { - Mock -CommandName Set-DnsClientNrptGlobal + Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { + It 'Should return false' { + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } - It 'Should not throw error' { - { - $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - $setTargetResourceParameters.QueryPolicy = 'QueryBoth' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw - } + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + } + } - It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 - } + Context 'DNS Client NRPT Global Settings QueryPolicy is different' { + It 'Should return false' { + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters.QueryPolicy = 'QueryBoth' + Test-TargetResource @testTargetResourceParameters | Should -Be $False } - Context 'DNS Client NRPT Global Settings SecureNameQueryFallback is different' { - Mock -CommandName Set-DnsClientNrptGlobal - - It 'Should not throw error' { - { - $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - $setTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw - } - - It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 - } + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 } } - Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { - Context 'DNS Client NRPT Global Settings configuration' { - BeforeEach { - Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } - } - - Context 'DNS Client NRPT Global Settings all parameters are the same' { - It 'Should return true' { - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - Test-TargetResource @testTargetResourceParameters | Should -Be $true - } - - It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - } - } - - Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { - It 'Should return false' { - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - $testTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' - Test-TargetResource @testTargetResourceParameters | Should -Be $False - } - - It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - } - } - - Context 'DNS Client NRPT Global Settings QueryPolicy is different' { - It 'Should return false' { - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - $testTargetResourceParameters.QueryPolicy = 'QueryBoth' - Test-TargetResource @testTargetResourceParameters | Should -Be $False - } - - It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - } - } - - Context 'DNS Client NRPT Global Settings UseDevolution is different' { - It 'Should return false' { - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - $testTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' - Test-TargetResource @testTargetResourceParameters | Should -Be $False - } - - It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - } - } + Context 'DNS Client NRPT Global Settings UseDevolution is different' { + It 'Should return false' { + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } + + It 'Should call expected Mocks' { + Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 } } } } -finally -{ - Invoke-TestCleanup -} diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 index 5eb2272b..f753af5e 100644 --- a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -1,16 +1,32 @@ -$script:dscModuleName = 'NetworkingDsc' -$script:dscResourceName = 'DSC_DnsClientNrptRule' +# Suppressing this rule because Script Analyzer does not understand Pester's syntax. +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] +param () -function Invoke-TestSetup -{ +BeforeDiscovery { try { - Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' + 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' 3>&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 -Tasks build" first.' + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' } +} + +BeforeAll { + $script:dscModuleName = 'NetworkingDsc' + $script:dscResourceName = 'DSC_DnsClientNrptRule' $script:testEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:dscModuleName ` @@ -19,19 +35,29 @@ function Invoke-TestSetup -TestType 'Unit' Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscResourceName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:dscResourceName + $PSDefaultParameterValues['Should:ModuleName'] = $script:dscResourceName } -function Invoke-TestCleanup -{ +AfterAll { + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + Restore-TestEnvironment -TestEnvironment $script:testEnvironment + + # Remove module common test helper. + Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force + + # Unload the module being tested so that it doesn't impact any other tests. + Get-Module -Name $script:dscResourceName -All | Remove-Module -Force } -Invoke-TestSetup -# Begin Testing -try -{ - InModuleScope $script:dscResourceName { +Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { + BeforeAll { # Create the Mock Objects that will be used for running tests $testNrptRule = [PSObject]@{ Name = 'Server' @@ -39,270 +65,287 @@ try NameServers = ('192.168.1.1') Ensure = 'Present' } - + $testNrptRuleKeys = [PSObject]@{ Name = $testNrptRule.Name Namespace = $testNrptRule.Namespace NameServers = $testNrptRule.NameServers NextHop = $testNrptRule.NextHop } - + $mockNrptRule = [PSObject]@{ Name = $testNrptRule.Name Namespace = $testNrptRule.Namespace NameServers = $testNrptRule.NameServers Ensure = $testNrptRule.Ensure + + } + } + Context 'NRPT Rule does not exist' { + Mock -CommandName Get-DnsClientNrptRule + It 'Should return absent NRPT Rule' { + $result = Get-TargetResource @testNrptRuleKeys + $result.Ensure | Should -Be 'Absent' } - Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { - Context 'NRPT Rule does not exist' { - Mock -CommandName Get-DnsClientNrptRule + It 'Should call the expected mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } - It 'Should return absent NRPT Rule' { - $result = Get-TargetResource @testNrptRuleKeys - $result.Ensure | Should -Be 'Absent' - } + Context 'NRPT Rule does exist' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } - It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - } - } + It 'Should return correct NRPT Rule' { + $result = Get-TargetResource @testNrptRuleKeys + $result.Ensure | Should -Be 'Present' + $result.Namespace | Should -Be $testNrptRule.Namespace + $result.NameServers | Should -Be $testNrptRule.NameServers + } - Context 'NRPT Rule does exist' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + It 'Should call the expected mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } +} - It 'Should return correct NRPT Rule' { - $result = Get-TargetResource @testNrptRuleKeys - $result.Ensure | Should -Be 'Present' - $result.Namespace | Should -Be $testNrptRule.Namespace - $result.NameServers | Should -Be $testNrptRule.NameServers - } +Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { + Context 'NRPT Rule does not exist but should' { + Mock -CommandName Get-DnsClientNrptRule + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } - It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - } - } + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 } + } - Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { - Context 'NRPT Rule does not exist but should' { - Mock -CommandName Get-DnsClientNrptRule - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule - - It 'Should not throw error' { - { - $setTargetResourceParameters = $testNrptRule.Clone() - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw - } - - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 - } - } + Context 'NRPT Rule exists and should but has a different Namespace' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + $setTargetResourceParameters.Namespace = '.fabrikam.com' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } - Context 'NRPT Rule exists and should but has a different Namespace' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule - - It 'Should not throw error' { - { - $setTargetResourceParameters = $testNrptRule.Clone() - $setTargetResourceParameters.Namespace = '.fabrikam.com' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw - } - - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 - } - } + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + } + } - Context 'NRPT Rule exists and should but has a different NameServers' { - Mock -CommandName Get-DnsClientNrptRule-MockWith { $mockNrptRule } - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule - - It 'Should not throw error' { - { - $setTargetResourceParameters = $testNrptRule.Clone() - $setTargetResourceParameters.NameServers = ('192.168.0.1') - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw - } - - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 - } - } + Context 'NRPT Rule exists and should but has a different NameServers' { + Mock -CommandName Get-DnsClientNrptRule-MockWith { $mockNrptRule } + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + $setTargetResourceParameters.NameServers = ('192.168.0.1') + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + } + } - Context 'NRPT Rule exists and but should not' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule ` - -ParameterFilter { + Context 'NRPT Rule exists and but should not' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule ` + -ParameterFilter { + ($Namespace -eq $testNrptRule.Namespace) -and ` + ($NameServers -eq $testNrptRule.NameServers) + } + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + $setTargetResourceParameters.Ensure = 'Absent' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw + } + + It 'Should call expected mocks and parameters' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule ` + -ParameterFilter { ($Namespace -eq $testNrptRule.Namespace) -and ` ($NameServers -eq $testNrptRule.NameServers) - } - - It 'Should not throw error' { - { - $setTargetResourceParameters = $testNrptRule.Clone() - $setTargetResourceParameters.Ensure = 'Absent' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw - } - - It 'Should call expected mocks and parameters' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule ` - -ParameterFilter { - ($Namespace -eq $testNrptRule.Namespace) -and ` - ($NameServers -eq $testNrptRule.NameServers) - } ` - -Exactly -Times 1 - } - } + } ` + -Exactly -Times 1 + } + } - Context 'NRPT Rule does not exist and should not' { - Mock -CommandName Get-DnsClientNrptRule - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule - - It 'Should not throw error' { - { - $setTargetResourceParameters = $testNrptRule.Clone() - $setTargetResourceParameters.Ensure = 'Absent' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw - } - - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 - } - } + Context 'NRPT Rule does not exist and should not' { + Mock -CommandName Get-DnsClientNrptRule + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + + It 'Should not throw error' { + { + $setTargetResourceParameters = $testNrptRule.Clone() + $setTargetResourceParameters.Ensure = 'Absent' + Set-TargetResource @setTargetResourceParameters + } | Should -Not -Throw } - Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { - Context 'NRPT Rule does not exist but should' { - Mock -CommandName Get-DnsClientNrptRule + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 + Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + } + } +} + +Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { + BeforeAll { + # Create the Mock Objects that will be used for running tests + $testNrptRule = [PSObject]@{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + + $testNrptRuleKeys = [PSObject]@{ + Name = $testNrptRule.Name + Namespace = $testNrptRule.Namespace + NameServers = $testNrptRule.NameServers + NextHop = $testNrptRule.NextHop + } + + $mockNrptRule = [PSObject]@{ + Name = $testNrptRule.Name + Namespace = $testNrptRule.Namespace + NameServers = $testNrptRule.NameServers + Ensure = $testNrptRule.Ensure + + } + } + Context 'NRPT Rule does not exist but should' { + Mock -CommandName Get-DnsClientNrptRule - It 'Should return false' { - $testTargetResourceParameters = $testNrptRule.Clone() - Test-TargetResource @testTargetResourceParameters | Should -Be $False + It 'Should return false' { + $testTargetResourceParameters = $testNrptRule.Clone() + Test-TargetResource @testTargetResourceParameters | Should -Be $False - } + } - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - } - } + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } - Context 'NRPT Rule exists and should but has a different Namespace' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Context 'NRPT Rule exists and should but has a different Namespace' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } - It 'Should return false' { - { - $testTargetResourceParameters = $testNrptRule.Clone() - $testTargetResourceParameters.Namespace = '.fabrikam.com' - Test-TargetResource @testTargetResourceParameters | Should -Be $False - } | Should -Not -Throw - } + It 'Should return false' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + $testTargetResourceParameters.Namespace = '.fabrikam.com' + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } | Should -Not -Throw + } - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - } - } + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } - Context 'NRPT Rule exists and should but has a different NameServers' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Context 'NRPT Rule exists and should but has a different NameServers' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } - It 'Should return false' { - { - $testTargetResourceParameters = $testNrptRule.Clone() - $testTargetResourceParameters.NameServers = ('192.168.0.1') - Test-TargetResource @testTargetResourceParameters | Should -Be $False - } | Should -Not -Throw - } + It 'Should return false' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + $testTargetResourceParameters.NameServers = ('192.168.0.1') + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } | Should -Not -Throw + } - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - } - } + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } - Context 'NRPT Rule exists and should and all parameters match' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Context 'NRPT Rule exists and should and all parameters match' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } - It 'Should return true' { - { - $testTargetResourceParameters = $testNrptRule.Clone() - Test-TargetResource @testTargetResourceParameters | Should -Be $True - } | Should -Not -Throw - } + It 'Should return true' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + Test-TargetResource @testTargetResourceParameters | Should -Be $True + } | Should -Not -Throw + } - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - } - } + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } - Context 'NRPT Rule exists but should not' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Context 'NRPT Rule exists but should not' { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } - It 'Should return false' { - { - $testTargetResourceParameters = $testNrptRule.Clone() - $testTargetResourceParameters.Ensure = 'Absent' - Test-TargetResource @testTargetResourceParameters | Should -Be $False - } | Should -Not -Throw - } + It 'Should return false' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + $testTargetResourceParameters.Ensure = 'Absent' + Test-TargetResource @testTargetResourceParameters | Should -Be $False + } | Should -Not -Throw + } - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - } - } + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + } + } - Context 'NRPT Rule does not exist and should not' { - Mock -CommandName Get-DnsClientNrptRule + Context 'NRPT Rule does not exist and should not' { + Mock -CommandName Get-DnsClientNrptRule - It 'Should return true' { - { - $testTargetResourceParameters = $testNrptRule.Clone() - $testTargetResourceParameters.Ensure = 'Absent' - Test-TargetResource @testTargetResourceParameters | Should -Be $True - } | Should -Not -Throw - } + It 'Should return true' { + { + $testTargetResourceParameters = $testNrptRule.Clone() + $testTargetResourceParameters.Ensure = 'Absent' + Test-TargetResource @testTargetResourceParameters | Should -Be $True + } | Should -Not -Throw + } - It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - } - } + It 'Should call expected Mocks' { + Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 } } } -finally -{ - Invoke-TestCleanup -} From bf83cf258ac435a84c7629953cfdb30357667f36 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 11:48:13 +0100 Subject: [PATCH 07/35] Fixing tests --- .../DSC_DnsClientNrptGlobal.Integration.Tests.ps1 | 6 +++--- .../DSC_DnsClientNrptRule.Integration.Tests.ps1 | 2 +- tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 index be494cf1..ae00ef3b 100644 --- a/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 +++ b/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 @@ -92,9 +92,9 @@ finally { # Clean up Set-DnsClientNrptGlobal ` - -SuffixSearchList $script:currentDnsClientNrptGlobal.EnableDAForAllNetworks ` - -UseDevolution $script:currentDnsClientNrptGlobal.QueryPolicy ` - -DevolutionLevel $script:currentDnsClientNrptGlobal.SecureNameQueryFallback + -EnableDAForAllNetworks $script:currentDnsClientNrptGlobal.EnableDAForAllNetworks ` + -QueryPolicy $script:currentDnsClientNrptGlobal.QueryPolicy ` + -SecureNameQueryFallback $script:currentDnsClientNrptGlobal.SecureNameQueryFallback Restore-TestEnvironment -TestEnvironment $script:testEnvironment } diff --git a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 index c341a4d6..dee5d8cd 100644 --- a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 +++ b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 @@ -134,7 +134,7 @@ finally { # Clean up any created rules just in case the integration tests fail $null = Remove-DnsClientNrptRule $script:dummyRule.Name ` - -Force` + -Force ` -ErrorAction SilentlyContinue Restore-TestEnvironment -TestEnvironment $script:testEnvironment diff --git a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 index 97613155..4ba66fdf 100644 --- a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 @@ -92,6 +92,21 @@ Describe 'DSC_DnsClientNrptGlobal\Get-TargetResource' -Tag 'Get' { } Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { + BeforeAll { + # Create the Mock Objects that will be used for running tests + $DnsClientNrptGlobal = [PSObject] @{ + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + + $DnsClientNrptGlobalSplat = [PSObject]@{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = $DnsClientNrptGlobal.EnableDAForAllNetworks + QueryPolicy = $DnsClientNrptGlobal.QueryPolicy + SecureNameQueryFallback = $DnsClientNrptGlobal.SecureNameQueryFallback + } + } BeforeEach { Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } } From dcd14ffc191dd85892b5dc99f95293f4ceca54f7 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 12:42:03 +0100 Subject: [PATCH 08/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 | 196 +++++++++++-------- 1 file changed, 118 insertions(+), 78 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 index 4ba66fdf..0de892c1 100644 --- a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 @@ -55,38 +55,32 @@ AfterAll { Get-Module -Name $script:dscResourceName -All | Remove-Module -Force } - Describe 'DSC_DnsClientNrptGlobal\Get-TargetResource' -Tag 'Get' { - BeforeAll { - # Create the Mock Objects that will be used for running tests - $DnsClientNrptGlobal = [PSObject] @{ - EnableDAForAllNetworks = 'Disable' - QueryPolicy = 'Disable' - SecureNameQueryFallback = 'Disable' - } - - $DnsClientNrptGlobalSplat = [PSObject]@{ - IsSingleInstance = 'Yes' - EnableDAForAllNetworks = $DnsClientNrptGlobal.EnableDAForAllNetworks - QueryPolicy = $DnsClientNrptGlobal.QueryPolicy - SecureNameQueryFallback = $DnsClientNrptGlobal.SecureNameQueryFallback - } - } - BeforeEach { - Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } + Mock -CommandName Get-DnsClientNrptGlobal -MockWith { + @{ + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + } } Context 'DNS Client NRPT Global Settings Exists' { It 'Should return correct DNS Client NRPT Global Settings values' { - $getTargetResourceParameters = Get-TargetResource -IsSingleInstance 'Yes' - $getTargetResourceParameters.EnableDAForAllNetworks | Should -Be $DnsClientNrptGlobal.EnableDAForAllNetworks - $getTargetResourceParameters.QueryPolicy | Should -Be $DnsClientNrptGlobal.QueryPolicy - $getTargetResourceParameters.SecureNameQueryFallback | Should -Be $DnsClientNrptGlobal.SecureNameQueryFallback + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + + $getTargetResourceParameters = Get-TargetResource -IsSingleInstance 'Yes' + + $getTargetResourceParameters.EnableDAForAllNetworks | Should -Be 'Disable' + $getTargetResourceParameters.QueryPolicy | Should -Be 'Disable' + $getTargetResourceParameters.SecureNameQueryFallback | Should -Be 'Disable' + } } It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context } } } @@ -94,88 +88,109 @@ Describe 'DSC_DnsClientNrptGlobal\Get-TargetResource' -Tag 'Get' { Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { BeforeAll { # Create the Mock Objects that will be used for running tests - $DnsClientNrptGlobal = [PSObject] @{ + $DnsClientNrptGlobalSplat = @{ + IsSingleInstance = 'Yes' EnableDAForAllNetworks = 'Disable' QueryPolicy = 'Disable' SecureNameQueryFallback = 'Disable' } - - $DnsClientNrptGlobalSplat = [PSObject]@{ - IsSingleInstance = 'Yes' - EnableDAForAllNetworks = $DnsClientNrptGlobal.EnableDAForAllNetworks - QueryPolicy = $DnsClientNrptGlobal.QueryPolicy - SecureNameQueryFallback = $DnsClientNrptGlobal.SecureNameQueryFallback - } } + BeforeEach { - Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } + Mock -CommandName Get-DnsClientNrptGlobal -MockWith { + @{ + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + } } Context 'DNS Client NRPT Global Settings all parameters are the same' { - Mock -CommandName Set-DnsClientNrptGlobal + BeforeAll { + Mock -CommandName Set-DnsClientNrptGlobal + } It 'Should not throw error' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw + + { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw + } } It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 0 + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal-Exactly -Times 0 -Scope Context } } Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { - Mock -CommandName Set-DnsClientNrptGlobal + BeforeAll { + Mock -CommandName Set-DnsClientNrptGlobal + } It 'Should not throw error' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() $setTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw + + { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw + } } It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal-Exactly -Times 0 -Scope Context } } Context 'DNS Client NRPT Global Settings QueryPolicy is different' { - Mock -CommandName Set-DnsClientNrptGlobal + BeforeAll { + Mock -CommandName Set-DnsClientNrptGlobal + } It 'Should not throw error' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() $setTargetResourceParameters.QueryPolicy = 'QueryBoth' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw + + { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw + } } It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal-Exactly -Times 0 -Scope Context } } Context 'DNS Client NRPT Global Settings SecureNameQueryFallback is different' { - Mock -CommandName Set-DnsClientNrptGlobal + BeforeAll { + Mock -CommandName Set-DnsClientNrptGlobal + } It 'Should not throw error' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() $setTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw + + { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw + } } It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 - Assert-MockCalled -commandName Set-DnsClientNrptGlobal -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal-Exactly -Times 0 -Scope Context } } } @@ -183,19 +198,24 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { BeforeAll { # Create the Mock Objects that will be used for running tests - $DnsClientNrptGlobal = [PSObject] @{ + $DnsClientNrptGlobalSplat = @{ + IsSingleInstance = 'Yes' EnableDAForAllNetworks = 'Disable' QueryPolicy = 'Disable' SecureNameQueryFallback = 'Disable' } - - $DnsClientNrptGlobalSplat = [PSObject]@{ - IsSingleInstance = 'Yes' - EnableDAForAllNetworks = $DnsClientNrptGlobal.EnableDAForAllNetworks - QueryPolicy = $DnsClientNrptGlobal.QueryPolicy - SecureNameQueryFallback = $DnsClientNrptGlobal.SecureNameQueryFallback + } + + BeforeEach { + Mock -CommandName Get-DnsClientNrptGlobal -MockWith { + @{ + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } } } + Context 'DNS Client NRPT Global Settings configuration' { BeforeEach { Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } @@ -203,48 +223,68 @@ Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { Context 'DNS Client NRPT Global Settings all parameters are the same' { It 'Should return true' { - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - Test-TargetResource @testTargetResourceParameters | Should -Be $true + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + + Test-TargetResource @testTargetResourceParameters | Should -BeTrue + } } It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context } } Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { It 'Should return false' { - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - $testTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' - Test-TargetResource @testTargetResourceParameters | Should -Be $False + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' + + Test-TargetResource @testTargetResourceParameters | Should -BeFalse + } } It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context } } Context 'DNS Client NRPT Global Settings QueryPolicy is different' { It 'Should return false' { - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - $testTargetResourceParameters.QueryPolicy = 'QueryBoth' - Test-TargetResource @testTargetResourceParameters | Should -Be $False + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters.QueryPolicy = 'QueryBoth' + + Test-TargetResource @testTargetResourceParameters | Should -BeFalse + } } It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context } } Context 'DNS Client NRPT Global Settings UseDevolution is different' { It 'Should return false' { - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() - $testTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' - Test-TargetResource @testTargetResourceParameters | Should -Be $False + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + + $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' + + Test-TargetResource @testTargetResourceParameters | Should -BeFalse + } } It 'Should call expected Mocks' { - Assert-MockCalled -commandName Get-DnsClientNrptGlobal -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context } } } From ee6222058d68b5efb0dc921ae12178f40b2a28a9 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 13:07:02 +0100 Subject: [PATCH 09/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 | 82 +++++++++++++------- 1 file changed, 54 insertions(+), 28 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 index 0de892c1..ba549fb9 100644 --- a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 @@ -86,16 +86,6 @@ Describe 'DSC_DnsClientNrptGlobal\Get-TargetResource' -Tag 'Get' { } Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { - BeforeAll { - # Create the Mock Objects that will be used for running tests - $DnsClientNrptGlobalSplat = @{ - IsSingleInstance = 'Yes' - EnableDAForAllNetworks = 'Disable' - QueryPolicy = 'Disable' - SecureNameQueryFallback = 'Disable' - } - } - BeforeEach { Mock -CommandName Get-DnsClientNrptGlobal -MockWith { @{ @@ -115,7 +105,12 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw } @@ -136,7 +131,13 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + $setTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw @@ -159,7 +160,13 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + $setTargetResourceParameters.QueryPolicy = 'QueryBoth' { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw @@ -181,7 +188,13 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $setTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $setTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + $setTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw @@ -196,16 +209,6 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { } Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { - BeforeAll { - # Create the Mock Objects that will be used for running tests - $DnsClientNrptGlobalSplat = @{ - IsSingleInstance = 'Yes' - EnableDAForAllNetworks = 'Disable' - QueryPolicy = 'Disable' - SecureNameQueryFallback = 'Disable' - } - } - BeforeEach { Mock -CommandName Get-DnsClientNrptGlobal -MockWith { @{ @@ -226,7 +229,12 @@ Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } Test-TargetResource @testTargetResourceParameters | Should -BeTrue } @@ -242,7 +250,13 @@ Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + $testTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' Test-TargetResource @testTargetResourceParameters | Should -BeFalse @@ -259,7 +273,13 @@ Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + $testTargetResourceParameters.QueryPolicy = 'QueryBoth' Test-TargetResource @testTargetResourceParameters | Should -BeFalse @@ -276,7 +296,13 @@ Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $testTargetResourceParameters = $DnsClientNrptGlobalSplat.Clone() + $testTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' + } + $testTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' Test-TargetResource @testTargetResourceParameters | Should -BeFalse From c6477af1cded6320ee32791fc414a7fc449ddea9 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 13:24:56 +0100 Subject: [PATCH 10/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 | 138 +++++++++---------- 1 file changed, 66 insertions(+), 72 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 index ba549fb9..77fa5806 100644 --- a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 @@ -118,7 +118,7 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { It 'Should call expected Mocks' { Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context - Should -Invoke -CommandName Set-DnsClientNrptGlobal-Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 0 -Scope Context } } @@ -146,7 +146,7 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { It 'Should call expected Mocks' { Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context - Should -Invoke -CommandName Set-DnsClientNrptGlobal-Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 0 -Scope Context } } @@ -175,7 +175,7 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { It 'Should call expected Mocks' { Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context - Should -Invoke -CommandName Set-DnsClientNrptGlobal-Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 0 -Scope Context } } @@ -203,7 +203,7 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { It 'Should call expected Mocks' { Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context - Should -Invoke -CommandName Set-DnsClientNrptGlobal-Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 0 -Scope Context } } } @@ -219,99 +219,93 @@ Describe 'DSC_DnsClientNrptGlobal\Test-TargetResource' -Tag 'Test' { } } - Context 'DNS Client NRPT Global Settings configuration' { - BeforeEach { - Mock -CommandName Get-DnsClientNrptGlobal -MockWith { $DnsClientNrptGlobal } - } - - Context 'DNS Client NRPT Global Settings all parameters are the same' { - It 'Should return true' { - InModuleScope -ScriptBlock { - Set-StrictMode -Version 1.0 - - $testTargetResourceParameters = @{ - IsSingleInstance = 'Yes' - EnableDAForAllNetworks = 'Disable' - QueryPolicy = 'Disable' - SecureNameQueryFallback = 'Disable' - } + Context 'DNS Client NRPT Global Settings all parameters are the same' { + It 'Should return true' { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 - Test-TargetResource @testTargetResourceParameters | Should -BeTrue + $testTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' } - } - It 'Should call expected Mocks' { - Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + Test-TargetResource @testTargetResourceParameters | Should -BeTrue } } - Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { - It 'Should return false' { - InModuleScope -ScriptBlock { - Set-StrictMode -Version 1.0 - - $testTargetResourceParameters = @{ - IsSingleInstance = 'Yes' - EnableDAForAllNetworks = 'Disable' - QueryPolicy = 'Disable' - SecureNameQueryFallback = 'Disable' - } + It 'Should call expected Mocks' { + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + } + } - $testTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' + Context 'DNS Client NRPT Global Settings EnableDAForAllNetworks is different' { + It 'Should return false' { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 - Test-TargetResource @testTargetResourceParameters | Should -BeFalse + $testTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' } - } - It 'Should call expected Mocks' { - Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + $testTargetResourceParameters.EnableDAForAllNetworks = 'EnableAlways' + + Test-TargetResource @testTargetResourceParameters | Should -BeFalse } } - Context 'DNS Client NRPT Global Settings QueryPolicy is different' { - It 'Should return false' { - InModuleScope -ScriptBlock { - Set-StrictMode -Version 1.0 - - $testTargetResourceParameters = @{ - IsSingleInstance = 'Yes' - EnableDAForAllNetworks = 'Disable' - QueryPolicy = 'Disable' - SecureNameQueryFallback = 'Disable' - } + It 'Should call expected Mocks' { + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + } + } - $testTargetResourceParameters.QueryPolicy = 'QueryBoth' + Context 'DNS Client NRPT Global Settings QueryPolicy is different' { + It 'Should return false' { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 - Test-TargetResource @testTargetResourceParameters | Should -BeFalse + $testTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' } - } - It 'Should call expected Mocks' { - Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + $testTargetResourceParameters.QueryPolicy = 'QueryBoth' + + Test-TargetResource @testTargetResourceParameters | Should -BeFalse } } - Context 'DNS Client NRPT Global Settings UseDevolution is different' { - It 'Should return false' { - InModuleScope -ScriptBlock { - Set-StrictMode -Version 1.0 - - $testTargetResourceParameters = @{ - IsSingleInstance = 'Yes' - EnableDAForAllNetworks = 'Disable' - QueryPolicy = 'Disable' - SecureNameQueryFallback = 'Disable' - } + It 'Should call expected Mocks' { + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + } + } - $testTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' + Context 'DNS Client NRPT Global Settings UseDevolution is different' { + It 'Should return false' { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 - Test-TargetResource @testTargetResourceParameters | Should -BeFalse + $testTargetResourceParameters = @{ + IsSingleInstance = 'Yes' + EnableDAForAllNetworks = 'Disable' + QueryPolicy = 'Disable' + SecureNameQueryFallback = 'Disable' } - } - It 'Should call expected Mocks' { - Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + $testTargetResourceParameters.SecureNameQueryFallback = 'FallbackSecure' + + Test-TargetResource @testTargetResourceParameters | Should -BeFalse } } + + It 'Should call expected Mocks' { + Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context + } } } From dadd41397447ff2c40383dde756b70ebfdb2ca10 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 13:31:52 +0100 Subject: [PATCH 11/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 index 77fa5806..c756d4d3 100644 --- a/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptGlobal.Tests.ps1 @@ -146,7 +146,7 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { It 'Should call expected Mocks' { Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context - Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context } } @@ -175,7 +175,7 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { It 'Should call expected Mocks' { Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context - Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context } } @@ -203,7 +203,7 @@ Describe 'DSC_DnsClientNrptGlobal\Set-TargetResource' -Tag 'Set' { It 'Should call expected Mocks' { Should -Invoke -CommandName Get-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context - Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptGlobal -Exactly -Times 1 -Scope Context } } } From 060abf65547c4e621e6f89db235699b0e5f23601 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 14:00:43 +0100 Subject: [PATCH 12/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 387 ++++++++++++++------- 1 file changed, 253 insertions(+), 134 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 index f753af5e..bdb77846 100644 --- a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -55,297 +55,416 @@ AfterAll { Get-Module -Name $script:dscResourceName -All | Remove-Module -Force } - Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { BeforeAll { - # Create the Mock Objects that will be used for running tests - $testNrptRule = [PSObject]@{ + $mockNrptRule = @{ Name = 'Server' Namespace = '.contoso.com' NameServers = ('192.168.1.1') Ensure = 'Present' } - - $testNrptRuleKeys = [PSObject]@{ - Name = $testNrptRule.Name - Namespace = $testNrptRule.Namespace - NameServers = $testNrptRule.NameServers - NextHop = $testNrptRule.NextHop - } - - $mockNrptRule = [PSObject]@{ - Name = $testNrptRule.Name - Namespace = $testNrptRule.Namespace - NameServers = $testNrptRule.NameServers - Ensure = $testNrptRule.Ensure - + + $testNrptRule = @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + + InModuleScope -ScriptBlock { + $script:testNrptRule = @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } } } Context 'NRPT Rule does not exist' { - Mock -CommandName Get-DnsClientNrptRule + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule + } It 'Should return absent NRPT Rule' { - $result = Get-TargetResource @testNrptRuleKeys - $result.Ensure | Should -Be 'Absent' + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + + $testNrptRuleKeys = @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + } + + $result = Get-TargetResource @testNrptRuleKeys + $result.Ensure | Should -Be 'Absent' + } } It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context } } Context 'NRPT Rule does exist' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + } It 'Should return correct NRPT Rule' { - $result = Get-TargetResource @testNrptRuleKeys - $result.Ensure | Should -Be 'Present' - $result.Namespace | Should -Be $testNrptRule.Namespace - $result.NameServers | Should -Be $testNrptRule.NameServers + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + + $testNrptRuleKeys = @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + } + + $result = Get-TargetResource @testNrptRuleKeys + $result.Ensure | Should -Be 'Present' + $result.Namespace | Should -Be $testNrptRule.Namespace + $result.NameServers | Should -Be $testNrptRule.NameServers + } } It 'Should call the expected mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context } } } Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { + BeforeAll { + $mockNrptRule = @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + + $testNrptRule = @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + + InModuleScope -ScriptBlock { + $script:testNrptRule = @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + } + } Context 'NRPT Rule does not exist but should' { - Mock -CommandName Get-DnsClientNrptRule - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + } It 'Should not throw error' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $setTargetResourceParameters = $testNrptRule.Clone() - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw + + { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Add-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptRule -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 -Scope Context } } Context 'NRPT Rule exists and should but has a different Namespace' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + } It 'Should not throw error' { - { - $setTargetResourceParameters = $testNrptRule.Clone() + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + + $setTargetResourceParameters = $script:testNrptRule.Clone() $setTargetResourceParameters.Namespace = '.fabrikam.com' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw + + $result = Set-TargetResource @setTargetResourceParameters + + { $result } | Should -Not -Throw + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Add-DnsClientNrptRule -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 -Scope Context } } Context 'NRPT Rule exists and should but has a different NameServers' { - Mock -CommandName Get-DnsClientNrptRule-MockWith { $mockNrptRule } - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + } It 'Should not throw error' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $setTargetResourceParameters = $testNrptRule.Clone() $setTargetResourceParameters.NameServers = ('192.168.0.1') - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw + + { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Add-DnsClientNrptRule -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 -Scope Context } } - Context 'NRPT Rule exists and but should not' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule ` - -ParameterFilter { - ($Namespace -eq $testNrptRule.Namespace) -and ` - ($NameServers -eq $testNrptRule.NameServers) + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule ` + -ParameterFilter { + ($Namespace -eq $testNrptRule.Namespace) -and ` + ($NameServers -eq $testNrptRule.NameServers) + } } It 'Should not throw error' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $setTargetResourceParameters = $testNrptRule.Clone() $setTargetResourceParameters.Ensure = 'Absent' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw + + { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw + } } It 'Should call expected mocks and parameters' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule ` + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Add-DnsClientNrptRule -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptRule -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Remove-DnsClientNrptRule ` -ParameterFilter { ($Namespace -eq $testNrptRule.Namespace) -and ` ($NameServers -eq $testNrptRule.NameServers) - } ` - -Exactly -Times 1 + } -Exactly -Times 1 -Scope Context } } Context 'NRPT Rule does not exist and should not' { - Mock -CommandName Get-DnsClientNrptRule - Mock -CommandName Add-DnsClientNrptRule - Mock -CommandName Set-DnsClientNrptRule - Mock -CommandName Remove-DnsClientNrptRule + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule + Mock -CommandName Add-DnsClientNrptRule + Mock -CommandName Set-DnsClientNrptRule + Mock -CommandName Remove-DnsClientNrptRule + } It 'Should not throw error' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $setTargetResourceParameters = $testNrptRule.Clone() $setTargetResourceParameters.Ensure = 'Absent' - Set-TargetResource @setTargetResourceParameters - } | Should -Not -Throw + + { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 - Assert-MockCalled -CommandName Add-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Set-DnsClientNrptRule -Exactly -Times 0 - Assert-MockCalled -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Add-DnsClientNrptRule -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Set-DnsClientNrptRule -Exactly -Times 0 -Scope Context + Should -Invoke -CommandName Remove-DnsClientNrptRule -Exactly -Times 0 -Scope Context } } } Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { BeforeAll { - # Create the Mock Objects that will be used for running tests - $testNrptRule = [PSObject]@{ + $mockNrptRule = @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + + $testNrptRule = @{ Name = 'Server' Namespace = '.contoso.com' NameServers = ('192.168.1.1') Ensure = 'Present' } - - $testNrptRuleKeys = [PSObject]@{ - Name = $testNrptRule.Name - Namespace = $testNrptRule.Namespace - NameServers = $testNrptRule.NameServers - NextHop = $testNrptRule.NextHop - } - - $mockNrptRule = [PSObject]@{ - Name = $testNrptRule.Name - Namespace = $testNrptRule.Namespace - NameServers = $testNrptRule.NameServers - Ensure = $testNrptRule.Ensure - + + InModuleScope -ScriptBlock { + $script:testNrptRule = @{ + Name = 'Server' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } } } Context 'NRPT Rule does not exist but should' { - Mock -CommandName Get-DnsClientNrptRule + BeforeAll { + Mock -CommandName Get-NetAdapter -MockWith { $mockNetAdapter } + Mock -CommandName Get-DnsClientNrptRule + } It 'Should return false' { - $testTargetResourceParameters = $testNrptRule.Clone() - Test-TargetResource @testTargetResourceParameters | Should -Be $False + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + + $testTargetResourceParameters = $testNrptRule.Clone() + Test-TargetResource @testTargetResourceParameters | Should -BeFalse + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context } } Context 'NRPT Rule exists and should but has a different Namespace' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + } It 'Should return false' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $testTargetResourceParameters = $testNrptRule.Clone() $testTargetResourceParameters.Namespace = '.fabrikam.com' - Test-TargetResource @testTargetResourceParameters | Should -Be $False - } | Should -Not -Throw + + $result = Test-TargetResource @testTargetResourceParameters + + { $result } | Should -Not -Throw + $result | Should -BeFalse + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context } } Context 'NRPT Rule exists and should but has a different NameServers' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + } It 'Should return false' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $testTargetResourceParameters = $testNrptRule.Clone() $testTargetResourceParameters.NameServers = ('192.168.0.1') - Test-TargetResource @testTargetResourceParameters | Should -Be $False - } | Should -Not -Throw + + $result = Test-TargetResource @testTargetResourceParameters + + { $result } | Should -Not -Throw + $result | Should -BeFalse + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context } } Context 'NRPT Rule exists and should and all parameters match' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + } It 'Should return true' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $testTargetResourceParameters = $testNrptRule.Clone() - Test-TargetResource @testTargetResourceParameters | Should -Be $True - } | Should -Not -Throw + + $result = Test-TargetResource @testTargetResourceParameters + + { $result } | Should -Not -Throw + $result | Should -BeTrue + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context } } Context 'NRPT Rule exists but should not' { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + } It 'Should return false' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $testTargetResourceParameters = $testNrptRule.Clone() $testTargetResourceParameters.Ensure = 'Absent' - Test-TargetResource @testTargetResourceParameters | Should -Be $False - } | Should -Not -Throw + + $result = Test-TargetResource @testTargetResourceParameters + + { $result } | Should -Not -Throw + $result | Should -BeFalse + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context } } Context 'NRPT Rule does not exist and should not' { - Mock -CommandName Get-DnsClientNrptRule + BeforeAll { + Mock -CommandName Get-DnsClientNrptRule + } It 'Should return true' { - { + InModuleScope -ScriptBlock { + Set-StrictMode -Version 1.0 + $testTargetResourceParameters = $testNrptRule.Clone() $testTargetResourceParameters.Ensure = 'Absent' - Test-TargetResource @testTargetResourceParameters | Should -Be $True - } | Should -Not -Throw + + $result = Test-TargetResource @testTargetResourceParameters + + { $result } | Should -Not -Throw + $result | Should -BeTrue + } } It 'Should call expected Mocks' { - Assert-MockCalled -CommandName Get-DnsClientNrptRule -Exactly -Times 1 + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context } } } From 36ece967b33620e697e142b77ed395e26ca916fb Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 14:20:43 +0100 Subject: [PATCH 13/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 index bdb77846..3ea715e5 100644 --- a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -89,13 +89,7 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $testNrptRuleKeys = @{ - Name = 'Server' - Namespace = '.contoso.com' - NameServers = ('192.168.1.1') - } - - $result = Get-TargetResource @testNrptRuleKeys + $result = Get-TargetResource -Name 'Server' $result.Ensure | Should -Be 'Absent' } } @@ -114,13 +108,7 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $testNrptRuleKeys = @{ - Name = 'Server' - Namespace = '.contoso.com' - NameServers = ('192.168.1.1') - } - - $result = Get-TargetResource @testNrptRuleKeys + $result = Get-TargetResource -Name 'Server' $result.Ensure | Should -Be 'Present' $result.Namespace | Should -Be $testNrptRule.Namespace $result.NameServers | Should -Be $testNrptRule.NameServers From 24a291dbd9acc76796430a6cd65c9abc6b18b636 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 14:51:29 +0100 Subject: [PATCH 14/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 54 +++++++--------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 index 3ea715e5..2396ba0a 100644 --- a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -57,15 +57,8 @@ AfterAll { Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { BeforeAll { - $mockNrptRule = @{ - Name = 'Server' - Namespace = '.contoso.com' - NameServers = ('192.168.1.1') - Ensure = 'Present' - } - $testNrptRule = @{ - Name = 'Server' + Name = 'Contoso Dns Policy' Namespace = '.contoso.com' NameServers = ('192.168.1.1') Ensure = 'Present' @@ -73,7 +66,7 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { InModuleScope -ScriptBlock { $script:testNrptRule = @{ - Name = 'Server' + Name = 'Contoso Dns Policy' Namespace = '.contoso.com' NameServers = ('192.168.1.1') Ensure = 'Present' @@ -89,7 +82,7 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $result = Get-TargetResource -Name 'Server' + $result = Get-TargetResource -Name 'Contoso Dns Policy' $result.Ensure | Should -Be 'Absent' } } @@ -101,14 +94,14 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { Context 'NRPT Rule does exist' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' } It 'Should return correct NRPT Rule' { InModuleScope -ScriptBlock { Set-StrictMode -Version 1.0 - $result = Get-TargetResource -Name 'Server' + $result = Get-TargetResource -Name 'Contoso Dns Policy' $result.Ensure | Should -Be 'Present' $result.Namespace | Should -Be $testNrptRule.Namespace $result.NameServers | Should -Be $testNrptRule.NameServers @@ -123,15 +116,8 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { BeforeAll { - $mockNrptRule = @{ - Name = 'Server' - Namespace = '.contoso.com' - NameServers = ('192.168.1.1') - Ensure = 'Present' - } - $testNrptRule = @{ - Name = 'Server' + Name = 'Contoso Dns Policy' Namespace = '.contoso.com' NameServers = ('192.168.1.1') Ensure = 'Present' @@ -139,7 +125,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { InModuleScope -ScriptBlock { $script:testNrptRule = @{ - Name = 'Server' + Name = 'Contoso Dns Policy' Namespace = '.contoso.com' NameServers = ('192.168.1.1') Ensure = 'Present' @@ -174,7 +160,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Context 'NRPT Rule exists and should but has a different Namespace' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' Mock -CommandName Add-DnsClientNrptRule Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule @@ -203,7 +189,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Context 'NRPT Rule exists and should but has a different NameServers' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' Mock -CommandName Add-DnsClientNrptRule Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule @@ -230,7 +216,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Context 'NRPT Rule exists and but should not' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' Mock -CommandName Add-DnsClientNrptRule Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule ` @@ -293,15 +279,8 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { BeforeAll { - $mockNrptRule = @{ - Name = 'Server' - Namespace = '.contoso.com' - NameServers = ('192.168.1.1') - Ensure = 'Present' - } - $testNrptRule = @{ - Name = 'Server' + Name = 'Contoso Dns Policy' Namespace = '.contoso.com' NameServers = ('192.168.1.1') Ensure = 'Present' @@ -309,7 +288,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { InModuleScope -ScriptBlock { $script:testNrptRule = @{ - Name = 'Server' + Name = 'Contoso Dns Policy' Namespace = '.contoso.com' NameServers = ('192.168.1.1') Ensure = 'Present' @@ -318,7 +297,6 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { } Context 'NRPT Rule does not exist but should' { BeforeAll { - Mock -CommandName Get-NetAdapter -MockWith { $mockNetAdapter } Mock -CommandName Get-DnsClientNrptRule } @@ -339,7 +317,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists and should but has a different Namespace' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' } It 'Should return false' { @@ -363,7 +341,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists and should but has a different NameServers' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' } It 'Should return false' { @@ -387,7 +365,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists and should and all parameters match' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' } It 'Should return true' { @@ -410,7 +388,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists but should not' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } + Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' } It 'Should return false' { From 0b5c524bacf9cf5f933b28a10eccbd744b29ffbe Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 15:15:33 +0100 Subject: [PATCH 15/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 48 ++++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 index 2396ba0a..0af1f992 100644 --- a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -94,7 +94,11 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { Context 'NRPT Rule does exist' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' + Mock -CommandName Get-DnsClientNrptRule -MockWith { + @{ + Name = 'Contoso Dns Policy' + } + } } It 'Should return correct NRPT Rule' { @@ -160,7 +164,11 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Context 'NRPT Rule exists and should but has a different Namespace' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' + Mock -CommandName Get-DnsClientNrptRule -MockWith { + @{ + Name = 'Contoso Dns Policy' + } + } Mock -CommandName Add-DnsClientNrptRule Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule @@ -189,7 +197,11 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Context 'NRPT Rule exists and should but has a different NameServers' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' + Mock -CommandName Get-DnsClientNrptRule -MockWith { + @{ + Name = 'Contoso Dns Policy' + } + } Mock -CommandName Add-DnsClientNrptRule Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule @@ -216,7 +228,11 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Context 'NRPT Rule exists and but should not' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' + Mock -CommandName Get-DnsClientNrptRule -MockWith { + @{ + Name = 'Contoso Dns Policy' + } + } Mock -CommandName Add-DnsClientNrptRule Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule ` @@ -317,7 +333,11 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists and should but has a different Namespace' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' + Mock -CommandName Get-DnsClientNrptRule -MockWith { + @{ + Name = 'Contoso Dns Policy' + } + } } It 'Should return false' { @@ -341,7 +361,11 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists and should but has a different NameServers' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' + Mock -CommandName Get-DnsClientNrptRule -MockWith { + @{ + Name = 'Contoso Dns Policy' + } + } } It 'Should return false' { @@ -365,7 +389,11 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists and should and all parameters match' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' + Mock -CommandName Get-DnsClientNrptRule -MockWith { + @{ + Name = 'Contoso Dns Policy' + } + } } It 'Should return true' { @@ -388,7 +416,11 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists but should not' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -Name 'Contoso Dns Policy' + Mock -CommandName Get-DnsClientNrptRule -MockWith { + @{ + Name = 'Contoso Dns Policy' + } + } } It 'Should return false' { From b4c94e1ee097fda66c8cc1c93f9c4a4c1e33e13c Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 19:54:54 +0100 Subject: [PATCH 16/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 index 0af1f992..475d0310 100644 --- a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -57,6 +57,13 @@ AfterAll { Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { BeforeAll { + $mockNrptRule = @{ + Name = 'Contoso Dns Policy' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + $testNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' @@ -120,6 +127,13 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { BeforeAll { + $mockNrptRule = @{ + Name = 'Contoso Dns Policy' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + $testNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' @@ -295,6 +309,13 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { BeforeAll { + $mockNrptRule = @{ + Name = 'Contoso Dns Policy' + Namespace = '.contoso.com' + NameServers = ('192.168.1.1') + Ensure = 'Present' + } + $testNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' From 8d5a0b01dbdef427d9361959708db0ad71180284 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 20:12:02 +0100 Subject: [PATCH 17/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 70 ++++++---------------- 1 file changed, 19 insertions(+), 51 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 index 475d0310..340022a2 100644 --- a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -60,14 +60,14 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { $mockNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') Ensure = 'Present' } $testNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') Ensure = 'Present' } @@ -75,7 +75,7 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { $script:testNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') Ensure = 'Present' } } @@ -101,11 +101,7 @@ Describe 'DSC_DnsClientNrptRule\Get-TargetResource' -Tag 'Get' { Context 'NRPT Rule does exist' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { - @{ - Name = 'Contoso Dns Policy' - } - } + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } } It 'Should return correct NRPT Rule' { @@ -130,14 +126,14 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { $mockNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') Ensure = 'Present' } $testNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') Ensure = 'Present' } @@ -145,7 +141,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { $script:testNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') Ensure = 'Present' } } @@ -178,11 +174,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Context 'NRPT Rule exists and should but has a different Namespace' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { - @{ - Name = 'Contoso Dns Policy' - } - } + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } Mock -CommandName Add-DnsClientNrptRule Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule @@ -211,11 +203,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Context 'NRPT Rule exists and should but has a different NameServers' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { - @{ - Name = 'Contoso Dns Policy' - } - } + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } Mock -CommandName Add-DnsClientNrptRule Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule @@ -226,7 +214,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Set-StrictMode -Version 1.0 $setTargetResourceParameters = $testNrptRule.Clone() - $setTargetResourceParameters.NameServers = ('192.168.0.1') + $setTargetResourceParameters.NameServers = @('192.168.0.1') { Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw } @@ -242,11 +230,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Context 'NRPT Rule exists and but should not' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { - @{ - Name = 'Contoso Dns Policy' - } - } + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } Mock -CommandName Add-DnsClientNrptRule Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule ` @@ -312,14 +296,14 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { $mockNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') Ensure = 'Present' } $testNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') Ensure = 'Present' } @@ -327,7 +311,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { $script:testNrptRule = @{ Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') Ensure = 'Present' } } @@ -354,11 +338,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists and should but has a different Namespace' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { - @{ - Name = 'Contoso Dns Policy' - } - } + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } } It 'Should return false' { @@ -382,11 +362,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists and should but has a different NameServers' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { - @{ - Name = 'Contoso Dns Policy' - } - } + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } } It 'Should return false' { @@ -394,7 +370,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Set-StrictMode -Version 1.0 $testTargetResourceParameters = $testNrptRule.Clone() - $testTargetResourceParameters.NameServers = ('192.168.0.1') + $testTargetResourceParameters.NameServers = @('192.168.0.1') $result = Test-TargetResource @testTargetResourceParameters @@ -410,11 +386,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists and should and all parameters match' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { - @{ - Name = 'Contoso Dns Policy' - } - } + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } } It 'Should return true' { @@ -437,11 +409,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { Context 'NRPT Rule exists but should not' { BeforeAll { - Mock -CommandName Get-DnsClientNrptRule -MockWith { - @{ - Name = 'Contoso Dns Policy' - } - } + Mock -CommandName Get-DnsClientNrptRule -MockWith { $mockNrptRule } } It 'Should return false' { From d9ff741a730d952bb5e1eccba4067c06ffc83b78 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 21:34:32 +0100 Subject: [PATCH 18/35] Switch Nrpt Global tests to Pester 5 --- tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 index 340022a2..78ab03c1 100644 --- a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -235,8 +235,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Mock -CommandName Set-DnsClientNrptRule Mock -CommandName Remove-DnsClientNrptRule ` -ParameterFilter { - ($Namespace -eq $testNrptRule.Namespace) -and ` - ($NameServers -eq $testNrptRule.NameServers) + ($Name -eq $testNrptRule.Name) } } @@ -257,8 +256,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { Should -Invoke -CommandName Set-DnsClientNrptRule -Exactly -Times 0 -Scope Context Should -Invoke -CommandName Remove-DnsClientNrptRule ` -ParameterFilter { - ($Namespace -eq $testNrptRule.Namespace) -and ` - ($NameServers -eq $testNrptRule.NameServers) + ($Name -eq $testNrptRule.Name) } -Exactly -Times 1 -Scope Context } } @@ -356,7 +354,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { } It 'Should call expected Mocks' { - Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 2 -Scope Context } } @@ -380,7 +378,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { } It 'Should call expected Mocks' { - Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 2 -Scope Context } } @@ -403,7 +401,7 @@ Describe 'DSC_DnsClientNrptRule\Test-TargetResource' -Tag 'Test' { } It 'Should call expected Mocks' { - Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 1 -Scope Context + Should -Invoke -CommandName Get-DnsClientNrptRule -Exactly -Times 2 -Scope Context } } From a658a2fece4b0ee6dbbfde6930f9bcd594fb490f Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 22:18:27 +0100 Subject: [PATCH 19/35] Switch Nrpt integration tests to Pester 5 --- ..._DnsClientNrptGlobal.Integration.Tests.ps1 | 165 +++++++------ ...SC_DnsClientNrptRule.Integration.Tests.ps1 | 220 +++++++++++------- 2 files changed, 229 insertions(+), 156 deletions(-) diff --git a/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 index ae00ef3b..e1e4b209 100644 --- a/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 +++ b/tests/Integration/DSC_DnsClientNrptGlobal.Integration.Tests.ps1 @@ -1,100 +1,131 @@ -$script:dscModuleName = 'NetworkingDsc' -$script:dscResourceName = 'DSC_DnsClientNrptGlobal' +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] +param () -try -{ - Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' -} -catch [System.IO.FileNotFoundException] -{ - throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' +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' 3>&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.' + } + + <# + Need to define that variables here to be used in the Pester Discover to + build the ForEach-blocks. + #> + $script:dscResourceFriendlyName = 'DnsClientNrptGlobal' + $script:dscResourceName = "DSC_$($script:dscResourceFriendlyName)" } -$script:testEnvironment = Initialize-TestEnvironment ` - -DSCModuleName $script:dscModuleName ` - -DSCResourceName $script:dscResourceName ` - -ResourceType 'Mof' ` - -TestType 'Integration' +BeforeAll { + # Need to define the variables here which will be used in Pester Run. + $script:dscModuleName = 'NetworkingDsc' + $script:dscResourceFriendlyName = 'DnsClientNrptGlobal' + $script:dscResourceName = "DSC_$($script:dscResourceFriendlyName)" -Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + $script:testEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dscResourceName ` + -ResourceType 'Mof' ` + -TestType 'Integration' -# Load the parameter List from the data file -$moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) -$resourceData = Import-LocalizedData ` - -BaseDirectory (Join-Path -Path $moduleRoot -ChildPath 'Source\DscResources\DSC_DnsClientNrptGlobal') ` - -FileName 'DSC_DnsClientNrptGlobal.data.psd1' + Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') +} + +AfterAll { + # Remove module common test helper. + Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force -$parameterList = $resourceData.ParameterList | Where-Object -Property IntTest -eq $True + Restore-TestEnvironment -TestEnvironment $script:testEnvironment +} -# Begin Testing -try -{ - Describe 'DnsClientNrptGlobal Integration Tests' { + +Describe 'DnsClientNrptGlobal Integration Tests' { + BeforeAll { # Backup the existing settings $script:currentDnsClientNrptGlobal = Get-DnsClientNrptGlobal # Set the DNS Client Global settings to known values Set-DnsClientNrptGlobal ` - -EnableDAForAllNetworks 'EnableAlways' ` - -QueryPolicy 'QueryIPv6Only' ` - -SecureNameQueryFallback 'FallbackSecure' + -EnableDAForAllNetworks 'Disable' ` + -QueryPolicy 'Disable' ` + -SecureNameQueryFallback 'Disable' $configData = @{ AllNodes = @( @{ NodeName = 'localhost' - EnableDAForAllNetworks = 'DisableDA' + EnableDAForAllNetworks = 'EnableAlways' QueryPolicy = 'QueryBoth' - SecureNameQueryFallback = 'FallbackPrivate' + SecureNameQueryFallback = 'FallbackSecure' } ) } $configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1" . $configFile + } - Describe "$($script:dscResourceName)_Integration" { - It 'Should compile and apply the MOF without throwing' { - { - & "$($script:dscResourceName)_Config" ` - -OutputPath $TestDrive ` - -ConfigurationData $configData - Start-DscConfiguration ` - -Path $TestDrive ` - -ComputerName localhost ` - -Wait ` - -Verbose ` - -Force - } | Should -Not -Throw - } - - It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw - } + AfterAll { + # Clean up + Set-DnsClientNrptGlobal ` + -EnableDAForAllNetworks $script:currentDnsClientNrptGlobal.EnableDAForAllNetworks ` + -QueryPolicy $script:currentDnsClientNrptGlobal.QueryPolicy ` + -SecureNameQueryFallback $script:currentDnsClientNrptGlobal.SecureNameQueryFallback + } - # Get the DNS Client Global Settings details - $DnsClientNrptGlobalNew = Get-DnsClientNrptGlobal + Describe "$($script:dscResourceName)_Integration" { + AfterEach { + Wait-ForIdleLcm + } - # Use the Parameters List to perform these tests - foreach ($parameter in $parameterList) + It 'Should compile and apply the MOF without throwing' { { - $parameterCurrentValue = (Get-Variable -Name 'DnsClientNrptGlobalNew').value.$($parameter.name) - $parameterNewValue = (Get-Variable -Name configData).Value.AllNodes[0].$($parameter.Name) + & "$($script:dscResourceName)_Config" ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force + } | Should -Not -Throw + } - It "Should have set the '$parameterName' to '$parameterNewValue'" { - $parameterCurrentValue | Should -Be $parameterNewValue - } + It 'Should be able to call Get-DscConfiguration without throwing' { + { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + } + + Context 'When testing each of the parameter values' { + BeforeAll { + # Get the DNS Client Global Settings details + $DnsClientNrptGlobalNew = Get-DnsClientNrptGlobal + } + + It 'Should have the correct value for <_>' -ForEach @( + 'EnableDAForAllNetworks', + 'QueryPolicy', + 'SecureNameQueryFallback' + ) { + $parameterCurrentValue = (Get-Variable -Name DnsClientNrptGlobalNew).value.$_ + $parameterNewValue = (Get-Variable -Name configData).Value.AllNodes[0].$_ + + $parameterCurrentValue | Should -Be $parameterNewValue } } } } -finally -{ - # Clean up - Set-DnsClientNrptGlobal ` - -EnableDAForAllNetworks $script:currentDnsClientNrptGlobal.EnableDAForAllNetworks ` - -QueryPolicy $script:currentDnsClientNrptGlobal.QueryPolicy ` - -SecureNameQueryFallback $script:currentDnsClientNrptGlobal.SecureNameQueryFallback - - Restore-TestEnvironment -TestEnvironment $script:testEnvironment -} diff --git a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 index dee5d8cd..ccd8d612 100644 --- a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 +++ b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 @@ -1,27 +1,59 @@ -$script:dscModuleName = 'NetworkingDsc' -$script:dscResourceName = 'DSC_DnsClientNrptRule' +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] +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' 3>&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 -{ - Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' + <# + Need to define that variables here to be used in the Pester Discover to + build the ForEach-blocks. + #> + $script:dscResourceFriendlyName = 'DnsClientNrptRule' + $script:dscResourceName = "DSC_$($script:dscResourceFriendlyName)" } -catch [System.IO.FileNotFoundException] -{ - throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' + +BeforeAll { + # Need to define the variables here which will be used in Pester Run. + $script:dscModuleName = 'NetworkingDsc' + $script:dscResourceFriendlyName = 'DnsClientNrptRule' + $script:dscResourceName = "DSC_$($script:dscResourceFriendlyName)" + + $script:testEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dscResourceName ` + -ResourceType 'Mof' ` + -TestType 'Integration' + + Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') } -$script:testEnvironment = Initialize-TestEnvironment ` - -DSCModuleName $script:dscModuleName ` - -DSCResourceName $script:dscResourceName ` - -ResourceType 'Mof' ` - -TestType 'Integration' +AfterAll { + # Remove module common test helper. + Get-Module -Name 'CommonTestHelper' -All | Remove-Module -Force -Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1') + Restore-TestEnvironment -TestEnvironment $script:testEnvironment +} -# Begin Testing -try -{ - Describe 'NRPT Rule Integration Tests' { +Describe 'DnsClientNrptRule Integration Tests' { + BeforeAll { $script:dummyRule = [PSObject] @{ Name = 'Server' Namespace = '.contoso.com' @@ -29,9 +61,18 @@ try } $configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1" - . $configFile -Verbose -ErrorAction Stop + . $configFile + } - Describe "$($script:dscResourceName)_Add_Integration" { + AfterAll { + # Clean up any created rules just in case the integration tests fail + $null = Remove-DnsClientNrptRule -Name $dummyRule.Name ` + -Force ` + -ErrorAction SilentlyContinue + } + + Describe "$($script:dscResourceName)_Add_Integration" { + BeforeAll { $configData = @{ AllNodes = @( @{ @@ -43,44 +84,50 @@ try } ) } + } - It 'Should compile and apply the MOF without throwing' { - { - & "$($script:dscResourceName)_Config" ` - -OutputPath $TestDrive ` - -ConfigurationData $configData - - Start-DscConfiguration ` - -Path $TestDrive ` - -ComputerName localhost ` - -Wait ` - -Verbose ` - -Force ` - -ErrorAction Stop - } | Should -Not -Throw - } + AfterEach { + Wait-ForIdleLcm + } - It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw - } + It 'Should compile and apply the MOF without throwing' { + { + & "$($script:dscResourceName)_Config" ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + } | Should -Not -Throw + } - It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object -FilterScript { - $_.ConfigurationName -eq "$($script:dscResourceName)_Config" - } + It 'Should be able to call Get-DscConfiguration without throwing' { + { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + } - $current.Name | Should -Be $configData.AllNodes[0].Name - $current.Namespace | Should -Be $configData.AllNodes[0].Namespace - $current.NameServers | Should -Be $configData.AllNodes[0].NameServers - $current.Ensure | Should -Be $configData.AllNodes[0].Ensure + It 'Should have set the resource and all the parameters should match' { + $current = Get-DscConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq "$($script:dscResourceName)_Config" } - It 'Should have created the NRPT rule' { - Get-DnsClientNrptRule -Name $script:dummyRule.Name -ErrorAction SilentlyContinue | Should -Not -BeNullOrEmpty - } + $current.Name | Should -Be $configData.AllNodes[0].Name + $current.Namespace | Should -Be $configData.AllNodes[0].Namespace + $current.NameServers | Should -Be $configData.AllNodes[0].NameServers + $current.Ensure | Should -Be $configData.AllNodes[0].Ensure } - Describe "$($script:dscResourceName)_Remove_Integration" { + It 'Should have created the NRPT rule' { + Get-DnsClientNrptRule -Name $script:dummyRule.Name -ErrorAction SilentlyContinue | Should -Not -BeNullOrEmpty + } + } + + Describe "$($script:dscResourceName)_Remove_Integration" { + BeforeAll { $configData = @{ AllNodes = @( @{ @@ -88,54 +135,49 @@ try Name = $script:dummyRule.Name Namespace = $script:dummyRule.Namespace NameServers = $script:dummyRule.NameServers - Ensure = 'Absent' + Ensure = 'Present' } ) } + } - It 'Should compile and apply the MOF without throwing' { - { - & "$($script:dscResourceName)_Config" ` - -OutputPath $TestDrive ` - -ConfigurationData $configData - - Start-DscConfiguration ` - -Path $TestDrive ` - -ComputerName localhost ` - -Wait ` - -Verbose ` - -Force ` - -ErrorAction Stop - } | Should -Not -Throw - } + AfterEach { + Wait-ForIdleLcm + } - It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw - } + It 'Should compile and apply the MOF without throwing' { + { + & "$($script:dscResourceName)_Config" ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + } | Should -Not -Throw + } - It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object -FilterScript { - $_.ConfigurationName -eq "$($script:dscResourceName)_Config" - } + It 'Should be able to call Get-DscConfiguration without throwing' { + { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + } - $current.Name | Should -Be $configData.AllNodes[0].Name - $current.Namespace | Should -Be $configData.AllNodes[0].Namespace - $current.NameServers | Should -Be $configData.AllNodes[0].NameServers - $current.Ensure | Should -Be $configData.AllNodes[0].Ensure + It 'Should have set the resource and all the parameters should match' { + $current = Get-DscConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq "$($script:dscResourceName)_Config" } - It 'Should have deleted the NRPT rule' { - Get-DnsClientNrptRule -Name $script:dummyRule.Name | Should -BeNullOrEmpty - } + $current.Name | Should -Be $configData.AllNodes[0].Name + $current.Namespace | Should -Be $configData.AllNodes[0].Namespace + $current.NameServers | Should -Be $configData.AllNodes[0].NameServers + $current.Ensure | Should -Be $configData.AllNodes[0].Ensure } - } -} -finally -{ - # Clean up any created rules just in case the integration tests fail - $null = Remove-DnsClientNrptRule $script:dummyRule.Name ` - -Force ` - -ErrorAction SilentlyContinue - Restore-TestEnvironment -TestEnvironment $script:testEnvironment + It 'Should have deleted the NRPT rule' { + Get-DnsClientNrptRule -Name $script:dummyRule.Name | Should -BeNullOrEmpty + } + } } From 3c1dae5aec1969206b507b192533251c8462e8c3 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 22:26:20 +0100 Subject: [PATCH 20/35] Switch Nrpt integration tests to Pester 5 --- tests/Integration/DSC_DnsClientNrptRule.config.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Integration/DSC_DnsClientNrptRule.config.ps1 b/tests/Integration/DSC_DnsClientNrptRule.config.ps1 index c5090f0f..6b265b68 100644 --- a/tests/Integration/DSC_DnsClientNrptRule.config.ps1 +++ b/tests/Integration/DSC_DnsClientNrptRule.config.ps1 @@ -12,7 +12,6 @@ configuration DSC_DnsClientNrptRule_Config { DnsClientNrptRule Integration_Test { Name = $Node.Name Namespace = $Node.Namespace - NameEncoding = $Node.NameEncoding NameServers = $Node.NameServers } } From 507b9d5810ec38bc5147acf07e992d32e448925d Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 22:32:40 +0100 Subject: [PATCH 21/35] Switch Nrpt integration tests to Pester 5 --- tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 index ccd8d612..358668fa 100644 --- a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 +++ b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 @@ -66,7 +66,7 @@ Describe 'DnsClientNrptRule Integration Tests' { AfterAll { # Clean up any created rules just in case the integration tests fail - $null = Remove-DnsClientNrptRule -Name $dummyRule.Name ` + $null = Remove-DnsClientNrptRule -Name $script:dummyRule.Name ` -Force ` -ErrorAction SilentlyContinue } From 754aeb43acaf788b3856ebd1c2a296ae26a91625 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 22:40:52 +0100 Subject: [PATCH 22/35] Switch Nrpt integration tests to Pester 5 --- tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 index 358668fa..ebca4d18 100644 --- a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 +++ b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 @@ -135,7 +135,7 @@ Describe 'DnsClientNrptRule Integration Tests' { Name = $script:dummyRule.Name Namespace = $script:dummyRule.Namespace NameServers = $script:dummyRule.NameServers - Ensure = 'Present' + Ensure = 'Absent' } ) } From 21b366d28855320cad2be41877071248aeaac869 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 22:53:56 +0100 Subject: [PATCH 23/35] Switch Nrpt integration tests to Pester 5 --- tests/Integration/DSC_DnsClientNrptRule.config.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Integration/DSC_DnsClientNrptRule.config.ps1 b/tests/Integration/DSC_DnsClientNrptRule.config.ps1 index 6b265b68..764fa281 100644 --- a/tests/Integration/DSC_DnsClientNrptRule.config.ps1 +++ b/tests/Integration/DSC_DnsClientNrptRule.config.ps1 @@ -13,6 +13,7 @@ configuration DSC_DnsClientNrptRule_Config { Name = $Node.Name Namespace = $Node.Namespace NameServers = $Node.NameServers + Ensure = $Node.Ensure } } } From fdb5f44bfdcba77f89d4bf72a5ed0a7031132818 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 23:03:17 +0100 Subject: [PATCH 24/35] Switch Nrpt integration tests to Pester 5 --- .../Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 index ebca4d18..99a01039 100644 --- a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 +++ b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 @@ -55,9 +55,9 @@ AfterAll { Describe 'DnsClientNrptRule Integration Tests' { BeforeAll { $script:dummyRule = [PSObject] @{ - Name = 'Server' + Name = 'Contoso Dns Policy' Namespace = '.contoso.com' - NameServers = ('192.168.1.1') + NameServers = @('192.168.1.1') } $configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1" @@ -171,8 +171,6 @@ Describe 'DnsClientNrptRule Integration Tests' { } $current.Name | Should -Be $configData.AllNodes[0].Name - $current.Namespace | Should -Be $configData.AllNodes[0].Namespace - $current.NameServers | Should -Be $configData.AllNodes[0].NameServers $current.Ensure | Should -Be $configData.AllNodes[0].Ensure } From f058404cfaab380b233b2f030360229a978b43a1 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 23:25:30 +0100 Subject: [PATCH 25/35] Switch Nrpt integration tests to Pester 5 --- tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 index 99a01039..f9cb0fce 100644 --- a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 +++ b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 @@ -175,7 +175,7 @@ Describe 'DnsClientNrptRule Integration Tests' { } It 'Should have deleted the NRPT rule' { - Get-DnsClientNrptRule -Name $script:dummyRule.Name | Should -BeNullOrEmpty + Get-DnsClientNrptRule -Name $script:dummyRule.Name -ErrorAction SilentlyContinue | Should -BeNullOrEmpty } } } From d771795a2b6c04ab0d40e38ce9ed771f299e7268 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 23:34:04 +0100 Subject: [PATCH 26/35] Switch Nrpt integration tests to Pester 5 --- .../DSC_DnsClientNrptRule.Integration.Tests.ps1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 index f9cb0fce..4d097c53 100644 --- a/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 +++ b/tests/Integration/DSC_DnsClientNrptRule.Integration.Tests.ps1 @@ -66,9 +66,10 @@ Describe 'DnsClientNrptRule Integration Tests' { AfterAll { # Clean up any created rules just in case the integration tests fail - $null = Remove-DnsClientNrptRule -Name $script:dummyRule.Name ` - -Force ` - -ErrorAction SilentlyContinue + if (Get-DnsClientNrptRule -Name $script:dummyRule.Name -ErrorAction SilentlyContinue) + { + Remove-DnsClientNrptRule -Name $script:dummyRule.Name -Force -ErrorAction SilentlyContinue + } } Describe "$($script:dscResourceName)_Add_Integration" { From f516d863e24f3f466372fee47d3fd9e9699e5fd2 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 23:46:07 +0100 Subject: [PATCH 27/35] Fix syntax statement --- .../DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 index 752dbe65..e67a5605 100644 --- a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 @@ -206,7 +206,8 @@ function Test-TargetResource $parameterSourceValue = $DnsClientNrptGlobal.$($parameter.name) $parameterNewValue = (Get-Variable -Name ($parameter.name)).Value - if ($parameterNewValue -ne $parameterSourceValue) { + if ($parameterNewValue -ne $parameterSourceValue) + { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DnsClientNrptGlobalParameterNeedsUpdateMessage) ` -f $parameter.Name, ($parameterSourceValue -join ','), ($parameterNewValue -join ',') From fda08d20d69724ccfa27168db07b01a47a4080fa Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 17 Mar 2025 23:53:45 +0100 Subject: [PATCH 28/35] Fix syntax statement --- .../DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 index 17e47858..cf5ffaeb 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 @@ -268,7 +268,8 @@ function Set-TargetResource -ErrorAction Stop).Name # If rule has been created, rename it by registry as Name cannot be provided in Add-DnsClientNrptRule cmdlet - if ($NrptRuleName -match "^\{[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}\}$") { + if ($NrptRuleName -match "^\{[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}\}$") + { # Rename the registry key Rename-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig\$($NrptRuleName)" -NewName $Name } From 1ea40947804a6284ec5dc112df10cf8af6458d15 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Tue, 18 Mar 2025 00:04:30 +0100 Subject: [PATCH 29/35] Generate new GUID for examples --- .../DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 | 2 +- .../DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 | 2 +- .../DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 | 2 +- .../DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 b/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 index 8303af1a..c4fc7d55 100644 --- a/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 @@ -1,6 +1,6 @@ <#PSScriptInfo .VERSION 1.0.0 -.GUID ef9d0f9a-5a08-43c0-9645-48e103188971 +.GUID 1b31527d-8de4-4a7e-a000-9182a0e87cfb .AUTHOR DSC Community .COMPANYNAME DSC Community .COPYRIGHT Copyright the DSC Community contributors. All rights reserved. diff --git a/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 index e2bc361d..976b7a3f 100644 --- a/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 @@ -1,6 +1,6 @@ <#PSScriptInfo .VERSION 1.0.0 -.GUID 9783741d-7f08-409d-8c93-d2e16a76e1ee +.GUID ffeb7cf3-f0d8-4e26-8f1f-132d889e639b .AUTHOR DSC Community .COMPANYNAME DSC Community .COPYRIGHT Copyright the DSC Community contributors. All rights reserved. diff --git a/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 index 5e7eb175..77388f44 100644 --- a/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 @@ -1,6 +1,6 @@ <#PSScriptInfo .VERSION 1.0.0 -.GUID 87e17ee4-64b0-476a-8aa8-1018a5de6353 +.GUID 2c8c4bf7-04ca-464a-941b-3b95c566693d .AUTHOR DSC Community .COMPANYNAME DSC Community .COPYRIGHT Copyright the DSC Community contributors. All rights reserved. diff --git a/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 index 366d23ea..03ae786a 100644 --- a/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 @@ -1,6 +1,6 @@ <#PSScriptInfo .VERSION 1.0.0 -.GUID cc42de3d-7497-4179-8756-84154b528895 +.GUID eafe8bba-ac2c-4509-b6c5-3dc9d57facfd .AUTHOR DSC Community .COMPANYNAME DSC Community .COPYRIGHT Copyright the DSC Community contributors. All rights reserved. From d63e72ba37a4ae9cff1b70edc5201f1a4d6902b9 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Tue, 18 Mar 2025 00:24:23 +0100 Subject: [PATCH 30/35] Add ValidateSet for NRPT global --- .../DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 index e67a5605..b80d8663 100644 --- a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 @@ -90,15 +90,18 @@ function Set-TargetResource $IsSingleInstance, [Parameter()] + [ValidateSet("EnableOnNetworkID", "EnableAlways", "Disable", "DisableDA")] [System.String] $EnableDAForAllNetworks, [Parameter()] [System.String] + [ValidateSet("Disable", "QueryIPv6Only", "QueryBoth")] $QueryPolicy, [Parameter()] [System.String] + [ValidateSet("Disable", "FallbackSecure", "FallbackUnsecure", "FallbackPrivate")] $SecureNameQueryFallback ) @@ -176,15 +179,18 @@ function Test-TargetResource $IsSingleInstance, [Parameter()] + [ValidateSet("EnableOnNetworkID", "EnableAlways", "Disable", "DisableDA")] [System.String] $EnableDAForAllNetworks, [Parameter()] [System.String] + [ValidateSet("Disable", "QueryIPv6Only", "QueryBoth")] $QueryPolicy, [Parameter()] [System.String] + [ValidateSet("Disable", "FallbackSecure", "FallbackUnsecure", "FallbackPrivate")] $SecureNameQueryFallback ) From 9f051d2f7b2fa02d44fd5e21d685add8b33ff0ce Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Tue, 18 Mar 2025 11:30:23 +0100 Subject: [PATCH 31/35] Review documentation/syntax --- CHANGELOG.md | 4 ++-- README.md | 4 ++-- .../DSC_DnsClientNrptGlobal.psm1 | 12 ++++++------ .../1-DnsClientNrptGlobal_Config.ps1 | 14 ++++++++++---- .../1-DnsClientNrptRule_Server_Config.ps1 | 10 ++++++++-- .../2-DnsClientNrptRule_DNSSEC_Config.ps1 | 8 +++++++- .../3-DnsClientNrptRule_Punycode_Config.ps1 | 10 +++++++++- 7 files changed, 44 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f22cc97..d3df523f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Added the following resources: - - **DnsClientNrptGlobal**: Configure the global Name Resolution Policy Table (NRPT) settings. - - **DnsClientNrptRule**: Sets a NRPT rule on a node. + - **DnsClientNrptGlobal**: Configure Dns Client global Name Resolution Policy Table (NRPT) settings. + - **DnsClientNrptRule**: Sets a Dns Client NRPT rule on a node. - Updated CHANGELOG.md - Renamed NetworkingDSc to NetworkingDsc in CHANGELOG.md - fixes [Issue #513](https://github.com/dsccommunity/NetworkingDsc/issues/513). diff --git a/README.md b/README.md index 74273b44..a01c3905 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ The **NetworkingDsc** module contains the following resources: - **DefaultGatewayAddress**: Sets a node's default gateway address. - **DnsClientGlobalSetting**: Configure DNS client global settings. -- **DnsClientNrptGlobal**: Configure the global Name Resolution Policy Table (NRPT) settings. -- **DnsClientNrptRule**: Sets a NRPT rule on a node. +- **DnsClientNrptGlobal**: Configure DNS client global Name Resolution Policy Table (NRPT) settings. +- **DnsClientNrptRule**: Sets a DNS client NRPT rule on a node. - **DnsConnectionSuffix**: Sets a node's network interface connection-specific DNS suffix. - **DnsServerAddress**: Sets a node's DNS server address(s). diff --git a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 index b80d8663..65d1576a 100644 --- a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 @@ -90,18 +90,18 @@ function Set-TargetResource $IsSingleInstance, [Parameter()] - [ValidateSet("EnableOnNetworkID", "EnableAlways", "Disable", "DisableDA")] + [ValidateSet('EnableOnNetworkID', 'EnableAlways', 'Disable', 'DisableDA')] [System.String] $EnableDAForAllNetworks, [Parameter()] [System.String] - [ValidateSet("Disable", "QueryIPv6Only", "QueryBoth")] + [ValidateSet('Disable', 'QueryIPv6Only', 'QueryBoth')] $QueryPolicy, [Parameter()] [System.String] - [ValidateSet("Disable", "FallbackSecure", "FallbackUnsecure", "FallbackPrivate")] + [ValidateSet('Disable', 'FallbackSecure', 'FallbackUnsecure', 'FallbackPrivate')] $SecureNameQueryFallback ) @@ -179,18 +179,18 @@ function Test-TargetResource $IsSingleInstance, [Parameter()] - [ValidateSet("EnableOnNetworkID", "EnableAlways", "Disable", "DisableDA")] + [ValidateSet('EnableOnNetworkID', 'EnableAlways', 'Disable', 'DisableDA')] [System.String] $EnableDAForAllNetworks, [Parameter()] [System.String] - [ValidateSet("Disable", "QueryIPv6Only", "QueryBoth")] + [ValidateSet('Disable', 'QueryIPv6Only', 'QueryBoth')] $QueryPolicy, [Parameter()] [System.String] - [ValidateSet("Disable", "FallbackSecure", "FallbackUnsecure", "FallbackPrivate")] + [ValidateSet('Disable', 'FallbackSecure', 'FallbackUnsecure', 'FallbackPrivate')] $SecureNameQueryFallback ) diff --git a/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 b/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 index c4fc7d55..1d2de334 100644 --- a/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 @@ -19,7 +19,13 @@ <# .DESCRIPTION - Configure the NRPT global configuration. + Configure Dns Client NRPT global configuration. + .PARAMETER EnableDAForAllNetworks + Specifies DirectAccess (DA) settings. (Default: Disable) + .PARAMETER QueryPolicy. + Specifies the DNS client query policy. (Default: Disable) + .PARAMETER SecureNameQueryFallback + Specifies the DNS client name resolution fallback policy. (Default: Disable) #> Configuration DnsClientNrptGlobal_Config { @@ -30,9 +36,9 @@ Configuration DnsClientNrptGlobal_Config DnsClientNrptGlobal DnsClientNrptGlobal { IsSingleInstance = 'Yes' - EnableDAForAllNetworks = 'Disable' - QueryPolicy = 'Disable' - SecureNameQueryFallback = 'Disable' + EnableDAForAllNetworks = 'EnableAlways' + QueryPolicy = 'QueryBoth' + SecureNameQueryFallback = 'FallbackSecure' } } } diff --git a/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 index 976b7a3f..c8306548 100644 --- a/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 @@ -19,7 +19,13 @@ <# .DESCRIPTION - Add an NRPT rule to configure a server. + Sets a Dns Client NRPT rule to configure a conditional dns forwarder for a specific namespace. + .PARAMETER Name + Specifies the name which uniquely identifies a rule. + .PARAMETER NameServers + Specifies the DNS servers to which the DNS query is sent when DirectAccess is disabled. + .PARAMETER Namespace + Specifies the DNS namespace. #> Configuration DnsClientNrptRule_Server_Config { @@ -29,7 +35,7 @@ Configuration DnsClientNrptRule_Server_Config { DnsClientNrptRule Server { - Name = 'Server' + Name = 'Contoso Dns Policy' Namespace = '.contoso.com' NameServers = ('192.168.1.1') } diff --git a/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 index 77388f44..7fe8ce45 100644 --- a/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 @@ -19,7 +19,13 @@ <# .DESCRIPTION - Add an NRPT rule to enable DNSSEC queries. + Sets a Dns Client NRPT rule to enable DNSSEC queries for a specific namespace. + .PARAMETER Name + Specifies the name which uniquely identifies a rule. + .PARAMETER DnsSecEnable + Enables Domain Name System Security Extensions (DNSSEC) on the rule. + .PARAMETER Namespace + Specifies the DNS namespace. #> Configuration DnsClientNrptRule_DNSSEC_Config { diff --git a/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 index 03ae786a..0e35c179 100644 --- a/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 @@ -19,7 +19,15 @@ <# .DESCRIPTION - Add an NRPT rule to send Punycode DNS queries. + Sets a Dns Client NRPT rule to send Punycode DNS queries using a conditional dns forwarder for a specific namespace. + .PARAMETER Name + Specifies the name which uniquely identifies a rule. + .PARAMETER NameEncoding + Specifies the encoding format for host names in the DNS query. + .PARAMETER NameServers + Specifies the DNS servers to which the DNS query is sent when DirectAccess is disabled. + .PARAMETER Namespace + Specifies the DNS namespace. #> Configuration DnsClientNrptRule_Punycode_Config { From 36e2b397af4d950a87a9949351ebcbc09424cfb2 Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 24 Mar 2025 20:24:01 +0100 Subject: [PATCH 32/35] Improve code based on review --- CHANGELOG.md | 4 +- .../DSC_DnsClientNrptGlobal.psm1 | 6 +- .../DSC_DnsClientNrptRule.psm1 | 208 ++++++++++++++++-- .../DSC_DnsClientNrptRule.schema.mof | 2 +- .../DSC_DnsClientNrptRule/README.MD | 2 +- .../en-US/DSC_DnsClientNrptRule.strings.psd1 | 26 +-- .../1-DnsClientNrptGlobal_Config.ps1 | 8 +- .../1-DnsClientNrptRule_Server_Config.ps1 | 10 +- .../2-DnsClientNrptRule_DNSSEC_Config.ps1 | 8 +- .../3-DnsClientNrptRule_Punycode_Config.ps1 | 10 +- 10 files changed, 213 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3df523f..48e8e3e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Added the following resources: - - **DnsClientNrptGlobal**: Configure Dns Client global Name Resolution Policy Table (NRPT) settings. - - **DnsClientNrptRule**: Sets a Dns Client NRPT rule on a node. + - **DnsClientNrptGlobal**: Configure DNS Client global Name Resolution Policy Table (NRPT) settings. + - **DnsClientNrptRule**: Sets a DNS Client NRPT rule on a node. - Updated CHANGELOG.md - Renamed NetworkingDSc to NetworkingDsc in CHANGELOG.md - fixes [Issue #513](https://github.com/dsccommunity/NetworkingDsc/issues/513). diff --git a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 index 65d1576a..66fb4373 100644 --- a/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptGlobal/DSC_DnsClientNrptGlobal.psm1 @@ -44,7 +44,7 @@ function Get-TargetResource $($script:localizedData.GettingDnsClientNrptGlobalMessage) ) -join '' ) - # Get the current Dns Client Global Settings + # Get the current DNS Client Global Settings $DnsClientNrptGlobal = Get-DnsClientNrptGlobal ` -ErrorAction Stop @@ -110,7 +110,7 @@ function Set-TargetResource $($script:localizedData.SettingDnsClientNrptGlobalMessage) ) -join '' ) - # Get the current Dns Client Nrpt Global Settings + # Get the current DNS Client Nrpt Global Settings $DnsClientNrptGlobal = Get-DnsClientNrptGlobal ` -ErrorAction Stop @@ -202,7 +202,7 @@ function Test-TargetResource # Flag to signal whether settings are correct $desiredConfigurationMatch = $true - # Get the current Dns Client Nrpt Global Settings + # Get the current DNS Client Nrpt Global Settings $DnsClientNrptGlobal = Get-DnsClientNrptGlobal ` -ErrorAction Stop diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 index cf5ffaeb..47f5b8c5 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 @@ -10,9 +10,12 @@ Import-Module -Name (Join-Path -Path $modulePath -ChildPath 'DscResource.Common' # Import Localization Strings $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' +# This must be a script parameter so that it is accessible +$script:dnsPolicyConfigRegistryPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig' + <# .SYNOPSIS - Returns the current state of a NRPT Rule. + Returns the current state of a DNS Client NRPT Rule. .PARAMETER Name Specifies the name which uniquely identifies a rule. @@ -186,7 +189,7 @@ function Set-TargetResource [ValidateSet('NoProxy', 'UseDefault', 'UseProxyName')] [System.String] $DAProxyType, - + [Parameter()] [System.String] $DisplayName, @@ -260,20 +263,9 @@ function Set-TargetResource } else { - # Remove Name parameter as Add-DnsClientNrptRule cmdlet doesn't support it - $null = $PSBoundParameters.Remove("Name") - # The NrptRule does not exit - create it (PassThru to get the name of the rule created) - $NrptRuleName=(Add-DnsClientNrptRule @PSBoundParameters ` - -PassThru ` - -ErrorAction Stop).Name - - # If rule has been created, rename it by registry as Name cannot be provided in Add-DnsClientNrptRule cmdlet - if ($NrptRuleName -match "^\{[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}\}$") - { - # Rename the registry key - Rename-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig\$($NrptRuleName)" -NewName $Name - } - + # The NrptRule does not exit - create it + Add-NrptRule @PSBoundParameters ` + -ErrorAction Stop Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " @@ -521,7 +513,7 @@ function Test-TargetResource <# .SYNOPSIS - This function looks up NRPT Rule using the parameters and returns + This function looks up DNS Client NRPT Rule using the parameters and returns it. If the rule is not found $null is returned. .PARAMETER Name @@ -539,20 +531,196 @@ function Get-NrptRule try { - $NrptRule = Get-DnsClientNrptRule ` + $nrptRule = Get-DnsClientNrptRule ` -Name $Name ` -ErrorAction SilentlyContinue } catch [Microsoft.PowerShell.Cmdletization.Cim.CimJobException] { - $NrptRule = $null + $nrptRule = $null } catch { throw $_ } - return $NrptRule + return $nrptRule } +<# + .SYNOPSIS + This function create a DNS Client NRPT Rule using the parameters + + .PARAMETER Name + Specifies the name which uniquely identifies a rule. + + .PARAMETER Comment + Stores administrator notes. + + .PARAMETER DAEnable + Indicates the rule state for DirectAccess. + + .PARAMETER DAIPsecEncryptionType + Specifies the Internet Protocol security (IPsec) encryption setting for DirectAccess. + + .PARAMETER DAIPsecRequired + Indicates that IPsec is required for DirectAccess. + + .PARAMETER DANameServers + Specifies an array of DNS servers to query when DirectAccess is enabled. + + .PARAMETER DAProxyServerName + "Specifies the proxy server to use when connecting to the Internet. + This parameter is only applicable if the DAProxyType parameter is set to UseProxyName. + + .PARAMETER DAProxyType + Specifies the proxy server type to be used when connecting to the Internet. + + .PARAMETER DisplayName + Specifies an optional friendly name for the NRPT rule. + + .PARAMETER DnsSecEnable + Enables Domain Name System Security Extensions (DNSSEC) on the rule. + + .PARAMETER DnsSecIPsecEncryptionType + Specifies the IPsec tunnel encryption setting. + + .PARAMETER DnsSecIPsecRequired + Indicates the DNS client must set up an IPsec connection to the DNS server. + + .PARAMETER DnsSecValidationRequired + Indicates that DNSSEC validation is required. + + .PARAMETER IPsecTrustAuthority + Specifies the certification authority to validate the IPsec channel. + + .PARAMETER NameEncoding + Specifies the encoding format for host names in the DNS query. + + .PARAMETER NameServers + Specifies the DNS servers to which the DNS query is sent when DirectAccess is disabled. + + .PARAMETER Namespace + Specifies the DNS namespace. +#> +function Add-NrptRule +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.String] + $Name, + + [Parameter()] + [System.String] + $Comment, + + [Parameter()] + [System.Boolean] + $DAEnable, + + [Parameter()] + [ValidateSet('None', 'Low', 'Medium', 'High')] + [System.String] + $DAIPsecEncryptionType, + + [Parameter()] + [System.Boolean] + $DAIPsecRequired, + + [Parameter()] + [System.String[]] + $DANameServers, + + [Parameter()] + [System.String] + $DAProxyServerName, + + [Parameter()] + [ValidateSet('NoProxy', 'UseDefault', 'UseProxyName')] + [System.String] + $DAProxyType, + + [Parameter()] + [System.String] + $DisplayName, + + [Parameter()] + [System.Boolean] + $DnsSecEnable, + + [Parameter()] + [ValidateSet('None', 'Low', 'Medium', 'High')] + [System.String] + $DnsSecIPsecEncryptionType, + + [Parameter()] + [System.Boolean] + $DnsSecIPsecRequired, + + [Parameter()] + [System.Boolean] + $DnsSecValidationRequired, + + [Parameter()] + [System.String] + $IPsecTrustAuthority, + + [Parameter()] + [ValidateSet('Disable', 'Utf8WithMapping', 'Utf8WithoutMapping', 'Punycode')] + [System.String] + $NameEncoding, + + [Parameter()] + [System.String[]] + $NameServers, + + [Parameter()] + [System.String] + $Namespace + ) + + # Remove Name parameter as Add-DnsClientNrptRule cmdlet doesn't support it + $null = $PSBoundParameters.Remove("Name") + + # The NrptRule does not exit - create it (PassThru to get the name of the rule created) + $NrptRuleName=(Add-DnsClientNrptRule @PSBoundParameters ` + -PassThru ` + -ErrorAction Stop).Name + # If rule has been created, rename it by registry as Name cannot be provided in Add-DnsClientNrptRule cmdlet + if (Test-IsGuid -InputValue $NrptRuleName) + { + # Rename the registry key + Rename-Item -Path "$($script:dnsPolicyConfigRegistryPath)\$($NrptRuleName)" ` + -NewName $Name ` + -ErrorAction Stop + } +} + +<# + .SYNOPSIS + This function check if the string provided is a GUID. + + .PARAMETER InputValue + Specifies the value to test. +#> +function Test-IsGuid { + param ( + [string]$InputValue + ) + + try { + # Attempt to parse the string as a GUID + [void][Guid]::Parse($InputValue) + # If successful, return true + return $true + } catch { + # If an exception is thrown, the string is not a valid GUID + return $false + } +} + + Export-ModuleMember -Function *-TargetResource diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof index e559bf49..9de11ef2 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof @@ -1,7 +1,7 @@ [ClassVersion("1.0.0.0"), FriendlyName("DnsClientNrptRule")] class DSC_DnsClientNrptRule : OMI_BaseResource { - [Key, Description("Specifies the NRPT rule name.")] string Name; + [Key, Description("Specifies the DNS Client NRPT rule name.")] string Name; [Write, Description("Specifies whether the NRPT rule should exist. Defaults to 'Present'."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; [Write, Description("Stores administrator notes.")] string Comment; [Write, Description("Indicates the rule state for DirectAccess.")] Boolean DAEnable; diff --git a/source/DSCResources/DSC_DnsClientNrptRule/README.MD b/source/DSCResources/DSC_DnsClientNrptRule/README.MD index d3fbfcd1..b7ae95aa 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/README.MD +++ b/source/DSCResources/DSC_DnsClientNrptRule/README.MD @@ -1,3 +1,3 @@ # Description -This resource is used to control NRPT rules for a node. +This resource is used to control DNS Client NRPT rules for a node. diff --git a/source/DSCResources/DSC_DnsClientNrptRule/en-US/DSC_DnsClientNrptRule.strings.psd1 b/source/DSCResources/DSC_DnsClientNrptRule/en-US/DSC_DnsClientNrptRule.strings.psd1 index 5b913730..dfcc838b 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/en-US/DSC_DnsClientNrptRule.strings.psd1 +++ b/source/DSCResources/DSC_DnsClientNrptRule/en-US/DSC_DnsClientNrptRule.strings.psd1 @@ -1,17 +1,17 @@ # Localized resources for DSC_DnsClientNrptRule ConvertFrom-StringData @' - GettingNrptRuleMessage = Getting '{0}' NRPT rule. - NrptRuleExistsMessage = '{0}' NRPT rule exists. - NrptRuleDoesNotExistMessage = '{0}' NRPT rule does not exist. - SettingNrptRuleMessage = Setting '{0}' NRPT rule. - EnsureNrptRuleExistsMessage = Ensuring '{0}' NRPT rule exists. - EnsureNrptRuleDoesNotExistMessage = Ensuring '{0}' NRPT rule does not exist. - NrptRuleCreatedMessage = '{0}' NRPT rule has been created. - NrptRuleUpdatedMessage = '{0}' NRPT rule has been updated. - NrptRuleRemovedMessage = '{0}' NRPT rule has been removed. - TestingNrptRuleMessage = Testing '{0}' NRPT rule. - NrptRuleDoesNotExistButShouldMessage = '{0}' NRPT rule does not exist but should. Change required. - NrptRuleExistsButShouldNotMessage = '{0}' NRPT rule exists but should not. Change required. - NrptRuleDoesNotExistAndShouldNotMessage = '{0}' NRPT rule does not exist and should not. Change not required. + GettingNrptRuleMessage = Getting '{0}' DNS Client NRPT rule. + NrptRuleExistsMessage = '{0}' DNS Client NRPT rule exists. + NrptRuleDoesNotExistMessage = '{0}' DNS Client NRPT rule does not exist. + SettingNrptRuleMessage = Setting '{0}' DNS Client NRPT rule. + EnsureNrptRuleExistsMessage = Ensuring '{0}' DNS Client NRPT rule exists. + EnsureNrptRuleDoesNotExistMessage = Ensuring '{0}' DNS Client NRPT rule does not exist. + NrptRuleCreatedMessage = '{0}' DNS Client NRPT rule has been created. + NrptRuleUpdatedMessage = '{0}' DNS Client NRPT rule has been updated. + NrptRuleRemovedMessage = '{0}' DNS Client NRPT rule has been removed. + TestingNrptRuleMessage = Testing '{0}' DNS Client NRPT rule. + NrptRuleDoesNotExistButShouldMessage = '{0}' DNS Client NRPT rule does not exist but should. Change required. + NrptRuleExistsButShouldNotMessage = '{0}' DNS Client NRPT rule exists but should not. Change required. + NrptRuleDoesNotExistAndShouldNotMessage = '{0}' DNS Client NRPT rule does not exist and should not. Change not required. '@ diff --git a/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 b/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 index 1d2de334..c1f93379 100644 --- a/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptGlobal/1-DnsClientNrptGlobal_Config.ps1 @@ -19,13 +19,7 @@ <# .DESCRIPTION - Configure Dns Client NRPT global configuration. - .PARAMETER EnableDAForAllNetworks - Specifies DirectAccess (DA) settings. (Default: Disable) - .PARAMETER QueryPolicy. - Specifies the DNS client query policy. (Default: Disable) - .PARAMETER SecureNameQueryFallback - Specifies the DNS client name resolution fallback policy. (Default: Disable) + Configure DNS Client NRPT global configuration and set EnableDAForAllNetworks to 'EnableAlways', QueryPolicy to 'QueryBoth' and SecureNameQueryFallback to 'FallbackSecure'. #> Configuration DnsClientNrptGlobal_Config { diff --git a/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 index c8306548..bf701ebf 100644 --- a/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptRule/1-DnsClientNrptRule_Server_Config.ps1 @@ -19,13 +19,7 @@ <# .DESCRIPTION - Sets a Dns Client NRPT rule to configure a conditional dns forwarder for a specific namespace. - .PARAMETER Name - Specifies the name which uniquely identifies a rule. - .PARAMETER NameServers - Specifies the DNS servers to which the DNS query is sent when DirectAccess is disabled. - .PARAMETER Namespace - Specifies the DNS namespace. + Sets a DNS Client NRPT rule named 'Contoso DNS Policy' to configure a conditional DNS forwarder (192.168.1.1) for a specific namespace (contoso.com). #> Configuration DnsClientNrptRule_Server_Config { @@ -35,7 +29,7 @@ Configuration DnsClientNrptRule_Server_Config { DnsClientNrptRule Server { - Name = 'Contoso Dns Policy' + Name = 'Contoso DNS Policy' Namespace = '.contoso.com' NameServers = ('192.168.1.1') } diff --git a/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 index 7fe8ce45..78314727 100644 --- a/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptRule/2-DnsClientNrptRule_DNSSEC_Config.ps1 @@ -19,13 +19,7 @@ <# .DESCRIPTION - Sets a Dns Client NRPT rule to enable DNSSEC queries for a specific namespace. - .PARAMETER Name - Specifies the name which uniquely identifies a rule. - .PARAMETER DnsSecEnable - Enables Domain Name System Security Extensions (DNSSEC) on the rule. - .PARAMETER Namespace - Specifies the DNS namespace. + Sets a DNS Client NRPT rule named 'DNSSEC' to enable DNSSEC queries for a specific namespace (contoso.com). #> Configuration DnsClientNrptRule_DNSSEC_Config { diff --git a/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 b/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 index 0e35c179..00eb0952 100644 --- a/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 +++ b/source/Examples/Resources/DnsClientNrptRule/3-DnsClientNrptRule_Punycode_Config.ps1 @@ -19,15 +19,7 @@ <# .DESCRIPTION - Sets a Dns Client NRPT rule to send Punycode DNS queries using a conditional dns forwarder for a specific namespace. - .PARAMETER Name - Specifies the name which uniquely identifies a rule. - .PARAMETER NameEncoding - Specifies the encoding format for host names in the DNS query. - .PARAMETER NameServers - Specifies the DNS servers to which the DNS query is sent when DirectAccess is disabled. - .PARAMETER Namespace - Specifies the DNS namespace. + Sets a DNS Client NRPT rule named 'Punycode' to send Punycode DNS queries using a conditional DNS forwarder for a specific namespace(contoso.com). #> Configuration DnsClientNrptRule_Punycode_Config { From e6819ed6b6df83b113b70fca35b243033bb9dfdb Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 24 Mar 2025 20:35:54 +0100 Subject: [PATCH 33/35] Fix syntax --- .../DSC_DnsClientNrptRule.psm1 | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 index 47f5b8c5..073d734f 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 @@ -265,7 +265,7 @@ function Set-TargetResource { # The NrptRule does not exit - create it Add-NrptRule @PSBoundParameters ` - -ErrorAction Stop + -ErrorAction Stop Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " @@ -686,9 +686,7 @@ function Add-NrptRule $null = $PSBoundParameters.Remove("Name") # The NrptRule does not exit - create it (PassThru to get the name of the rule created) - $NrptRuleName=(Add-DnsClientNrptRule @PSBoundParameters ` - -PassThru ` - -ErrorAction Stop).Name + $NrptRuleName=(Add-DnsClientNrptRule @PSBoundParameters -PassThru).Name # If rule has been created, rename it by registry as Name cannot be provided in Add-DnsClientNrptRule cmdlet if (Test-IsGuid -InputValue $NrptRuleName) { @@ -706,17 +704,23 @@ function Add-NrptRule .PARAMETER InputValue Specifies the value to test. #> -function Test-IsGuid { - param ( +function Test-IsGuid +{ + [CmdletBinding()] + param + ( [string]$InputValue ) - try { + try + { # Attempt to parse the string as a GUID [void][Guid]::Parse($InputValue) # If successful, return true return $true - } catch { + } + catch + { # If an exception is thrown, the string is not a valid GUID return $false } From d16f191b01aba3af707353e6eb2b500edfec102a Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Mon, 24 Mar 2025 20:47:23 +0100 Subject: [PATCH 34/35] Fix syntax --- .../DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 index 073d734f..22e1bfe0 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 @@ -706,12 +706,13 @@ function Add-NrptRule #> function Test-IsGuid { - [CmdletBinding()] + [OutputType([System.Boolean])] param ( - [string]$InputValue + [Parameter()] + [System.String] + $InputValue ) - try { # Attempt to parse the string as a GUID From 951dcafa1d371299c933fe7dacd35b79998f553c Mon Sep 17 00:00:00 2001 From: Alexandre JARDON <28548335+webalexeu@users.noreply.github.com> Date: Tue, 8 Apr 2025 18:01:07 +0200 Subject: [PATCH 35/35] Syntax + documentation --- .../DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 | 7 +++++-- .../DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof | 2 +- tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 index 22e1bfe0..8a8d559e 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.psm1 @@ -549,7 +549,9 @@ function Get-NrptRule <# .SYNOPSIS - This function create a DNS Client NRPT Rule using the parameters + This function create a DNS Client NRPT Rule using the parameters. + This is a dedicated function adding the Rule Name parameter as + the built-in Add-DnsClientNrptRule cmdlet does not support it. .PARAMETER Name Specifies the name which uniquely identifies a rule. @@ -686,7 +688,7 @@ function Add-NrptRule $null = $PSBoundParameters.Remove("Name") # The NrptRule does not exit - create it (PassThru to get the name of the rule created) - $NrptRuleName=(Add-DnsClientNrptRule @PSBoundParameters -PassThru).Name + $NrptRuleName = (Add-DnsClientNrptRule @PSBoundParameters -PassThru).Name # If rule has been created, rename it by registry as Name cannot be provided in Add-DnsClientNrptRule cmdlet if (Test-IsGuid -InputValue $NrptRuleName) { @@ -713,6 +715,7 @@ function Test-IsGuid [System.String] $InputValue ) + try { # Attempt to parse the string as a GUID diff --git a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof index 9de11ef2..cfdce6bf 100644 --- a/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof +++ b/source/DSCResources/DSC_DnsClientNrptRule/DSC_DnsClientNrptRule.schema.mof @@ -2,7 +2,7 @@ class DSC_DnsClientNrptRule : OMI_BaseResource { [Key, Description("Specifies the DNS Client NRPT rule name.")] string Name; - [Write, Description("Specifies whether the NRPT rule should exist. Defaults to 'Present'."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; + [Write, Description("Specifies whether the DNS Client NRPT rule should exist. Defaults to 'Present'."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; [Write, Description("Stores administrator notes.")] string Comment; [Write, Description("Indicates the rule state for DirectAccess.")] Boolean DAEnable; [Write, Description("Specifies the Internet Protocol security (IPsec) encryption setting for DirectAccess."), ValueMap{"None", "Low", "Medium", "High"},Values{"None", "Low", "Medium", "High"}] string DAIPsecEncryptionType; diff --git a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 index 78ab03c1..631c579a 100644 --- a/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 +++ b/tests/Unit/DSC_DnsClientNrptRule.Tests.ps1 @@ -146,6 +146,7 @@ Describe 'DSC_DnsClientNrptRule\Set-TargetResource' -Tag 'Set' { } } } + Context 'NRPT Rule does not exist but should' { BeforeAll { Mock -CommandName Get-DnsClientNrptRule