Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim the line before splitting parts in HostsFile #527

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion source/DSCResources/DSC_HostsFile/DSC_HostsFile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
48 changes: 48 additions & 0 deletions tests/Integration/DSC_HostsFile.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/DSC_HostsFile.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading