MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4<#5 --------------------------------------6 Script Notes: opportunities for improving this test script7 --------------------------------------8 Localization9 Many of the tests below looking-up timezones by Name do not support localization.10 That is, the current tests use us english versions of StandardName and DaylightName for tests.11 12 ref: https://msdn.microsoft.com/library/windows/desktop/ms725481.aspx13 [snippet] Both StandardName and DaylightName are localized according to the current user default UI language.14#>15 16function Assert-ListsSame {17 param([object[]] $expected, [object[]] $observed )18 $compResult = Compare-Object $observed $expected | Select-Object -ExpandProperty InputObject19 if ($compResult) {20 $observedList = ([string]::Join("|", $observed))21 $expectedList = ([string]::Join("|", $expected))22 $observedList | Should -Be $expectedList23 }24}25 26Describe "Get-Timezone test cases" -Tags "CI" {27 28 BeforeAll {29 $TimeZonesAvailable = [System.TimeZoneInfo]::GetSystemTimeZones()30 31 $defaultParamValues = $PSDefaultParameterValues.Clone()32 $PSDefaultParameterValues["it:skip"] = ($TimeZonesAvailable.Count -eq 0)33 }34 35 AfterAll {36 $global:PSDefaultParameterValues = $defaultParamValues37 }38 39 It "Call without ListAvailable switch returns current TimeZoneInfo" {40 $observed = (Get-TimeZone).Id41 $expected = ([System.TimeZoneInfo]::Local).Id42 $observed | Should -Be $expected43 }44 45 It "Call without ListAvailable switch returns an object of type TimeZoneInfo" {46 $result = Get-TimeZone47 $result | Should -BeOfType TimeZoneInfo48 }49 50 It "Call WITH ListAvailable switch returns ArrayList of TimeZoneInfo objects where the list is greater than 0 item" {51 $list = Get-TimeZone -ListAvailable52 $list.Count | Should -BeGreaterThan 053 54 , $list | Should -BeOfType Object[]55 $list[0] | Should -BeOfType TimeZoneInfo56 }57 58 ## The local time zone could be set to UTC or GMT*. In this case, the .NET API returns the region ID59 ## and not 'UTC'. To avoid a string matching error, we compare the BaseUtcOffset instead.60 It "Call with ListAvailable switch returns a list containing TimeZoneInfo.Local" {61 $observedIdList = Get-TimeZone -ListAvailable | Select-Object -ExpandProperty BaseUtcOffset62 $oneExpectedOffset = ([System.TimeZoneInfo]::Local).BaseUtcOffset63 $oneExpectedOffset | Should -BeIn $observedIdList64 }65 66 ## The local time zone could be set to UTC or GMT*. In this case, the .NET API returns the region ID67 ## and not UTC. To avoid a string matching error, we compare the BaseUtcOffset instead.68 It "Call with ListAvailable switch returns a list containing one returned by Get-TimeZone" {69 $observedIdList = Get-TimeZone -ListAvailable | Select-Object -ExpandProperty BaseUtcOffset70 $oneExpectedOffset = (Get-TimeZone).BaseUtcOffset71 $oneExpectedOffset | Should -BeIn $observedIdList72 }73 74 It "Call with -ListAvailable:`$false returns current TimeZoneInfo (not list)" {75 $result = Get-TimeZone -ListAvailable:$false76 $result | Should -BeOfType TimeZoneInfo77 $result.Id | Should -Be ([System.TimeZoneInfo]::Local).Id78 }79 80 It "Call Get-TimeZone using ID param and single item" {81 $selectedTZ = $TimeZonesAvailable[0]82 (Get-TimeZone -Id $selectedTZ.Id).Id | Should -Be $selectedTZ.Id83 }84 85 It "Call Get-TimeZone using ID param and multiple items" {86 $selectedTZ = $TimeZonesAvailable | Select-Object -First 3 -ExpandProperty Id87 $result = (Get-TimeZone -Id $selectedTZ).Id88 Assert-ListsSame $result $selectedTZ89 }90 91 It "Call Get-TimeZone using ID param and multiple items, where first and third are invalid ids - expect error" {92 $selectedTZ = $TimeZonesAvailable[0].Id93 $null = Get-TimeZone -Id @("Cape Verde Standard", $selectedTZ, "Azores Standard") `94 -ErrorVariable errVar -ErrorAction SilentlyContinue95 $errVar.Count | Should -Be 296 $errVar[0].FullyQualifiedErrorID | Should -Be "TimeZoneNotFound,Microsoft.PowerShell.Commands.GetTimeZoneCommand"97 }98 99 It "Call Get-TimeZone using ID param and multiple items, one is wild card but error action ignore works as expected" {100 $selectedTZ = $TimeZonesAvailable | Select-Object -First 3 -ExpandProperty Id101 $inputArray = $selectedTZ + "*"102 $result = Get-TimeZone -Id $inputArray -ErrorAction SilentlyContinue | ForEach-Object Id103 Assert-ListsSame $selectedTZ $result104 }105 106 It "Call Get-TimeZone using Name param and singe item" {107 $timezoneList = Get-TimeZone -ListAvailable108 $timezoneName = $timezoneList[0].StandardName109 $observed = Get-TimeZone -Name $timezoneName110 $observed.StandardName | Should -Be $timezoneName111 }112 113 It "Call Get-TimeZone using Name param with wild card" {114 $result = (Get-TimeZone -Name "Pacific*").Id115 $expectedIdList = ($TimeZonesAvailable | Where-Object { $_.StandardName -match "^Pacific" }).Id116 Assert-ListsSame $expectedIdList $result117 }118 119 It "Call Get-TimeZone Name parameter from pipeline by value " {120 $result = ("Pacific*" | Get-TimeZone).Id121 $expectedIdList = ($TimeZonesAvailable | Where-Object { $_.StandardName -match "^Pacific" }).Id122 Assert-ListsSame $expectedIdList $result123 }124 125 It "Call Get-TimeZone Id parameter from pipeline by ByPropertyName" {126 $timezoneList = Get-TimeZone -ListAvailable127 $timezone = $timezoneList[0]128 $observed = $timezone | Get-TimeZone129 $observed.StandardName | Should -Be $timezone.StandardName130 }131}132 133 134Describe "Set-Timezone test case: call by single Id" -Tags @('CI', 'RequireAdminOnWindows') {135 BeforeAll {136 137 $defaultParamValues = $PSdefaultParameterValues.Clone()138 139 if (-not $IsWindows -or (Test-IsWinServer2012R2)) {140 # Set-TimeZone fails due to missing ApiSet dependency on Windows Server 2012 R2.141 $PSDefaultParameterValues["it:skip"] = $true142 return143 }144 145 $originalTimeZoneId = (Get-TimeZone).Id146 }147 AfterAll {148 if (-not $IsWindows -or (Test-IsWinServer2012R2)) {149 $global:PSDefaultParameterValues = $defaultParamValues150 return151 }152 153 Set-TimeZone -Id $originalTimeZoneId154 }155 156 It "Call Set-TimeZone by Id" {157 $origTimeZoneID = (Get-TimeZone).Id158 $timezoneList = Get-TimeZone -ListAvailable159 $testTimezone = $null160 foreach ($timezone in $timezoneList) {161 if ($timezone.Id -ne $origTimeZoneID) {162 $testTimezone = $timezone163 break164 }165 }166 Set-TimeZone -Id $testTimezone.Id167 $observed = Get-TimeZone168 $testTimezone.Id | Should -Be $observed.Id169 }170}171 172Describe "Set-Timezone test cases" -Tags @('Feature', 'RequireAdminOnWindows') {173 BeforeAll {174 $defaultParamValues = $PSdefaultParameterValues.Clone()175 176 if (-not $IsWindows -or (Test-IsWinServer2012R2)) {177 # Set-TimeZone fails due to missing ApiSet dependency on Windows Server 2012 R2.178 $PSDefaultParameterValues["it:skip"] = $true179 return180 }181 182 $originalTimeZoneId = (Get-TimeZone).Id183 }184 AfterAll {185 if (-not $IsWindows -or (Test-IsWinServer2012R2)) {186 $global:PSDefaultParameterValues = $defaultParamValues187 return188 }189 190 Set-TimeZone -Id $originalTimeZoneId191 }192 193 It "Call Set-TimeZone with invalid Id" {194 { Set-TimeZone -Id "zzInvalidID" } | Should -Throw -ErrorId "TimeZoneNotFound,Microsoft.PowerShell.Commands.SetTimeZoneCommand"195 }196 197 It "Call Set-TimeZone by Name" {198 $origTimeZoneName = (Get-TimeZone).StandardName199 $timezoneList = Get-TimeZone -ListAvailable200 $testTimezone = $null201 foreach ($timezone in $timezoneList) {202 if ($timezone.StandardName -ne $origTimeZoneName) {203 $testTimezone = $timezone204 break205 }206 }207 Set-TimeZone -Name $testTimezone.StandardName208 $observed = Get-TimeZone209 $testTimezone.StandardName | Should -Be $observed.StandardName210 }211 212 It "Call Set-TimeZone with invalid Name" {213 { Set-TimeZone -Name "zzINVALID_Name" } | Should -Throw -ErrorId "TimeZoneNotFound,Microsoft.PowerShell.Commands.SetTimeZoneCommand"214 }215 216 It "Call Set-TimeZone from pipeline input object of type TimeZoneInfo" {217 $origTimeZoneID = (Get-TimeZone).Id218 $timezoneList = Get-TimeZone -ListAvailable219 $testTimezone = $null220 foreach ($timezone in $timezoneList) {221 if ($timezone.Id -ne $origTimeZoneID) {222 $testTimezone = $timezone223 break224 }225 }226 227 $testTimezone | Set-TimeZone228 $observed = Get-TimeZone229 $observed.ID | Should -Be $testTimezone.Id230 }231 232 It "Call Set-TimeZone from pipeline input object of type TimeZoneInfo, verify supports whatif" {233 $origTimeZoneID = (Get-TimeZone).Id234 $timezoneList = Get-TimeZone -ListAvailable235 $testTimezone = $null236 foreach ($timezone in $timezoneList) {237 if ($timezone.Id -ne $origTimeZoneID) {238 $testTimezone = $timezone239 break240 }241 }242 243 Set-TimeZone -Id $testTimezone.Id -WhatIf > $null244 $observed = Get-TimeZone245 $observed.Id | Should -Be $origTimeZoneID246 }247}248 