Tests/Unit/UpdateServicesClientDSC.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 |
$DSCResourceName = 'UpdateServicesClientDSC' $root = (Resolve-Path $PSScriptRoot\..\..).Path Import-Module (Join-Path $root "DSCResources\$DSCResourceName\$DSCResourceName.psm1") -Force InModuleScope $DSCResourceName { Describe "$DSCResourceName\Get-TargetResource" -Tag UnitTest { Mock Get-AutomaticUpdate Mock Get-UpdateOption Mock Get-UpdateServer Mock Get-UpdateTargetGroup Mock Get-UpdateScheduledInstallDay Mock Get-UpdateScheduledInstallHour Context 'Invoking Get-TargetResource' { It 'Should return Ensure = Present and is a hashtable' { $props = @{ Ensure = 'Present' AutomaticUpdateEnabled = $true } $Result = Get-TargetResource @props $Result.Ensure | Should Be $props.Ensure $Result -is [System.Collections.Hashtable] | Should Be $true } It 'Should call all the mocks' { Assert-MockCalled -CommandName Get-AutomaticUpdate -Exactly 1 Assert-MockCalled -CommandName Get-UpdateOption -Exactly 1 Assert-MockCalled -CommandName Get-UpdateServer -Exactly 1 Assert-MockCalled -CommandName Get-UpdateTargetGroup -Exactly 1 Assert-MockCalled -CommandName Get-UpdateScheduledInstallDay -Exactly 1 Assert-MockCalled -CommandName Get-UpdateScheduledInstallHour -Exactly 1 } } } Describe "$DSCResourceName\Set-TargetResource" -Tag UnitTest { Mock Get-TargetResource -MockWith { [PSCustomObject]@{ Ensure = 'Present' AutomaticUpdateEnabled = $true AutomaticUpdateOption = 'NotifyBeforeDownload' UpdateServer = 'https://wsus01.testdomain.local:8530' UpdateTargetGroup = 'test' } } Mock Set-AutomaticUpdate -MockWith { [PSCustomObject]@{ Enabled = $true } } Mock Remove-AutomaticUpdate Mock Set-UpdateOption -MockWith { [PSCustomObject]@{ Setting = 'AutoDownloadAndNotifyForInstall' } } Mock Remove-UpdateOption Mock Set-UpdateServer -MockWith { [PSCustomObject]@{ Enabled = $true URL = 'https://wsus01:8530' } } Mock Remove-UpdateServer Mock Set-UpdateTargetGroup -MockWith { [PSCustomObject]@{ Name = 'test' } } Mock Remove-UpdateTargetGroup Context 'invoking Set-TargetResource' { It 'Should return $null' { $props = @{ Ensure = 'Present' AutomaticUpdateEnabled = $true AutomaticUpdateOption = 'AutoDownloadAndNotifyForInstall' UpdateServer = 'https://wsus01:8530' UpdateTargetGroup = 'test' } { $Result = Set-TargetResource @props } | Should Not Throw $Result | Should BeNullOrEmpty } It 'Should call mock: Get-TargetResource' { Assert-MockCalled -CommandName Get-TargetResource -Exactly 1 } It 'Should call mock: Set-UpdateOption' { Assert-MockCalled -CommandName Set-UpdateOption -Exactly 1 } It 'Should call mock: Set-UpdateServer' { Assert-MockCalled -CommandName Set-UpdateServer -Exactly 1 } It 'Should not call all the other mocks' { Assert-MockCalled -CommandName Set-AutomaticUpdate -Exactly 0 Assert-MockCalled -CommandName Remove-AutomaticUpdate -Exactly 0 Assert-MockCalled -CommandName Remove-UpdateOption -Exactly 0 Assert-MockCalled -CommandName Remove-UpdateServer -Exactly 0 Assert-MockCalled -CommandName Set-UpdateTargetGroup -Exactly 0 Assert-MockCalled -CommandName Remove-UpdateTargetGroup -Exactly 0 } } } Describe "$DSCResourceName\Test-TargetResource" -Tag UnitTest { Mock Get-TargetResource -MockWith { [PSCustomObject]@{ Ensure = 'Present' AutomaticUpdateEnabled = $true AutomaticUpdateOption = 'NotifyBeforeDownload' UpdateServer = 'https://wsus01.testdomain.local:8530' UpdateTargetGroup = 'test' ScheduledInstallDay = 'Friday' ScheduledInstallHour = '1' } } Context 'invoking Test-TargetResource with same settings' { It 'Should return $true' { $testprops = @{ Ensure = 'Present' AutomaticUpdateEnabled = $true AutomaticUpdateOption = 'NotifyBeforeDownload' UpdateServer = 'https://wsus01.testdomain.local:8530' UpdateTargetGroup = 'test' ScheduledInstallDay = 'Friday' ScheduledInstallHour = '01' } $Result = Test-TargetResource @testprops $Result | Should Be $true } It 'Should call Get-TargetResource 1 time' { Assert-MockCalled -CommandName Get-TargetResource -Exactly 1 } } Context 'invoking Test-TargetResource with different UpdateServer' { It 'Should return $false' { $properties = @{ Ensure = 'Present' AutomaticUpdateEnabled = $true AutomaticUpdateOption = 'NotifyBeforeDownload' UpdateServer = 'https://wsus02:8530' UpdateTargetGroup = 'test' ScheduledInstallDay = 'Friday' ScheduledInstallHour = '01' } $Result = Test-TargetResource @properties $Result | Should Be $false } It 'Should call Get-TargetResource 1 time' { Assert-MockCalled -CommandName Get-TargetResource -Exactly 1 } } } } |