diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f2166b0..487a10a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,16 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md) present in the module DscResource.Common. - Removed the function `Get-CurrentUser` since no code were using it. +### Changed + +- Website + - Add Ensure to LogCustomFieldInformation. ([issue #571](https://github.com/dsccommunity/WebAdministrationDsc/issues/571)) + +### Fixed + +- IisLogging + - Can now remove all LogCustomFields using Ensure. ([issue #571](https://github.com/dsccommunity/WebAdministrationDsc/issues/571)) + ## [4.1.0] - 2023-01-03 ### Fixed @@ -32,10 +42,10 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md) - CommonTestHelper Added `Invoke-UnitTestCleanup` to get consistent cleanup of stubs. Gives correct execution of integration tests when run in same PowerShell session as unit tests (no longer calling stubs). - Gives correct `Restore-WebConfiguration` after integration tests when run in same PowerShell session as unit tests (no longer calling stub). + Gives correct `Restore-WebConfiguration` after integration tests when run in same PowerShell session as unit tests (no longer calling stub). - MockWebAdministrationWindowsFeature [Issue #351](https://github.com/dsccommunity/WebAdministrationDsc/issues/351) - Stubs now throw StubNotImplemented when they are called in order to show when a cmdlet is not mocked correctly. + Stubs now throw StubNotImplemented when they are called in order to show when a cmdlet is not mocked correctly. ## [4.0.0] - 2022-09-17 @@ -174,9 +184,9 @@ For older change log history see the [historic changelog](HISTORIC_CHANGELOG.md) - xWebAdministration.Common - Added new helper function `Get-WebConfigurationPropertyValue` to - help return a value of a `WebConfigurationProperty`. *This helper* - *function is unable to be unit tested because it is using a type* - *that cannot be mocked.* + help return a value of a `WebConfigurationProperty`. _This helper_ + _function is unable to be unit tested because it is using a type_ + _that cannot be mocked._ - xWebAppPoolDefaults - Changed to use the new helper function `Get-WebConfigurationPropertyValue` so that the resource can be properly unit tested. diff --git a/source/DSCResources/DSC_IisLogging/DSC_IisLogging.psm1 b/source/DSCResources/DSC_IisLogging/DSC_IisLogging.psm1 index fb709167..c298d50d 100644 --- a/source/DSCResources/DSC_IisLogging/DSC_IisLogging.psm1 +++ b/source/DSCResources/DSC_IisLogging/DSC_IisLogging.psm1 @@ -500,8 +500,14 @@ function Set-LogCustomField <# Set-WebConfigurationProperty updates logfile.customFields. #> - - Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/Sites/SiteDefaults/logFile/customFields" -Name "." -Value $setCustomFields + if ($setCustomFields.Count -gt 0) + { + Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/Sites/SiteDefaults/logFile/customFields" -Name "." -Value $setCustomFields + } + else + { + Remove-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/Sites/SiteDefaults/logFile/customFields" -Name "." + } } <# diff --git a/source/DSCResources/DSC_WebSite/DSC_WebSite.psm1 b/source/DSCResources/DSC_WebSite/DSC_WebSite.psm1 index 12ff46e8..253d7f81 100644 --- a/source/DSCResources/DSC_WebSite/DSC_WebSite.psm1 +++ b/source/DSCResources/DSC_WebSite/DSC_WebSite.psm1 @@ -1793,17 +1793,27 @@ function Set-LogCustomField $setCustomFields = @() foreach ($customField in $LogCustomField) { - $setCustomFields += @{ - logFieldName = $customField.LogFieldName - sourceName = $customField.SourceName - sourceType = $customField.SourceType + if ($customField.Ensure -ne 'Absent') + { + $setCustomFields += @{ + logFieldName = $customField.LogFieldName + sourceName = $customField.SourceName + sourceType = $customField.SourceType + } } } # The second Set-WebConfigurationProperty is to handle an edge case where logfile.customFields is not updated correctly. May be caused by a possible bug in the IIS provider for ($i = 1; $i -le 2; $i++) { - Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/sites/site[@name='$Site']/logFile/customFields" -Name "." -Value $setCustomFields + if ($setCustomFields.Count -gt 0) + { + Set-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/sites/site[@name='$Site']/logFile/customFields" -Name "." -Value $setCustomFields + } + else + { + Remove-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter "system.applicationHost/sites/site[@name='$Site']/logFile/customFields" -Name "." + } } } @@ -2087,29 +2097,35 @@ function Test-LogCustomField $LogCustomField ) - $inDesiredSate = $true + $inDesiredState = $true foreach ($customField in $LogCustomField) { $filterString = "/system.applicationHost/sites/site[@name='{0}']/logFile/customFields/add[@logFieldName='{1}']" -f $Site, $customField.LogFieldName $presentCustomField = Get-WebConfigurationProperty -Filter $filterString -Name "." + $shouldBePresent = $customField.Ensure -ne 'Absent' + if ($presentCustomField) { $sourceNameMatch = $customField.SourceName -eq $presentCustomField.SourceName $sourceTypeMatch = $customField.SourceType -eq $presentCustomField.sourceType - if (-not ($sourceNameMatch -and $sourceTypeMatch)) + $doesNotMatchEnsure = (-not ($sourceNameMatch -and $sourceTypeMatch)) -eq $shouldBePresent + if ($doesNotMatchEnsure) { - $inDesiredSate = $false + $inDesiredState = $false } } else { - $inDesiredSate = $false + if ($shouldBePresent) + { + $inDesiredState = $false + } } } - return $inDesiredSate + return $inDesiredState } <# diff --git a/source/DSCResources/DSC_WebSite/DSC_WebSite.schema.mof b/source/DSCResources/DSC_WebSite/DSC_WebSite.schema.mof index 10f127f5..fb245e8c 100644 --- a/source/DSCResources/DSC_WebSite/DSC_WebSite.schema.mof +++ b/source/DSCResources/DSC_WebSite/DSC_WebSite.schema.mof @@ -55,4 +55,5 @@ class DSC_LogCustomFieldInformation [Write, Description("Field name to identify the custom field within the log file. Please note that the field name cannot contain spaces.")] String LogFieldName; [Write, Description("The acceptable values for this property are: RequestHeader, ResponseHeader or ServerVariable (note that enhanced logging cannot log a server variable with a name that contains lower-case characters - to include a server variable in the event log just make sure that its name consists of all upper-case characters)."),ValueMap{"RequestHeader","ResponseHeader","ServerVariable"},Values{"RequestHeader","ResponseHeader","ServerVariable"}] String SourceType; [Write, Description("Name of the HTTP header or server variable (depending on the Source Type you selected) that contains a value that you want to log.")] String SourceName; + [Write, Description("Indicates if the custom log field should be present or absent. Defaults to Present."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; }; diff --git a/source/Examples/Resources/IisLogging/Sample_IisLogging_LogCustomFields_EnsureAbsent.ps1 b/source/Examples/Resources/IisLogging/Sample_IisLogging_LogCustomFields_EnsureAbsent.ps1 new file mode 100644 index 00000000..76b7cfab --- /dev/null +++ b/source/Examples/Resources/IisLogging/Sample_IisLogging_LogCustomFields_EnsureAbsent.ps1 @@ -0,0 +1,31 @@ +configuration Sample_IisLogging_LogCustomFields_EnsureAbsent +{ + param + ( + # Target nodes to apply the configuration + [String[]] $NodeName = 'localhost' + ) + + # Import the module that defines custom resources + Import-DscResource -Module WebAdministrationDsc + + Node $NodeName + { + IisLogging Logging + { + LogPath = 'C:\IISLogFiles' + Logflags = @('Date','Time','ClientIP','ServerIP','UserAgent') + LogFormat = 'W3C' + LogTargetW3C = 'File,ETW' + LogCustomFields = @( + DSC_LogCustomField + { + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + Ensure = 'Absent' + } + ) + } + } +} diff --git a/source/Examples/Resources/IisLogging/Sample_IisLogging_LogCustomFields_EnsurePresentDefault.ps1 b/source/Examples/Resources/IisLogging/Sample_IisLogging_LogCustomFields_EnsurePresentDefault.ps1 new file mode 100644 index 00000000..cf3410ed --- /dev/null +++ b/source/Examples/Resources/IisLogging/Sample_IisLogging_LogCustomFields_EnsurePresentDefault.ps1 @@ -0,0 +1,30 @@ +configuration Sample_IisLogging_LogCustomFields_EnsurePresentDefault +{ + param + ( + # Target nodes to apply the configuration + [String[]] $NodeName = 'localhost' + ) + + # Import the module that defines custom resources + Import-DscResource -Module WebAdministrationDsc + + Node $NodeName + { + IisLogging Logging + { + LogPath = 'C:\IISLogFiles' + Logflags = @('Date','Time','ClientIP','ServerIP','UserAgent') + LogFormat = 'W3C' + LogTargetW3C = 'File,ETW' + LogCustomFields = @( + DSC_LogCustomField + { + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + } + ) + } + } +} diff --git a/source/Examples/Resources/IisLogging/Sample_IisLogging_LogCustomFields_EnsurePresentExplicitly.ps1 b/source/Examples/Resources/IisLogging/Sample_IisLogging_LogCustomFields_EnsurePresentExplicitly.ps1 new file mode 100644 index 00000000..e07bb701 --- /dev/null +++ b/source/Examples/Resources/IisLogging/Sample_IisLogging_LogCustomFields_EnsurePresentExplicitly.ps1 @@ -0,0 +1,31 @@ +configuration Sample_IisLogging_LogCustomFields_EnsurePresentExplicitly +{ + param + ( + # Target nodes to apply the configuration + [String[]] $NodeName = 'localhost' + ) + + # Import the module that defines custom resources + Import-DscResource -Module WebAdministrationDsc + + Node $NodeName + { + IisLogging Logging + { + LogPath = 'C:\IISLogFiles' + Logflags = @('Date','Time','ClientIP','ServerIP','UserAgent') + LogFormat = 'W3C' + LogTargetW3C = 'File,ETW' + LogCustomFields = @( + DSC_LogCustomField + { + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + Ensure = 'Present' + } + ) + } + } +} diff --git a/source/Examples/Resources/WebSite/Sample_WebSite_NewWebsite.ps1 b/source/Examples/Resources/WebSite/Sample_WebSite_NewWebsite.ps1 index dc80859f..ffe13674 100644 --- a/source/Examples/Resources/WebSite/Sample_WebSite_NewWebsite.ps1 +++ b/source/Examples/Resources/WebSite/Sample_WebSite_NewWebsite.ps1 @@ -76,7 +76,7 @@ configuration Sample_WebSite_NewWebsite { Ensure = 'Present' Name = $WebSiteName - SiteId = $SiteId + SiteId = $SiteId State = 'Started' ServerAutoStart = $true PhysicalPath = $DestinationPath diff --git a/source/Examples/Resources/WebSite/Sample_WebSite_WithCustomLogFields_EnsureAbsent.ps1 b/source/Examples/Resources/WebSite/Sample_WebSite_WithCustomLogFields_EnsureAbsent.ps1 new file mode 100644 index 00000000..49eb7a13 --- /dev/null +++ b/source/Examples/Resources/WebSite/Sample_WebSite_WithCustomLogFields_EnsureAbsent.ps1 @@ -0,0 +1,97 @@ +configuration Sample_WebSite_WithCustomLogFields_EnsureAbsent +{ + param + ( + # Target nodes to apply the configuration + [String[]] + $NodeName = 'localhost', + + # Name of the website to create + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $WebSiteName, + + # Optional Site Id for the website + [Parameter()] + [UInt32] + $SiteId, + + # Source Path for Website content + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $SourcePath, + + # Destination path for Website content + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $DestinationPath + ) + + # Import the module that defines custom resources + Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration + + Node $NodeName + { + # Install the IIS role + WindowsFeature IIS + { + Ensure = 'Present' + Name = 'Web-Server' + } + + # Install the ASP .NET 4.5 role + WindowsFeature AspNet45 + { + Ensure = 'Present' + Name = 'Web-Asp-Net45' + } + + # Stop the default website + WebSite DefaultSite + { + Ensure = 'Present' + Name = 'Default Web Site' + State = 'Stopped' + ServerAutoStart = $false + PhysicalPath = 'C:\inetpub\wwwroot' + DependsOn = '[WindowsFeature]IIS' + } + + # Copy the website content + File WebContent + { + Ensure = 'Present' + SourcePath = $SourcePath + DestinationPath = $DestinationPath + Recurse = $true + Type = 'Directory' + DependsOn = '[WindowsFeature]AspNet45' + } + + # Create the new Website + WebSite NewWebsite + { + Ensure = 'Present' + Name = $WebSiteName + SiteId = $SiteId + State = 'Started' + ServerAutoStart = $true + PhysicalPath = $DestinationPath + DependsOn = '[File]WebContent' + LogFlags = @('Date','Time','ClientIP','ServerIP','UserAgent') + LogFormat = 'W3C' + LogCustomFields = @( + DSC_LogCustomFieldInformation + { + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + Ensure = 'Absent' + } + ) + } + } +} diff --git a/source/Examples/Resources/WebSite/Sample_WebSite_WithCustomLogFields_EnsurePresentDefault.ps1 b/source/Examples/Resources/WebSite/Sample_WebSite_WithCustomLogFields_EnsurePresentDefault.ps1 new file mode 100644 index 00000000..445314f9 --- /dev/null +++ b/source/Examples/Resources/WebSite/Sample_WebSite_WithCustomLogFields_EnsurePresentDefault.ps1 @@ -0,0 +1,96 @@ +configuration Sample_WebSite_WithCustomLogFields_EnsurePresentDefault +{ + param + ( + # Target nodes to apply the configuration + [String[]] + $NodeName = 'localhost', + + # Name of the website to create + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $WebSiteName, + + # Optional Site Id for the website + [Parameter()] + [UInt32] + $SiteId, + + # Source Path for Website content + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $SourcePath, + + # Destination path for Website content + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $DestinationPath + ) + + # Import the module that defines custom resources + Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration + + Node $NodeName + { + # Install the IIS role + WindowsFeature IIS + { + Ensure = 'Present' + Name = 'Web-Server' + } + + # Install the ASP .NET 4.5 role + WindowsFeature AspNet45 + { + Ensure = 'Present' + Name = 'Web-Asp-Net45' + } + + # Stop the default website + WebSite DefaultSite + { + Ensure = 'Present' + Name = 'Default Web Site' + State = 'Stopped' + ServerAutoStart = $false + PhysicalPath = 'C:\inetpub\wwwroot' + DependsOn = '[WindowsFeature]IIS' + } + + # Copy the website content + File WebContent + { + Ensure = 'Present' + SourcePath = $SourcePath + DestinationPath = $DestinationPath + Recurse = $true + Type = 'Directory' + DependsOn = '[WindowsFeature]AspNet45' + } + + # Create the new Website + WebSite NewWebsite + { + Ensure = 'Present' + Name = $WebSiteName + SiteId = $SiteId + State = 'Started' + ServerAutoStart = $true + PhysicalPath = $DestinationPath + DependsOn = '[File]WebContent' + LogFlags = @('Date','Time','ClientIP','ServerIP','UserAgent') + LogFormat = 'W3C' + LogCustomFields = @( + DSC_LogCustomFieldInformation + { + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + } + ) + } + } +} diff --git a/source/Examples/Resources/WebSite/Sample_WebSite_WithCustomLogFields_EnsurePresentExplicitly.ps1 b/source/Examples/Resources/WebSite/Sample_WebSite_WithCustomLogFields_EnsurePresentExplicitly.ps1 new file mode 100644 index 00000000..298befbe --- /dev/null +++ b/source/Examples/Resources/WebSite/Sample_WebSite_WithCustomLogFields_EnsurePresentExplicitly.ps1 @@ -0,0 +1,97 @@ +configuration Sample_WebSite_WithCustomLogFields_EnsurePresentExplicitly +{ + param + ( + # Target nodes to apply the configuration + [String[]] + $NodeName = 'localhost', + + # Name of the website to create + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $WebSiteName, + + # Optional Site Id for the website + [Parameter()] + [UInt32] + $SiteId, + + # Source Path for Website content + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $SourcePath, + + # Destination path for Website content + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [String] + $DestinationPath + ) + + # Import the module that defines custom resources + Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration + + Node $NodeName + { + # Install the IIS role + WindowsFeature IIS + { + Ensure = 'Present' + Name = 'Web-Server' + } + + # Install the ASP .NET 4.5 role + WindowsFeature AspNet45 + { + Ensure = 'Present' + Name = 'Web-Asp-Net45' + } + + # Stop the default website + WebSite DefaultSite + { + Ensure = 'Present' + Name = 'Default Web Site' + State = 'Stopped' + ServerAutoStart = $false + PhysicalPath = 'C:\inetpub\wwwroot' + DependsOn = '[WindowsFeature]IIS' + } + + # Copy the website content + File WebContent + { + Ensure = 'Present' + SourcePath = $SourcePath + DestinationPath = $DestinationPath + Recurse = $true + Type = 'Directory' + DependsOn = '[WindowsFeature]AspNet45' + } + + # Create the new Website + WebSite NewWebsite + { + Ensure = 'Present' + Name = $WebSiteName + SiteId = $SiteId + State = 'Started' + ServerAutoStart = $true + PhysicalPath = $DestinationPath + DependsOn = '[File]WebContent' + LogFlags = @('Date','Time','ClientIP','ServerIP','UserAgent') + LogFormat = 'W3C' + LogCustomFields = @( + DSC_LogCustomFieldInformation + { + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + Ensure = 'Present' + } + ) + } + } +} diff --git a/tests/Integration/DSC_IISLogging.Integration.Tests.ps1 b/tests/Integration/DSC_IISLogging.Integration.Tests.ps1 index 12859ea8..eb7fe8aa 100644 --- a/tests/Integration/DSC_IISLogging.Integration.Tests.ps1 +++ b/tests/Integration/DSC_IISLogging.Integration.Tests.ps1 @@ -132,6 +132,36 @@ try $currentLogSettings.customFields.Collection[1].SourceType | Should Be 'ResponseHeader' } } + + Describe "$($script:dscResourceName)_LogCustomFields" { + #region DEFAULT TESTS + It 'Should compile without throwing' { + { + Invoke-Expression -Command "$($script:dscResourceName)_LogCustomFields -OutputPath `$TestDrive" + 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 + } + #endregion + It 'Should remove all custom log fields' -test { + + Invoke-Expression -Command "$($script:dscResourceName)_LogCustomFields -OutputPath `$TestDrive" + Start-DscConfiguration -Path $TestDrive -ComputerName localhost -Wait -Verbose -Force + + $currentLogSettings = Get-WebConfiguration -filter '/system.applicationHost/sites/siteDefaults/Logfile' + + $currentLogSettings.directory | Should Be 'C:\IISLogFiles' + $currentLogSettings.logExtFileFlags | Should Be 'Date,Time,ClientIP,ServerIP,UserAgent' + $currentLogSettings.logformat | Should Be 'W3C' + $currentLogSettings.logTargetW3C | Should Be 'File,ETW' + $currentLogSettings.TruncateSize | Should Be '2097152' + $currentLogSettings.localTimeRollover | Should Be 'True' + $currentLogSettings.customFields.Collection | Should -BeNullOrEmpty + } + } } finally { diff --git a/tests/Integration/DSC_IISLogging.config.ps1 b/tests/Integration/DSC_IISLogging.config.ps1 index 2bb05039..b364f3af 100644 --- a/tests/Integration/DSC_IISLogging.config.ps1 +++ b/tests/Integration/DSC_IISLogging.config.ps1 @@ -84,3 +84,34 @@ configuration DSC_IisLogging_LogFlags ) } } + +configuration DSC_IisLogging_LogCustomFields +{ + Import-DscResource -ModuleName WebAdministrationDsc + + IisLogging Logging + { + LogPath = 'C:\IISLogFiles' + Logflags = @('Date','Time','ClientIP','ServerIP','UserAgent') + LoglocalTimeRollover = $true + LogTruncateSize = '2097152' + LogFormat = 'W3C' + LogTargetW3C = 'File,ETW' + LogCustomFields = @( + DSC_LogCustomField + { + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + Ensure = 'Absent' + } + DSC_LogCustomField + { + LogFieldName = 'X-Powered-By' + SourceName = 'ASP.NET' + SourceType = 'ResponseHeader' + Ensure = 'Absent' + } + ) + } +} diff --git a/tests/Integration/DSC_WebSite.Integration.Tests.ps1 b/tests/Integration/DSC_WebSite.Integration.Tests.ps1 index 450c9701..53eacfd3 100644 --- a/tests/Integration/DSC_WebSite.Integration.Tests.ps1 +++ b/tests/Integration/DSC_WebSite.Integration.Tests.ps1 @@ -266,6 +266,32 @@ try } } + Describe "$($script:dscResourceName)_Custom_Logging_Configured" { + #region DEFAULT TESTS + It 'Should compile without throwing' { + { + Invoke-Expression -Command "$($script:dscResourceName)_Custom_Logging_Configured -ConfigurationData `$dscConfig -OutputPath `$TestDrive -CertificateThumbprint `$selfSignedCert.Thumbprint" + 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 + } + #endregion + + It 'Should remove all custom log fields' -test { + + Invoke-Expression -Command "$($script:dscResourceName)_Custom_Logging_Configured -ConfigurationData `$dscConfig -OutputPath `$TestDrive -CertificateThumbprint `$selfSignedCert.Thumbprint" + + # Build results to test + $result = Get-Website -Name $dscConfig.AllNodes.Website + + # Test Website has updated CustomLogFields + $result.logFile.LogCustomFields | Should -BeNullOrEmpty + } + } + Describe "$($script:dscResourceName)_Present_Stopped" { #region DEFAULT TESTS It 'Should compile the MOF without throwing' { diff --git a/tests/Integration/DSC_WebSite.config.ps1 b/tests/Integration/DSC_WebSite.config.ps1 index 736ce613..025014db 100644 --- a/tests/Integration/DSC_WebSite.config.ps1 +++ b/tests/Integration/DSC_WebSite.config.ps1 @@ -331,3 +331,41 @@ configuration DSC_WebSite_Logging_Configured } } } + +configuration DSC_WebSite_Custom_Logging_Configured +{ + param( + + [Parameter(Mandatory = $true)] + [String] $CertificateThumbprint + + ) + + Import-DscResource -ModuleName WebAdministrationDsc + + Node $AllNodes.NodeName + { + WebSite Website + { + Name = $Node.Website + LogFlags = $Node.LogFlags2 + LogFormat = $Node.LogFormat + LogCustomFields = @( + DSC_LogCustomFieldInformation + { + LogFieldName = $Node.LogFieldName1 + SourceName = $Node.SourceName1 + SourceType = $Node.SourceType1 + Ensure = 'Absent' + } + DSC_LogCustomFieldInformation + { + LogFieldName = $Node.LogFieldName2 + SourceName = $Node.SourceName2 + SourceType = $Node.SourceType2 + Ensure = 'Absent' + } + ) + } + } +} diff --git a/tests/Unit/DSC_IISLogging.Tests.ps1 b/tests/Unit/DSC_IISLogging.Tests.ps1 index 606d89a9..7a5b7ca0 100644 --- a/tests/Unit/DSC_IISLogging.Tests.ps1 +++ b/tests/Unit/DSC_IISLogging.Tests.ps1 @@ -970,6 +970,7 @@ try Context 'Create new LogCustomField' { Mock -CommandName Set-WebConfigurationProperty + Mock -CommandName Remove-WebConfigurationProperty It 'Should not throw an error with default Ensure (Present)' { { Set-LogCustomField -LogCustomField $MockCimLogCustomFields } | Should Not Throw @@ -984,13 +985,15 @@ try } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 3 + Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 2 + Assert-MockCalled -CommandName Remove-WebConfigurationProperty -Exactly 1 } } Context 'Modify existing LogCustomField' { Mock -CommandName Set-WebConfigurationProperty + Mock -CommandName Remove-WebConfigurationProperty It 'Should not throw an error with default Ensure (Present)' { { Set-LogCustomField -LogCustomField $MockCimLogCustomFields } | Should Not Throw @@ -1005,7 +1008,8 @@ try } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 3 + Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 2 + Assert-MockCalled -CommandName Remove-WebConfigurationProperty -Exactly 1 } } } diff --git a/tests/Unit/DSC_Website.Tests.ps1 b/tests/Unit/DSC_Website.Tests.ps1 index 14b73f96..bf9848c0 100644 --- a/tests/Unit/DSC_Website.Tests.ps1 +++ b/tests/Unit/DSC_Website.Tests.ps1 @@ -3977,6 +3977,29 @@ try } ` -ClientOnly ) + $MockCimLogCustomFieldsEnsurePresentExplicitly = @( + New-CimInstance -ClassName DSC_LogCustomFieldInformation ` + -Namespace root/microsoft/Windows/DesiredStateConfiguration ` + -Property @{ + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + Ensure = 'Present' + } ` + -ClientOnly + ) + + $MockCimLogCustomFieldsEnsureAbsent = @( + New-CimInstance -ClassName DSC_LogCustomFieldInformation ` + -Namespace root/microsoft/Windows/DesiredStateConfiguration ` + -Property @{ + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + Ensure = 'Absent' + } ` + -ClientOnly + ) Context 'LogCustomField in desired state'{ $MockDesiredLogCustomFields = @{ @@ -3987,9 +4010,18 @@ try Mock -CommandName Get-WebConfigurationProperty -MockWith { return $MockDesiredLogCustomFields } - It 'should return True' { + It 'Should return True with default Ensure (Present)' { Test-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFields | Should Be $True } + + It 'Should return True with explicit Ensure Present' { + Test-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFieldsEnsurePresentExplicitly | Should Be $True + } + + It 'Should return True with Ensure Absent and Custom Field Absent' { + Mock -CommandName Get-WebConfigurationProperty -MockWith { return $null } + Test-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFieldsEnsureAbsent | Should Be $True + } } Context 'LogCustomField not in desired state'{ @@ -4001,15 +4033,16 @@ try Mock -CommandName Get-WebConfigurationProperty -MockWith { return $MockWrongLogCustomFields } - It 'should return False' { + It 'Should return False with default Ensure (Present)' { Test-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFields | Should Be $False } - } - Context 'LogCustomField not present'{ - Mock -CommandName Get-WebConfigurationProperty -MockWith { return $false } + It 'Should return False with explicit Ensure Present' { + Test-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFieldsEnsurePresentExplicitly | Should Be $False + } - It 'should return False' { + It 'Should return False with Ensure Present and Custom Field Absent' { + Mock -CommandName Get-WebConfigurationProperty -MockWith { return $null } Test-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFields | Should Be $False } } @@ -4030,27 +4063,71 @@ try -ClientOnly ) + $MockCimLogCustomFieldsEnsurePresentExplicitly = @( + New-CimInstance -ClassName DSC_LogCustomFieldInformation ` + -Namespace root/microsoft/Windows/DesiredStateConfiguration ` + -Property @{ + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + Ensure = 'Present' + } ` + -ClientOnly + ) + + $MockCimLogCustomFieldsEnsureAbsent = @( + New-CimInstance -ClassName DSC_LogCustomFieldInformation ` + -Namespace root/microsoft/Windows/DesiredStateConfiguration ` + -Property @{ + LogFieldName = 'ClientEncoding' + SourceName = 'Accept-Encoding' + SourceType = 'RequestHeader' + Ensure = 'Absent' + } ` + -ClientOnly + ) + Context 'Create new LogCustomField'{ Mock -CommandName Set-WebConfigurationProperty + Mock -CommandName Remove-WebConfigurationProperty - It 'should not throw an error' { + It 'Should not throw an error with default Ensure (Present)' { { Set-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFields } | Should Not Throw } - It 'should call should call expected mocks' { - Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 2 + It 'Should not throw an error with explicit Ensure Present' { + { Set-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFieldsEnsurePresentExplicitly } | Should Not Throw + } + + It 'Should not throw an error with Ensure Absent' { + { Set-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFieldsEnsureAbsent } | Should Not Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 4 + Assert-MockCalled -CommandName Remove-WebConfigurationProperty -Exactly 2 } } Context 'Modify existing LogCustomField'{ Mock -CommandName Set-WebConfigurationProperty + Mock -CommandName Remove-WebConfigurationProperty - It 'should not throw an error' { + It 'Should not throw an error with default Ensure (Present)' { { Set-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFields } | Should Not Throw } - It 'should call should call expected mocks' { - Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 2 + It 'Should not throw an error with explicit Ensure Present' { + { Set-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFieldsEnsurePresentExplicitly } | Should Not Throw + } + + It 'Should not throw an error with Ensure Absent' { + { Set-LogCustomField -Site $MockWebsiteName -LogCustomField $MockCimLogCustomFieldsEnsureAbsent } | Should Not Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Set-WebConfigurationProperty -Exactly 4 + Assert-MockCalled -CommandName Remove-WebConfigurationProperty -Exactly 2 } } }