Tests/xPendingReboot.tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<#
.summary
    Test suite for MSFT_xPendingReboot.psm1
    For PR https://github.com/PowerShell/xPendingReboot/pull/1
#>

[CmdletBinding()]
param()


Import-Module $PSScriptRoot\..\DSCResources\MSFT_xPendingReboot\MSFT_xPendingReboot.psm1 -Force

$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest

Describe 'Get-TargetResource' {
    Context "All Reboots Are Required" {
        # Used by ComponentBasedServicing
        Mock Get-ChildItem {
            return @{ Name = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending' }
        } -ParameterFilter { $Path -eq 'hklm:SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

        # Used by WindowsUpdate
        Mock Get-ChildItem {
            return @{ Name = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired' } 
        } -ParameterFilter { $Path -eq 'hklm:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

        # Used by PendingFileRename
        Mock Get-ItemProperty {
            return @{ PendingFileRenameOperations= @("File1", "File2") }
        } -ParameterFilter { $Path -eq 'hklm:\SYSTEM\CurrentControlSet\Control\Session Manager\' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

         # Used by PendingComputerRename
        Mock Get-ItemProperty {
            return @{ ComputerName = "box2" }
        } -ParameterFilter { $Path -eq 'hklm:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

        Mock Get-ItemProperty {
            return @{ ComputerName = "box" }
        } -ParameterFilter { $Path -eq 'hklm:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

        Mock Invoke-WmiMethod {
            return New-Object PSObject -Property @{
                    ReturnValue = 0
                    IsHardRebootPending = $false
                    RebootPending = $true
                }
        } -ModuleName "MSFT_xPendingReboot" -Verifiable

        $value = Get-TargetResource -Name "Test"
        It "All mocks were called" {
            Assert-VerifiableMocks
        }
        
        It "Component Based Servicing should be true" {
            $value.ComponentBasedServicing | Should Be $true
        }

        It "WindowsUpdate should be true" {
            $value.ComponentBasedServicing | Should Be $true
        }
        It "Pending File Rename should be true" {
            $value.PendingFileRename | Should Be $true
        }
        It "Pending Computer Rename should be true" {
            $value.PendingComputerRename | Should Be $true
        }
        It "Ccm Client SDK should be true" {
            $value.CcmClientSDK | Should Be $true
        }
    }
    
    Context "No Reboots Are Required" {
        # Used by ComponentBasedServicing
        Mock Get-ChildItem {
            <# return nothing to catch issue #4 #>
        } -ParameterFilter { $Path -eq 'hklm:SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

        # Used by WindowsUpdate
        Mock Get-ChildItem {
            <# return nothing to catch issue #4 #>
        } -ParameterFilter { $Path -eq 'hklm:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

        # Used by PendingFileRename
        Mock Get-ItemProperty {
            return @{ PendingFileRenameOperations= @() }
        } -ParameterFilter { $Path -eq 'hklm:\SYSTEM\CurrentControlSet\Control\Session Manager\' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

         # Used by PendingComputerRename
        Mock Get-ItemProperty {
            return @{  }
        } -ParameterFilter { $Path -eq 'hklm:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

        Mock Get-ItemProperty {
            return @{  }
        } -ParameterFilter { $Path -eq 'hklm:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName' }  -ModuleName "MSFT_xPendingReboot" -Verifiable

        Mock Invoke-WmiMethod {
            return New-Object PSObject -Property @{
                    ReturnValue = 0
                    IsHardRebootPending = $false
                    RebootPending = $false
                }
        } -ModuleName "MSFT_xPendingReboot" -Verifiable

        $value = Get-TargetResource -Name "Test"
        
        It "Component Based Servicing should be false" {
            $value.ComponentBasedServicing | Should Be $false
        }

        It "WindowsUpdate should be false" {
            $value.ComponentBasedServicing | Should Be $false
        }
        It "Pending File Rename should be false" {
            $value.PendingFileRename | Should Be $false
        }
        It "Pending Computer Rename should be false" {
            $value.PendingComputerRename | Should Be $false
        }
        It "Ccm Client SDK should be false" {
            $value.CcmClientSDK | Should Be $false
        }
    }
    
    Context "SkipCcmClientSdk" {
        
        It "Calls 'Invoke-WmiMethod' when 'SkipCcmClientSdk' is not specified" {
            Mock Invoke-WmiMethod { } -ModuleName "MSFT_xPendingReboot"
            
            $value = Get-TargetResource -Name "Test" -SkipCcmClientSdk $false
            
            Assert-MockCalled Invoke-WmiMethod -Scope It -ModuleName "MSFT_xPendingReboot"
        }
        
        It "Does not call 'Invoke-WmiMethod' when 'SkipCcmClientSdk' is specified" {
            Mock Invoke-WmiMethod { } -ModuleName "MSFT_xPendingReboot"
            
            $value = Get-TargetResource -Name "Test" -SkipCcmClientSdk $true
            
            Assert-MockCalled Invoke-WmiMethod -Scope It -Exactly 0 -ModuleName "MSFT_xPendingReboot"
        }
         
    }
}

Describe 'Test-TargetResource' {
    Context "All Reboots Are Required" {
        # Used by ComponentBasedServicing
        Mock Get-TargetResource -ModuleName "MSFT_xPendingReboot" {
            return @{
            Name = $Name
            ComponentBasedServicing = $true
            WindowsUpdate = $true
            PendingFileRename = $true
            PendingComputerRename = $true
            CcmClientSDK = $true
            }
        }

        It "All Reboots are Skipped" {
            $result = Test-TargetResource -Name "Test" -SkipComponentBasedServicing $true -SkipWindowsUpdate $true -SkipPendingFileRename $true -SkipPendingComputerRename $true -SkipCcmClientSDK $true

            $result | Should Be $true
        }

        It "No Reboots are Skipped" {
            $result = Test-TargetResource -Name "Test" -SkipComponentBasedServicing $false -SkipWindowsUpdate $false -SkipPendingFileRename $false -SkipPendingComputerRename $false -SkipCcmClientSDK $false

            $result | Should Be $false
        }
    }
}