Skip to content

Commit 1d15ce4

Browse files
authored
Test-ShouldUpgradeExchange did not handle the version correctly (#410) (#494)
1 parent 62e07e3 commit 1d15ce4

File tree

4 files changed

+69
-76
lines changed

4 files changed

+69
-76
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3131
- Fixed typo.
3232
- xExchangeHelper
3333
- Fixed module import error ([issue #481](https://github.com/dsccommunity/xExchange/issues/481)).
34-
- xExchangeHelper
3534
- Fixed error on first Exchange Module import ([Issue #488](https://github.com/dsccommunity/xExchange/issues/488)).
35+
- Changed return value of `Get-SetupExeVersion` to `System.Version` which fixes #410.
3636

3737
## [1.33.0] - 2021-10-31
3838

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ variables:
1818
testArtifactName: testResults
1919
sourceFolderName: source
2020
defaultBranch: main
21+
Agent.Source.Git.ShallowFetchDepth: 0
2122

2223
stages:
2324
- stage: Build

source/Modules/xExchangeHelper/xExchangeHelper.psm1

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@ function Get-ExchangeVersionYear
305305

306306
if ($null -ne $installedVersionDetails)
307307
{
308-
switch ($installedVersionDetails.VersionMajor)
308+
switch ($installedVersionDetails.Major)
309309
{
310310
15
311311
{
312-
switch ($installedVersionDetails.VersionMinor)
312+
switch ($installedVersionDetails.Minor)
313313
{
314314
0
315315
{
@@ -385,21 +385,17 @@ function Get-DetailedInstalledVersion
385385
$displayVersion = Get-ItemProperty -Path $uninstallKeyPath -Name 'DisplayVersion' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty DisplayVersion
386386

387387
$versionBuild = $null
388-
$displayVersion -match '(?<VersionMajor>\d+).(?<VersionMinor>\d+).(?<VersionBuild>\d+)'
389388

390-
if ($Matches)
389+
if ($displayVersion -match '(?<VersionMajor>\d+).(?<VersionMinor>\d+).(?<VersionBuild>\d+).(?<VersionRevision>\d+)')
391390
{
392391
$versionBuild = $Matches['VersionBuild']
392+
$versionRevision = $Matches['VersionRevision']
393393
}
394394

395-
$versionDetails = @{
396-
VersionMajor = Get-ItemProperty -Path $uninstallKeyPath -Name 'VersionMajor' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty VersionMajor
397-
VersionMinor = Get-ItemProperty -Path $uninstallKeyPath -Name 'VersionMinor' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty VersionMinor
398-
VersionBuild = $versionBuild
399-
DisplayVersion = $displayVersion
400-
}
395+
$versionMajor = Get-ItemProperty -Path $uninstallKeyPath -Name 'VersionMajor' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty VersionMajor
396+
$versionMinor = Get-ItemProperty -Path $uninstallKeyPath -Name 'VersionMinor' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty VersionMinor
401397

402-
$installedVersionDetails = New-Object -TypeName PSCustomObject -Property $versionDetails
398+
$installedVersionDetails = [System.Version]::new($versionMajor, $versionMinor, $versionBuild, $versionRevision)
403399
}
404400

405401
return $installedVersionDetails
@@ -494,7 +490,7 @@ function Test-ExchangeSetupPartiallyCompleted
494490
<#
495491
.SYNOPSIS
496492
Gets Exchange's setup.exe file's version info.
497-
It will return VersionMajor, VersionMinor, VersionBuild values as PSCustomObject
493+
It will return a System.Version object
498494
or NULL if not readable.
499495
500496
.PARAMETER Path
@@ -518,18 +514,13 @@ function Get-SetupExeVersion
518514
{
519515
$setupexeVersionInfo = (Get-ChildItem -Path $Path).VersionInfo
520516

521-
$setupexeVersionInfo = @{
522-
VersionMajor = [System.Int32] $setupexeVersionInfo.ProductMajorPart
523-
VersionMinor = [System.Int32] $setupexeVersionInfo.ProductMinorPart
524-
VersionBuild = [System.Int32] $setupexeVersionInfo.ProductBuildPart
525-
}
526-
527-
$version = New-Object -TypeName PSCustomObject -Property $setupexeVersionInfo
517+
$version = [System.Version]::new($setupexeVersionInfo.ProductMajorPart, $setupexeVersionInfo.ProductMinorPart, $setupexeVersionInfo.ProductBuildPart)
528518
}
529519

530520
return $version
531521
}
532522

523+
533524
<#
534525
.SYNOPSIS
535526
Checks if installed Exchange version is older than the version of the setup.exe,
@@ -569,25 +560,25 @@ function Test-ShouldUpgradeExchange
569560
$setupExeVersion = Get-SetupExeVersion -Path $Path
570561

571562
if ($null -ne $setupExeVersion`
572-
-and $null -ne $setupExeVersion.VersionMajor`
573-
-and $null -ne $setupExeVersion.VersionMinor`
574-
-and $null -ne $setupExeVersion.VersionBuild)
563+
-and $null -ne $setupExeVersion.Major`
564+
-and $null -ne $setupExeVersion.Minor`
565+
-and $null -ne $setupExeVersion.Build)
575566
{
576-
Write-Verbose -Message "Setup.exe version is: '$('Major: {0}, Minor: {1}, Build: {2}' -f $setupExeVersion.VersionMajor,$setupexeVersion.VersionMinor, $setupexeVersion.VersionBuild)'"
567+
Write-Verbose -Message "Setup.exe version is: '$('Major: {0}, Minor: {1}, Build: {2}' -f $setupExeVersion.Major, $setupexeVersion.Minor, $setupexeVersion.Build)'"
577568

578569
$exchangeDisplayVersion = Get-DetailedInstalledVersion
579570

580571
if ($null -ne $exchangeDisplayVersion`
581-
-and $null -ne $exchangeDisplayVersion.VersionMajor`
582-
-and $null -ne $exchangeDisplayVersion.VersionMinor`
583-
-and $null -ne $exchangeDisplayVersion.VersionBuild)
572+
-and $null -ne $exchangeDisplayVersion.Major`
573+
-and $null -ne $exchangeDisplayVersion.Minor`
574+
-and $null -ne $exchangeDisplayVersion.Build)
584575
{
585576
# If we have an exchange installed
586-
Write-Verbose -Message "Exchange version is: '$('Major: {0}, Minor: {1}, Build: {2}' -f $exchangeDisplayVersion.VersionMajor,$exchangeDisplayVersion.VersionMinor, $exchangeDisplayVersion.VersionBuild)'"
577+
Write-Verbose -Message "Exchange version is: '$('Major: {0}, Minor: {1}, Build: {2}' -f $exchangeDisplayVersion.Major, $exchangeDisplayVersion.Minor, $exchangeDisplayVersion.Build)'"
587578

588-
if (($exchangeDisplayVersion.VersionMajor -eq $setupExeVersion.VersionMajor)`
589-
-and ($exchangeDisplayVersion.VersionMinor -eq $setupExeVersion.VersionMinor)`
590-
-and ($exchangeDisplayVersion.VersionBuild -lt $setupExeVersion.VersionBuild) )
579+
if (($exchangeDisplayVersion.Major -eq $setupExeVersion.Major)`
580+
-and ($exchangeDisplayVersion.Minor -eq $setupExeVersion.Minor)`
581+
-and ($exchangeDisplayVersion.Build -lt $setupExeVersion.Build) )
591582
{
592583
# If server has lower version of CU installed
593584
Write-Verbose -Message 'Version upgrade is requested.'

tests/Unit/xExchangeHelper.tests.ps1

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -905,103 +905,103 @@ try
905905

906906
$validProductVersions = @(
907907
@{
908-
VersionMajor = 15
909-
VersionMinor = 0
908+
Major = 15
909+
Minor = 0
910910
Year = '2013'
911911
}
912912
@{
913-
VersionMajor = 15
914-
VersionMinor = 1
913+
Major = 15
914+
Minor = 1
915915
Year = '2016'
916916
}
917917
@{
918-
VersionMajor = 15
919-
VersionMinor = 2
918+
Major = 15
919+
Minor = 2
920920
Year = '2019'
921921
}
922922
)
923923

924924
$invalidProductVersions = @(
925925
@{
926-
VersionMajor = 15
927-
VersionMinor = 7
926+
Major = 15
927+
Minor = 7
928928
Year = $null
929929
}
930930
@{
931-
VersionMajor = 14
932-
VersionMinor = 0
931+
Major = 14
932+
Minor = 0
933933
Year = $null
934934
}
935935
)
936936

937-
Context 'When Get-ExchangeVersionYear is called and finds a valid VersionMajor and VersionMinor' {
937+
Context 'When Get-ExchangeVersionYear is called and finds a valid Major and Minor' {
938938
It 'Should return the correct Exchange year' -TestCases $validProductVersions {
939939
param
940940
(
941941
[System.Int32]
942-
$VersionMajor,
942+
$Major,
943943

944944
[System.Int32]
945-
$VersionMinor,
945+
$Minor,
946946

947947
[System.String]
948948
$Year
949949
)
950950

951951
Mock -CommandName Get-DetailedInstalledVersion -Verifiable -MockWith {
952952
return @{
953-
VersionMajor = $VersionMajor
954-
VersionMinor = $VersionMinor
953+
Major = $Major
954+
Minor = $Minor
955955
}
956956
}
957957

958958
Get-ExchangeVersionYear | Should -Be $Year
959959
}
960960
}
961961

962-
Context 'When Get-ExchangeVersionYear is called and finds an invalid VersionMajor or VersionMinor without ThrowIfUnknownVersion' {
962+
Context 'When Get-ExchangeVersionYear is called and finds an invalid Major or Minor without ThrowIfUnknownVersion' {
963963
It 'Should return <Year>' -TestCases $invalidProductVersions {
964964
param
965965
(
966966
[System.Int32]
967-
$VersionMajor,
967+
$Major,
968968

969969
[System.Int32]
970-
$VersionMinor,
970+
$Minor,
971971

972972
[System.String]
973973
$Year
974974
)
975975

976976
Mock -CommandName Get-DetailedInstalledVersion -Verifiable -MockWith {
977977
return @{
978-
VersionMajor = $VersionMajor
979-
VersionMinor = $VersionMinor
978+
Major = $Major
979+
Minor = $Minor
980980
}
981981
}
982982

983983
Get-ExchangeVersionYear | Should -Be $null
984984
}
985985
}
986986

987-
Context 'When Get-ExchangeVersionYear is called and finds an invalid VersionMajor or VersionMinor and ThrowIfUnknownVersion is specified' {
987+
Context 'When Get-ExchangeVersionYear is called and finds an invalid Major or Minor and ThrowIfUnknownVersion is specified' {
988988
It 'Should throw an exception' -TestCases $invalidProductVersions {
989989
param
990990
(
991991
[System.Int32]
992-
$VersionMajor,
992+
$Major,
993993

994994
[System.Int32]
995-
$VersionMinor,
995+
$Minor,
996996

997997
[System.String]
998998
$Year
999999
)
10001000

10011001
Mock -CommandName Get-DetailedInstalledVersion -Verifiable -MockWith {
10021002
return @{
1003-
VersionMajor = $VersionMajor
1004-
VersionMinor = $VersionMinor
1003+
Major = $Major
1004+
Minor = $nMinor
10051005
}
10061006
}
10071007

@@ -1048,22 +1048,23 @@ try
10481048
}
10491049

10501050
Context 'When DetailedInstalledVersion is called and a valid key is returned by Get-ExchangeUninstallKey' {
1051-
It 'Should return custom object with VersionMajor and VersionMinor properties' {
1052-
Mock -CommandName Get-ExchangeUninstallKey -Verifiable -MockWith { return @{Name = 'SomeKeyName' } }
1051+
It 'Should return custom object with Major and Minor properties' {
1052+
Mock -CommandName Get-ExchangeUninstallKey -Verifiable -MockWith { return @{ Name = 'SomeKeyName' } }
10531053
Mock -CommandName Get-ItemProperty -Verifiable -ParameterFilter { $Name -eq 'DisplayVersion' } -MockWith {
1054-
return [PSCustomObject] @{DisplayVersion = '15.1.1531.13' } }
1054+
return [PSCustomObject] @{ DisplayVersion = '15.1.1531.13' } }
10551055
Mock -CommandName Get-ItemProperty -Verifiable -ParameterFilter { $Name -eq 'VersionMajor' } -MockWith {
1056-
return [PSCustomObject] @{VersionMajor = 15 } }
1056+
return [PSCustomObject] @{ VersionMajor = 15 } }
10571057
Mock -CommandName Get-ItemProperty -Verifiable -ParameterFilter { $Name -eq 'VersionMinor' } -MockWith {
10581058
return [PSCustomObject] @{ VersionMinor = 1 }
10591059
}
10601060

10611061
$installedVersionDetails = Get-DetailedInstalledVersion
10621062

1063-
$installedVersionDetails.VersionMajor | Should -Be 15
1064-
$installedVersionDetails.VersionMinor | Should -Be 1
1065-
$installedVersionDetails.VersionBuild | Should -Be 1531
1066-
$installedVersionDetails.DisplayVersion | Should -Be '15.1.1531.13'
1063+
$installedVersionDetails.Major | Should -Be 15
1064+
$installedVersionDetails.Minor | Should -Be 1
1065+
$installedVersionDetails.Build | Should -Be 1531
1066+
$installedVersionDetails.Revision | Should -Be 13
1067+
$installedVersionDetails.ToString() | Should -Be '15.1.1531.13'
10671068
}
10681069
}
10691070

@@ -1173,17 +1174,17 @@ try
11731174

11741175
Mock -CommandName Get-SetupExeVersion -Verifiable -MockWith {
11751176
return [PSCustomObject] @{
1176-
VersionMajor = $SetupVersionMajor
1177-
VersionMinor = $SetupVersionMinor
1178-
VersionBuild = $SetupVersionBuild
1177+
Major = $SetupVersionMajor
1178+
Minor = $SetupVersionMinor
1179+
Build = $SetupVersionBuild
11791180
}
11801181
}
11811182

11821183
Mock -CommandName Get-DetailedInstalledVersion -Verifiable -MockWith {
11831184
return [PSCustomObject] @{
1184-
VersionMajor = $ExchangeVersionMajor
1185-
VersionMinor = $ExchangeVersionMinor
1186-
VersionBuild = $ExchangeVersionBuild
1185+
Major = $ExchangeVersionMajor
1186+
Minor = $ExchangeVersionMinor
1187+
Build = $ExchangeVersionBuild
11871188
}
11881189
}
11891190

@@ -1213,9 +1214,9 @@ try
12131214

12141215
Mock -CommandName Get-SetupExeVersion -Verifiable -MockWith {
12151216
return [PSCustomObject] @{
1216-
VersionMajor = 15
1217-
VersionMinor = 1
1218-
VersionBuild = 1234
1217+
Major = 15
1218+
Minor = 1
1219+
Build = 1234
12191220
}
12201221
}
12211222

@@ -1270,9 +1271,9 @@ try
12701271

12711272
$version = Get-SetupExeVersion -Path 'SomePath'
12721273

1273-
$version.VersionMajor | Should -Be 1
1274-
$version.VersionMinor | Should -Be 2
1275-
$version.VersionBuild | Should -Be 3
1274+
$version.Major | Should -Be 1
1275+
$version.Minor | Should -Be 2
1276+
$version.Build | Should -Be 3
12761277
}
12771278
}
12781279

0 commit comments

Comments
 (0)