Test/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
###NOTE: This test module requires use of credentials. The first run through of the tests will prompt for credentials from the logged on user.
###This module also requires that this server is a member of a DAG"

Import-Module $PSScriptRoot\..\DSCResources\MSFT_xExchMaintenanceMode\MSFT_xExchMaintenanceMode.psm1
Import-Module $PSScriptRoot\..\Misc\xExchangeCommon.psm1 -Verbose:0
Import-Module $PSScriptRoot\xExchange.Tests.Common.psm1 -Verbose:0

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

    Context "Do additional Get-TargetResource verification after taking server out of maintenance mode" {
        [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.
        [string[]]$expectedActiveComponentsList = "ServerWideOffline","HubTransport","FrontendTransport","Monitoring","RecoveryActionsEnabled","AutoDiscoverProxy","ActiveSyncProxy","EcpProxy","EwsProxy","ImapProxy","OabProxy","OwaProxy","PopProxy","PushNotificationsProxy","RpsProxy","RwsProxy","RpcProxy","UMCallRouter","XropProxy","HttpProxyAvailabilityGroup","MapiProxy","EdgeTransport","HighAvailability","SharedCache"

        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 {$_.Status -eq "Mounted"}

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

function EnsureOutOfMaintenanceMode
{
    [CmdletBinding()]
    param
    (
        [System.Boolean]
        $WaitBetweenTests = $true
    )

    Write-Verbose "Ensuring server is out of maintenance mode"

        #Put server in maintenance mode
        $testParams = @{
            Enabled = $false
            Credential = $Global:ShellCredentials
            AdditionalComponentsToActivate = "AutoDiscoverProxy","ActiveSyncProxy","EcpProxy","EwsProxy","ImapProxy","OabProxy","OwaProxy","PopProxy","PushNotificationsProxy","RpsProxy","RwsProxy","RpcProxy","UMCallRouter","XropProxy","HttpProxyAvailabilityGroup","MapiProxy","EdgeTransport","HighAvailability","SharedCache"
            MovePreferredDatabasesBack = $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)
        {
            WaitBetweenTests -Verbose
        }
}

function WaitBetweenTests
{
    [CmdletBinding()]
    param([int]$SleepSeconds = 60)

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

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

if ($exchangeInstalled)
{
    #Get required credentials to use for the test
    if ($null -eq $Global:ShellCredentials)
    {
        [PSCredential]$Global:ShellCredentials = Get-Credential -Message "Enter credentials for connecting a Remote PowerShell session to Exchange"
    }
   
    #Make sure server is a DAG member
    if ($null -eq $Global:IsDagMember)
    {
        GetRemoteExchangeSession -Credential $Global:ShellCredentials -CommandsToLoad "Get-MailboxServer","Get-MailboxDatabaseCopyStatus","Get-MailboxDatabase"

        $mbxServer = Get-MailboxServer $env:COMPUTERNAME

        [boolean]$Global:IsDagMember = !([string]::IsNullOrEmpty($mbxServer.DatabaseAvailabilityGroup))
    }

    if ($Global:IsDagMember -eq $false)
    {
        Write-Verbose "Tests in this file require that this server be a member of a Database Availability Group"
        return
    }

    #Make sure server only has replicated DB's
    if ($null -eq $Global:HasNonReplicationDBs)
    {
        $nonReplicatedDBs = Get-MailboxDatabase -Server $env:COMPUTERNAME -ErrorAction SilentlyContinue | where {$_.ReplicationType -like "None"}

        if ($null -ne $nonReplicatedDBs)
        {
            $Global:HasNonReplicationDBs = $true
        }
    }

    if ($Global:HasNonReplicationDBs -eq $true)
    {
        Write-Verbose "Tests in this file require that all databases on this server must have copies on other DAG members."
        return
    }

    #Get Domain Controller
    if ($null -eq $Global:DomainController)
    {
        [string]$Global:DomainController = Read-Host -Prompt "Enter Domain Controller to use for DC tests"
    }

    Write-Verbose "Ensuring server is out of maintenance mode before beginning tests"
    EnsureOutOfMaintenanceMode

    Describe "Test Putting a Server in and out of Maintenance Mode" {
        #Put server in maintenance mode
        $testParams = @{
            Enabled = $true
            Credential = $Global:ShellCredentials
            AdditionalComponentsToActivate = "AutoDiscoverProxy","ActiveSyncProxy","EcpProxy","EwsProxy","ImapProxy","OabProxy","OwaProxy","PopProxy","PushNotificationsProxy","RpsProxy","RwsProxy","RpcProxy","UMCallRouter","XropProxy","HttpProxyAvailabilityGroup","MapiProxy","EdgeTransport","HighAvailability","SharedCache"
            MovePreferredDatabasesBack = $true
        }

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

        Test-AllTargetResourceFunctions -Params $testParams -ContextLabel "Put server in maintenance mode" -ExpectedGetResults $expectedGetResults
        WaitBetweenTests -Verbose


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

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

        Test-AllTargetResourceFunctions -Params $testParams -ContextLabel "Take server out of maintenance mode" -ExpectedGetResults $expectedGetResults
        Test-ServerIsOutOfMaintenanceMode
        WaitBetweenTests -Verbose


        #Test passing in UpgradedServerVersion that is lower than the current server version
        $testParams = @{
            Enabled = $true
            Credential = $Global:ShellCredentials
            MovePreferredDatabasesBack = $true
            UpgradedServerVersion = "15.0.0.0"
        }

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

        Test-AllTargetResourceFunctions -Params $testParams -ContextLabel "Try to put server in maintenance mode using UpgradedServerVersion of an older server." -ExpectedGetResults $expectedGetResults
        WaitBetweenTests -Verbose


        #Test using Domain Controller switch to put server in maintenance mode
        $testParams = @{
            Enabled = $true
            Credential = $Global:ShellCredentials
            MovePreferredDatabasesBack = $true
            DomainController = $Global:DomainController
        }

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

        Test-AllTargetResourceFunctions -Params $testParams -ContextLabel "Put server in maintenance mode using Domain Controller switch" -ExpectedGetResults $expectedGetResults
        WaitBetweenTests -Verbose


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

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

        Test-AllTargetResourceFunctions -Params $testParams -ContextLabel "Take server out of maintenance mode using Domain Controller switch" -ExpectedGetResults $expectedGetResults
        Test-ServerIsOutOfMaintenanceMode
        WaitBetweenTests -Verbose


        #Test SetInactiveComponentsFromAnyRequesterToActive Parameter
        #First put the server in maintenance mode
        $testParams = @{
            Enabled = $true
            Credential = $Global:ShellCredentials
            AdditionalComponentsToActivate = "AutoDiscoverProxy","ActiveSyncProxy","EcpProxy","EwsProxy","ImapProxy","OabProxy","OwaProxy","PopProxy","PushNotificationsProxy","RpsProxy","RwsProxy","RpcProxy","UMCallRouter","XropProxy","HttpProxyAvailabilityGroup","MapiProxy","EdgeTransport","HighAvailability","SharedCache"
            MovePreferredDatabasesBack = $true
            SetInactiveComponentsFromAnyRequesterToActive = $false
        }

        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 "Tests in this file require that Exchange is installed to be run."
}