Tests/Integration/MSFT_xExchMailboxDatabase.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
<#
    .SYNOPSIS
        Automated integration test for MSFT_xExchMailboxDatabase 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 = 'xExchMailboxDatabase'
[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
        Removes the specified Mailbox Database and associated files
 
    .PARAMETER Database
        The name of the Mailbox Database to remove.
#>

function Initialize-ExchDscDatabase
{
    [CmdletBinding()]
    param
    (
        [Parameter()]
        [System.String]
        $Database
    )

    Write-Verbose -Message 'Cleaning up test database'

    GetRemoteExchangeSession -Credential $shellCredentials -CommandsToLoad '*-MailboxDatabase'
    Get-MailboxDatabase | Where-Object -FilterScript {
        $_.Name -like "$($Database)"
    } | Remove-MailboxDatabase -Confirm:$false

    Get-ChildItem -LiteralPath "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$($Database)" -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue

    if ($null -ne (Get-MailboxDatabase | Where-Object {$_.Name -like "$($Database)"}))
    {
        throw 'Failed to cleanup test database'
    }

    Write-Verbose -Message 'Finished cleaning up test database'
}

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

    $TestDBName = 'Mailbox Database Test 123'

    Initialize-ExchDscDatabase -Database $TestDBName

    #Get the test OAB
    $testOabName = Get-TestOfflineAddressBook -ShellCredentials $shellCredentials

    Describe 'Test Creating a DB and Setting Properties with xExchMailboxDatabase' {
        #First create and set properties on a test database
        $testParams = @{
            Name = $TestDBName
            Credential = $shellCredentials
            Server = $env:COMPUTERNAME
            EdbFilePath = "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$($TestDBName)\$($TestDBName).edb"
            LogFolderPath = "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$($TestDBName)"
            AllowServiceRestart = $true
            AutoDagExcludeFromMonitoring = $true
            BackgroundDatabaseMaintenance = $true
            CalendarLoggingQuota = 'unlimited'
            CircularLoggingEnabled = $true
            DatabaseCopyCount = 1
            DeletedItemRetention = '14.00:00:00'
            EventHistoryRetentionPeriod = '03:04:05'
            IndexEnabled = $true
            IsExcludedFromProvisioning = $false
            IsSuspendedFromProvisioning = $false
            MailboxRetention = '30.00:00:00'
            MountAtStartup = $true
            OfflineAddressBook = $testOabName
            RetainDeletedItemsUntilBackup = $false
            IssueWarningQuota = '27 MB'
            ProhibitSendQuota = '1GB'
            ProhibitSendReceiveQuota = '1.5 GB'
            RecoverableItemsQuota = 'uNlImItEd'
            RecoverableItemsWarningQuota = '1,000,448'
        }

        $expectedGetResults = @{
            Name = $TestDBName
            Server = $env:COMPUTERNAME
            EdbFilePath = "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$($TestDBName)\$($TestDBName).edb"
            LogFolderPath = "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$($TestDBName)"
            AutoDagExcludeFromMonitoring = $true
            BackgroundDatabaseMaintenance = $true
            CalendarLoggingQuota = 'unlimited'
            CircularLoggingEnabled = $true
            DatabaseCopyCount = 1
            DeletedItemRetention = '14.00:00:00'
            EventHistoryRetentionPeriod = '03:04:05'
            IndexEnabled = $true
            IsExcludedFromProvisioning = $false
            IsSuspendedFromProvisioning = $false
            MailboxRetention = '30.00:00:00'
            MountAtStartup = $true
            OfflineAddressBook = "\$testOabName"
            RetainDeletedItemsUntilBackup = $false
            IssueWarningQuota = '27 MB (28,311,552 bytes)'
            ProhibitSendQuota = '1 GB (1,073,741,824 bytes)'
            ProhibitSendReceiveQuota = '1.5 GB (1,610,612,736 bytes)'
            RecoverableItemsQuota = 'uNlImItEd'
            RecoverableItemsWarningQuota = '977 KB (1,000,448 bytes)'
        }

        Test-TargetResourceFunctionality -Params $testParams `
                                         -ContextLabel 'Create Test Database' `
                                         -ExpectedGetResults $expectedGetResults

        #Now change properties on the test database
        $testParams.CalendarLoggingQuota = '30mb'
        $testParams.CircularLoggingEnabled = $false
        $testParams.DeletedItemRetention = '15.00:00:00'
        $testParams.EventHistoryRetentionPeriod = '04:05:06'
        $testParams.IndexEnabled = $false
        $testParams.IsExcludedFromProvisioning = $true
        $testParams.IsSuspendedFromProvisioning = $true
        $testParams.MailboxRetention = '31.00:00:00'
        $testParams.MountAtStartup = $false
        $testParams.RetainDeletedItemsUntilBackup = $true
        $testParams.IssueWarningQuota = '28 MB'
        $testParams.ProhibitSendQuota = '2GB'
        $testParams.ProhibitSendReceiveQuota = '2.5 GB'
        $testParams.RecoverableItemsQuota = '2 GB'
        $testParams.RecoverableItemsWarningQuota = '1.5 GB'

        $expectedGetResults.CalendarLoggingQuota = '30 MB (31,457,280 bytes)'
        $expectedGetResults.CircularLoggingEnabled = $false
        $expectedGetResults.DeletedItemRetention = '15.00:00:00'
        $expectedGetResults.EventHistoryRetentionPeriod = '04:05:06'
        $expectedGetResults.IndexEnabled = $false
        $expectedGetResults.IsExcludedFromProvisioning = $true
        $expectedGetResults.IsSuspendedFromProvisioning = $true
        $expectedGetResults.MailboxRetention = '31.00:00:00'
        $expectedGetResults.MountAtStartup = $false
        $expectedGetResults.RetainDeletedItemsUntilBackup = $true
        $expectedGetResults.IssueWarningQuota = '28 MB (29,360,128 bytes)'
        $expectedGetResults.ProhibitSendQuota = '2 GB (2,147,483,648 bytes)'
        $expectedGetResults.ProhibitSendReceiveQuota = '2.5 GB (2,684,354,560 bytes)'
        $expectedGetResults.RecoverableItemsQuota = '2 GB (2,147,483,648 bytes)'
        $expectedGetResults.RecoverableItemsWarningQuota = '1.5 GB (1,610,612,736 bytes)'

        $serverVersion = Get-ExchangeVersion

        if ($serverVersion -in '2016','2019')
        {
            $testParams.Add('IsExcludedFromProvisioningReason', 'Testing Excluding the Database')
            $expectedGetResults.Add('IsExcludedFromProvisioningReason', 'Testing Excluding the Database')
        }

        Test-TargetResourceFunctionality -Params $testParams -ContextLabel 'Change many DB properties' -ExpectedGetResults $expectedGetResults

        <#
            Test setting database quotas to unlimited, when they aren't already unlimited.
            To reproduce the issue in (https://github.com/PowerShell/xExchange/issues/193), you must run
            Test-TargetResource with an Unlimited parameter when the current value is not Unlimited.
            If the current value is already Unlimited, the Test will succeed.
        #>


        Context 'Test Looking For Unlimited Value When Currently Set to Non-Unlimited Value' {
            $caughtException = $false

            #First set a quota to a non-Unlimited value
            $testParams.ProhibitSendReceiveQuota = '10GB'
            Set-TargetResource @testParams

            #Now test for the value and look to see if it's Unlimited
            $testParams.ProhibitSendReceiveQuota = 'Unlimited'

            try
            {
                $testResults = Test-TargetResource @testParams
            }
            catch
            {
                $caughtException = $true
            }

            It 'Should not hit exception trying to test for Unlimited' {
                $caughtException | Should Be $false
            }

            It 'Test results should be false after testing for new quota' {
                $testResults | Should Be $false
            }
        }

        #Test setting database quotas to non-unlimited value, when they are currently set to a different non-unlimited value
        Context 'Test Looking For Non-Unlimited Value When Currently Set to Different Non-Unlimited Value' {
            #First set a quota to a non-Unlimited value
            $testParams.ProhibitSendReceiveQuota = '10GB'
            Set-TargetResource @testParams

            #Now test for the value and look to see if it's a different non-unlimited value
            $testParams.ProhibitSendReceiveQuota = '11GB'

            $testResults = Test-TargetResource @testParams

            It 'Test results should be false after testing for new quota' {
                $testResults | Should Be $false
            }
        }

        Context 'Test Looking For Non-Unlimited Value When Currently Set to Unlimited Value' {
            #First set a quota to an Unlimited value
            $testParams.ProhibitSendReceiveQuota = 'Unlimited'
            Set-TargetResource @testParams

            #Now test for the value and look to see if it's non-Unlimited
            $testParams.ProhibitSendReceiveQuota = '11GB'

            $testResults = Test-TargetResource @testParams

            It 'Test results should be false after testing for new quota' {
                $testResults | Should Be $false
            }
        }

        Context 'Test Looking For Same Value In A Different Size Format' {
            #First set a quota to a non-Unlimited value in megabytes
            $testParams.ProhibitSendReceiveQuota = '10240MB'
            Set-TargetResource @testParams

            #Now test for the value and look to see if it's the same value, but in gigabytes
            $testParams.ProhibitSendReceiveQuota = '10GB'

            $testResults = Test-TargetResource @testParams

            It 'Test results should be true after testing for new quota' {
                $testResults | Should Be $true
            }
        }

        #Set all quotas to unlimited
        $testParams.IssueWarningQuota = 'unlimited'
        $testParams.ProhibitSendQuota = 'Unlimited'
        $testParams.ProhibitSendReceiveQuota = 'unlimited'
        $testParams.RecoverableItemsQuota = 'unlimited'
        $testParams.RecoverableItemsWarningQuota = 'unlimited'

        $expectedGetResults.IssueWarningQuota = 'unlimited'
        $expectedGetResults.ProhibitSendQuota = 'Unlimited'
        $expectedGetResults.ProhibitSendReceiveQuota = 'unlimited'
        $expectedGetResults.RecoverableItemsQuota = 'unlimited'
        $expectedGetResults.RecoverableItemsWarningQuota = 'unlimited'

        Test-TargetResourceFunctionality -Params $testParams `
                                         -ContextLabel 'Set remaining quotas to Unlimited' `
                                         -ExpectedGetResults $expectedGetResults
    }

    # Clean up the test database
    Initialize-ExchDscDatabase -Database $TestDBName
}
else
{
    Write-Verbose -Message 'Tests in this file require that Exchange is installed to be run.'
}