diff --git a/CHANGELOG.md b/CHANGELOG.md index 2252902a..022ad711 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Updated CHANGELOG.md - Renamed NetworkingDSc to NetworkingDsc in CHANGELOG.md - fixes [Issue #513](https://github.com/dsccommunity/NetworkingDsc/issues/513). +- HostsFile + - Fix bad return data when line contains leading spaces - fixes [Issue #526](https://github.com/dsccommunity/NetworkingDsc/issues/526). - CI Pipeline - Updated pipeline files to match current DSC Community patterns - fixes [Issue #528](https://github.com/dsccommunity/NetworkingDsc/issues/528). - Updated HQRM and build steps to use windows-latest image. diff --git a/source/DSCResources/DSC_HostsFile/DSC_HostsFile.psm1 b/source/DSCResources/DSC_HostsFile/DSC_HostsFile.psm1 index 279af660..8217bc4d 100644 --- a/source/DSCResources/DSC_HostsFile/DSC_HostsFile.psm1 +++ b/source/DSCResources/DSC_HostsFile/DSC_HostsFile.psm1 @@ -230,7 +230,7 @@ function Get-HostEntry foreach ($hosts in $allHosts) { - $data = $hosts -split '\s+' + $data = $hosts.Trim() -split '\s+' if ($data.Length -gt 2) { diff --git a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 index 20d5e621..dd2dd12b 100644 --- a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 +++ b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 @@ -140,6 +140,54 @@ try $result.IPAddress | Should -BeNullOrEmpty } } + + Describe "$($script:dscResourceName)_Integration - Update single line with leading space" { + $configData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + HostName = 'Host01' + IPAddress = ' 192.168.0.1' + Ensure = 'present' + } + ) + } + + $configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1" + . $configFile -Verbose -ErrorAction Stop + + 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 + } + + It 'Should have set the resource and all the parameters should match' { + $result = Get-DscConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq "$($script:dscResourceName)_Config" + } + $result.Ensure | Should -Be $configData.AllNodes[0].Ensure + $result.HostName | Should -Be $configData.AllNodes[0].HostName + $result.IPAddress | Should -Be $configData.AllNodes[0].IPAddress.Trim() + } + + It 'Should include the extra whitespace' { + "${env:SystemRoot}\System32\Drivers\Etc\Hosts" | Should -FileContentMatch "$($configData.AllNodes[0].IPAddress)`t$($configData.AllNodes[0].HostName)" + } + + It 'Should not fail subsiquent Test invocations' { + $result = Test-DscConfiguration -Path $TestDrive + $result | Should -BeTrue + } + } } } finally diff --git a/tests/Unit/DSC_HostsFile.Tests.ps1 b/tests/Unit/DSC_HostsFile.Tests.ps1 index 43d858b1..b7ebdcd6 100644 --- a/tests/Unit/DSC_HostsFile.Tests.ps1 +++ b/tests/Unit/DSC_HostsFile.Tests.ps1 @@ -297,6 +297,62 @@ try { Set-TargetResource @testParams } | Should -Throw $script:localizedData.UnableToEnsureWithoutIP } } + + Context 'When a host entry has leading spaces' { + $testParams = @{ + HostName = 'www.anotherexample.com' + IPAddress = '127.0.0.1' + Verbose = $true + } + + Mock -CommandName Get-Content -MockWith { + return @( + '# A mocked example of a host file - this line is a comment', + '', + '127.0.0.1 localhost', + '# comment', + ' 127.0.0.1 www.anotherexample.com', + '' + ) + } + + It 'Should return absent from the get method' { + (Get-TargetResource @testParams).Ensure | Should -Be 'Present' + } + } + + Context 'When a host entry exists and is not correct, but has a leading space' { + $testParams = @{ + HostName = 'www.contoso.com' + IPAddress = '192.168.0.156' + Verbose = $true + } + + Mock -CommandName Get-Content -MockWith { + return @( + '# A mocked example of a host file - this line is a comment', + '', + '127.0.0.1 localhost', + '127.0.0.1 www.anotherexample.com', + " 127.0.0.1 $($testParams.HostName)", + '127.0.0.5 anotherexample.com', + '' + ) + } + + It 'Should return present from the get method' { + (Get-TargetResource @testParams).Ensure | Should -Be 'Present' + } + + It 'Should return false from the test method' { + Test-TargetResource @testParams | Should -Be $false + } + + It 'Should update the entry in the set method' { + Set-TargetResource @testParams + Assert-MockCalled -CommandName Set-Content + } + } } } #end InModuleScope $DSCResourceName }