1
1
Param (
2
- [switch ]$Batch
2
+ [switch ]$Batch ,
3
+ [string ]$Type = " SQLServer"
3
4
)
5
+ $commandName = $MyInvocation.MyCommand.Name.Replace (" .Tests.ps1" , " " )
4
6
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
+ ) {
31
10
BeforeAll {
11
+ . $PSScriptRoot \fixtures.ps1 - CommandName $Command - Type $Type - Batch $Batch
12
+
32
13
if ((Test-Path $workFolder ) -and $workFolder -like ' *.Tests.dbops' ) { Remove-Item $workFolder - Recurse }
33
14
$null = New-Item $workFolder - ItemType Directory - Force
34
15
$null = New-Item $unpackedFolder - ItemType Directory - Force
35
16
$packageName = New-DBOPackage - Path $packageName - ScriptPath $v1scripts - Build 1.0 - Force
36
17
$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
41
20
}
42
21
AfterAll {
43
- $null = Invoke-DBOQuery - SqlInstance $ script :mssqlInstance - Silent - Credential $ script :mssqlCredential - Database master - Query $dropDatabaseScript
22
+ $null = Invoke-DBOQuery @saConnectionParams - Query $dropDatabaseScript
44
23
if ((Test-Path $workFolder ) -and $workFolder -like ' *.Tests.dbops' ) { Remove-Item $workFolder - Recurse }
45
24
}
46
25
Context " testing deployment of extracted package" {
47
26
BeforeEach {
48
- $null = Invoke-DBOQuery - SqlInstance $ script :mssqlInstance - Silent - Credential $ script :mssqlCredential - Database $newDbName - InputFile $cleanupScript
27
+ Reset-TestDatabase
49
28
}
50
29
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 += @ {
55
32
SchemaVersionTable = $logTable
56
- Silent = $true
57
33
DeploymentMethod = ' NoTransaction'
58
34
}
59
35
$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
81
40
}
82
41
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
105
46
}
106
47
It " should deploy with no components loaded" {
107
48
$scriptBlock = {
108
49
param (
109
50
$Path ,
110
51
$DotSource ,
52
+ $Type ,
111
53
$Database
112
54
)
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
127
60
Get-ChildItem function:\ | Where-Object Name -eq Invoke-Deployment | Should - BeNullOrEmpty
128
61
}
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
130
63
$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
- # }
165
64
}
166
65
}
167
66
Context " $commandName whatif tests" {
168
67
BeforeAll {
169
- $null = Invoke-DBOQuery - SqlInstance $script :mssqlInstance - Silent - Credential $script :mssqlCredential - Database $newDbName - InputFile $cleanupScript
170
- }
171
- AfterAll {
68
+ Reset-TestDatabase
172
69
}
173
70
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
189
75
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
197
77
}
198
78
}
199
- }
79
+ }
0 commit comments