Skip to content

Commit 7f34a71

Browse files
adding fixtures file
1 parent a4dbef0 commit 7f34a71

File tree

5 files changed

+197
-161
lines changed

5 files changed

+197
-161
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,7 @@ MigrationBackup/
368368
.ionide/
369369

370370
# Fody - auto-generated XML schema
371-
FodyWeavers.xsd
371+
FodyWeavers.xsd
372+
373+
# build folder
374+
tests/build

dbops.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@{
22

33
# Script module or binary module file associated with this manifest
4-
RootModule = 'dbops.psm1'
4+
RootModule = '.\dbops.psm1'
55

66
# Version number of this module.
77
ModuleVersion = '0.7.1'
@@ -53,10 +53,10 @@
5353
ScriptsToProcess = @()
5454

5555
# Type files (.ps1xml) to be loaded when importing this module
56-
TypesToProcess = @('internal\xml\dbops.types.ps1xml')
56+
TypesToProcess = @()
5757

5858
# Format files (.ps1xml) to be loaded when importing this module
59-
FormatsToProcess = @('internal\xml\dbops.format.ps1xml')
59+
FormatsToProcess = @()
6060

6161
# Modules to import as nested modules of the module specified in
6262
# ModuleToProcess

dbops.psm1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ foreach ($bin in (Get-DBOModuleFileList -Type Libraries -Edition $PSVersionTable
55
if ($PSVersionTable.Platform -eq 'Win32NT') {
66
Unblock-File -Path $bin -ErrorAction SilentlyContinue
77
}
8-
Add-Type -Path $bin
8+
Add-Type -Path $bin -ErrorAction SilentlyContinue
99
}
1010

1111
'Functions', 'Internal' | ForEach-Object {
1212
foreach ($function in (Get-DBOModuleFileList -Type $_).FullName) {
1313
. $function
1414
}
1515
}
16+
# import type and format data
17+
if (!(Get-TypeData -TypeName DBOpsPackageFile)) {
18+
Update-TypeData -Path $PSScriptRoot\internal\xml\dbops.types.ps1xml
19+
}
20+
if (!(Get-FormatData -TypeName DBOpsPackageFile)) {
21+
Update-FormatData -Path $PSScriptRoot\internal\xml\dbops.format.ps1xml
22+
}
1623

1724
# defining validations
1825

tests/functional/deploy.ps1.Tests.ps1

Lines changed: 36 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,199 +1,79 @@
11
Param (
2-
[switch]$Batch
2+
[switch]$Batch,
3+
[string]$Type = "SQLServer"
34
)
5+
$commandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
46

5-
if ($PSScriptRoot) { $commandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", ""); $here = $PSScriptRoot }
6-
else { $commandName = "_ManualExecution"; $here = (Get-Item . ).FullName }
7-
8-
if (!$Batch) {
9-
# Is not a part of the global batch => import module
10-
#Explicitly import the module for testing
11-
Import-Module "$here\..\dbops.psd1" -Force; Get-DBOModuleFileList -Type internal | ForEach-Object { . $_.FullName }
12-
}
13-
else {
14-
# Is a part of a batch, output some eye-catching happiness
15-
Write-Host "Running $commandName tests" -ForegroundColor Cyan
16-
}
17-
18-
. "$here\constants.ps1"
19-
20-
$workFolder = Join-PSFPath -Normalize "$here\etc" "$commandName.Tests.dbops"
21-
$unpackedFolder = Join-PSFPath -Normalize $workFolder 'unpacked'
22-
$logTable = "testdeploymenthistory"
23-
$cleanupScript = Join-PSFPath -Normalize "$here\etc\sqlserver-tests\Cleanup.sql"
24-
$v1scripts = Join-PSFPath -Normalize "$here\etc\sqlserver-tests\success\1.sql"
25-
$v1Journal = Get-Item $v1scripts | ForEach-Object { '1.0\' + $_.Name }
26-
$verificationScript = Join-PSFPath -Normalize "$here\etc\sqlserver-tests\verification\select.sql"
27-
$packageName = Join-PSFPath -Normalize $workFolder 'TempDeployment.zip'
28-
$newDbName = "_test_$commandName"
29-
30-
Describe "deploy.ps1 integration tests" -Tag $commandName, IntegrationTests {
7+
Describe "<command> deploy.ps1 integration tests" -Tag $commandName, IntegrationTests -ForEach @(
8+
@{ Batch = $Batch; $Type = "SQLServer"; Command = $commandName }
9+
) {
3110
BeforeAll {
11+
. $PSScriptRoot\fixtures.ps1 -CommandName $Command -Type $Type -Batch $Batch
12+
3213
if ((Test-Path $workFolder) -and $workFolder -like '*.Tests.dbops') { Remove-Item $workFolder -Recurse }
3314
$null = New-Item $workFolder -ItemType Directory -Force
3415
$null = New-Item $unpackedFolder -ItemType Directory -Force
3516
$packageName = New-DBOPackage -Path $packageName -ScriptPath $v1scripts -Build 1.0 -Force
3617
$null = Expand-Archive -Path $packageName -DestinationPath $workFolder -Force
37-
$dropDatabaseScript = 'IF EXISTS (SELECT * FROM sys.databases WHERE name = ''{0}'') BEGIN ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{0}]; END' -f $newDbName
38-
$createDatabaseScript = 'IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = ''{0}'') BEGIN CREATE DATABASE [{0}]; END' -f $newDbName
39-
$null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database master -Query $dropDatabaseScript
40-
$null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database master -Query $createDatabaseScript
18+
$null = Invoke-DBOQuery @saConnectionParams -Query $dropDatabaseScript
19+
$null = Invoke-DBOQuery @saConnectionParams -Query $createDatabaseScript
4120
}
4221
AfterAll {
43-
$null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database master -Query $dropDatabaseScript
22+
$null = Invoke-DBOQuery @saConnectionParams -Query $dropDatabaseScript
4423
if ((Test-Path $workFolder) -and $workFolder -like '*.Tests.dbops') { Remove-Item $workFolder -Recurse }
4524
}
4625
Context "testing deployment of extracted package" {
4726
BeforeEach {
48-
$null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript
27+
Reset-TestDatabase
4928
}
5029
It "should deploy with a -Configuration parameter" {
51-
$deploymentConfig = @{
52-
SqlInstance = $script:mssqlInstance
53-
Credential = $script:mssqlCredential
54-
Database = $newDbName
30+
$deploymentConfig = $dbConnectionParams.Clone()
31+
$deploymentConfig += @{
5532
SchemaVersionTable = $logTable
56-
Silent = $true
5733
DeploymentMethod = 'NoTransaction'
5834
}
5935
$testResults = & $workFolder\deploy.ps1 -Configuration $deploymentConfig
60-
$testResults.Successful | Should Be $true
61-
$testResults.Scripts.Name | Should Be $v1Journal
62-
$testResults.SqlInstance | Should Be $script:mssqlInstance
63-
$testResults.Database | Should Be $newDbName
64-
$testResults.SourcePath | Should Be $workFolder
65-
$testResults.ConnectionType | Should Be 'SQLServer'
66-
$testResults.Configuration.SchemaVersionTable | Should Be $logTable
67-
$testResults.Error | Should BeNullOrEmpty
68-
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
69-
$testResults.StartTime | Should Not BeNullOrEmpty
70-
$testResults.EndTime | Should Not BeNullOrEmpty
71-
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
72-
'Upgrade successful' | Should BeIn $testResults.DeploymentLog
73-
74-
#Verifying objects
75-
$testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript
76-
$logTable | Should BeIn $testResults.name
77-
'a' | Should BeIn $testResults.name
78-
'b' | Should BeIn $testResults.name
79-
'c' | Should Not BeIn $testResults.name
80-
'd' | Should Not BeIn $testResults.name
36+
$testResults.SourcePath | Should -Be $workFolder
37+
$testResults.Configuration.SchemaVersionTable | Should -Be $logTable
38+
$testResults | Test-DeploymentOutput -Version 1
39+
$testResults | Test-DeploymentState -Version 1 -HasJournal
8140
}
8241
It "should deploy with a set of parameters" {
83-
$testResults = & $workFolder\deploy.ps1 -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent
84-
$testResults.Successful | Should Be $true
85-
$testResults.Scripts.Name | Should Be $v1Journal
86-
$testResults.SqlInstance | Should Be $script:mssqlInstance
87-
$testResults.Database | Should Be $newDbName
88-
$testResults.SourcePath | Should Be $workFolder
89-
$testResults.ConnectionType | Should Be 'SQLServer'
90-
$testResults.Configuration.SchemaVersionTable | Should Be $logTable
91-
$testResults.Error | Should BeNullOrEmpty
92-
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
93-
$testResults.StartTime | Should Not BeNullOrEmpty
94-
$testResults.EndTime | Should Not BeNullOrEmpty
95-
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
96-
'Upgrade successful' | Should BeIn $testResults.DeploymentLog
97-
98-
#Verifying objects
99-
$testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript
100-
$logTable | Should BeIn $testResults.name
101-
'a' | Should BeIn $testResults.name
102-
'b' | Should BeIn $testResults.name
103-
'c' | Should Not BeIn $testResults.name
104-
'd' | Should Not BeIn $testResults.name
42+
$testResults = & $workFolder\deploy.ps1 @dbConnectionParams -SchemaVersionTable $logTable
43+
$testResults.Configuration.SchemaVersionTable | Should -Be $logTable
44+
$testResults | Test-DeploymentOutput -Version 1
45+
$testResults | Test-DeploymentState -Version 1 -HasJournal
10546
}
10647
It "should deploy with no components loaded" {
10748
$scriptBlock = {
10849
param (
10950
$Path,
11051
$DotSource,
52+
$Type,
11153
$Database
11254
)
113-
. $DotSource
114-
$testResults = & $Path\deploy.ps1 -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $Database -Silent
115-
$testResults.Successful | Should -Be $true
116-
$testResults.Scripts.Name | Should -Not -BeNullOrEmpty
117-
$testResults.SqlInstance | Should -Be $script:mssqlInstance
118-
$testResults.Database | Should Be $Database
119-
$testResults.SourcePath | Should Be $Path
120-
$testResults.ConnectionType | Should Be 'SQLServer'
121-
$testResults.Configuration.SchemaVersionTable | Should Be 'SchemaVersions'
122-
$testResults.Error | Should BeNullOrEmpty
123-
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
124-
$testResults.StartTime | Should -Not -BeNullOrEmpty
125-
$testResults.EndTime | Should -Not -BeNullOrEmpty
126-
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
55+
. $DotSource -Type $Type -Batch $true
56+
$testResults = & $Path\deploy.ps1 @dbConnectionParams
57+
$testResults | Test-DeploymentOutput -Version 1
58+
$testResults.Configuration.SchemaVersionTable | Should -Be 'SchemaVersions'
59+
$testResults.SourcePath | Should -Be $Path
12760
Get-ChildItem function:\ | Where-Object Name -eq Invoke-Deployment | Should -BeNullOrEmpty
12861
}
129-
$job = Start-Job -ScriptBlock $scriptBlock -ArgumentList $workFolder, "$here\constants.ps1", $newDbName
62+
$job = Start-Job -ScriptBlock $scriptBlock -ArgumentList $workFolder, "$PSScriptRoot\fixtures.ps1", $Type, $newDbName
13063
$job | Wait-Job | Receive-Job -ErrorAction Stop
131-
# # Get modules
132-
# $modules = Get-Module Pester | Select-Object -ExpandProperty Path
133-
# $sessionstate = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
134-
# foreach ($modulePath in $modules) {
135-
# $sessionstate.ImportPSModule($modulePath)
136-
# }
137-
# # Create runspace pool
138-
# $runspacepool = [runspacefactory]::CreateRunspacePool(1, 5, $sessionstate, $Host)
139-
# $runspacepool.Open()
140-
# $powershell = [powershell]::Create()
141-
# $params = @{
142-
# Path = $workFolder
143-
# Instance = $script:mssqlInstance
144-
# Credential = $script:mssqlCredential
145-
# Database = $newDbName
146-
# }
147-
# [void]$powershell.AddScript($scriptBlock).AddParameters($params)
148-
# $powershell.RunspacePool = $runspacepool
149-
# try {
150-
# $handle = $powershell.BeginInvoke()
151-
# $cycles = 0
152-
# do { Start-Sleep 1; $cycles++ } while (-not $handle.IsCompleted -and $cycles -lt 5)
153-
# if ($powershell.Streams.Error.Count -gt 0) {
154-
# throw $powershell.Streams.Error[0]
155-
# }
156-
# $powershell.EndInvoke($handle)
157-
# }
158-
# catch {
159-
# throw $_
160-
# }
161-
# finally {
162-
# $powershell.Dispose()
163-
# $runspacepool.Close()
164-
# }
16564
}
16665
}
16766
Context "$commandName whatif tests" {
16867
BeforeAll {
169-
$null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript
170-
}
171-
AfterAll {
68+
Reset-TestDatabase
17269
}
17370
It "should deploy nothing" {
174-
$testResults = & $workFolder\deploy.ps1 -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent -WhatIf
175-
$testResults.Successful | Should Be $true
176-
$testResults.Scripts.Name | Should Be $v1Journal
177-
$testResults.SqlInstance | Should Be $script:mssqlInstance
178-
$testResults.Database | Should Be $newDbName
179-
$testResults.SourcePath | Should Be $workFolder
180-
$testResults.ConnectionType | Should Be 'SQLServer'
181-
$testResults.Configuration.SchemaVersionTable | Should Be $logTable
182-
$testResults.Error | Should BeNullOrEmpty
183-
$testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0
184-
$testResults.StartTime | Should Not BeNullOrEmpty
185-
$testResults.EndTime | Should Not BeNullOrEmpty
186-
$testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime
187-
"No deployment performed - WhatIf mode." | Should BeIn $testResults.DeploymentLog
188-
$v1Journal | ForEach-Object { "$_ would have been executed - WhatIf mode." } | Should BeIn $testResults.DeploymentLog
71+
$testResults = & $workFolder\deploy.ps1 @dbConnectionParams -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -WhatIf
72+
$testResults | Test-DeploymentOutput -Version 1 -WhatIf
73+
$testResults.Configuration.SchemaVersionTable | Should -Be $logTable
74+
$v1Journal | ForEach-Object { "$_ would have been executed - WhatIf mode." } | Should -BeIn $testResults.DeploymentLog
18975

190-
#Verifying objects
191-
$testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript
192-
$logTable | Should Not BeIn $testResults.name
193-
'a' | Should Not BeIn $testResults.name
194-
'b' | Should Not BeIn $testResults.name
195-
'c' | Should Not BeIn $testResults.name
196-
'd' | Should Not BeIn $testResults.name
76+
$testResults | Test-DeploymentState -Version 0
19777
}
19878
}
199-
}
79+
}

0 commit comments

Comments
 (0)