-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathComputerManagementDsc.Common.Tests.ps1
More file actions
132 lines (107 loc) · 4.88 KB
/
Copy pathComputerManagementDsc.Common.Tests.ps1
File metadata and controls
132 lines (107 loc) · 4.88 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
<#
.SYNOPSIS
Integration test for ComputerManagementDsc Common.
#>
# Suppressing this rule because Script Analyzer does not understand Pester's syntax.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
param ()
BeforeDiscovery {
try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null
}
# If the dependencies has not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
}
}
BeforeAll {
$script:dscModuleName = 'ComputerManagementDsc'
$script:subModuleName = 'ComputerManagementDsc.Common'
$script:parentModule = Get-Module -Name $script:dscModuleName -ListAvailable | Select-Object -First 1
$script:subModulesFolder = Join-Path -Path $script:parentModule.ModuleBase -ChildPath 'Modules'
$script:subModulePath = Join-Path -Path $script:subModulesFolder -ChildPath $script:subModuleName
Import-Module -Name $script:subModulePath -Force -ErrorAction 'Stop'
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:subModuleName
$PSDefaultParameterValues['Mock:ModuleName'] = $script:subModuleName
$PSDefaultParameterValues['Should:ModuleName'] = $script:subModuleName
}
AfterAll {
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
$PSDefaultParameterValues.Remove('Mock:ModuleName')
$PSDefaultParameterValues.Remove('Should:ModuleName')
# Unload the module being tested so that it doesn't impact any other tests.
Get-Module -Name $script:subModuleName -All | Remove-Module -Force
}
Describe 'ComputerManagementDsc.Common\Set-TimeZoneId' {
BeforeAll {
# Store the test machine timezone
$currentTimeZone = & tzutil.exe /g
# Change the current timezone so that a complete test occurs.
tzutil.exe /s 'Eastern Standard Time'
}
AfterAll {
# Restore the test machine timezone
& tzutil.exe /s $CurrentTimeZone
}
<#
The purpose of this test is to ensure the C# .NET code
that is used to set the time zone if the Set-TimeZone
cmdlet is not available but the Add-Type cmdlet is available
The other conditions can be effectively tested with
the unit tests, but the only way to test the C# .NET code
is to execute it without mocking. This results in
a destrutive change which is only allowed within the
integration tests.
#>
Context '''Set-TimeZone'' is not available but ''Add-Type'' is available' {
BeforeAll {
# Pester v6 throws instead of calling the real command when a mock has only
# -ParameterFilter behaviours and none match. Forward unmatched Get-Command
# calls (e.g. the 'Get-TimeZone' lookup in Get-TimeZoneId) to the real cmdlet
# to keep the v5 fall-through behaviour.
Mock -CommandName Get-Command -MockWith { & (Get-Command -Name 'Get-Command' -CommandType Cmdlet) @PesterBoundParameters }
Mock -CommandName Get-Command -ParameterFilter {
$Name -eq 'Add-Type'
} -MockWith { 'Add-Type' }
Mock -CommandName Get-Command -ParameterFilter {
$Name -eq 'Set-TimeZone'
}
Mock -CommandName 'TzUtil.exe' -MockWith {
$Script:LASTEXITCODE = 0
return 'OK'
}
}
It 'Should not throw an exception' {
InModuleScope -ScriptBlock {
Set-StrictMode -Version 1.0
{ Set-TimeZoneId -TimezoneId 'Eastern Standard Time' } | Should -Not -Throw
}
}
It 'Should have set the time zone to ''Eastern Standard Time''' {
InModuleScope -ScriptBlock {
Set-StrictMode -Version 1.0
Get-TimeZoneId | Should -Be 'Eastern Standard Time'
}
}
It 'Should call expected mocks' {
Should -Invoke -CommandName Get-Command -ParameterFilter {
$Name -eq 'Add-Type'
} -Exactly -Times 1 -Scope Context
Should -Invoke -CommandName Get-Command -ParameterFilter {
$Name -eq 'Set-TimeZone'
} -Exactly -Times 1 -Scope Context
Should -Invoke -CommandName TzUtil.exe -Exactly -Times 0 -Scope Context
}
}
}