Tests/Integration/MSFT_xExchMaintenanceMode.Integration.Tests.ps1

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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<#
    .SYNOPSIS
        Automated integration test for MSFT_xExchMaintenanceMode DSC Resource.
        This test module requires use of credentials.
        The first run through of the tests will prompt for credentials from the logged on user.
#>


#region HEADER
[System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
[System.String] $script:DSCModuleName = 'xExchange'
[System.String] $script:DSCResourceFriendlyName = 'xExchMaintenanceMode'
[System.String] $script:DSCResourceName = "MSFT_$($script:DSCResourceFriendlyName)"

Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'Tests' -ChildPath (Join-Path -Path 'TestHelpers' -ChildPath 'xExchangeTestHelper.psm1'))) -Force
Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'Modules' -ChildPath 'xExchangeHelper.psm1')) -Force
Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResources' -ChildPath (Join-Path -Path "$($script:DSCResourceName)" -ChildPath "$($script:DSCResourceName).psm1")))

#Check if Exchange is installed on this machine. If not, we can't run tests
[System.Boolean] $exchangeInstalled = Get-IsSetupComplete

#endregion HEADER

<#
    .SYNOPSIS
        Runs tests to ensure that the server is fully out of maintenance mode
#>

function Test-ServerIsOutOfMaintenanceMode
{
    [CmdletBinding()]
    param()

    Context 'Do additional Get-TargetResource verification after taking server out of maintenance mode' {
        [System.Collections.Hashtable] $getResult = Get-TargetResource @testParams -Verbose

        It 'ActiveComponentCount is greater than 0' {
            $getResult.ActiveComponentCount -gt 0 | Should Be $true
        }

        <#
            Verify that all components in the following list are Active.
            This list comes from an Exchange 2013 CU9 machine with both the CAS and MBX roles.
        #>

        [System.String[]] $expectedActiveComponentsList = Get-VersionSpecificComponentsToActivate

        $expectedActiveComponentsList += 'ServerWideOffline','HubTransport','FrontendTransport','Monitoring','RecoveryActionsEnabled'

        foreach ($expectedActiveComponent in $expectedActiveComponentsList)
        {
            It "Component $($expectedActiveComponent) should be Active" {
                $getResult.ActiveComponentsList.Contains($expectedActiveComponent) | Should Be $true
            }
        }

        $status = $null
        $status = Get-MailboxDatabaseCopyStatus -Server $env:COMPUTERNAME | Where-Object {$_.Status -eq 'Mounted'}

        It 'Databases were failed back' {
            ($null -ne $status) | Should Be $true
        }
    }
}

<#
    .SYNOPSIS
        Performs steps to take the server out of maintenance mode, verifies
        that the server was fully taken out of maintenance mode, and throws
        an exception if it was not.
 
    .PARAMETER WaitBetweenTests
        Whether the script should sleep a predetermined amount of time after
        the server has been taken out of maintenance mode before exiting the
        function.
#>

function Set-ThenAssertOutOfMaintenanceMode
{
    [CmdletBinding()]
    param
    (
        [Parameter()]
        [System.Boolean]
        $WaitBetweenTests = $true
    )

    Write-Verbose -Message 'Ensuring server is out of maintenance mode'

    #Take server out of maintenance mode
    $testParams = @{
        Enabled                                       = $false
        Credential                                    = $shellCredentials
        AdditionalComponentsToActivate                = Get-VersionSpecificComponentsToActivate
        MountDialOverride                             = 'BestEffort'
        MovePreferredDatabasesBack                    = $true
        SetInactiveComponentsFromAnyRequesterToActive = $true
        SkipClientExperienceChecks                    = $true
        SkipLagChecks                                 = $true
        SkipMoveSuppressionChecks                     = $true
    }

    Set-TargetResource @testParams -Verbose
    $inMM = Test-TargetResource @testParams -Verbose

    if ($inMM -eq $false)
    {
        throw 'Failed to take server out of maintenance mode'
    }
    elseif ($WaitBetweenTests -eq $true)
    {
        Wait-Verbose -Verbose
    }
}

<#
    .SYNOPSIS
        Performs a Write-Verbose that the script is about to sleep for
        $SleepSeconds seconds, then sleeps for $SleepSeconds seconds.
 
    .PARAMETER SleepSeconds
        The number of seconds to sleep.
#>

function Wait-Verbose
{
    [CmdletBinding()]
    param
    (
        [Parameter()]
        [System.Int32]
        $SleepSeconds = 15
    )

    Write-Verbose -Message "Sleeping $($SleepSeconds) between tests."
    Start-Sleep -Seconds $SleepSeconds
}

<#
    .SYNOPSIS
        Returns a String Array of extra components to attempt to activate when
        taking a server out of maintenance mode. Ensures that the components
        returned are relevant to the installed version of Exchange.
