Tests/Integration/MSFT_xExchDatabaseAvailabilityGroup.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
<#
    .SYNOPSIS
        Automated unit integration for MSFT_xExchDatabaseAvailabilityGroup 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.
        This module has the following additional requirements;
            * Requires that the ActiveDirectory module is installed
            * Requires that the DAG computer account has been precreated
            * Requires that the Failover-Clustering feature has been installed, and the machine has since been rebooted
#>


#region HEADER
[System.String]$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
[System.String]$script:DSCModuleName = 'xExchange'
[System.String]$script:DSCResourceFriendlyName = 'xExchDatabaseAvailabilityGroup'
[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

$adModule = Get-Module -ListAvailable ActiveDirectory -ErrorAction SilentlyContinue

if ($null -ne $adModule)
{
    if ($null -eq $dagName)
    {
        $dagName = Read-Host -Prompt 'Enter the name of the DAG to use for testing'
    }

    $compAccount = Get-ADComputer -Identity $dagName -ErrorAction SilentlyContinue

    if ($null -ne $compAccount)
    {
        [System.String]$testDBName = 'TestDAGDB1'

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

            #Get the Server FQDN for using in URL's
            if ($null -eq $serverFqdn)
            {
                $serverFqdn = [System.Net.Dns]::GetHostByName($env:COMPUTERNAME).HostName
            }

            if ($null -eq $secondDAGMember)
            {
                $secondDAGMember = Read-Host -Prompt 'Enter the host name of the second DAG member to use for testing, or press ENTER to skip.'

                #If they didn't enter anything, set to an empty string so we don't prompt again on a re-run of the test
                if ($null -eq $secondDAGMember)
                {
                    $secondDAGMember = ''
                }
            }

            #Make sure failover clustering is installed on both nodes. Start with this node.
            $fcNode1 = Get-WindowsFeature -Name Failover-Clustering -ComputerName $serverFqdn -ErrorAction SilentlyContinue

            if ($null -eq $fcNode1 -or !$fcNode1.Installed)
            {
                Write-Error -Message ('The Failover-Clustering role must be fully installed on' + $($serverFqdn) + `
                                      'before the server can be added to the cluster. Skipping all DAG tests.')
                return
            }

            if (!([System.String]::IsNullOrEmpty($secondDAGMember)))
            {
                $fcNode2 = Get-WindowsFeature -Name Failover-Clustering -ComputerName $secondDAGMember -ErrorAction SilentlyContinue

                if ($null -eq $fcNode2 -or !$fcNode2.Installed)
                {
                    Write-Error -Message ('The Failover-Clustering role must be fully installed on' + $($secondDAGMember) + `
                                          'before the server can be added to the cluster. Skipping tests that would utilize this DAG member.')

                    $secondDAGMember = ''
                }
            }

            while (([System.String]::IsNullOrEmpty($witness1)))
            {
                $witness1 = Read-Host -Prompt 'Enter the FQDN of the first File Share Witness for testing'
            }

            #Allow for the second witness to not be specified
            if ($null -eq $witness2)
            {
                $witness2 = Read-Host -Prompt 'Enter the FQDN of the second File Share Witness for testing, or press ENTER to skip.'

                #If they didn't enter anything, set to an empty string so we don't prompt again on a re-run of the test
                if ($null -eq $witness2)
                {
                    $witness2 = ''
                }
            }

            #Remove the existing DAG
            Initialize-TestForDAG -ServerName @($env:COMPUTERNAME,$secondDAGMember) `
                                  -DAGName $dagName `
                                  -DatabaseName $testDBName `
                                  -ShellCredentials $shellCredentials

            Describe 'Test Creating and Modifying a DAG, adding DAG members, creating a DAG database, and adding database copies' {
                #Create a new DAG
                $dagTestParams = @{
                    Name = $dagName
                    Credential = $shellCredentials
                    AutoDagAutoReseedEnabled = $true
                    AutoDagDatabaseCopiesPerDatabase = 2
                    AutoDagDatabaseCopiesPerVolume = 2
                    AutoDagDatabasesRootFolderPath = 'C:\ExchangeDatabases'
                    AutoDagDiskReclaimerEnabled = $true
                    AutoDagTotalNumberOfServers = 2
                    AutoDagTotalNumberOfDatabases = 2
                    AutoDagVolumesRootFolderPath = 'C:\ExchangeVolumes'
                    DatabaseAvailabilityGroupIpAddresses = '192.168.1.99','192.168.2.99'
                    DatacenterActivationMode = 'DagOnly'
                    ManualDagNetworkConfiguration = $true
                    NetworkCompression = 'Enabled'
                    NetworkEncryption = 'InterSubnetOnly'
                    ReplayLagManagerEnabled = $true
                    SkipDagValidation = $true
                    WitnessDirectory = 'C:\FSW'
                    WitnessServer = $witness1
                }

                #Skip checking DatacenterActivationMode until we have DAG members
                $dagExpectedGetResults = @{
                    Name = $dagName
                    AutoDagAutoReseedEnabled = $true
                    AutoDagDatabaseCopiesPerDatabase = 2
                    AutoDagDatabaseCopiesPerVolume = 2
                    AutoDagDatabasesRootFolderPath = 'C:\ExchangeDatabases'
                    AutoDagDiskReclaimerEnabled = $true
                    AutoDagTotalNumberOfServers = 2
                    AutoDagTotalNumberOfDatabases = 2
                    AutoDagVolumesRootFolderPath = 'C:\ExchangeVolumes'
                    ManualDagNetworkConfiguration = $true
                    NetworkCompression = 'Enabled'
                    NetworkEncryption = 'InterSubnetOnly'
                    ReplayLagManagerEnabled = $true
                    WitnessDirectory = 'C:\FSW'
                    WitnessServer = $witness1
                }

                if (!([System.String]::IsNullOrEmpty($witness2)))
                {
                    $dagTestParams.Add('AlternateWitnessServer', $witness2)
                    $dagTestParams.Add('AlternateWitnessDirectory', 'C:\FSW')
                    $dagExpectedGetResults.Add('AlternateWitnessServer', $witness2)
                    $dagExpectedGetResults.Add('AlternateWitnessDirectory', 'C:\FSW')
                }

                $serverVersion = Get-ExchangeVersion

                if ($serverVersion -in '2016','2019')
                {
                    $dagTestParams.Add('FileSystem', 'ReFS')
                    $dagTestParams.Add('AutoDagAutoRedistributeEnabled', $true)
                    $dagTestParams.Add('PreferenceMoveFrequency', "$(([System.Threading.Timeout]::InfiniteTimeSpan).ToString())")
                    $dagExpectedGetResults.Add('FileSystem', 'ReFS')
                    $dagExpectedGetResults.Add('AutoDagAutoRedistributeEnabled', $true)
                    $dagExpectedGetResults.Add('PreferenceMoveFrequency', "$(([System.Threading.Timeout]::InfiniteTimeSpan).ToString())")
                }

                Test-TargetResourceFunctionality -Params $dagTestParams `
                                                 -ContextLabel 'Create the test DAG' `
                                                 -ExpectedGetResults $dagExpectedGetResults

                Test-ArrayContentsEqual -TestParams $dagTestParams `
                                        -DesiredArrayContents $dagTestParams.DatabaseAvailabilityGroupIpAddresses `
                                        -GetResultParameterName 'DatabaseAvailabilityGroupIpAddresses' `
                                        -ContextLabel 'Verify DatabaseAvailabilityGroupIpAddresses' `
                                        -ItLabel 'DatabaseAvailabilityGroupIpAddresses should contain two values'

                #Add this server as a DAG member
                Get-Module MSFT_xExch* | Remove-Module -ErrorAction SilentlyContinue
                Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResources' -ChildPath (Join-Path -Path "MSFT_xExchDatabaseAvailabilityGroupMember" -ChildPath "MSFT_xExchDatabaseAvailabilityGroupMember.psm1")))

                $dagMemberTestParams = @{
                    MailboxServer = $env:COMPUTERNAME
                    Credential = $shellCredentials
                    DAGName = $dagName
                    SkipDagValidation = $true
                }

                $dagMemberExpectedGetResults = @{
                    MailboxServer = $env:COMPUTERNAME
                    DAGName = $dagName
                }

                Test-TargetResourceFunctionality -Params $dagMemberTestParams `
                                                 -ContextLabel 'Add first member to the test DAG' `
                                                 -ExpectedGetResults $dagMemberExpectedGetResults

                #Do second DAG member tests if a second member was specified
                if (([System.String]::IsNullOrEmpty($secondDAGMember)) -eq $false)
                {
                    #Add second DAG member
                    $dagMemberTestParams = @{
                        MailboxServer = $secondDAGMember
                        Credential = $shellCredentials
                        DAGName = $dagName
                        SkipDagValidation = $true
                    }

                    $dagMemberExpectedGetResults = @{
                        MailboxServer = $secondDAGMember
                        DAGName = $dagName
                    }

                    Test-TargetResourceFunctionality -Params $dagMemberTestParams `
                                                     -ContextLabel 'Add second member to the test DAG' `
                                                     -ExpectedGetResults $dagMemberExpectedGetResults

                    #Test the DAG again, with props that only take effect once there are members
                    Get-Module MSFT_xExch* | Remove-Module -ErrorAction SilentlyContinue
                    Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResources' -ChildPath (Join-Path -Path "$($script:DSCResourceName)" -ChildPath "$($script:DSCResourceName).psm1")))

                    $dagExpectedGetResults.DatacenterActivationMode = 'DagOnly'

                    Test-TargetResourceFunctionality -Params $dagTestParams `
                                                     -ContextLabel 'Set remaining props on the test DAG' `
                                                     -ExpectedGetResults $dagExpectedGetResults

                    Test-ArrayContentsEqual -TestParams $dagTestParams `
                                            -DesiredArrayContents $dagTestParams.DatabaseAvailabilityGroupIpAddresses `
                                            -GetResultParameterName 'DatabaseAvailabilityGroupIpAddresses' `
                                            -ContextLabel 'Verify DatabaseAvailabilityGroupIpAddresses' `
                                            -ItLabel 'DatabaseAvailabilityGroupIpAddresses should contain two values'

                    #Create a new DAG database
                    Get-Module MSFT_xExch* | Remove-Module -ErrorAction SilentlyContinue
                    Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResources' -ChildPath (Join-Path -Path "MSFT_xExchMailboxDatabase" -ChildPath "MSFT_xExchMailboxDatabase.psm1")))

                    $dagDBTestParams = @{
                        Name = $testDBName
                        Credential = $shellCredentials
                        AllowServiceRestart = $true
                        DatabaseCopyCount = 2
                        EdbFilePath = "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$($testDBName)\$($testDBName).edb"
                        LogFolderPath = "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$($testDBName)"
                        Server = $env:COMPUTERNAME
                    }

                    $dagDBExpectedGetResults = @{
                        Name = $testDBName
                        EdbFilePath = "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$($testDBName)\$($testDBName).edb"
                        LogFolderPath = "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$($testDBName)"
                        Server = $env:COMPUTERNAME
                    }

                    Test-TargetResourceFunctionality -Params $dagDBTestParams `
                                                     -ContextLabel 'Create test database' `
                                                     -ExpectedGetResults $dagDBExpectedGetResults

                    #Add DB Copy
                    Get-Module MSFT_xExch* | Remove-Module -ErrorAction SilentlyContinue
                    Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResources' -ChildPath (Join-Path -Path "MSFT_xExchMailboxDatabaseCopy" -ChildPath "MSFT_xExchMailboxDatabaseCopy.psm1")))

                    $dagDBCopyTestParams = @{
                        Identity = $testDBName
                        MailboxServer = $secondDAGMember
                        Credential = $shellCredentials
                        ActivationPreference = 2
                    }

                    $dagDBCopyExpectedGetResults = @{
                        Identity = $testDBName
                        MailboxServer = $secondDAGMember
                        ActivationPreference = 2
                    }

                    Test-TargetResourceFunctionality -Params $dagDBCopyTestParams `
                                                     -ContextLabel 'Add a database copy' `
                                                     -ExpectedGetResults $dagDBCopyExpectedGetResults
                }
            }
        }
        else
        {
            Write-Verbose -Message 'Tests in this file require that Exchange is installed to be run.'
        }
    }
    else
    {
        Write-Verbose -Message 'Tests in this file require that the computer account for the DAG is precreated'
    }
}
else
{
    Write-Verbose -Message 'Tests in this file require that the ActiveDirectory module is installed. Run: Add-WindowsFeature RSAT-ADDS'
}