Skip to content

Commit 8564c86

Browse files
committed
Added Integration Tests for Set-TimeZoneId
1 parent f43812f commit 8564c86

3 files changed

Lines changed: 286 additions & 191 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
$script:ModuleName = 'ComputerManagementDsc.Common'
2+
3+
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
4+
5+
#region HEADER
6+
# Unit Test Template Version: 1.1.0
7+
$script:moduleRoot = Join-Path -Path $(Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))) -ChildPath 'Modules\ComputerManagementDsc'
8+
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
9+
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
10+
{
11+
& git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
12+
}
13+
14+
Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
15+
Import-Module (Join-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'Modules' -ChildPath $script:ModuleName)) -ChildPath "$script:ModuleName.psm1") -Force
16+
#endregion HEADER
17+
18+
# Store the test machine timezone
19+
$currentTimeZone = & tzutil.exe /g
20+
21+
# Change the current timezone so that a complete test occurs.
22+
tzutil.exe /s 'Eastern Standard Time'
23+
24+
# Using try/finally to always cleanup even if something awful happens.
25+
try
26+
{
27+
InModuleScope $script:ModuleName {
28+
29+
Describe 'ComputerManagementDsc.Common\Set-TimeZoneId' {
30+
<#
31+
The purpose of this test is to ensure the C# .NET code
32+
that is used to set the time zone if the Set-TimeZone
33+
cmdlet is not available but the Add-Type cmdlet is available
34+
35+
The other conditions can be effectively tested with
36+
the unit tests, but the only way to test the C# .NET code
37+
is to execute it without mocking. This results in
38+
a destrutive change which is only allowed within the
39+
integration tests.
40+
#>
41+
Context '"Set-TimeZone" is not available but "Add-Type" is available' {
42+
Mock `
43+
-CommandName Get-Command `
44+
-ParameterFilter {
45+
$Name -eq 'Add-Type'
46+
} -MockWith {
47+
'Add-Type'
48+
}
49+
50+
Mock `
51+
-CommandName Get-Command `
52+
-ParameterFilter {
53+
$Name -eq 'Set-TimeZone'
54+
}
55+
56+
Mock -CommandName 'TzUtil.exe' -MockWith {
57+
$global:LASTEXITCODE = 0
58+
return 'OK'
59+
}
60+
61+
Mock -CommandName Add-Type
62+
Mock -CommandName Set-TimeZoneUsingDotNet
63+
64+
It 'Should not throw an exception' {
65+
{ Set-TimeZoneId -TimezoneId 'Eastern Standard Time' } | Should -Not -Throw
66+
}
67+
68+
It 'Should have set the time zone to Eastern Standard Time' {
69+
Get-TimeZoneId | Should -Be 'Eastern Standard Time'
70+
}
71+
72+
It 'Should call expected mocks' {
73+
Assert-MockCalled `
74+
-CommandName Get-Command `
75+
-ParameterFilter {
76+
$Name -eq 'Add-Type'
77+
} -Exactly -Times 1
78+
79+
Assert-MockCalled `
80+
-CommandName Get-Command `
81+
-ParameterFilter {
82+
$Name -eq 'Set-TimeZone'
83+
} -Exactly -Times 1
84+
85+
Assert-MockCalled -CommandName TzUtil.exe -Exactly -Times 0
86+
Assert-MockCalled -CommandName Add-Type -Exactly -Times 0
87+
Assert-MockCalled -CommandName Set-TimeZoneUsingDotNet -Exactly -Times 1
88+
}
89+
}
90+
}
91+
}
92+
}
93+
finally
94+
{
95+
# Restore the test machine timezone
96+
& tzutil.exe /s $CurrentTimeZone
97+
}

Tests/Integration/MSFT_TimeZone.Integration.Tests.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ try
3030
. $configFile -Verbose -ErrorAction Stop
3131

3232
Describe "$($script:DSCResourceName)_Integration" {
33-
#region DEFAULT TESTS
3433
$configData = @{
3534
AllNodes = @(
3635
@{
@@ -60,7 +59,6 @@ try
6059
It 'Should be able to call Get-DscConfiguration without throwing' {
6160
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
6261
}
63-
#endregion
6462

6563
It 'Should have set the configuration and all the parameters should match' {
6664
$current = Get-DscConfiguration | Where-Object -FilterScript {

0 commit comments

Comments
 (0)