#>

function Get-VersionSpecificComponentsToActivate
{
    [CmdletBinding()]
    [OutputType([System.String[]])]
    param()

    $serverVersion = Get-ExchangeVersion

    [System.String[]] $componentsToActivate = @('AutoDiscoverProxy',
                                               'ActiveSyncProxy',
                                               'EcpProxy',
                                               'EwsProxy',
                                               'ImapProxy',
                                               'OabProxy',
                                               'OwaProxy',
                                               'PopProxy',
                                               'PushNotificationsProxy',
                                               'RpsProxy',
                                               'RwsProxy',
                                               'RpcProxy',
                                               'XropProxy',
                                               'HttpProxyAvailabilityGroup',
                                               'MapiProxy',
                                               'EdgeTransport',
                                               'HighAvailability',
                                               'SharedCache')

    if ($serverVersion -in '2013','2016')
    {
        $componentsToActivate += 'UMCallRouter'
    }

    return $componentsToActivate
}

<#
    .SYNOPSIS
        Waits up to 15 minutes for the Content Index state of all database
        copies of Activation Preference 1 on this server to become healthy.
        Returns $true if all Indexes are healthy after the wait, else returns
        $false. Will force a reseed of each unhealthy Content Index if another
        server in the DAG has a healthy Content Index.
#>

function Wait-ForPrimaryHealthyIndexState
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param()

    $hasHealthyContentIndexes = $false

    [System.Object[]] $unhealthyDBs = Get-UnhealthyPrimaryIndexesOnServer

    if ($unhealthyDBs.Count -gt 0)
    {
        # See if we have a healthy DB partner we can reseed the content index from
        foreach ($db in $unhealthyDBs)
        {
            if ($null -ne (Get-MailboxDatabaseCopyStatus -Identity $db.DatabaseName | Where-Object {$_.MailboxServer -notlike "$($env:COMPUTERNAME)" -and $_.ContentIndexState -like "Healthy"}))
            {
                Write-Verbose "Reseeding content index of copy $($db.Identity)"

                Update-MailboxDatabaseCopy -Identity $db.Identity -CatalogOnly -BeginSeed -Confirm:$false -Force
            }
        }

        # Now wait up to 15 minutes for the indexes to go healthy
        for ($i = 0; $i -lt 15 -and !$hasHealthyContentIndexes; $i++)
        {
            $hasHealthyContentIndexes = (Get-UnhealthyPrimaryIndexesOnServer).Count -eq 0

            if (!$hasHealthyContentIndexes)
            {
                Write-Warning -Message 'One or more databases on this server have a content index in an unhealthy state. Waiting up to 15 minutes for them to become healthy. Sleeping for 60 seconds.'
                Start-Sleep -Seconds 60
            }
        }
    }
    else
    {
        $hasHealthyContentIndexes = $true
    }

    return $hasHealthyContentIndexes
}

<#
    .SYNOPSIS
        Waits up to 15 minutes for the Content Index state of all database
        copy partners on other DAG members to become healthy.
        Returns $true if all Indexes are healthy after the wait, else returns
        $false. Will force a reseed of each unhealthy Content Index if the copy
        on this server has a healthy Content Index.
#>

function Wait-ForSecondaryHealthyIndexState
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param()

    $hasHealthyContentIndexes = $false

    [System.Object[]] $unhealthyDBs = Get-UnhealthySecondaryPartnerIndexesInDAG

    if ($unhealthyDBs.Count -gt 0)
    {
        # See if we have a healthy DB on this server we can reseed the partner from
        foreach ($db in $unhealthyDBs)
        {
            if ($null -ne (Get-MailboxDatabaseCopyStatus -Identity $db.DatabaseName | Where-Object {$_.MailboxServer -like "$($env:COMPUTERNAME)" -and $_.ContentIndexState -like "Healthy"}))
            {
                Write-Verbose "Reseeding content index of copy $($db.Identity)"

                Update-MailboxDatabaseCopy -Identity $db.Identity -CatalogOnly -BeginSeed -Confirm:$false -Force
            }
        }

        # Now wait up to 15 minutes for the indexes to go healthy
        for ($i = 0; $i -lt 15 -and !$hasHealthyContentIndexes; $i++)
        {
            $hasHealthyContentIndexes = (Get-UnhealthySecondaryPartnerIndexesInDAG).Count -eq 0

            if (!$hasHealthyContentIndexes)
            {
                Write-Warning -Message 'One or more databases on another DAG server have a content index in an unhealthy state. Waiting up to 15 minutes for them to become healthy. Sleeping for 60 seconds.'
                Start-Sleep -Seconds 60
            }
        }
    }
    else
    {
        $hasHealthyContentIndexes = $true
    }

    return $hasHealthyContentIndexes
}

