This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathMSFT_WindowsPackageCab.Tests.ps1
More file actions
147 lines (114 loc) · 7.95 KB
/
MSFT_WindowsPackageCab.Tests.ps1
File metadata and controls
147 lines (114 loc) · 7.95 KB
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
$errorActionPreference = 'Stop'
Set-StrictMode -Version 'Latest'
$script:testFolderPath = Split-Path -Path $PSScriptRoot -Parent
$script:testHelpersPath = Join-Path -Path $script:testFolderPath -ChildPath 'TestHelpers'
Import-Module -Name (Join-Path -Path $script:testHelpersPath -ChildPath 'CommonTestHelper.psm1')
$script:testEnvironment = Enter-DscResourceTestEnvironment `
-DscResourceModuleName 'PSDscResources' `
-DscResourceName 'MSFT_WindowsPackageCab' `
-TestType 'Unit'
try
{
InModuleScope 'MSFT_WindowsPackageCab' {
Describe 'WindowsPackageCab Unit Tests' {
BeforeAll {
Import-Module -Name 'Dism'
$script:testPackageName = 'TestPackage'
$script:testSourcePath = Join-Path -Path $TestDrive -ChildPath 'FakeCabFile.cab'
$script:testLogPath = Join-Path -Path $TestDrive -ChildPath 'WindowsPackageCabTestLog.log'
New-Item -Path $script:testSourcePath -ItemType 'File'
}
Context 'Get-TargetResource' {
Mock -CommandName 'Dism\Get-WindowsPackage' -MockWith { }
$getTargetResourceCommonParams = @{
SourcePath = $script:testSourcePath
Ensure = 'Present'
}
It 'Should return Ensure as Absent when package is not installed' {
$getTargetResourceResult = Get-TargetResource -Name $script:testPackageName @getTargetResourceCommonParams
$getTargetResourceResult.Ensure | Should Be 'Absent'
Assert-MockCalled -CommandName 'Dism\Get-WindowsPackage'
}
It 'Should return Ensure as Absent when package is on machine but not installed' {
Mock -CommandName 'Dism\Get-WindowsPackage' -MockWith { return @{ PackageState = 'NotPresent' } }
$getTargetResourceResult = Get-TargetResource -Name $script:testPackageName @getTargetResourceCommonParams
$getTargetResourceResult.Ensure | Should Be 'Absent'
Assert-MockCalled -CommandName 'Dism\Get-WindowsPackage'
}
It 'Should return Ensure as Present when package is installed' {
Mock -CommandName 'Dism\Get-WindowsPackage' -MockWith { return @{ PackageState = 'Installed' } }
$getTargetResourceResult = Get-TargetResource -Name $script:testPackageName @getTargetResourceCommonParams
$getTargetResourceResult.Ensure | Should Be 'Present'
Assert-MockCalled -CommandName 'Dism\Get-WindowsPackage'
}
It 'Should return Ensure as Present when package install is pending' {
Mock -CommandName 'Dism\Get-WindowsPackage' -MockWith { return @{ PackageState = 'InstallPending' } }
$getTargetResourceResult = Get-TargetResource -Name $script:testPackageName @getTargetResourceCommonParams
$getTargetResourceResult.Ensure | Should Be 'Present'
Assert-MockCalled -CommandName 'Dism\Get-WindowsPackage'
}
It 'Should pass specified log path to Get-WindowsPackage' {
$null = Get-TargetResource -Name $script:testPackageName -LogPath $script:testLogPath @getTargetResourceCommonParams
Assert-MockCalled -CommandName 'Dism\Get-WindowsPackage' -ParameterFilter { $LogPath -eq $script:testLogPath }
}
It 'Should return an empty log path when it was not specified' {
$getTargetResourceResult = Get-TargetResource -Name $script:testPackageName @getTargetResourceCommonParams
$getTargetResourceResult.LogPath | Should be ''
}
}
Context 'Set-TargetResource' {
Mock -CommandName 'Dism\Add-WindowsPackage' -MockWith { }
Mock -CommandName 'Dism\Remove-WindowsPackage' -MockWith { }
It 'Should throw when SourcePath is invalid' {
$invalidSourcePath = (Join-Path -Path $TestDrive -ChildPath 'DoesNotExist')
{ Set-TargetResource -Name 'Name' -SourcePath $invalidSourcePath -Ensure 'Absent' } |
Should Throw ($script:localizedData.SourcePathDoesNotExist -f $invalidSourcePath)
}
It 'Should call Add-WindowsPackage when Ensure is Present' {
Set-TargetResource -Name $script:testPackageName -SourcePath $script:testSourcePath -Ensure 'Present'
Assert-MockCalled -CommandName 'Dism\Add-WindowsPackage' -ParameterFilter { $null -eq $LogPath }
}
It 'Should call Remove-WindowsPackage when Ensure is Absent' {
Set-TargetResource -Name $script:testPackageName -SourcePath $script:testSourcePath -Ensure 'Absent'
Assert-MockCalled -CommandName 'Dism\Remove-WindowsPackage' -ParameterFilter { $null -eq $LogPath }
}
It 'Should pass specified log path to Add-WindowsPackage' {
Set-TargetResource -Name $script:testPackageName -SourcePath $script:testSourcePath -Ensure 'Present' -LogPath $script:testLogPath
Assert-MockCalled -CommandName 'Dism\Add-WindowsPackage' -ParameterFilter { $LogPath -eq $script:testLogPath }
}
It 'Should pass specified log path to Remove-WindowsPackage' {
Set-TargetResource -Name $script:testPackageName -SourcePath $script:testSourcePath -Ensure 'Absent' -LogPath $script:testLogPath
Assert-MockCalled -CommandName 'Dism\Remove-WindowsPackage' -ParameterFilter { $LogPath -eq $script:testLogPath }
}
}
Context 'Test-TargetResource' {
Mock -CommandName 'Get-TargetResource' -MockWith { return @{ Ensure = 'Absent' } }
It 'Should return true when Get-TargetResource returns Ensure Absent and Ensure is set to Absent' {
Test-TargetResource -Name $script:testPackageName -SourcePath $script:testSourcePath -Ensure 'Absent' | Should Be $true
Assert-MockCalled -CommandName 'Get-TargetResource'
}
It 'Should return false when Get-TargetResource returns Ensure Absent and Ensure is set to Present' {
Test-TargetResource -Name $script:testPackageName -SourcePath $script:testSourcePath -Ensure 'Present' | Should Be $false
Assert-MockCalled -CommandName 'Get-TargetResource'
}
Mock -CommandName 'Get-TargetResource' -MockWith { return @{ Ensure = 'Present' } }
It 'Should return true when Get-TargetResource returns Ensure Present and Ensure is set to Present' {
Test-TargetResource -Name $script:testPackageName -SourcePath $script:testSourcePath -Ensure 'Present' | Should Be $true
Assert-MockCalled -CommandName 'Get-TargetResource'
}
It 'Should return false when Get-TargetResource returns Ensure Present and Ensure is set to Absent' {
Test-TargetResource -Name $script:testPackageName -SourcePath $script:testSourcePath -Ensure 'Absent' | Should Be $false
Assert-MockCalled -CommandName 'Get-TargetResource'
}
It 'Should pass specified log path to Get-TargetResource' {
$null = Test-TargetResource -Name $script:testPackageName -SourcePath $script:testSourcePath -Ensure 'Absent' -LogPath $script:testLogPath
Assert-MockCalled -CommandName 'Get-TargetResource' -ParameterFilter { $LogPath -eq $script:testLogPath }
}
}
}
}
}
finally
{
Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
}