From 9cae2739177455bec34bf9e9505b2c1effcb0949 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Fri, 31 May 2024 09:27:05 -0400 Subject: [PATCH 01/11] Trim the line before splitting parts --- source/DSCResources/DSC_HostsFile/DSC_HostsFile.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) { From 5a1d42b8d7363ac4641f331f88355e936c33cdc7 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Fri, 31 May 2024 09:31:22 -0400 Subject: [PATCH 02/11] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 824bd648..0d40e35e 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) ## [9.0.0] - 2022-05-30 From 2d3a5b87cd182473a568facfa8704e3eec3a1992 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Fri, 31 May 2024 09:35:51 -0400 Subject: [PATCH 03/11] Add a unit test to validate handling of leading spaces --- tests/Unit/DSC_HostsFile.Tests.ps1 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Unit/DSC_HostsFile.Tests.ps1 b/tests/Unit/DSC_HostsFile.Tests.ps1 index 43d858b1..b5da5756 100644 --- a/tests/Unit/DSC_HostsFile.Tests.ps1 +++ b/tests/Unit/DSC_HostsFile.Tests.ps1 @@ -298,6 +298,30 @@ try } } } + + 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' + } + } } #end InModuleScope $DSCResourceName } finally From 9f5e4a5b7f9c8602fd77d21593e02a04862404d3 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 11 Jun 2024 10:04:14 -0400 Subject: [PATCH 04/11] Add a test for updating a line with a leading space --- tests/Unit/DSC_HostsFile.Tests.ps1 | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Unit/DSC_HostsFile.Tests.ps1 b/tests/Unit/DSC_HostsFile.Tests.ps1 index b5da5756..4915e2cd 100644 --- a/tests/Unit/DSC_HostsFile.Tests.ps1 +++ b/tests/Unit/DSC_HostsFile.Tests.ps1 @@ -322,6 +322,39 @@ try (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 } finally From fb27bd9557502613e181d797dbd686021854fa00 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 11 Jun 2024 10:05:07 -0400 Subject: [PATCH 05/11] Test handling of a leading space in the parameters --- .../DSC_HostsFile.Integration.Tests.ps1 | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 index 20d5e621..9d7ddb52 100644 --- a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 +++ b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 @@ -140,6 +140,45 @@ try $result.IPAddress | Should -BeNullOrEmpty } } + + Describe "$($script:dscResourceName)_Integration - Remove Single Line" { + $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() + } + } } } finally From c93742210fbfbd995c524e01451364cd98c2ee14 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 11 Jun 2024 10:05:36 -0400 Subject: [PATCH 06/11] Full stop --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3484dcc0..022ad711 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ 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) + - 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. From daab227d56dfe8603d1ade434cec23e6a30a1825 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 11 Jun 2024 10:22:00 -0400 Subject: [PATCH 07/11] Fix test scope --- tests/Unit/DSC_HostsFile.Tests.ps1 | 93 +++++++++++++++--------------- 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/tests/Unit/DSC_HostsFile.Tests.ps1 b/tests/Unit/DSC_HostsFile.Tests.ps1 index 4915e2cd..b7ebdcd6 100644 --- a/tests/Unit/DSC_HostsFile.Tests.ps1 +++ b/tests/Unit/DSC_HostsFile.Tests.ps1 @@ -297,62 +297,61 @@ 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 - } + 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', - '' - ) - } + 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' + 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 - } + 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', - '' - ) - } + 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 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 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 + It 'Should update the entry in the set method' { + Set-TargetResource @testParams + Assert-MockCalled -CommandName Set-Content + } } } } #end InModuleScope $DSCResourceName From 963bd127d49b3e602c23d265e2851e6fb5dec55a Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 11 Jun 2024 10:51:59 -0400 Subject: [PATCH 08/11] Fix describe name --- tests/Integration/DSC_HostsFile.Integration.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 index 9d7ddb52..08e54dae 100644 --- a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 +++ b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 @@ -141,7 +141,7 @@ try } } - Describe "$($script:dscResourceName)_Integration - Remove Single Line" { + Describe "$($script:dscResourceName)_Integration - Update single line with leading space" { $configData = @{ AllNodes = @( @{ From caeef419cb4c73a1e313113fed0ed7a677494a2b Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 11 Jun 2024 10:52:13 -0400 Subject: [PATCH 09/11] Add more assertions --- tests/Integration/DSC_HostsFile.Integration.Tests.ps1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 index 08e54dae..6bbe86cc 100644 --- a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 +++ b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 @@ -178,6 +178,15 @@ try $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) $($configData.AllNodes[0].HostName)" + } + + It 'Should not fail subsiquent Test invocations' { + $result = Test-DscConfiguration -Path $TestDrive -Wait -Verbose + $result | Should -BeTrue + } } } } From 82cd36d7f9fe379ca3ae3a585cbd39c8a161e0a1 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 11 Jun 2024 11:03:29 -0400 Subject: [PATCH 10/11] Fixing tests --- tests/Integration/DSC_HostsFile.Integration.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 index 6bbe86cc..d10cda82 100644 --- a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 +++ b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 @@ -180,11 +180,11 @@ try } It 'Should include the extra whitespace' { - "${env:SystemRoot}\System32\Drivers\Etc\Hosts" | Should -FileContentMatch " $($configData.AllNodes[0].IPAddress) $($configData.AllNodes[0].HostName)" + "${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 -Wait -Verbose + $result = Test-DscConfiguration -Path $TestDrive $result | Should -BeTrue } } From 43ce5ae7d117ca473fb511dfeb380b4c18084003 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 11 Jun 2024 11:12:29 -0400 Subject: [PATCH 11/11] Correcting whitespace once more --- tests/Integration/DSC_HostsFile.Integration.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 index d10cda82..dd2dd12b 100644 --- a/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 +++ b/tests/Integration/DSC_HostsFile.Integration.Tests.ps1 @@ -180,7 +180,7 @@ try } It 'Should include the extra whitespace' { - "${env:SystemRoot}\System32\Drivers\Etc\Hosts" | Should -FileContentMatch " $($configData.AllNodes[0].IPAddress)`t$($configData.AllNodes[0].HostName)" + "${env:SystemRoot}\System32\Drivers\Etc\Hosts" | Should -FileContentMatch "$($configData.AllNodes[0].IPAddress)`t$($configData.AllNodes[0].HostName)" } It 'Should not fail subsiquent Test invocations' {