forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvoke-SqlDscQuery.Integration.Tests.ps1
More file actions
221 lines (179 loc) · 11.7 KB
/
Invoke-SqlDscQuery.Integration.Tests.ps1
File metadata and controls
221 lines (179 loc) · 11.7 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
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
[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'
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
}
Describe 'Invoke-SqlDscQuery' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
$script:mockInstanceName = 'DSCSQLTEST'
$script:mockComputerName = Get-ComputerName
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$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
# Create a test database for our queries
$script:testDatabaseName = 'SqlDscTestInvokeQuery_' + (Get-Random)
$null = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -ErrorAction 'Stop'
# Create a test table with some data
$createTableQuery = @"
CREATE TABLE TestTable (
ID int IDENTITY(1,1) PRIMARY KEY,
Name nvarchar(50),
Value int
)
INSERT INTO TestTable (Name, Value) VALUES ('Test1', 100), ('Test2', 200), ('Test3', 300)
"@
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $createTableQuery -Force -ErrorAction 'Stop'
}
AfterAll {
# Clean up test database
if ($script:testDatabaseName)
{
try
{
Remove-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseName -Force -DropConnections -ErrorAction 'Stop'
}
catch
{
Write-Warning -Message "Failed to remove test database '$($script:testDatabaseName)': $($_.Exception.Message)"
}
}
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
}
Context 'When executing a query using ServerObject parameter set' {
Context 'When executing a query without returning results' {
It 'Should execute the query without throwing (using Force parameter)' {
$null = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query 'UPDATE TestTable SET Value = 500 WHERE ID = 1' -Force -ErrorAction 'Stop'
}
It 'Should execute the query without throwing (using Confirm:$false parameter)' {
$null = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query 'UPDATE TestTable SET Value = 600 WHERE ID = 2' -Confirm:$false -ErrorAction 'Stop'
}
}
Context 'When executing a query with PassThru parameter' {
It 'Should return results when using PassThru parameter' {
$result = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query 'SELECT * FROM TestTable' -PassThru -Force -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType 'System.Data.DataSet'
$result.Tables[0].Rows.Count | Should -BeGreaterOrEqual 3
}
It 'Should return specific results when querying with WHERE clause' {
$result = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query "SELECT Name FROM TestTable WHERE Name = 'Test1'" -PassThru -Force -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType 'System.Data.DataSet'
$result.Tables[0].Rows.Count | Should -Be 1
$result.Tables[0].Rows[0]['Name'] | Should -Be 'Test1'
}
}
Context 'When using optional parameters with ServerObject parameter set' {
It 'Should execute query with custom StatementTimeout' {
$null = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query 'SELECT COUNT(*) FROM TestTable' -StatementTimeout 30 -PassThru -Force -ErrorAction 'Stop'
}
It 'Should execute query with RedactText parameter' {
$null = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query "SELECT * FROM TestTable WHERE Name = 'SensitiveData'" -RedactText @('SensitiveData') -PassThru -Force -ErrorAction 'Stop'
}
}
Context 'When accepting ServerObject from pipeline' {
It 'Should execute query when ServerObject is passed through pipeline' {
$result = $script:serverObject | Invoke-SqlDscQuery -DatabaseName $script:testDatabaseName -Query 'SELECT COUNT(*) as RecordCount FROM TestTable' -PassThru -Force -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType 'System.Data.DataSet'
$result.Tables[0].Rows[0]['RecordCount'] | Should -BeGreaterOrEqual 3
}
}
}
Context 'When executing a query using ByServerName parameter set' {
Context 'When executing a query without returning results' {
It 'Should execute the query without throwing' {
$null = Invoke-SqlDscQuery -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -DatabaseName $script:testDatabaseName -Query 'UPDATE TestTable SET Value = 700 WHERE ID = 3' -Force -ErrorAction 'Stop'
}
}
Context 'When executing a query with PassThru parameter' {
It 'Should return results when using PassThru parameter' {
$result = Invoke-SqlDscQuery -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -DatabaseName $script:testDatabaseName -Query 'SELECT Name, Value FROM TestTable ORDER BY ID' -PassThru -Force -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType 'System.Data.DataSet'
$result.Tables[0].Rows.Count | Should -BeGreaterOrEqual 3
}
}
Context 'When using optional parameters with ByServerName parameter set' {
It 'Should execute query with Encrypt parameter' {
$null = Invoke-SqlDscQuery -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -DatabaseName $script:testDatabaseName -Query 'SELECT 1 as TestValue' -Encrypt -PassThru -Force -ErrorAction 'Stop'
}
It 'Should execute query with LoginType parameter' {
# Create SQL Server credential for 'sa' login
$sqlLoginPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$sqlLoginCredential = [System.Management.Automation.PSCredential]::new('sa', $sqlLoginPassword)
$null = Invoke-SqlDscQuery -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -Credential $sqlLoginCredential -LoginType 'SqlLogin' -DatabaseName $script:testDatabaseName -Query 'SELECT 1 as TestValue' -PassThru -Force -ErrorAction 'Stop'
}
It 'Should execute query with custom StatementTimeout' {
$null = Invoke-SqlDscQuery -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -DatabaseName $script:testDatabaseName -Query 'SELECT 1 as TestValue' -StatementTimeout 60 -PassThru -Force -ErrorAction 'Stop'
}
}
}
Context 'When testing error handling' {
It 'Should throw error when querying non-existent database' {
{
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName 'NonExistentDatabase' -Query 'SELECT 1' -Force -ErrorAction 'Stop'
} | Should -Throw
}
It 'Should throw error when executing invalid SQL query' {
{
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query 'INVALID SQL SYNTAX' -Force -ErrorAction 'Stop'
} | Should -Throw
}
It 'Should throw error when querying non-existent table' {
{
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query 'SELECT * FROM NonExistentTable' -Force -ErrorAction 'Stop'
} | Should -Throw
}
}
Context 'When testing system databases' {
It 'Should execute query against master database' {
$result = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName 'master' -Query 'SELECT name FROM sys.databases WHERE name = ''master''' -PassThru -Force -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType 'System.Data.DataSet'
$result.Tables[0].Rows.Count | Should -Be 1
$result.Tables[0].Rows[0]['name'] | Should -Be 'master'
}
It 'Should execute query against msdb database' {
$result = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName 'msdb' -Query 'SELECT COUNT(*) as TableCount FROM INFORMATION_SCHEMA.TABLES' -PassThru -Force -ErrorAction 'Stop'
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType 'System.Data.DataSet'
$result.Tables[0].Rows[0]['TableCount'] | Should -BeGreaterThan 0
}
}
Context 'When testing WhatIf functionality' {
It 'Should not execute query when using WhatIf parameter' {
# Get initial count
$initialResult = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query 'SELECT COUNT(*) as RecordCount FROM TestTable' -PassThru -Force -ErrorAction 'Stop'
$initialCount = $initialResult.Tables[0].Rows[0]['RecordCount']
# Run WhatIf query that would add a record
$null = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query "INSERT INTO TestTable (Name, Value) VALUES ('WhatIfTest', 999)" -WhatIf -ErrorAction 'Stop'
# Verify count is unchanged
$finalResult = Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query 'SELECT COUNT(*) as RecordCount FROM TestTable' -PassThru -Force -ErrorAction 'Stop'
$finalCount = $finalResult.Tables[0].Rows[0]['RecordCount']
$finalCount | Should -Be $initialCount
}
}
}