-
Notifications
You must be signed in to change notification settings - Fork 52
/
StorageDsc.VirtualHardDisk.Win32Helpers.Tests.ps1
404 lines (339 loc) · 14.5 KB
/
StorageDsc.VirtualHardDisk.Win32Helpers.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#region HEADER, boilerplate used from StorageDSC.Common.Tests
$script:projectPath = "$PSScriptRoot\..\.." | Convert-Path
$script:projectName = (Get-ChildItem -Path "$script:projectPath\*\*.psd1" | Where-Object -FilterScript {
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try
{
Test-ModuleManifest -Path $_.FullName -ErrorAction Stop
}
catch
{
$false
})
}).BaseName
$script:parentModule = Get-Module -Name $script:projectName -ListAvailable | Select-Object -First 1
$script:subModulesFolder = Join-Path -Path $script:parentModule.ModuleBase -ChildPath 'Modules'
Remove-Module -Name $script:parentModule -Force -ErrorAction 'SilentlyContinue'
$script:subModuleName = (Split-Path -Path $PSCommandPath -Leaf) -replace '\.Tests.ps1'
$script:subModuleFile = Join-Path -Path $script:subModulesFolder -ChildPath "$($script:subModuleName)/$($script:subModuleName).psm1"
Import-Module $script:subModuleFile -Force -ErrorAction Stop
#endregion HEADER
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1')
# Begin Testing
InModuleScope $script:subModuleName {
Function New-VirtualDiskUsingWin32
{
[CmdletBinding()]
[OutputType([System.Int32])]
Param
(
[Parameter(Mandatory = $true)]
[ref]
$VirtualStorageType,
[Parameter(Mandatory = $true)]
[System.String]
$VirtualDiskPath,
[Parameter(Mandatory = $true)]
[UInt32]
$AccessMask,
[Parameter(Mandatory = $true)]
[System.IntPtr]
$SecurityDescriptor,
[Parameter(Mandatory = $true)]
[UInt32]
$Flags,
[Parameter(Mandatory = $true)]
[System.UInt32]
$ProviderSpecificFlags,
[Parameter(Mandatory = $true)]
[ref]
$CreateVirtualDiskParameters,
[Parameter(Mandatory = $true)]
[System.IntPtr]
$Overlapped,
[Parameter(Mandatory = $true)]
[ref]
$Handle
)
}
Function Add-VirtualDiskUsingWin32
{
[CmdletBinding()]
[OutputType([System.Int32])]
Param
(
[Parameter(Mandatory = $true)]
[ref]
$Handle,
[Parameter(Mandatory = $true)]
[System.IntPtr]
$SecurityDescriptor,
[Parameter(Mandatory = $true)]
[System.UInt32]
$Flags,
[Parameter(Mandatory = $true)]
[System.Int32]
$ProviderSpecificFlags,
[Parameter(Mandatory = $true)]
[ref]
$AttachVirtualDiskParameters,
[Parameter(Mandatory = $true)]
[System.IntPtr]
$Overlapped
)
}
Function Get-VirtualDiskUsingWin32
{
[CmdletBinding()]
[OutputType([System.Int32])]
Param
(
[Parameter(Mandatory = $true)]
[ref]
$VirtualStorageType,
[Parameter(Mandatory = $true)]
[System.String]
$VirtualDiskPath,
[Parameter(Mandatory = $true)]
[System.UInt32]
$AccessMask,
[Parameter(Mandatory = $true)]
[System.UInt32]
$Flags,
[Parameter(Mandatory = $true)]
[ref]
$OpenVirtualDiskParameters,
[Parameter(Mandatory = $true)]
[ref]
$Handle
)
}
$script:DiskImageGoodVhdxPath = 'C:\test.vhdx'
$script:AccessDeniedWin32Error = 5
$script:vhdDiskFormat = 'vhd'
[ref]$script:TestHandle = [Microsoft.Win32.SafeHandles.SafeFileHandle]::Zero
$script:mockedParams = [pscustomobject] @{
DiskSizeInBytes = 65Gb
VirtualDiskPath = $script:DiskImageGoodVhdxPath
DiskType = 'dynamic'
DiskFormat = 'vhdx'
}
$script:mockedVhdParams = [pscustomobject] @{
DiskSizeInBytes = 65Gb
VirtualDiskPath = $script:DiskImageGoodVhdxPath
DiskType = 'dynamic'
DiskFormat = 'vhd'
}
Describe 'StorageDsc.VirtualHardDisk.Win32Helpers\New-SimpleVirtualDisk' -Tag 'New-SimpleVirtualDisk' {
Context 'Creating and attaching a new virtual disk (vhdx) successfully' {
Mock `
-CommandName New-VirtualDiskUsingWin32 `
-MockWith { 0 } `
-Verifiable
Mock `
-CommandName Add-VirtualDiskUsingWin32 `
-MockWith { 0 } `
-Verifiable
It 'Should not throw an exception' {
{
New-SimpleVirtualDisk `
-VirtualDiskPath $script:mockedParams.VirtualDiskPath `
-DiskSizeInBytes $script:mockedParams.DiskSizeInBytes `
-DiskFormat $script:mockedParams.DiskFormat `
-DiskType $script:mockedParams.DiskType`
-Verbose
} | Should -Not -Throw
}
It 'Should only call required mocks' {
Assert-VerifiableMock
Assert-MockCalled -CommandName New-VirtualDiskUsingWin32 -Exactly 1
Assert-MockCalled -CommandName Add-VirtualDiskUsingWin32 -Exactly 1
}
}
Context 'Creating and attaching a new virtual disk (vhd) successfully' {
Mock `
-CommandName New-VirtualDiskUsingWin32 `
-MockWith { 0 } `
-Verifiable
Mock `
-CommandName Add-VirtualDiskUsingWin32 `
-MockWith { 0 } `
-Verifiable
It 'Should not throw an exception' {
{
New-SimpleVirtualDisk `
-VirtualDiskPath $script:mockedVhdParams.VirtualDiskPath `
-DiskSizeInBytes $script:mockedVhdParams.DiskSizeInBytes `
-DiskFormat $script:mockedVhdParams.DiskFormat `
-DiskType $script:mockedVhdParams.DiskType`
-Verbose
} | Should -Not -Throw
}
It 'Should only call required mocks' {
Assert-VerifiableMock
Assert-MockCalled -CommandName New-VirtualDiskUsingWin32 -Exactly 1
Assert-MockCalled -CommandName Add-VirtualDiskUsingWin32 -Exactly 1
}
}
Context 'Creating a new virtual disk failed due to exception' {
Mock `
-CommandName New-VirtualDiskUsingWin32 `
-MockWith { $script:AccessDeniedWin32Error } `
-Verifiable
$win32Error = [System.ComponentModel.Win32Exception]::new($script:AccessDeniedWin32Error)
$exception = [System.Exception]::new( `
($script:localizedData.CreateVirtualDiskError -f $script:mockedParams.VirtualDiskPath, $win32Error.Message), `
$win32Error)
It 'Should throw an exception in creation method' {
{
New-SimpleVirtualDisk `
-VirtualDiskPath $script:mockedParams.VirtualDiskPath `
-DiskSizeInBytes $script:mockedParams.DiskSizeInBytes `
-DiskFormat $script:mockedParams.DiskFormat `
-DiskType $script:mockedParams.DiskType`
-Verbose
} | Should -Throw -ExpectedMessage $exception.Message
}
It 'Should only call required mocks' {
Assert-VerifiableMock
Assert-MockCalled -CommandName New-VirtualDiskUsingWin32 -Exactly 1
}
}
}
Describe 'StorageDsc.VirtualHardDisk.Win32Helpers\Add-SimpleVirtualDisk' -Tag 'Add-SimpleVirtualDisk' {
Context 'Attaching a virtual disk failed due to exception' {
Mock `
-CommandName Get-VirtualDiskHandle `
-MockWith { $script:TestHandle } `
-Verifiable
Mock `
-CommandName Add-VirtualDiskUsingWin32 `
-MockWith { $script:AccessDeniedWin32Error } `
-Verifiable
$win32Error = [System.ComponentModel.Win32Exception]::new($script:AccessDeniedWin32Error)
$exception = [System.Exception]::new( `
($script:localizedData.AttachVirtualDiskError -f $script:mockedParams.VirtualDiskPath, $win32Error.Message), `
$win32Error)
It 'Should throw an exception during attach function' {
{
Add-SimpleVirtualDisk `
-VirtualDiskPath $script:mockedParams.VirtualDiskPath `
-DiskFormat $script:mockedParams.DiskFormat `
-Verbose
} | Should -Throw -ExpectedMessage $exception.Message
}
It 'Should only call required mocks' {
Assert-VerifiableMock
Assert-MockCalled -CommandName Add-VirtualDiskUsingWin32 -Exactly 2
Assert-MockCalled -CommandName Get-VirtualDiskHandle -Exactly 1
}
}
Context 'Attaching a virtual disk successfully' {
Mock `
-CommandName Add-VirtualDiskUsingWin32 `
-MockWith { 0 } `
-Verifiable
Mock `
-CommandName Get-VirtualDiskHandle `
-MockWith { $script:TestHandle } `
-Verifiable
It 'Should not throw an exception' {
{
Add-SimpleVirtualDisk `
-VirtualDiskPath $script:mockedParams.VirtualDiskPath `
-DiskFormat $script:mockedParams.DiskFormat `
-Verbose
} | Should -Not -Throw
}
It 'Should only call required mocks' {
Assert-VerifiableMock
Assert-MockCalled -CommandName Get-VirtualDiskHandle -Exactly 1
Assert-MockCalled -CommandName Add-VirtualDiskUsingWin32 -Exactly 1
}
}
}
Describe 'StorageDsc.VirtualHardDisk.Win32Helpers\Get-VirtualDiskHandle' -Tag 'Get-VirtualDiskHandle' {
Context 'Opening a virtual disk file failed due to exception' {
Mock `
-CommandName Get-VirtualDiskUsingWin32 `
-MockWith { $script:AccessDeniedWin32Error } `
-Verifiable
$win32Error = [System.ComponentModel.Win32Exception]::new($script:AccessDeniedWin32Error)
$exception = [System.Exception]::new( `
($script:localizedData.OpenVirtualDiskError -f $script:mockedParams.VirtualDiskPath, $win32Error.Message), `
$win32Error)
It 'Should throw an exception while attempting to open virtual disk file' {
{
Get-VirtualDiskHandle `
-VirtualDiskPath $script:mockedParams.VirtualDiskPath `
-DiskFormat $script:mockedParams.DiskFormat `
-Verbose
} | Should -Throw -ExpectedMessage $exception.Message
}
It 'Should only call required mocks' {
Assert-VerifiableMock
Assert-MockCalled -CommandName Get-VirtualDiskUsingWin32 -Exactly 1
}
}
Context 'Opening a virtual disk file successfully' {
Mock `
-CommandName Get-VirtualDiskUsingWin32 `
-MockWith { 0 } `
-Verifiable
It 'Should not throw an exception' {
{
Get-VirtualDiskHandle `
-VirtualDiskPath $script:mockedParams.VirtualDiskPath `
-DiskFormat $script:mockedParams.DiskFormat `
-Verbose
} | Should -Not -Throw
}
It 'Should only call required mocks' {
Assert-VerifiableMock
Assert-MockCalled -CommandName Get-VirtualDiskUsingWin32 -Exactly 1
}
}
}
Describe 'StorageDsc.VirtualHardDisk.Win32Helpers\Get-VirtualStorageType' -Tag 'Get-VirtualStorageType' {
Context 'Storage type requested for vhd disk format' {
$result = Get-VirtualStorageType -DiskFormat $script:vhdDiskFormat
It 'Should not throw an exception' {
{
Get-VirtualStorageType `
-DiskFormat $script:vhdDiskFormat `
-Verbose
} | Should -Not -Throw
}
Get-VirtDiskWin32HelperScript
$virtualStorageType = New-Object -TypeName VirtDisk.Helper+VIRTUAL_STORAGE_TYPE
$virtualStorageType.VendorId = [VirtDisk.Helper]::VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
$virtualStorageType.DeviceId = [VirtDisk.Helper]::VIRTUAL_STORAGE_TYPE_DEVICE_VHD
It "Should return vendorId $($virtualStorageType.VendorId)" {
$result.VendorId | Should -Be $virtualStorageType.VendorId
}
It "Should return DeviceId $($virtualStorageType.DeviceId)" {
$result.DeviceId | Should -Be $virtualStorageType.DeviceId
}
}
Context 'Storage type requested for vhdx disk format' {
$result = Get-VirtualStorageType -DiskFormat $script:mockedParams.DiskFormat
It 'Should not throw an exception' {
{
Get-VirtualStorageType `
-DiskFormat $script:mockedParams.DiskFormat `
-Verbose
} | Should -Not -Throw
}
Get-VirtDiskWin32HelperScript
$virtualStorageType = New-Object -TypeName VirtDisk.Helper+VIRTUAL_STORAGE_TYPE
$virtualStorageType.VendorId = [VirtDisk.Helper]::VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT
$virtualStorageType.DeviceId = [VirtDisk.Helper]::VIRTUAL_STORAGE_TYPE_DEVICE_VHDX
It "Should return vendorId $($virtualStorageType.VendorId)" {
$result.VendorId | Should -Be $virtualStorageType.VendorId
}
It "Should return DeviceId $($virtualStorageType.DeviceId)" {
$result.DeviceId | Should -Be $virtualStorageType.DeviceId
}
}
}
}