Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Improve speed of Test-IsNanoServer function #154

Merged
merged 10 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions DscResources/CommonResourceHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ function Test-IsNanoServer
[CmdletBinding()]
param ()

$isNanoServer = $false

if (Test-CommandExists -Name 'Get-ComputerInfo')
$serverLevelsRegKey = 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels'
if (Test-Path -Path $serverLevelsRegKey)
{
$computerInfo = Get-ComputerInfo -ErrorAction 'SilentlyContinue'

if ($null -ne $computerInfo)
$serverLevels = Get-ItemProperty -Path $serverLevelsRegKey
if ($serverLevels.NanoServer -eq 1)
{
$computerIsServer = 'Server' -ieq $computerInfo.OsProductType

if ($computerIsServer)
{
$isNanoServer = 'NanoServer' -ieq $computerInfo.OsServerLevel
}
$isNanoServer = $true
}
else
{
$isNanoServer = $false
}
}

else
{
$isNanoServer = $false
}

return $isNanoServer
}

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ The following parameters will be the same for each process in the set:
troubleshoot issues with the test HttpListener objects in the future.
Specifically fixes
[Issue #142](https://github.com/PowerShell/PSDscResources/issues/142)
* Improved speed of Test-IsNanoServer function

### 2.11.0.0

Expand Down
120 changes: 19 additions & 101 deletions Tests/Unit/CommonResourceHelper.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,136 +14,54 @@ Describe 'CommonResourceHelper Unit Tests' {
InModuleScope 'CommonResourceHelper' {
Describe 'Test-IsNanoServer' {
$testComputerInfoNanoServer = @{
OsProductType = 'Server'
OsServerLevel = 'NanoServer'
NanoServer = 1
}

$testComputerInfoServerNotNano = @{
OsProductType = 'Server'
OsServerLevel = 'NotNano'
}

$testComputerInfoNotServer = @{
OsProductType = 'NotServer'
OsServerLevel = 'NotNano'
}

Mock -CommandName 'Test-CommandExists' -MockWith { return $true }
Mock -CommandName 'Get-ComputerInfo' -MockWith { return $testComputerInfoNanoServer }

Context 'Get-ComputerInfo command exists and succeeds' {
Context 'Computer OS type is Server and OS server level is NanoServer' {
It 'Should not throw' {
{ $null = Test-IsNanoServer } | Should -Not -Throw
}

It 'Should test if the Get-ComputerInfo command exists' {
$testCommandExistsParameterFilter = {
return $Name -eq 'Get-ComputerInfo'
}

Assert-MockCalled -CommandName 'Test-CommandExists' -ParameterFilter $testCommandExistsParameterFilter -Exactly 1 -Scope 'Context'
}

It 'Should retrieve the computer info' {
Assert-MockCalled -CommandName 'Get-ComputerInfo' -Exactly 1 -Scope 'Context'
}

It 'Should return true' {
Test-IsNanoServer | Should -Be $true
}
Context 'Computer OS type is Server and OS server level is NanoServer' {
Mock -CommandName 'Test-Path' -MockWith { return $true }
Mock -CommandName 'Get-ItemProperty' -MockWith { return $testComputerInfoNanoServer }
It 'Should not throw' {
{ $null = Test-IsNanoServer } | Should -Not -Throw
}

Context 'Computer OS type is Server and OS server level is not NanoServer' {
Mock -CommandName 'Get-ComputerInfo' -MockWith { return $testComputerInfoServerNotNano }

It 'Should not throw' {
{ $null = Test-IsNanoServer } | Should -Not -Throw
}

It 'Should test if the Get-ComputerInfo command exists' {
$testCommandExistsParameterFilter = {
return $Name -eq 'Get-ComputerInfo'
}

Assert-MockCalled -CommandName 'Test-CommandExists' -ParameterFilter $testCommandExistsParameterFilter -Exactly 1 -Scope 'Context'
}

It 'Should retrieve the computer info' {
Assert-MockCalled -CommandName 'Get-ComputerInfo' -Exactly 1 -Scope 'Context'
}

It 'Should return false' {
Test-IsNanoServer | Should -Be $false
}
It 'Should check the ServerLevels registry path' {
Assert-MockCalled -CommandName 'Get-ItemProperty' -Exactly 1 -Scope 'Context'
}

Context 'Computer OS type is not Server' {
Mock -CommandName 'Get-ComputerInfo' -MockWith { return $testComputerInfoNotServer }

It 'Should not throw' {
{ $null = Test-IsNanoServer } | Should -Not -Throw
}

It 'Should test if the Get-ComputerInfo command exists' {
$testCommandExistsParameterFilter = {
return $Name -eq 'Get-ComputerInfo'
}

Assert-MockCalled -CommandName 'Test-CommandExists' -ParameterFilter $testCommandExistsParameterFilter -Exactly 1 -Scope 'Context'
}

It 'Should retrieve the computer info' {
Assert-MockCalled -CommandName 'Get-ComputerInfo' -Exactly 1 -Scope 'Context'
}

It 'Should return false' {
Test-IsNanoServer | Should -Be $false
}
It 'Should return true' {
Test-IsNanoServer | Should -Be $true
}
}

Context 'Get-ComputerInfo command exists but throws an error and returns null' {
Mock -CommandName 'Get-ComputerInfo' -MockWith { return $null }
Context 'Computer OS type is Server and OS server level is not NanoServer' {
Mock -CommandName 'Test-Path' -MockWith { return $true }
Mock -CommandName 'Get-ItemProperty' -MockWith { return $testComputerInfoServerNotNano }

It 'Should not throw' {
{ $null = Test-IsNanoServer } | Should -Not -Throw
}

It 'Should test if the Get-ComputerInfo command exists' {
$testCommandExistsParameterFilter = {
return $Name -eq 'Get-ComputerInfo'
}

Assert-MockCalled -CommandName 'Test-CommandExists' -ParameterFilter $testCommandExistsParameterFilter -Exactly 1 -Scope 'Context'
}

It 'Should retrieve the computer info' {
Assert-MockCalled -CommandName 'Get-ComputerInfo' -Exactly 1 -Scope 'Context'
It 'Should check the ServerLevels registry path' {
Assert-MockCalled -CommandName 'Get-ItemProperty' -Exactly 1 -Scope 'Context'
}

It 'Should return false' {
Test-IsNanoServer | Should -Be $false
}
}

Context 'Get-ComputerInfo command does not exist' {
Mock -CommandName 'Test-CommandExists' -MockWith { return $false }
Context 'Computer OS type is not Server' {
Mock -CommandName 'Test-Path' -MockWith { return $false }

It 'Should not throw' {
{ $null = Test-IsNanoServer } | Should -Not -Throw
}

It 'Should test if the Get-ComputerInfo command exists' {
$testCommandExistsParameterFilter = {
return $Name -eq 'Get-ComputerInfo'
}

Assert-MockCalled -CommandName 'Test-CommandExists' -ParameterFilter $testCommandExistsParameterFilter -Exactly 1 -Scope 'Context'
}

It 'Should not attempt to retrieve the computer info' {
Assert-MockCalled -CommandName 'Get-ComputerInfo' -Exactly 0 -Scope 'Context'
It 'Should check the ServerLevels registry path' {
Assert-MockCalled -CommandName 'Test-Path' -Exactly 1 -Scope 'Context'
}

It 'Should return false' {
Expand Down