forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeZone.Tests.ps1
More file actions
246 lines (213 loc) · 9.37 KB
/
Copy pathTimeZone.Tests.ps1
File metadata and controls
246 lines (213 loc) · 9.37 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
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
# This is a Pester test suite to validate the cmdlets in the TimeZone module
#
# Copyright (c) Microsoft Corporation, 2016
<#
--------------------------------------
Script Notes: opportunities for improving this test script
--------------------------------------
Localization
Many of the tests below looking-up timezones by Name do not support localization.
That is, the current tests use us english versions of StandardName and DaylightName for tests.
ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms725481.aspx
[snippet] Both StandardName and DaylightName are localized according to the current user default UI language.
#>
function Assert-ListsSame
{
param([object[]] $expected, [object[]] $observed )
$compResult = Compare-Object $observed $expected | Select-Object -ExpandProperty InputObject
if ($compResult)
{
$observedList = ([string]::Join("|",$observed))
$expectedList = ([string]::Join("|",$expected))
$observedList | Should Be $expectedList
}
}
Describe "Get-Timezone test cases" -Tags "CI" {
BeforeAll {
$TimeZonesAvailable = [System.TimeZoneInfo]::GetSystemTimeZones()
$defaultParamValues = $PSdefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = ($TimeZonesAvailable.Count -eq 0)
}
AfterAll {
$global:PSDefaultParameterValues = $defaultParamValues
}
It "Call without ListAvailable switch returns current TimeZoneInfo" {
$observed = (Get-TimeZone).Id
$expected = ([System.TimeZoneInfo]::Local).Id
$observed | Should Be $expected
}
It "Call without ListAvailable switch returns an object of type TimeZoneInfo" {
$result = Get-TimeZone
$result | Should BeOfType TimeZoneInfo
}
It "Call WITH ListAvailable switch returns ArrayList of TimeZoneInfo objects where the list is greater than 0 item" {
$list = Get-TimeZone -ListAvailable
$list.Count | Should BeGreaterThan 0
,$list | Should BeOfType "Object[]"
$list[0] | Should BeOfType "TimeZoneInfo"
}
## The local time zone could be set to UTC or GMT*. In this case, the .NET API returns the region ID
## and not 'UTC'. To avoid a string matching error, we compare the BaseUtcOffset instead.
It "Call with ListAvailable switch returns a list containing TimeZoneInfo.Local" {
$observedIdList = Get-TimeZone -ListAvailable | Select-Object -ExpandProperty BaseUtcOffset
$oneExpectedOffset = ([System.TimeZoneInfo]::Local).BaseUtcOffset
$observedIdList -eq $oneExpectedOffset | Should Be $oneExpectedOffset
}
## The local time zone could be set to UTC or GMT*. In this case, the .NET API returns the region ID
## and not UTC. To avoid a string matching error, we compare the BaseUtcOffset instead.
It "Call with ListAvailable switch returns a list containing one returned by Get-TimeZone" {
$observedIdList = Get-TimeZone -ListAvailable | Select-Object -ExpandProperty BaseUtcOffset
$oneExpectedOffset = (Get-TimeZone).BaseUtcOffset
$observedIdList -eq $oneExpectedOffset | Should Be $oneExpectedOffset
}
It "Call Get-TimeZone using ID param and single item" {
$selectedTZ = $TimeZonesAvailable[0]
(Get-TimeZone -Id $selectedTZ.Id).Id | Should Be $selectedTZ.Id
}
It "Call Get-TimeZone using ID param and multiple items" {
$selectedTZ = $TimeZonesAvailable | Select-Object -First 3 -ExpandProperty Id
$result = (Get-TimeZone -Id $selectedTZ).Id
Assert-ListsSame $result $selectedTZ
}
It "Call Get-TimeZone using ID param and multiple items, where first and third are invalid ids - expect error" {
$selectedTZ = $TimeZonesAvailable[0].Id
$null = Get-TimeZone -Id @("Cape Verde Standard",$selectedTZ,"Azores Standard") `
-ErrorVariable errVar -ErrorAction SilentlyContinue
$errVar.Count | Should Be 2
$errVar[0].FullyQualifiedErrorID | Should Be "TimeZoneNotFound,Microsoft.PowerShell.Commands.GetTimeZoneCommand"
}
It "Call Get-TimeZone using ID param and multiple items, one is wild card but error action ignore works as expected" {
$selectedTZ = $TimeZonesAvailable | Select-Object -First 3 -ExpandProperty Id
$inputArray = $selectedTZ + "*"
$result = Get-TimeZone -Id $inputArray -ErrorAction SilentlyContinue | ForEach-Object Id
Assert-ListsSame $selectedTZ $result
}
It "Call Get-TimeZone using Name param and singe item" {
$timezoneList = Get-TimeZone -ListAvailable
$timezoneName = $timezoneList[0].StandardName
$observed = Get-TimeZone -Name $timezoneName
$observed.StandardName | Should Be $timezoneName
}
It "Call Get-TimeZone using Name param with wild card" {
$result = (Get-TimeZone -Name "Pacific*").Id
$expectedIdList = ($TimeZonesAvailable | Where-Object { $_.StandardName -match "^Pacific" }).Id
Assert-ListsSame $expectedIdList $result
}
It "Call Get-TimeZone Name parameter from pipeline by value " {
$result = ("Pacific*" | Get-TimeZone).Id
$expectedIdList = ($TimeZonesAvailable | Where-Object { $_.StandardName -match "^Pacific" }).Id
Assert-ListsSame $expectedIdList $result
}
It "Call Get-TimeZone Id parameter from pipeline by ByPropertyName" {
$timezoneList = Get-TimeZone -ListAvailable
$timezone = $timezoneList[0]
$observed = $timezone | Get-TimeZone
$observed.StandardName | Should Be $timezone.StandardName
}
}
try {
$defaultParamValues = $PSdefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = !$IsWindows
Describe "Set-Timezone test case: call by single Id" -Tags @('CI', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsWindows) {
$originalTimeZoneId = (Get-TimeZone).Id
}
}
AfterAll {
if ($IsWindows) {
Set-TimeZone -ID $originalTimeZoneId
}
}
It "Call Set-TimeZone by Id" {
$origTimeZoneID = (Get-TimeZone).Id
$timezoneList = Get-TimeZone -ListAvailable
$testTimezone = $null
foreach ($timezone in $timezoneList) {
if ($timezone.Id -ne $origTimeZoneID) {
$testTimezone = $timezone
break
}
}
Set-TimeZone -Id $testTimezone.Id
$observed = Get-TimeZone
$testTimezone.Id | Should Be $observed.Id
}
}
Describe "Set-Timezone test cases" -Tags @('Feature', 'RequireAdminOnWindows') {
BeforeAll {
if ($IsWindows)
{
$originalTimeZoneId = (Get-TimeZone).Id
}
}
AfterAll {
if ($IsWindows) {
Set-TimeZone -ID $originalTimeZoneId
}
}
It "Call Set-TimeZone with invalid Id" {
try {
Set-TimeZone -Id "zzInvalidID"
throw "No Exception!"
}
catch {
$_.FullyQualifiedErrorID | Should Be "TimeZoneNotFound,Microsoft.PowerShell.Commands.SetTimeZoneCommand"
}
}
It "Call Set-TimeZone by Name" {
$origTimeZoneName = (Get-TimeZone).StandardName
$timezoneList = Get-TimeZone -ListAvailable
$testTimezone = $null
foreach ($timezone in $timezoneList) {
if ($timezone.StandardName -ne $origTimeZoneName) {
$testTimezone = $timezone
break
}
}
Set-TimeZone -Name $testTimezone.StandardName
$observed = Get-TimeZone
$testTimezone.StandardName | Should Be $observed.StandardName
}
It "Call Set-TimeZone with invalid Name" {
try {
Set-TimeZone -Name "zzINVALID_Name"
throw "No Exception!"
}
catch {
$_.FullyQualifiedErrorID | Should Be "TimeZoneNotFound,Microsoft.PowerShell.Commands.SetTimeZoneCommand"
}
}
It "Call Set-TimeZone from pipeline input object of type TimeZoneInfo" {
$origTimeZoneID = (Get-TimeZone).Id
$timezoneList = Get-TimeZone -ListAvailable
$testTimezone = $null
foreach ($timezone in $timezoneList) {
if ($timezone.Id -ne $origTimeZoneID) {
$testTimezone = $timezone
break
}
}
$testTimezone | Set-TimeZone
$observed = Get-TimeZone
$observed.ID | Should Be $testTimezone.Id
}
It "Call Set-TimeZone from pipeline input object of type TimeZoneInfo, verify supports whatif" {
$origTimeZoneID = (Get-TimeZone).Id
$timezoneList = Get-TimeZone -ListAvailable
$testTimezone = $null
foreach ($timezone in $timezoneList) {
if ($timezone.Id -ne $origTimeZoneID) {
$testTimezone = $timezone
break
}
}
Set-TimeZone -Id $testTimezone.Id -WhatIf > $null
$observed = Get-TimeZone
$observed.Id | Should Be $origTimeZoneID
}
}
}
finally {
$global:PSDefaultParameterValues = $defaultParamValues
}