Skip to content

Commit 875f868

Browse files
authored
SqlScript: Add integration tests with configuration and dependencies (#2450)
1 parent e2a8aaf commit 875f868

3 files changed

Lines changed: 279 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Changed
9+
10+
- SqlScript
11+
- Added integration test configuration that creates script files and executes
12+
the resource in a single configuration using `DependsOn`.
13+
814
## [17.5.1] - 2026-02-05
915

1016
### Added

tests/Integration/Resources/DSC_SqlScript.Integration.Tests.ps1

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,132 @@ Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2016',
334334
}
335335
}
336336

337+
Context ('When using configuration <_>') -ForEach @(
338+
"$($script:dscResourceName)_RunSqlScriptAsWindowsUserWithDependencies_Config"
339+
) {
340+
BeforeAll {
341+
$configurationName = $_
342+
}
343+
344+
AfterEach {
345+
Wait-ForIdleLcm
346+
}
347+
348+
It 'Should compile and apply the MOF without throwing' {
349+
$configurationParameters = @{
350+
OutputPath = $TestDrive
351+
# The variable $ConfigurationData was dot-sourced above.
352+
ConfigurationData = $ConfigurationData
353+
}
354+
355+
$null = & $configurationName @configurationParameters
356+
357+
$startDscConfigurationParameters = @{
358+
Path = $TestDrive
359+
ComputerName = 'localhost'
360+
Wait = $true
361+
Verbose = $true
362+
Force = $true
363+
ErrorAction = 'Stop'
364+
}
365+
366+
$null = Start-DscConfiguration @startDscConfigurationParameters
367+
}
368+
369+
It 'Should be able to call Get-DscConfiguration without throwing' {
370+
$script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction 'Stop'
371+
}
372+
373+
It 'Should have set the resource and all the parameters should match' {
374+
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
375+
$_.ConfigurationName -eq $configurationName `
376+
-and $_.ResourceId -eq $resourceId
377+
}
378+
379+
<#
380+
This returns an array of string containing the result of the
381+
get scripts JSON output. The output looks like the below.
382+
383+
```
384+
JSON_F52E2B61-18A1-11d1-B105-00805F49916B
385+
-----------------------------------------
386+
[{"Name":"ScriptDatabase4"}]
387+
```
388+
389+
This could have been easier by just having this test
390+
$resourceCurrentState.GetResult | Should -Match 'ScriptDatabase4'
391+
but for making sure the returned data is actually usable, this
392+
parses the returned data to an object.
393+
#>
394+
$regularExpression = [regex] '\[.*\]'
395+
if ($regularExpression.IsMatch($resourceCurrentState.GetResult))
396+
{
397+
$regularExpressionMatch = $regularExpression.Match($resourceCurrentState.GetResult).Value
398+
}
399+
else
400+
{
401+
Write-Verbose -Message ('Unexpected output from Get-TargetResource: {0}' -f $resourceCurrentState.GetResult) -Verbose
402+
$regularExpressionMatch = '[{"Name":""}]'
403+
}
404+
405+
try
406+
{
407+
$resultObject = $regularExpressionMatch | ConvertFrom-Json
408+
}
409+
catch
410+
{
411+
Write-Verbose -Message ('Output from Get-TargetResource: {0}' -f $resourceCurrentState.GetResult) -Verbose
412+
Write-Verbose -Message ('Result from regular expression match: {0}' -f $regularExpressionMatch) -Verbose
413+
throw $_
414+
}
415+
416+
$resultObject.Name | Should -Be $ConfigurationData.AllNodes.Database4Name
417+
418+
$resourceCurrentState.ServerName | Should -Be $ConfigurationData.AllNodes.ServerName
419+
$resourceCurrentState.InstanceName | Should -Be $ConfigurationData.AllNodes.InstanceName
420+
$resourceCurrentState.GetFilePath | Should -Be $ConfigurationData.AllNodes.GetSqlScriptPath2
421+
$resourceCurrentState.TestFilePath | Should -Be $ConfigurationData.AllNodes.TestSqlScriptPath2
422+
$resourceCurrentState.SetFilePath | Should -Be $ConfigurationData.AllNodes.SetSqlScriptPath2
423+
}
424+
425+
It 'Should return $true when Test-DscConfiguration is run' {
426+
Test-DscConfiguration -Verbose -ErrorAction 'Stop' | Should -Be 'True'
427+
}
428+
}
429+
430+
Context ('When using configuration <_>') -ForEach @(
431+
"$($script:dscResourceName)_RemoveDatabase4_Config"
432+
) {
433+
BeforeAll {
434+
$configurationName = $_
435+
}
436+
437+
AfterEach {
438+
Wait-ForIdleLcm
439+
}
440+
441+
It 'Should compile and apply the MOF without throwing' {
442+
$configurationParameters = @{
443+
OutputPath = $TestDrive
444+
# The variable $ConfigurationData was dot-sourced above.
445+
ConfigurationData = $ConfigurationData
446+
}
447+
448+
$null = & $configurationName @configurationParameters
449+
450+
$startDscConfigurationParameters = @{
451+
Path = $TestDrive
452+
ComputerName = 'localhost'
453+
Wait = $true
454+
Verbose = $true
455+
Force = $true
456+
ErrorAction = 'Stop'
457+
}
458+
459+
$null = Start-DscConfiguration @startDscConfigurationParameters
460+
}
461+
}
462+
337463
Context ('When using configuration <_>') -ForEach @(
338464
"$($script:dscResourceName)_RemoveDatabase3_Config"
339465
) {

tests/Integration/Resources/DSC_SqlScript.config.ps1

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ else
2929
Database1Name = 'ScriptDatabase1'
3030
Database2Name = 'ScriptDatabase2'
3131
Database3Name = '$(DatabaseName)'
32+
Database4Name = 'ScriptDatabase4'
3233

3334
GetSqlScriptPath = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName())
3435
SetSqlScriptPath = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName())
3536
TestSqlScriptPath = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName())
3637

38+
GetSqlScriptPath2 = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName())
39+
SetSqlScriptPath2 = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName())
40+
TestSqlScriptPath2 = Join-Path -Path $env:SystemDrive -ChildPath ([System.IO.Path]::GetRandomFileName())
41+
3742
GetSqlScript = @'
3843
SELECT Name FROM sys.databases WHERE Name = '$(DatabaseName)' FOR JSON AUTO
3944
'@
@@ -294,6 +299,148 @@ Configuration DSC_SqlScript_RunSqlScriptWithVariablesDisabled_Config
294299
}
295300
}
296301

302+
<#
303+
.SYNOPSIS
304+
Creates the script files and runs the SQL script as a Windows User
305+
in the same configuration using DependsOn.
306+
#>
307+
Configuration DSC_SqlScript_RunSqlScriptAsWindowsUserWithDependencies_Config
308+
{
309+
Import-DscResource -ModuleName 'xPSDesiredStateConfiguration' -ModuleVersion '9.1.0'
310+
Import-DscResource -ModuleName 'SqlServerDsc'
311+
312+
node $AllNodes.NodeName
313+
{
314+
xScript 'CreateFile_GetSqlScript'
315+
{
316+
SetScript = {
317+
$Using:Node.GetSqlScript | Out-File -FilePath $Using:Node.GetSqlScriptPath2 -Encoding ascii -NoClobber -Force
318+
}
319+
320+
TestScript = {
321+
$getScriptResult = & ([ScriptBlock]::Create($GetScript))
322+
323+
return $getScriptResult.Result -eq $Using:Node.GetSqlScript
324+
}
325+
326+
GetScript = {
327+
$fileContent = $null
328+
329+
if (Test-Path -Path $Using:Node.GetSqlScriptPath2)
330+
{
331+
$fileContent = Get-Content -Path $Using:Node.GetSqlScriptPath2 -Raw
332+
}
333+
334+
return @{
335+
Result = $fileContent
336+
}
337+
}
338+
}
339+
340+
xScript 'CreateFile_TestSqlScript'
341+
{
342+
SetScript = {
343+
$Using:Node.TestSqlScript | Out-File -FilePath $Using:Node.TestSqlScriptPath2 -Encoding ascii -NoClobber -Force
344+
}
345+
346+
TestScript = {
347+
$getScriptResult = & ([ScriptBlock]::Create($GetScript))
348+
349+
return $getScriptResult.Result -eq $Using:Node.TestSqlScript
350+
}
351+
352+
GetScript = {
353+
$fileContent = $null
354+
355+
if (Test-Path -Path $Using:Node.TestSqlScriptPath2)
356+
{
357+
$fileContent = Get-Content -Path $Using:Node.TestSqlScriptPath2 -Raw
358+
}
359+
360+
return @{
361+
Result = $fileContent
362+
}
363+
}
364+
}
365+
366+
xScript 'CreateFile_SetSqlScript'
367+
{
368+
SetScript = {
369+
$Using:Node.SetSqlScript | Out-File -FilePath $Using:Node.SetSqlScriptPath2 -Encoding ascii -NoClobber -Force
370+
}
371+
372+
TestScript = {
373+
$getScriptResult = & ([ScriptBlock]::Create($GetScript))
374+
375+
return $getScriptResult.Result -eq $Using:Node.SetSqlScript
376+
}
377+
378+
GetScript = {
379+
$fileContent = $null
380+
381+
if (Test-Path -Path $Using:Node.SetSqlScriptPath2)
382+
{
383+
$fileContent = Get-Content -Path $Using:Node.SetSqlScriptPath2 -Raw
384+
}
385+
386+
return @{
387+
Result = $fileContent
388+
}
389+
}
390+
}
391+
392+
SqlScript 'Integration_Test'
393+
{
394+
Id = 'Integration_Test'
395+
ServerName = $Node.ServerName
396+
InstanceName = $Node.InstanceName
397+
398+
GetFilePath = $Node.GetSqlScriptPath2
399+
TestFilePath = $Node.TestSqlScriptPath2
400+
SetFilePath = $Node.SetSqlScriptPath2
401+
Variable = @(
402+
('DatabaseName={0}' -f $Node.Database4Name)
403+
)
404+
QueryTimeout = 30
405+
Encrypt = 'Optional'
406+
407+
PsDscRunAsCredential = New-Object `
408+
-TypeName System.Management.Automation.PSCredential `
409+
-ArgumentList @($Node.Admin_UserName, (ConvertTo-SecureString -String $Node.Admin_Password -AsPlainText -Force))
410+
411+
DependsOn = @(
412+
'[xScript]CreateFile_GetSqlScript'
413+
'[xScript]CreateFile_TestSqlScript'
414+
'[xScript]CreateFile_SetSqlScript'
415+
)
416+
}
417+
}
418+
}
419+
420+
<#
421+
.SYNOPSIS
422+
Remove the database created from the combined configuration test.
423+
#>
424+
Configuration DSC_SqlScript_RemoveDatabase4_Config
425+
{
426+
Import-DscResource -ModuleName 'SqlServerDsc'
427+
428+
node $AllNodes.NodeName
429+
{
430+
SqlDatabase 'RemoveDatabase4'
431+
{
432+
Ensure = 'Absent'
433+
ServerName = $Node.ServerName
434+
InstanceName = $Node.InstanceName
435+
Name = $Node.Database4Name
436+
437+
PsDscRunAsCredential = New-Object `
438+
-TypeName System.Management.Automation.PSCredential `
439+
-ArgumentList @($Node.Admin_UserName, (ConvertTo-SecureString -String $Node.Admin_Password -AsPlainText -Force))
440+
}
441+
}
442+
}
443+
297444
<#
298445
.SYNOPSIS
299446
Remove the database created from the DisabledVariables test.

0 commit comments

Comments
 (0)