From 1e364f07af856bcc7d91382cc753a493a3ab209a Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Sat, 6 May 2017 12:15:52 +1200 Subject: [PATCH 1/4] Fix Issue 209 --- CHANGELOG.md | 6 ++ .../MSFT_xNetAdapterName.psm1 | 55 ++++++++------ .../en-us/MSFT_xNetAdapterName.strings.psd1 | 10 +-- .../NetworkingDsc.Common.psm1 | 26 ++++--- .../en-us/NetworkingDsc.Common.strings.psd1 | 2 +- ...MSFT_xNetAdapterName.Integration.Tests.ps1 | 74 +++++++++++++++++-- ...s1 => MSFT_xNetAdapterName_all.config.ps1} | 2 +- .../MSFT_xNetAdapterName_nameonly.config.ps1 | 10 +++ Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 | 19 +++-- Tests/Unit/NetworkingDsc.Common.tests.ps1 | 52 ++++++++----- 10 files changed, 186 insertions(+), 70 deletions(-) rename Tests/Integration/{MSFT_xNetAdapterName.config.ps1 => MSFT_xNetAdapterName_all.config.ps1} (92%) create mode 100644 Tests/Integration/MSFT_xNetAdapterName_nameonly.config.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 40431917..376736dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,12 @@ - Created new resource for renaming network adapters. - Added Find-NetAdapter cmdlet to NetworkingDsc.Common. - Correct example parameters format to meet style guidelines. +- Find-NetworkAdapter: + - Fixed to return null if exception thrown. + - Allowed passing no selection parameters. +- MSFT_xNetAdapterName: + - Fixed bug in Get-TargetResource when Name is the only adapter selector parameter. + - Improved verbose logging. ## 3.2.0.0 diff --git a/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/MSFT_xNetAdapterName.psm1 b/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/MSFT_xNetAdapterName.psm1 index 46708f41..3975d5a0 100644 --- a/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/MSFT_xNetAdapterName.psm1 +++ b/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/MSFT_xNetAdapterName.psm1 @@ -50,6 +50,7 @@ $localizedData = Get-LocalizedData ` #> function Get-TargetResource { + [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( @@ -102,15 +103,20 @@ function Get-TargetResource ) Write-Verbose -Message ( @("$($MyInvocation.MyCommand): " - $($LocalizedData.GettingNetAdapterNameMessage) + $($LocalizedData.GettingNetAdapterNameMessage -f $NewName) ) -join '') - $null = $PSBoundParameters.Remove('Name') - $null = $PSBoundParameters.Remove('NewName') - $adapter = Find-NetworkAdapter ` - @PSBoundParameters ` - -ErrorAction Stop + -Name $NewName ` + -ErrorAction SilentlyContinue + + if (-not $adapter) + { + $PSBoundParameters.Remove('NewName') + $adapter = Find-NetworkAdapter ` + @PSBoundParameters ` + -ErrorAction Stop + } Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($LocalizedData.NetAdapterNameFoundMessage -f $adapter.Name) @@ -173,6 +179,7 @@ function Get-TargetResource #> function Set-TargetResource { + [CmdletBinding()] param ( [Parameter(Mandatory = $true)] @@ -224,7 +231,7 @@ function Set-TargetResource ) Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " - $($LocalizedData.SettingNetAdapterNameMessage) + $($LocalizedData.SettingNetAdapterNameMessage -f $NewName) ) -join '') $null = $PSBoundParameters.Remove('NewName') @@ -286,6 +293,7 @@ function Set-TargetResource #> function Test-TargetResource { + [CmdletBinding()] [OutputType([System.Boolean])] param ( @@ -338,32 +346,33 @@ function Test-TargetResource ) Write-Verbose -Message ( @("$($MyInvocation.MyCommand): " - $($LocalizedData.TestingNetAdapterNameMessage) + $($LocalizedData.TestingNetAdapterNameMessage -f $NewName) ) -join '') $null = $PSBoundParameters.Remove('NewName') - try - { - $adapter = Find-NetworkAdapter ` - @PSBoundParameters ` - -ErrorAction Stop - } - catch - { - $PSBoundParameters.Name = $NewName - $adapter = Find-NetworkAdapter ` - @PSBoundParameters ` - -ErrorAction Stop - } - if ($adapter.Name -eq $NewName) + + # Can an adapter be found with the new name? + $adapterWithNewName = Find-NetworkAdapter ` + -Name $NewName ` + -Verbose:$Verbose ` + -ErrorAction SilentlyContinue + + if ($adapterWithNewName) { + # An adapter was found matching the new name Write-Verbose -Message ( @("$($MyInvocation.MyCommand): " - $($LocalizedData.NetAdapterNameMatchMessage -f $adapter.Name) + $($LocalizedData.NetAdapterWithNewNameExistsMessage -f $adapterWithNewName.Name) ) -join '') return $true } else { + # Find an adapter matching the parameters - throw if none can be found + $adapter = Find-NetworkAdapter ` + @PSBoundParameters ` + -ErrorAction Stop + + # An adapter was found that needs to be changed to the new name Write-Verbose -Message ( @("$($MyInvocation.MyCommand): " $($LocalizedData.NetAdapterNameNotMatchMessage -f $adapter.Name,$NewName) ) -join '') diff --git a/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/en-us/MSFT_xNetAdapterName.strings.psd1 b/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/en-us/MSFT_xNetAdapterName.strings.psd1 index 864e2026..198e1628 100644 --- a/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/en-us/MSFT_xNetAdapterName.strings.psd1 +++ b/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/en-us/MSFT_xNetAdapterName.strings.psd1 @@ -1,10 +1,10 @@ ConvertFrom-StringData @' - GettingNetAdapterNameMessage = Getting the Network Adapter Name. + GettingNetAdapterNameMessage = Getting the Network Adapter Name '{0}'. NetAdapterNameFoundMessage = Network Adapter '{0}' found. - SettingNetAdapterNameMessage = Setting the Network Adapter Name. + SettingNetAdapterNameMessage = Setting the Network Adapter Name '{0}'. RenamingNetAdapterNameMessage = Renaming Network Adapter '{0}' to '{1}'. NetAdapterNameRenamedMessage = Network Adapter renamed to '{0}'. - TestingNetAdapterNameMessage = Testing the Network Adapter Name. - NetAdapterNameMatchMessage = Network Adapter Name '{0}' matches the adapter that was found. - NetAdapterNameNotMatchMessage = Network Adapter Name '{0}' does not match the adapter '{1}' that was found. + TestingNetAdapterNameMessage = Testing the Network Adapter Name '{0}'. + NetAdapterWithNewNameExistsMessage = A Network Adapter was found with the intended new name '{0}' of the Adapter. No rename required. + NetAdapterNameNotMatchMessage = Network Adapter Name '{0}' does not match the adapter '{1}' that was found. Rename required. '@ diff --git a/Modules/xNetworking/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 b/Modules/xNetworking/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 index 8d8b22e6..3ff4c56e 100644 --- a/Modules/xNetworking/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 +++ b/Modules/xNetworking/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 @@ -108,6 +108,7 @@ function Convert-CIDRToSubhetMask #> function Find-NetworkAdapter { + [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( @@ -200,21 +201,26 @@ function Find-NetworkAdapter if ($adapterFilters.Count -eq 0) { - New-InvalidOperationException ` - -Message ($LocalizedData.NetAdapterParameterError) - } - - # Join all the filters together - $adapterFilterScript = '(' + ($adapterFilters -join ' -and ') + ')' + Write-Verbose -Message ( @("$($MyInvocation.MyCommand): " + $($LocalizedData.AllNetAdaptersFoundMessage) + ) -join '') - $matchingAdapters = @(Get-NetAdapter | - Where-Object -FilterScript ([ScriptBlock]::Create($adapterFilterScript))) + $matchingAdapters = @(Get-NetAdapter) + } + else + { + # Join all the filters together + $adapterFilterScript = '(' + ($adapterFilters -join ' -and ') + ')' + $matchingAdapters = @(Get-NetAdapter | + Where-Object -FilterScript ([ScriptBlock]::Create($adapterFilterScript))) + } # Were any adapters found matching the criteria? if ($matchingAdapters.Count -eq 0) { New-InvalidOperationException ` -Message ($LocalizedData.NetAdapterNotFoundError) + return $null } else { @@ -232,6 +238,7 @@ function Find-NetworkAdapter New-InvalidOperationException ` -Message ($LocalizedData.InvalidNetAdapterNumberError ` -f $matchingAdapters.Count,$InterfaceNumber) + return $null } # if } else @@ -239,6 +246,7 @@ function Find-NetworkAdapter New-InvalidOperationException ` -Message ($LocalizedData.MultipleMatchingNetAdapterFound ` -f $matchingAdapters.Count) + return $null } # if } # if } # if @@ -257,7 +265,7 @@ function Find-NetworkAdapter MatchingAdapterCount = $matchingAdapters.Count } - $returnValue + return $returnValue } # Find-NetworkAdapter Export-ModuleMember -Function ` diff --git a/Modules/xNetworking/Modules/NetworkingDsc.Common/en-us/NetworkingDsc.Common.strings.psd1 b/Modules/xNetworking/Modules/NetworkingDsc.Common/en-us/NetworkingDsc.Common.strings.psd1 index 456e3afb..d354b743 100644 --- a/Modules/xNetworking/Modules/NetworkingDsc.Common/en-us/NetworkingDsc.Common.strings.psd1 +++ b/Modules/xNetworking/Modules/NetworkingDsc.Common/en-us/NetworkingDsc.Common.strings.psd1 @@ -1,7 +1,7 @@ ConvertFrom-StringData @' FindingNetAdapterMessage = Finding Network Adapters matching the parameters. + AllNetAdaptersFoundMessage = Found all Network Adapters because no filter parameters provided. NetAdapterFoundMessage = {0} Network Adapters were found matching the parameters. - NetAdapterParameterError = At least one Network Adapter parameter must be passed. NetAdapterNotFoundError = A Network Adapter matching the parameters was not found. Please correct the properties and try again. MultipleMatchingNetAdapterFound = Please adjust the parameters or specify IgnoreMultipleMatchingAdapters to only use the first and try again. InvalidNetAdapterNumberError = Network Adapter interface number {0} was specified but only {1} was found. Please correct the interface number and try again. diff --git a/Tests/Integration/MSFT_xNetAdapterName.Integration.Tests.ps1 b/Tests/Integration/MSFT_xNetAdapterName.Integration.Tests.ps1 index 171c49d9..2c5f8288 100644 --- a/Tests/Integration/MSFT_xNetAdapterName.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_xNetAdapterName.Integration.Tests.ps1 @@ -24,11 +24,11 @@ $TestEnvironment = Initialize-TestEnvironment ` # Using try/finally to always cleanup even if something awful happens. try { - #region Integration Tests - $ConfigFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName).config.ps1" - . $ConfigFile -Verbose -ErrorAction Stop + #region Integration Tests with all parameters + Describe "$($script:DSCResourceName)_Integration using all parameters" { + $ConfigFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName)_all.config.ps1" + . $ConfigFile -Verbose -ErrorAction Stop - Describe "$($script:DSCResourceName)_Integration" { BeforeAll { $adapterName = 'xNetworkingLBA' New-IntegrationLoopbackAdapter -AdapterName $adapterName @@ -56,7 +56,7 @@ try ) } - & "$($script:DSCResourceName)_Config" ` + & "$($script:DSCResourceName)_Config_All" ` -OutputPath $TestDrive ` -ConfigurationData $configData Start-DscConfiguration -Path $TestDrive ` @@ -65,8 +65,8 @@ try } it 'should reapply the MOF without throwing' { - {Start-DscConfiguration -Path $TestDrive ` - -ComputerName localhost -Wait -Verbose -Force} | Should Not Throw + { Start-DscConfiguration -Path $TestDrive ` + -ComputerName localhost -Wait -Verbose -Force } | Should Not Throw } It 'should be able to call Get-DscConfiguration without throwing' { @@ -75,12 +75,70 @@ try #endregion It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object {$_.ConfigurationName -eq "$($script:DSCResourceName)_Config"} + $current = Get-DscConfiguration | Where-Object {$_.ConfigurationName -eq "$($script:DSCResourceName)_Config_All"} $current.Name | Should Be $newAdapterName } AfterAll { # Remove Loopback Adapter + Remove-IntegrationLoopbackAdapter -AdapterName $adapterName + Remove-IntegrationLoopbackAdapter -AdapterName $newAdapterName + } + } + #endregion + + #region Integration Tests with name parameter only + Describe "$($script:DSCResourceName)_Integration using name parameter only" { + $ConfigFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName)_nameonly.config.ps1" + . $ConfigFile -Verbose -ErrorAction Stop + + BeforeAll { + $adapterName = 'xNetworkingLBA' + New-IntegrationLoopbackAdapter -AdapterName $adapterName + $adapter = Get-NetAdapter -Name $adapterName + $newAdapterName = 'xNetworkingLBANew' + } + + #region DEFAULT TESTS + It 'should compile and apply the MOF without throwing' { + { + # This is to pass to the Config + $configData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + NewName = $newAdapterName + Name = $adapter.Name + } + ) + } + + & "$($script:DSCResourceName)_Config_NameOnly" ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + Start-DscConfiguration -Path $TestDrive ` + -ComputerName localhost -Wait -Verbose -Force + } | Should Not Throw + } + + it 'should reapply the MOF without throwing' { + { 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 have set the resource and all the parameters should match' { + $current = Get-DscConfiguration | Where-Object {$_.ConfigurationName -eq "$($script:DSCResourceName)_Config_NameOnly"} + $current.Name | Should Be $newAdapterName + } + + AfterAll { + # Remove Loopback Adapter + Remove-IntegrationLoopbackAdapter -AdapterName $adapterName Remove-IntegrationLoopbackAdapter -AdapterName $newAdapterName } } diff --git a/Tests/Integration/MSFT_xNetAdapterName.config.ps1 b/Tests/Integration/MSFT_xNetAdapterName_all.config.ps1 similarity index 92% rename from Tests/Integration/MSFT_xNetAdapterName.config.ps1 rename to Tests/Integration/MSFT_xNetAdapterName_all.config.ps1 index 8685a762..68342d25 100644 --- a/Tests/Integration/MSFT_xNetAdapterName.config.ps1 +++ b/Tests/Integration/MSFT_xNetAdapterName_all.config.ps1 @@ -1,4 +1,4 @@ -configuration MSFT_xNetAdapterName_Config { +configuration MSFT_xNetAdapterName_Config_All { Import-DscResource -ModuleName xNetworking node localhost { diff --git a/Tests/Integration/MSFT_xNetAdapterName_nameonly.config.ps1 b/Tests/Integration/MSFT_xNetAdapterName_nameonly.config.ps1 new file mode 100644 index 00000000..b087b2f8 --- /dev/null +++ b/Tests/Integration/MSFT_xNetAdapterName_nameonly.config.ps1 @@ -0,0 +1,10 @@ +configuration MSFT_xNetAdapterName_Config_NameOnly { + Import-DscResource -ModuleName xNetworking + + node localhost { + xNetAdapterName Integration_Test { + Name = $Node.Name + NewName = $Node.NewName + } + } +} diff --git a/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 b/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 index 0e3a7ced..4b3fec2a 100644 --- a/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 +++ b/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 @@ -139,8 +139,11 @@ try } } - Context 'Matching adapter can be found and has wrong Name' { - Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockAdapter } + Context 'Renamed adapter does not exist, but matching adapter can be found and has wrong Name' { + Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockAdapter } ` + -ParameterFilter {$Name -and $Name -eq $script:AdapterName} + Mock -CommandName Find-NetworkAdapter -MockWith { } ` + -ParameterFilter {$Name -and $Name -eq $script:newAdapterName} It 'should not throw' { { $script:result = Test-TargetResource @adapterParameters -Verbose } | Should Not Throw @@ -151,13 +154,16 @@ try } It 'Should call all the mocks' { - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + -ParameterFilter {$Name -and $Name -eq $script:AdapterName} + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + -ParameterFilter {$Name -and $Name -eq $script:newAdapterName} } } Context 'Adapter name changed by Set-TargetResource' { - Mock -CommandName Find-NetworkAdapter -MockWith { $PSCmdlet.ThrowTerminatingError('Wrong Adapter') } -ParameterFilter {$Name -and $Name -eq $script:adapterName} - Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockRenamedAdapter } -ParameterFilter {$Name -and $Name -eq $script:newAdapterName} + Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockRenamedAdapter } ` + -ParameterFilter {$Name -and $Name -eq $script:newAdapterName} It 'should not throw' { { $script:result = Test-TargetResource @adapterParameters -Verbose } | Should Not Throw @@ -168,7 +174,8 @@ try } It 'Should call all the mocks' { - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 2 + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + -ParameterFilter {$Name -and $Name -eq $script:newAdapterName} } } } diff --git a/Tests/Unit/NetworkingDsc.Common.tests.ps1 b/Tests/Unit/NetworkingDsc.Common.tests.ps1 index d2f7efca..f0942d51 100644 --- a/Tests/Unit/NetworkingDsc.Common.tests.ps1 +++ b/Tests/Unit/NetworkingDsc.Common.tests.ps1 @@ -106,23 +106,6 @@ try $adapterArray = @( $nomatchAdapter, $matchAdapter ) $multipleMatchingAdapterArray = @( $matchAdapter, $matchAdapter ) - Context 'No parameters are passed' { - Mock ` - -CommandName Get-NetAdapter ` - -MockWith { $adapterArray } - - $errorRecord = Get-InvalidOperationRecord ` - -Message ($LocalizedData.NetAdapterParameterError) - - It 'should throw exception' { - { $script:result = Find-NetworkAdapter -Verbose } | Should Throw $errorRecord - } - - It 'should call expected mocks' { - Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 0 - } - } - Context 'Name is passed and one adapter matches' { Mock ` -CommandName Get-NetAdapter ` @@ -403,6 +386,41 @@ try } } + Context 'No parameters are passed and multiple Adapters adapters match but IgnoreMultipleMatchingAdapters is not set' { + Mock ` + -CommandName Get-NetAdapter ` + -MockWith { $adapterArray } + + $errorRecord = Get-InvalidOperationRecord ` + -Message ($LocalizedData.MultipleMatchingNetAdapterFound -f 2) + + It 'should throw exception' { + { $script:result = Find-NetworkAdapter -Verbose } | Should Throw $errorRecord + } + + It 'should call expected mocks' { + Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 + } + } + + Context 'No parameters are passed and multiple Adapters adapters match and IgnoreMultipleMatchingAdapters is set and interface number is 2' { + Mock ` + -CommandName Get-NetAdapter ` + -MockWith { $adapterArray } + + It 'should throw exception' { + { $script:result = Find-NetworkAdapter -IgnoreMultipleMatchingAdapters:$true -InterfaceNumber 2 -Verbose } | Should Not Throw + } + + It 'should return expected adapter' { + $script:result.Name | Should Be $adapterName + } + + It 'should call expected mocks' { + Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 + } + } + Context 'Multiple Adapters adapters match but IgnoreMultipleMatchingAdapters is not set' { Mock ` -CommandName Get-NetAdapter ` From 9802c784dd3b74b263c8599d82fced479c8c73bd Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Sat, 6 May 2017 13:03:39 +1200 Subject: [PATCH 2/4] Fix unit test coverage --- Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 | 37 +++++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 b/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 index 4b3fec2a..d1aa5f87 100644 --- a/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 +++ b/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 @@ -76,13 +76,41 @@ try } Describe "MSFT_xNetAdapterName\Get-TargetResource" { - Context 'Matching adapter can be found' { - Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockAdapter } + Context 'Renamed adapter can be found' { + Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockRenamedAdapter } ` + -ParameterFilter { $Name -eq $script:newAdapterName } It 'should not throw' { { $script:result = Get-TargetResource @adapterParameters -Verbose } | Should Not Throw } + It 'should return existing adapter' { + $script:result.Name | Should Be $script:mockRenamedAdapter.Name + $script:result.PhysicalMediaType | Should Be $script:mockRenamedAdapter.PhysicalMediaType + $script:result.Status | Should Be $script:mockRenamedAdapter.Status + $script:result.MacAddress | Should Be $script:mockRenamedAdapter.MacAddress + $script:result.InterfaceDescription | Should Be $script:mockRenamedAdapter.InterfaceDescription + $script:result.InterfaceIndex | Should Be $script:mockRenamedAdapter.InterfaceIndex + $script:result.InterfaceGuid | Should Be $script:mockRenamedAdapter.InterfaceGuid + $script:result.DriverDescription | Should Be $script:mockRenamedAdapter.DriverDescription + } + + It 'Should call all the mocks' { + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + -ParameterFilter { $Name -eq $script:newAdapterName } + } + } + + Context 'Renamed adapter not found but matching adapter can be found' { + Mock -CommandName Find-NetworkAdapter ` + -ParameterFilter { $Name -eq $script:newAdapterName } + Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockAdapter } ` + -ParameterFilter { $Name -eq $script:adapterName } + + It 'should not throw' { + { $script:result = Get-TargetResource -Name $script:adapterName -NewName $script:newAdapterName -Verbose } | Should Not Throw + } + It 'should return existing adapter' { $script:result.Name | Should Be $script:mockAdapter.Name $script:result.PhysicalMediaType | Should Be $script:mockAdapter.PhysicalMediaType @@ -95,7 +123,10 @@ try } It 'Should call all the mocks' { - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + -ParameterFilter { $Name -eq $script:adapterName } + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + -ParameterFilter { $Name -eq $script:newAdapterName } } } } From fd4024d185b6e8feb9025c0bee7a4ac6ad9784e8 Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Sat, 3 Jun 2017 11:53:23 +1200 Subject: [PATCH 3/4] Changes as per PR comments --- CHANGELOG.md | 3 + .../MSFT_xNetAdapterName.psm1 | 11 +- .../en-us/MSFT_xNetAdapterName.strings.psd1 | 17 +-- .../NetworkingDsc.Common.psm1 | 6 + ...MSFT_xNetAdapterName.Integration.Tests.ps1 | 10 +- Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 | 40 +++---- Tests/Unit/NetworkingDsc.Common.tests.ps1 | 104 +++++++++--------- 7 files changed, 106 insertions(+), 85 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e93dde00..6acbf8a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- MSFT_xNetAdapterName: + - More improvements to verbose logging. + ## 4.1.0.0 - Added integration test to test for conflicts with other common resource kit modules. diff --git a/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/MSFT_xNetAdapterName.psm1 b/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/MSFT_xNetAdapterName.psm1 index 2f29645b..3d207318 100644 --- a/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/MSFT_xNetAdapterName.psm1 +++ b/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/MSFT_xNetAdapterName.psm1 @@ -119,7 +119,12 @@ function Get-TargetResource if (-not $adapter) { - $PSBoundParameters.Remove('NewName') + Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " + $($LocalizedData.FindNetAdapterMessage) + ) -join '') + + $null = $PSBoundParameters.Remove('NewName') + $adapter = Find-NetworkAdapter ` @PSBoundParameters ` -ErrorAction Stop @@ -375,6 +380,10 @@ function Test-TargetResource else { # Find an adapter matching the parameters - throw if none can be found + Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " + $($LocalizedData.FindNetAdapterMessage) + ) -join '') + $adapter = Find-NetworkAdapter ` @PSBoundParameters ` -ErrorAction Stop diff --git a/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/en-us/MSFT_xNetAdapterName.strings.psd1 b/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/en-us/MSFT_xNetAdapterName.strings.psd1 index 198e1628..5c925d65 100644 --- a/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/en-us/MSFT_xNetAdapterName.strings.psd1 +++ b/Modules/xNetworking/DSCResources/MSFT_xNetAdapterName/en-us/MSFT_xNetAdapterName.strings.psd1 @@ -1,10 +1,11 @@ ConvertFrom-StringData @' - GettingNetAdapterNameMessage = Getting the Network Adapter Name '{0}'. - NetAdapterNameFoundMessage = Network Adapter '{0}' found. - SettingNetAdapterNameMessage = Setting the Network Adapter Name '{0}'. - RenamingNetAdapterNameMessage = Renaming Network Adapter '{0}' to '{1}'. - NetAdapterNameRenamedMessage = Network Adapter renamed to '{0}'. - TestingNetAdapterNameMessage = Testing the Network Adapter Name '{0}'. - NetAdapterWithNewNameExistsMessage = A Network Adapter was found with the intended new name '{0}' of the Adapter. No rename required. - NetAdapterNameNotMatchMessage = Network Adapter Name '{0}' does not match the adapter '{1}' that was found. Rename required. + GettingNetAdapterNameMessage = Getting the network adapter Name '{0}'. + FindNetAdapterMessage = Finding network adapter matching search criteria. + NetAdapterNameFoundMessage = network adapter '{0}' found. + SettingNetAdapterNameMessage = Setting the network adapter Name '{0}'. + RenamingNetAdapterNameMessage = Renaming network adapter '{0}' to '{1}'. + NetAdapterNameRenamedMessage = network adapter renamed to '{0}'. + TestingNetAdapterNameMessage = Testing the network adapter Name '{0}'. + NetAdapterWithNewNameExistsMessage = A network adapter was found with the intended new name '{0}' of the Adapter. No rename required. + NetAdapterNameNotMatchMessage = network adapter Name '{0}' does not match the adapter '{1}' that was found. Rename required. '@ diff --git a/Modules/xNetworking/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 b/Modules/xNetworking/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 index 3ff4c56e..aada8ed9 100644 --- a/Modules/xNetworking/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 +++ b/Modules/xNetworking/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 @@ -220,6 +220,8 @@ function Find-NetworkAdapter { New-InvalidOperationException ` -Message ($LocalizedData.NetAdapterNotFoundError) + + # Return a null so that ErrorAction SilentlyContinue works correctly return $null } else @@ -238,6 +240,8 @@ function Find-NetworkAdapter New-InvalidOperationException ` -Message ($LocalizedData.InvalidNetAdapterNumberError ` -f $matchingAdapters.Count,$InterfaceNumber) + + # Return a null so that ErrorAction SilentlyContinue works correctly return $null } # if } @@ -246,6 +250,8 @@ function Find-NetworkAdapter New-InvalidOperationException ` -Message ($LocalizedData.MultipleMatchingNetAdapterFound ` -f $matchingAdapters.Count) + + # Return a null so that ErrorAction SilentlyContinue works correctly return $null } # if } # if diff --git a/Tests/Integration/MSFT_xNetAdapterName.Integration.Tests.ps1 b/Tests/Integration/MSFT_xNetAdapterName.Integration.Tests.ps1 index 2c5f8288..649808d4 100644 --- a/Tests/Integration/MSFT_xNetAdapterName.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_xNetAdapterName.Integration.Tests.ps1 @@ -37,7 +37,7 @@ try } #region DEFAULT TESTS - It 'should compile and apply the MOF without throwing' { + It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ @@ -59,6 +59,7 @@ try & "$($script:DSCResourceName)_Config_All" ` -OutputPath $TestDrive ` -ConfigurationData $configData + Start-DscConfiguration -Path $TestDrive ` -ComputerName localhost -Wait -Verbose -Force } | Should Not Throw @@ -69,7 +70,7 @@ try -ComputerName localhost -Wait -Verbose -Force } | Should Not Throw } - It 'should be able to call Get-DscConfiguration without throwing' { + It 'Should be able to call Get-DscConfiguration without throwing' { { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should Not throw } #endregion @@ -100,7 +101,7 @@ try } #region DEFAULT TESTS - It 'should compile and apply the MOF without throwing' { + It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ @@ -116,6 +117,7 @@ try & "$($script:DSCResourceName)_Config_NameOnly" ` -OutputPath $TestDrive ` -ConfigurationData $configData + Start-DscConfiguration -Path $TestDrive ` -ComputerName localhost -Wait -Verbose -Force } | Should Not Throw @@ -126,7 +128,7 @@ try -ComputerName localhost -Wait -Verbose -Force } | Should Not Throw } - It 'should be able to call Get-DscConfiguration without throwing' { + It 'Should be able to call Get-DscConfiguration without throwing' { { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should Not throw } #endregion diff --git a/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 b/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 index d1aa5f87..e31906bb 100644 --- a/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 +++ b/Tests/Unit/MSFT_xNetAdapterName.Tests.ps1 @@ -80,11 +80,11 @@ try Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockRenamedAdapter } ` -ParameterFilter { $Name -eq $script:newAdapterName } - It 'should not throw' { + It 'Should not throw' { { $script:result = Get-TargetResource @adapterParameters -Verbose } | Should Not Throw } - It 'should return existing adapter' { + It 'Should return existing adapter' { $script:result.Name | Should Be $script:mockRenamedAdapter.Name $script:result.PhysicalMediaType | Should Be $script:mockRenamedAdapter.PhysicalMediaType $script:result.Status | Should Be $script:mockRenamedAdapter.Status @@ -96,7 +96,7 @@ try } It 'Should call all the mocks' { - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly -Times 1 ` -ParameterFilter { $Name -eq $script:newAdapterName } } } @@ -107,11 +107,11 @@ try Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockAdapter } ` -ParameterFilter { $Name -eq $script:adapterName } - It 'should not throw' { + It 'Should not throw' { { $script:result = Get-TargetResource -Name $script:adapterName -NewName $script:newAdapterName -Verbose } | Should Not Throw } - It 'should return existing adapter' { + It 'Should return existing adapter' { $script:result.Name | Should Be $script:mockAdapter.Name $script:result.PhysicalMediaType | Should Be $script:mockAdapter.PhysicalMediaType $script:result.Status | Should Be $script:mockAdapter.Status @@ -123,9 +123,9 @@ try } It 'Should call all the mocks' { - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly -Times 1 ` -ParameterFilter { $Name -eq $script:adapterName } - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly -Times 1 ` -ParameterFilter { $Name -eq $script:newAdapterName } } } @@ -139,16 +139,16 @@ try -ParameterFilter { $NewName -eq $script:newAdapterName } ` -MockWith { $script:mockRenamedAdapter } - It 'should not throw' { + It 'Should not throw' { { Set-TargetResource @adapterParameters -Verbose } | Should Not Throw } It 'Should call all the mocks' { - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly -Times 1 Assert-MockCalled ` -commandName Rename-NetAdapter ` -ParameterFilter { $NewName -eq $script:newAdapterName } ` - -Exactly 1 + -Exactly -Times 1 } } } @@ -157,16 +157,16 @@ try Context 'Matching adapter can be found and has correct Name' { Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockRenamedAdapter } - It 'should not throw' { + It 'Should not throw' { { $script:result = Test-TargetResource @adapterParameters -Verbose } | Should Not Throw } - It 'should return true' { + It 'Should return true' { $script:result | Should Be $true } It 'Should call all the mocks' { - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly -Times 1 } } @@ -176,18 +176,18 @@ try Mock -CommandName Find-NetworkAdapter -MockWith { } ` -ParameterFilter {$Name -and $Name -eq $script:newAdapterName} - It 'should not throw' { + It 'Should not throw' { { $script:result = Test-TargetResource @adapterParameters -Verbose } | Should Not Throw } - It 'should return false' { + It 'Should return false' { $script:result | Should Be $false } It 'Should call all the mocks' { - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly -Times 1 ` -ParameterFilter {$Name -and $Name -eq $script:AdapterName} - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly -Times 1 ` -ParameterFilter {$Name -and $Name -eq $script:newAdapterName} } } @@ -196,16 +196,16 @@ try Mock -CommandName Find-NetworkAdapter -MockWith { $script:mockRenamedAdapter } ` -ParameterFilter {$Name -and $Name -eq $script:newAdapterName} - It 'should not throw' { + It 'Should not throw' { { $script:result = Test-TargetResource @adapterParameters -Verbose } | Should Not Throw } - It 'should return false' { + It 'Should return false' { $script:result | Should Be $True } It 'Should call all the mocks' { - Assert-MockCalled -commandName Find-NetworkAdapter -Exactly 1 ` + Assert-MockCalled -commandName Find-NetworkAdapter -Exactly -Times 1 ` -ParameterFilter {$Name -and $Name -eq $script:newAdapterName} } } diff --git a/Tests/Unit/NetworkingDsc.Common.tests.ps1 b/Tests/Unit/NetworkingDsc.Common.tests.ps1 index f0942d51..aa4e8f65 100644 --- a/Tests/Unit/NetworkingDsc.Common.tests.ps1 +++ b/Tests/Unit/NetworkingDsc.Common.tests.ps1 @@ -111,15 +111,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $adapterArray } - It 'should not throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -Name $adapterName -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -132,11 +132,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.NetAdapterNotFoundError) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -Name 'NOMATCH' -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -146,15 +146,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $adapterArray } - It 'should not throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -PhysicalMediaType $adapterPhysicalMediaType -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -167,11 +167,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.NetAdapterNotFoundError) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -PhysicalMediaType 'NOMATCH' -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -181,15 +181,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $adapterArray } - It 'should not throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -Status $adapterStatus -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -202,11 +202,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.NetAdapterNotFoundError) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -Status 'Disabled' -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -216,15 +216,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $adapterArray } - It 'should not throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -MacAddress $adapterMacAddress -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -237,11 +237,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.NetAdapterNotFoundError) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -MacAddress '00-00-00-00-00-00' -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -251,15 +251,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $adapterArray } - It 'should not throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -InterfaceDescription $adapterInterfaceDescription -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -272,11 +272,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.NetAdapterNotFoundError) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -InterfaceDescription 'NOMATCH' -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -286,15 +286,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $adapterArray } - It 'should not throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -InterfaceIndex $adapterInterfaceIndex -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -307,11 +307,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.NetAdapterNotFoundError) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -InterfaceIndex 99 -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -321,15 +321,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $adapterArray } - It 'should not throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -InterfaceGuid $adapterInterfaceGuid -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -342,11 +342,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.NetAdapterNotFoundError) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -InterfaceGuid 'NOMATCH' -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -356,15 +356,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $adapterArray } - It 'should not throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -DriverDescription $adapterDriverDescription -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -377,11 +377,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.NetAdapterNotFoundError) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -DriverDescription 'NOMATCH' -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -394,11 +394,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.MultipleMatchingNetAdapterFound -f 2) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -408,15 +408,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $adapterArray } - It 'should throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -IgnoreMultipleMatchingAdapters:$true -InterfaceNumber 2 -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -429,11 +429,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.MultipleMatchingNetAdapterFound -f 2) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -PhysicalMediaType $adapterPhysicalMediaType -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -443,15 +443,15 @@ try -CommandName Get-NetAdapter ` -MockWith { $multipleMatchingAdapterArray } - It 'should not throw exception' { + It 'Should not throw exception' { { $script:result = Find-NetworkAdapter -PhysicalMediaType $adapterPhysicalMediaType -IgnoreMultipleMatchingAdapters:$true -Verbose } | Should Not Throw } - It 'should return expected adapter' { + It 'Should return expected adapter' { $script:result.Name | Should Be $adapterName } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } @@ -464,11 +464,11 @@ try $errorRecord = Get-InvalidOperationRecord ` -Message ($LocalizedData.InvalidNetAdapterNumberError -f 2,3) - It 'should throw exception' { + It 'Should throw the correct exception' { { $script:result = Find-NetworkAdapter -PhysicalMediaType $adapterPhysicalMediaType -IgnoreMultipleMatchingAdapters:$true -InterfaceNumber 3 -Verbose } | Should Throw $errorRecord } - It 'should call expected mocks' { + It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 } } From 544052ab6cdab73226a4716cb4b3f0522f47a9ef Mon Sep 17 00:00:00 2001 From: Daniel Scott-Raynsford Date: Sun, 4 Jun 2017 11:45:52 +1200 Subject: [PATCH 4/4] Fixes as per PR comments --- CHANGELOG.md | 11 +++++------ .../en-us/NetworkingDsc.Common.strings.psd1 | 10 +++++----- README.md | 1 + 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6acbf8a8..bc3992b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,12 @@ ## Unreleased +- Find-NetworkAdapter: + - Fixed to return null if exception thrown. + - Allowed passing no selection parameters. - MSFT_xNetAdapterName: + - Fixed bug in Get-TargetResource when Name is the only adapter selector parameter. + - Improved verbose logging. - More improvements to verbose logging. ## 4.1.0.0 @@ -25,12 +30,6 @@ - Created new resource for renaming network adapters. - Added Find-NetAdapter cmdlet to NetworkingDsc.Common. - Correct example parameters format to meet style guidelines. -- Find-NetworkAdapter: - - Fixed to return null if exception thrown. - - Allowed passing no selection parameters. -- MSFT_xNetAdapterName: - - Fixed bug in Get-TargetResource when Name is the only adapter selector parameter. - - Improved verbose logging. ## 3.2.0.0 diff --git a/Modules/xNetworking/Modules/NetworkingDsc.Common/en-us/NetworkingDsc.Common.strings.psd1 b/Modules/xNetworking/Modules/NetworkingDsc.Common/en-us/NetworkingDsc.Common.strings.psd1 index d354b743..3b79330b 100644 --- a/Modules/xNetworking/Modules/NetworkingDsc.Common/en-us/NetworkingDsc.Common.strings.psd1 +++ b/Modules/xNetworking/Modules/NetworkingDsc.Common/en-us/NetworkingDsc.Common.strings.psd1 @@ -1,8 +1,8 @@ ConvertFrom-StringData @' - FindingNetAdapterMessage = Finding Network Adapters matching the parameters. - AllNetAdaptersFoundMessage = Found all Network Adapters because no filter parameters provided. - NetAdapterFoundMessage = {0} Network Adapters were found matching the parameters. - NetAdapterNotFoundError = A Network Adapter matching the parameters was not found. Please correct the properties and try again. + FindingNetAdapterMessage = Finding network adapters matching the parameters. + AllNetAdaptersFoundMessage = Found all network adapters because no filter parameters provided. + NetAdapterFoundMessage = {0} network adapters were found matching the parameters. + NetAdapterNotFoundError = A network adapter matching the parameters was not found. Please correct the properties and try again. MultipleMatchingNetAdapterFound = Please adjust the parameters or specify IgnoreMultipleMatchingAdapters to only use the first and try again. - InvalidNetAdapterNumberError = Network Adapter interface number {0} was specified but only {1} was found. Please correct the interface number and try again. + InvalidNetAdapterNumberError = network adapter interface number {0} was specified but only {1} was found. Please correct the interface number and try again. '@ diff --git a/README.md b/README.md index 0f17bac5..613b1508 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ The **xNetworking** module contains the following resources: - **xNetAdapterRDMA**: Enable or disable RDMA on a network adapter. - **xNetAdapterLso**: Enable or disable Lso for different protocols on a network adapter. +- **xNetAdapterName**: Rename a network interface that matches specified search parameters. This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)