forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisable-SqlDscDatabaseSnapshotIsolation.Integration.Tests.ps1
More file actions
161 lines (125 loc) · 8.43 KB
/
Disable-SqlDscDatabaseSnapshotIsolation.Integration.Tests.ps1
File metadata and controls
161 lines (125 loc) · 8.43 KB
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
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
param ()
BeforeDiscovery {
try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
}
# If the dependencies have not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.'
}
}
BeforeAll {
$script:moduleName = 'SqlServerDsc'
# Do not use -Force. Doing so, or unloading the module in AfterAll, causes
# PowerShell class types to get new identities, breaking type comparisons.
Import-Module -Name $script:moduleName -ErrorAction 'Stop'
}
Describe 'Disable-SqlDscDatabaseSnapshotIsolation' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022', 'Integration_SQL2025') {
BeforeAll {
$script:mockInstanceName = 'DSCSQLTEST'
$script:mockComputerName = Get-ComputerName
$mockSqlAdministratorUserName = 'SqlAdmin'
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
# Test database names
$script:testDatabaseName = 'SqlDscTestSnapshotIsolation_' + (Get-Random)
$script:testDatabaseNameForObject = 'SqlDscTestSnapshotIsolationObj_' + (Get-Random)
# Create test databases
$null = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop'
$null = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseNameForObject -Force -ErrorAction 'Stop'
# Get the current snapshot isolation state to restore later
$testDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -ErrorAction 'Stop'
$script:originalSnapshotIsolationState = $testDb.SnapshotIsolationState
Write-Verbose -Message "Original snapshot isolation state of database '$($script:testDatabaseName)' is '$($script:originalSnapshotIsolationState)'." -Verbose
}
AfterAll {
# Clean up test databases
$testDatabasesToRemove = @($script:testDatabaseName, $script:testDatabaseNameForObject)
foreach ($dbName in $testDatabasesToRemove)
{
$existingDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $dbName -ErrorAction 'SilentlyContinue'
if ($existingDb)
{
$null = Remove-SqlDscDatabase -DatabaseObject $existingDb -Force -ErrorAction 'Stop'
}
}
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject -ErrorAction 'Stop'
}
Context 'When disabling snapshot isolation using ServerObject parameter set' {
It 'Should disable snapshot isolation successfully' {
# First ensure it's enabled
$null = Enable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop'
$resultDb = Disable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -PassThru -ErrorAction 'Stop'
$resultDb.SnapshotIsolationState | Should -Be 'Disabled'
# Verify the change
$updatedDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Refresh -ErrorAction 'Stop'
$updatedDb.SnapshotIsolationState | Should -Be 'Disabled'
}
It 'Should be idempotent when snapshot isolation is already disabled' {
# Disable snapshot isolation
$null = Disable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop'
# Disable again - should not throw
$null = Disable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop'
# Verify the value is still correct
$updatedDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Refresh -ErrorAction 'Stop'
$updatedDb.SnapshotIsolationState | Should -Be 'Disabled'
}
It 'Should throw error when trying to disable snapshot isolation on non-existent database' {
{ Disable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name 'NonExistentDatabase' -Force -ErrorAction 'Stop' } |
Should -Throw
}
}
Context 'When disabling snapshot isolation using DatabaseObject parameter set' {
It 'Should disable snapshot isolation using database object' {
# First ensure it's enabled
$null = Enable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseNameForObject -Force -ErrorAction 'Stop'
$databaseObject = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseNameForObject -Refresh -ErrorAction 'Stop'
$null = Disable-SqlDscDatabaseSnapshotIsolation -DatabaseObject $databaseObject -Force -ErrorAction 'Stop'
# Verify the change
$updatedDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseNameForObject -Refresh -ErrorAction 'Stop'
$updatedDb.SnapshotIsolationState | Should -Be 'Disabled'
}
It 'Should support pipeline input with database object' {
# First ensure it's enabled
$null = Enable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseNameForObject -Force -ErrorAction 'Stop'
$databaseObject = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseNameForObject -Refresh -ErrorAction 'Stop'
$null = $databaseObject | Disable-SqlDscDatabaseSnapshotIsolation -Force -ErrorAction 'Stop'
# Verify the change
$updatedDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseNameForObject -Refresh -ErrorAction 'Stop'
$updatedDb.SnapshotIsolationState | Should -Be 'Disabled'
}
}
Context 'When using the Refresh parameter' {
It 'Should refresh the database collection before disabling snapshot isolation' {
# First ensure it's enabled
$null = Enable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop'
$null = Disable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseName -Refresh -Force -ErrorAction 'Stop'
# Verify the change
$updatedDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Refresh -ErrorAction 'Stop'
$updatedDb.SnapshotIsolationState | Should -Be 'Disabled'
}
}
Context 'When using the PassThru parameter' {
It 'Should return the database object when PassThru is specified' {
# First ensure it's enabled
$null = Enable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop'
$resultDb = Disable-SqlDscDatabaseSnapshotIsolation -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -PassThru -ErrorAction 'Stop'
$resultDb | Should -Not -BeNullOrEmpty
$resultDb.Name | Should -Be $script:testDatabaseName
$resultDb.SnapshotIsolationState | Should -Be 'Disabled'
}
}
}