<#
    .SYNOPSIS
        Returns an array containing the database copy status of each database
        on this server with Content Index state that is not Healthy.
#>

function Get-UnhealthyPrimaryIndexesOnServer
{
    [CmdletBinding()]
    [OutputType([System.Object[]])]
    param()

    return (Get-MailboxDatabaseCopyStatus -Server $env:COMPUTERNAME | Where-Object {$_.ActivationPreference -eq 1 -and $_.ContentIndexState -ne "Healthy"})
}

<#
    .SYNOPSIS
        Returns an array containing the database copy status of each database
        copy partner on other DAG mebmers with Content Index state that is not
        Healthy.
#>

function Get-UnhealthySecondaryPartnerIndexesInDAG
{
    [CmdletBinding()]
    [OutputType([System.Object[]])]
    param()

    return (Get-MailboxDatabase -Server $env:COMPUTERNAME | Get-MailboxDatabaseCopyStatus | Where-Object {$_.MailboxServer -notlike "*$($env:COMPUTERNAME)*" -and $_.ContentIndexState -ne "Healthy"})
}

if ($null -eq (Get-Module -ListAvailable ActiveDirectory -ErrorAction SilentlyContinue))
{
    Write-Verbose -Message 'Tests in this file require that the ActiveDirectory module is installed. Run: Add-WindowsFeature RSAT-ADDS'
    return
}

