. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Import-Module $functionPath -Force $moduleForMock = "" # Import the ServerManager module for function definitions necessary to mock. if (Test-IsWindowsServer) { Import-Module ServerManager | Out-Null } Describe "Install-ServerFeaturesAndRoles" { Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName -MockWith { return "Install-ServerFeaturesAndRoles.tests" } Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {} Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {} Mock -ModuleName $moduleForMock -CommandName Import-Module -MockWith {} Context "Results" { It "Imports ServerManager Module on Windows Server" { Mock -CommandName Get-RequiredServerFeaturesAndRoles -ModuleName $moduleForMock -MockWith { return $null } Mock -CommandName Test-IsWindowsServer -ModuleName $moduleForMock -MockWith { return $true } Install-ServerFeaturesAndRoles | Out-Null Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-RequiredServerFeaturesAndRoles -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-IsWindowsServer -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Import-Module -Scope It -Exactly 1 ` -ParameterFilter { $Name -match "ServerManager" } } It "Does Not Import ServerManager Module on Windows Client" { Mock -CommandName Get-RequiredServerFeaturesAndRoles -ModuleName $moduleForMock -MockWith { return $null } Mock -CommandName Test-IsWindowsServer -ModuleName $moduleForMock -MockWith { return $false } Install-ServerFeaturesAndRoles | Out-Null Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-RequiredServerFeaturesAndRoles -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-IsWindowsServer -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Import-Module -Scope It -Exactly 0 ` -ParameterFilter { $Name -match "ServerManager" } } It "Writes Warning and Skips if Feature Does Not Exist" { Mock -CommandName Test-IsWindowsServer -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-WindowsFeature -ModuleName $moduleForMock -MockWith { return $null } Mock -CommandName Install-WindowsFeature -ModuleName $moduleForMock -MockWith { return $null } Mock -CommandName Get-RequiredServerFeaturesAndRoles -ModuleName $moduleForMock -MockWith { return @( @{ Name = 'TestSet'; Features = @( 'TestFeature1', 'TestFeature2' ) } ) } Install-ServerFeaturesAndRoles | Should -BeTrue Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-RequiredServerFeaturesAndRoles -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-IsWindowsServer -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-WindowsFeature -Scope It -Exactly 0 Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-WindowsFeature -Scope It -Exactly 2 Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Scope It -Exactly 1 ` -ParameterFilter { $Message -match "Feature set TestSet contains feature TestFeature1 that does not exist" } Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Scope It -Exactly 1 ` -ParameterFilter { $Message -match "Feature set TestSet contains feature TestFeature2 that does not exist" } } It "Validates Existence of Each Feature in Feature Set" { Mock -CommandName Test-IsWindowsServer -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-WindowsFeature -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-RequiredServerFeaturesAndRoles -ModuleName $moduleForMock -MockWith { return @( @{ Name = 'TestSet' Features = @( 'TestFeature1', 'TestFeature2' ) } ) } Mock -CommandName Install-WindowsFeature -ModuleName $moduleForMock -MockWith { return @{ ExitCode = [Microsoft.Windows.ServerManager.Commands.FeatureOperationExitCode]::Success RestartNeeded = [Microsoft.Windows.ServerManager.Commands.RestartState]::No } } Install-ServerFeaturesAndRoles | Out-Null Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-RequiredServerFeaturesAndRoles -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-IsWindowsServer -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-WindowsFeature -Scope It -Exactly 2 } It "Writes Error and Aborts if Feature Set Installation Fails" { Mock -CommandName Test-IsWindowsServer -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-WindowsFeature -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-RequiredServerFeaturesAndRoles -ModuleName $moduleForMock -MockWith { return @( @{ Name = 'TestSet' Features = @( 'TestFeature1', 'TestFeature2' ) } ) } Mock -CommandName Install-WindowsFeature -ModuleName $moduleForMock -MockWith { return @{ ExitCode = [Microsoft.Windows.ServerManager.Commands.FeatureOperationExitCode]::ArgumentNotValid RestartNeeded = [Microsoft.Windows.ServerManager.Commands.RestartState]::No } } Install-ServerFeaturesAndRoles | Should -BeFalse Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-RequiredServerFeaturesAndRoles -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-IsWindowsServer -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-WindowsFeature -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-WindowsFeature -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Scope It -Exactly 1 ` -ParameterFilter { $Message -match "TestSet feature TestFeature1 failed to install" } } It "Installs All Features In A Set" { Mock -CommandName Test-IsWindowsServer -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-WindowsFeature -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-RequiredServerFeaturesAndRoles -ModuleName $moduleForMock -MockWith { return @( @{ Name = 'TestSet' Features = @( 'TestFeature1', 'TestFeature2' ) } ) } Mock -CommandName Install-WindowsFeature -ModuleName $moduleForMock -MockWith { return @{ ExitCode = [Microsoft.Windows.ServerManager.Commands.FeatureOperationExitCode]::Success RestartNeeded = [Microsoft.Windows.ServerManager.Commands.RestartState]::No } } Install-ServerFeaturesAndRoles | Out-Null Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-RequiredServerFeaturesAndRoles -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-IsWindowsServer -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-WindowsFeature -Scope It -Exactly 2 Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-WindowsFeature -Scope It -Exactly 2 Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Scope It -Exactly 0 } It "Installs All Features In All Sets" { Mock -CommandName Test-IsWindowsServer -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-WindowsFeature -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-RequiredServerFeaturesAndRoles -ModuleName $moduleForMock -MockWith { return @( @{ Name = 'TestSet1' Features = @( 'TestFeature1_1', 'TestFeature1_2' ) }, @{ Name = 'TestSet2' Features = @( 'TestFeature2_1', 'TestFeature2_2' ) } ) } Mock -CommandName Install-WindowsFeature -ModuleName $moduleForMock -MockWith { return @{ ExitCode = [Microsoft.Windows.ServerManager.Commands.FeatureOperationExitCode]::Success RestartNeeded = [Microsoft.Windows.ServerManager.Commands.RestartState]::No } } Install-ServerFeaturesAndRoles | Out-Null Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-RequiredServerFeaturesAndRoles -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-IsWindowsServer -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-WindowsFeature -Scope It -Exactly 4 Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-WindowsFeature -Scope It -Exactly 4 Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Scope It -Exactly 0 } It "Returns False if Feature Requires Reboot" { Mock -CommandName Test-IsWindowsServer -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-WindowsFeature -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-RequiredServerFeaturesAndRoles -ModuleName $moduleForMock -MockWith { return @( @{ Name = 'TestSet' Features = @( 'TestFeature1', 'TestFeature2' ) } ) } Mock -CommandName Install-WindowsFeature -ModuleName $moduleForMock -MockWith { return @{ ExitCode = [Microsoft.Windows.ServerManager.Commands.FeatureOperationExitCode]::SuccessRestartRequired RestartNeeded = [Microsoft.Windows.ServerManager.Commands.RestartState]::Yes } } Install-ServerFeaturesAndRoles | Should -BeFalse Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-RequiredServerFeaturesAndRoles -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-IsWindowsServer -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-WindowsFeature -Scope It -Exactly 2 Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-WindowsFeature -Scope It -Exactly 2 Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Scope It -Exactly 0 } It "Returns True if Feature Does Not Require Reboot" { Mock -CommandName Test-IsWindowsServer -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-WindowsFeature -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-RequiredServerFeaturesAndRoles -ModuleName $moduleForMock -MockWith { return @( @{ Name = 'TestSet' Features = @( 'TestFeature1', 'TestFeature2' ) } ) } Mock -CommandName Install-WindowsFeature -ModuleName $moduleForMock -MockWith { return @{ ExitCode = [Microsoft.Windows.ServerManager.Commands.FeatureOperationExitCode]::Success RestartNeeded = [Microsoft.Windows.ServerManager.Commands.RestartState]::No } } Install-ServerFeaturesAndRoles | Should -BeTrue Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-RequiredServerFeaturesAndRoles -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-IsWindowsServer -Scope It -Exactly 1 Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-WindowsFeature -Scope It -Exactly 2 Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-WindowsFeature -Scope It -Exactly 2 Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Scope It -Exactly 0 } } }