if ($exchangeInstalled)
{
    #Get required credentials to use for the test
    $shellCredentials = Get-TestCredential

    #Make sure server is a DAG member
    if ($null -eq $isDagMember)
    {
        GetRemoteExchangeSession -Credential $shellCredentials `
                                 -CommandsToLoad 'Get-MailboxServer','Get-MailboxDatabaseCopyStatus','Get-MailboxDatabase'

        $mbxServer = Get-MailboxServer $env:COMPUTERNAME

        [System.Boolean] $isDagMember = !([System.String]::IsNullOrEmpty($mbxServer.DatabaseAvailabilityGroup))
    }

    if ($isDagMember -eq $false)
    {
        Write-Warning -Message 'Tests in this file require that this server be a member of a Database Availability Group. Skipping testing.'
        return
    }

    # Make sure server only has replicated DB's
    $nonReplicatedDBs = Get-MailboxDatabase -Server $env:COMPUTERNAME -ErrorAction SilentlyContinue | Where-Object -FilterScript {
        $_.ReplicationType -like 'None'
    }

    if ($null -ne $nonReplicatedDBs)
    {
        Write-Warning -Message 'Tests in this file require that all databases on this server must have copies on other DAG members. Skipping testing.'
        return
    }

    #Get Domain Controller
    [System.Object[]] $foundDCs = Get-ADDomainController

    if ($foundDCs.Count -gt 0)
    {
        [System.String] $dcToTestAgainst = $foundDCs[0].HostName
    }
    else
    {
        Write-Error -Message 'Unable to discover Domain Controller to use for DC specific tests.'
        return
    }

    Write-Verbose -Message 'Ensuring server is out of maintenance mode before beginning tests'
    Set-ThenAssertOutOfMaintenanceMode

    # Make sure the content index is healthy on primary database copies
    if (!(Wait-ForPrimaryHealthyIndexState -Verbose))
    {
        Write-Error -Message 'One or more databases on this server have a content index in an unhealthy state. Unable to perform Maintenance Mode tests.'
        return
    }

    if (!(Wait-ForSecondaryHealthyIndexState -Verbose))
    {
        Write-Error -Message 'One or more database replica partners on other DAG servers have a content index in an unhealthy state. Unable to perform Maintenance Mode tests.'
        return
    }

    Describe 'Test Putting a Server in and out of Maintenance Mode' {
        #Put server in maintenance mode
        $testParams = @{
            Enabled                        = $true
            Credential                     = $shellCredentials
            AdditionalComponentsToActivate = Get-VersionSpecificComponentsToActivate
            MountDialOverride              = 'BestEffort' #Copy queue can get behind when rapidly failing over DB's for tests, so set this to BestEffort
            MovePreferredDatabasesBack     = $true
            SkipClientExperienceChecks     = $true #Content Index takes a while to become healthy after failing over. Override for tests.
            SkipLagChecks                  = $true #Copy queue can get behind when rapidly failing over DB's for tests, so skip LAG checks just in case
            SkipMoveSuppressionChecks      = $true #Exchange 2016 only allows a DB to be failed over 4 times in an hour. Override for tests.
        }

        $expectedGetResults = @{
            Enabled              = $true
            ActiveComponentCount = 2 #Monitoring and RecoveryActionsEnabled should still be Active after this
            ActiveDBCount        = 0
            ActiveUMCallCount    = 0
            ClusterState         = 'Paused'
            QueuedMessageCount   = 0
        }

        Test-TargetResourceFunctionality -Params $testParams `
                                         -ContextLabel 'Put server in maintenance mode' `
                                         -ExpectedGetResults $expectedGetResults
        Wait-Verbose -Verbose

        #Take server out of maintenance mode
        $testParams.Enabled = $false

        $expectedGetResults = @{
            Enabled      = $false
            ClusterState = 'Up'
        }

        Test-TargetResourceFunctionality -Params $testParams `
                                         -ContextLabel 'Take server out of maintenance mode' `
                                         -ExpectedGetResults $expectedGetResults
        Test-ServerIsOutOfMaintenanceMode
        Wait-Verbose -Verbose

        #Test passing in UpgradedServerVersion that is lower than the current server version
        $testParams = @{
            Enabled                    = $true
            Credential                 = $shellCredentials
            MountDialOverride          = 'BestEffort'
            MovePreferredDatabasesBack = $true
            UpgradedServerVersion      = '15.0.0.0'
            SkipClientExperienceChecks = $true
            SkipLagChecks              = $true
            SkipMoveSuppressionChecks  = $true
        }

        $expectedGetResults = @{
            Enabled      = $false
            ClusterState = 'Up'
        }

        Test-TargetResourceFunctionality -Params $testParams `
                                         -ContextLabel 'Try to put server in maintenance mode using UpgradedServerVersion of an older server.' `
                                         -ExpectedGetResults $expectedGetResults
        Wait-Verbose -Verbose

        #Test using Domain Controller switch to put server in maintenance mode
        $testParams = @{
            Enabled                    = $true
            Credential                 = $shellCredentials
            MountDialOverride          = 'BestEffort'
            MovePreferredDatabasesBack = $true
            DomainController           = $dcToTestAgainst
            SkipClientExperienceChecks = $true
            SkipLagChecks              = $true
            SkipMoveSuppressionChecks  = $true
        }

        $expectedGetResults = @{
            Enabled              = $true
            ActiveComponentCount = 2 #Monitoring and RecoveryActionsEnabled should still be Active after this
            ActiveDBCount        = 0
            ActiveUMCallCount    = 0
            ClusterState         = 'Paused'
            QueuedMessageCount   = 0
        }

        Test-TargetResourceFunctionality -Params $testParams `
                                         -ContextLabel 'Put server in maintenance mode using Domain Controller switch' `
                                         -ExpectedGetResults $expectedGetResults
        Wait-Verbose -Verbose

        #Test using Domain Controller switch to take server out of maintenance mode
        $testParams.Enabled = $false

        $expectedGetResults = @{
            Enabled      = $false
            ClusterState = 'Up'
        }

        Test-TargetResourceFunctionality -Params $testParams `
                                         -ContextLabel 'Take server out of maintenance mode using Domain Controller switch' `
                                         -ExpectedGetResults $expectedGetResults
        Test-ServerIsOutOfMaintenanceMode
        Wait-Verbose -Verbose

        #Test SetInactiveComponentsFromAnyRequesterToActive Parameter
        #First put the server in maintenance mode
        $testParams = @{
            Enabled                                       = $true
            Credential                                    = $shellCredentials
            AdditionalComponentsToActivate                = Get-VersionSpecificComponentsToActivate
            MountDialOverride                             = 'BestEffort'
            MovePreferredDatabasesBack                    = $true
            SetInactiveComponentsFromAnyRequesterToActive = $false
            SkipClientExperienceChecks                    = $true
            SkipLagChecks                                 = $true
            SkipMoveSuppressionChecks                     = $true
        }

        Set-TargetResource @testParams -Verbose
        $testResults = Test-TargetResource @testParams -Verbose

        It 'Server should be in maintenance mode' {
            $testResults | Should Be $true
        }

        if ($testResults -eq $true)
        {
            #Manually set a component to Inactive as the HealthApi
            Set-ServerComponentState -Identity $env:COMPUTERNAME -Component 'ImapProxy' -State 'Inactive' -Requester 'HealthApi'

            #Do a failed attempt to take server out of maintenance mode
            $testParams.Enabled = $false

            Set-TargetResource @testParams -Verbose
            $testResults = Test-TargetResource @testParams -Verbose

            It 'Test should fail' {
                $testResults | Should Be $false
            }

            #Now set SetInactiveComponentsFromAnyRequesterToActive to true and try again. This should succeed
            $testParams.SetInactiveComponentsFromAnyRequesterToActive = $true

            Set-TargetResource @testParams -Verbose
            $testResults = Test-TargetResource @testParams -Verbose

            It 'Test should succeed' {
                $testResults | Should Be $true
            }
        }
    }
}
else
{
    Write-Verbose -Message 'Tests in this file require that Exchange is installed to be run.'
}