tests/Install-DBOPackage.Tests.ps1
Param ( [switch]$Batch ) if ($PSScriptRoot) { $commandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", ""); $here = $PSScriptRoot } else { $commandName = "_ManualExecution"; $here = (Get-Item . ).FullName } if (!$Batch) { # Is not a part of the global batch => import module #Explicitly import the module for testing Import-Module "$here\..\dbops.psd1" -Force; Get-DBOModuleFileList -Type internal | ForEach-Object { . $_.FullName } } else { # Is a part of a batch, output some eye-catching happiness Write-Host "Running $commandName tests" -ForegroundColor Cyan } . "$here\constants.ps1" $workFolder = Join-PSFPath -Normalize "$here\etc" "$commandName.Tests.dbops" $unpackedFolder = Join-Path $workFolder 'unpacked' $logTable = "testdeploymenthistory" $cleanupScript = Join-PSFPath -Normalize "$here\etc\sqlserver-tests\Cleanup.sql" $tranFailScripts = Join-PSFPath -Normalize "$here\etc\sqlserver-tests\transactional-failure" $v1scripts = Join-PSFPath -Normalize "$here\etc\sqlserver-tests\success\1.sql" $v1Journal = Get-Item $v1scripts | ForEach-Object { '1.0\' + $_.Name } $v2scripts = Join-PSFPath -Normalize "$here\etc\sqlserver-tests\success\2.sql" $v2Journal = Get-Item $v2scripts | ForEach-Object { '2.0\' + $_.Name } $verificationScript = Join-PSFPath -Normalize "$here\etc\sqlserver-tests\verification\select.sql" $packageName = Join-Path $workFolder "TempDeployment.zip" $packageNamev1 = Join-Path $workFolder "TempDeployment_v1.zip" $fullConfig = Join-PSFPath -Normalize "$here\etc\tmp_full_config.json" $fullConfigSource = Join-PSFPath -Normalize "$here\etc\full_config.json" $testPassword = 'TestPassword' $encryptedString = $testPassword | ConvertTo-SecureString -Force -AsPlainText | ConvertTo-EncryptedString $newDbName = "_test_$commandName" $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 $createDatabaseScript = 'IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = ''{0}'') BEGIN CREATE DATABASE [{0}]; END' -f $newDbName Describe "Install-DBOPackage integration tests" -Tag $commandName, IntegrationTests { BeforeAll { if ((Test-Path $workFolder) -and $workFolder -like '*.Tests.dbops') { Remove-Item $workFolder -Recurse } $null = New-Item $workFolder -ItemType Directory -Force $null = New-Item $unpackedFolder -ItemType Directory -Force (Get-Content $fullConfigSource -Raw) -replace 'replaceMe', $encryptedString | Out-File $fullConfig -Force $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database master -Query $dropDatabaseScript $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database master -Query $createDatabaseScript } AfterAll { if ((Test-Path $workFolder) -and $workFolder -like '*.Tests.dbops') { Remove-Item $workFolder -Recurse } if (Test-Path $fullConfig) { Remove-Item $fullConfig } $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database master -Query $dropDatabaseScript } Context "testing regular deployment with CreateDatabase specified" { BeforeAll { $p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force } It "should deploy version 1.0 to a new database using -CreateDatabase switch" { # Drop database and allow the function to create it $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database master -Query $dropDatabaseScript $testResults = Install-DBOPackage $p1 -CreateDatabase -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v1Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Configuration.CreateDatabase | Should Be $true $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog "Created database $newDbName" | Should BeIn $testResults.DeploymentLog #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name } } Context "testing transactional deployment" { BeforeAll { $null = New-DBOPackage -ScriptPath $tranFailScripts -Name $packageName -Build 1.0 -Force } AfterAll { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } BeforeEach { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } It "Should throw an error and not create any objects" { #Running package try { $null = Install-DBOPackage $packageName -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -DeploymentMethod SingleTransaction -Silent } catch { $testResults = $_ } $testResults.Exception.Message | Should Be "There is already an object named 'a' in the database." #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should Not BeIn $testResults.name 'a' | Should Not BeIn $testResults.name 'b' | Should Not BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name } } Context "testing non transactional deployment" { BeforeAll { $null = New-DBOPackage -ScriptPath $tranFailScripts -Name $packageName -Build 1.0 -Force } AfterAll { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } It "Should throw an error and create one object" { #Running package try { $null = Install-DBOPackage $packageName -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -DeploymentMethod NoTransaction -Silent } catch { $testResults = $_ } $testResults.Exception.Message | Should Be "There is already an object named 'a' in the database." #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should Not BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name } } Context "testing regular deployment" { BeforeAll { $p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force $p1 = Add-DBOBuild -ScriptPath $v2scripts -Name $p1 -Build 2.0 #versions should not be sorted by default - creating a package where 1.0 is the second build $p3 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv3" -Build 2.0 -Force $null = Add-DBOBuild -ScriptPath $v2scripts -Name $p3 -Build 1.0 $outputFile = Join-PSFPath -Normalize "$workFolder\log.txt" $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } It "should deploy version 1.0" { $testResults = Install-DBOPackage $p1 -Build '1.0' -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v1Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog $output = Get-Content "$workFolder\log.txt" | Select-Object -Skip 1 $output | Should Be (Get-Content "$here\etc\log1.txt") #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name } It "should re-deploy version 1.0 pipelining a string" { $testResults = "$workFolder\pv1.zip" | Install-DBOPackage -Build '1.0' -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should BeNullOrEmpty $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'No new scripts need to be executed - completing.' | Should BeIn $testResults.DeploymentLog 'No new scripts need to be executed - completing.' | Should BeIn (Get-Content "$workFolder\log.txt" | Select-Object -Skip 1) #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name } It "should deploy version 2.0 using pipelined Get-DBOPackage" { $testResults = Get-DBOPackage "$workFolder\pv1.zip" | Install-DBOPackage -Build '1.0', '2.0' -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v2Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog $output = Get-Content "$workFolder\log.txt" | Select-Object -Skip 1 $output | Should Be (Get-Content "$here\etc\log2.txt") #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should BeIn $testResults.name 'd' | Should BeIn $testResults.name } It "should re-deploy version 2.0 using pipelined FileSystemObject" { $testResults = Get-Item "$workFolder\pv1.zip" | Install-DBOPackage -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should BeNullOrEmpty $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'No new scripts need to be executed - completing.' | Should BeIn $testResults.DeploymentLog 'No new scripts need to be executed - completing.' | Should BeIn (Get-Content "$workFolder\log.txt" | Select-Object -Skip 1) #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should BeIn $testResults.name 'd' | Should BeIn $testResults.name } It "should deploy in a reversed order: 2.0 before 1.0" { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript $testResults = Install-DBOPackage "$workFolder\pv3.zip" -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be (@((Get-Item $v1scripts).Name | ForEach-Object { "2.0\$_" }), ((Get-Item $v2scripts).Name | ForEach-Object { "1.0\$_" })) $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv3.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should BeIn $testResults.name 'd' | Should BeIn $testResults.name } } Context "testing timeouts" { BeforeAll { $file = Join-PSFPath -Normalize "$workFolder\delay.sql" "WAITFOR DELAY '00:00:03'; PRINT ('Successful!')" | Out-File $file $null = New-DBOPackage -ScriptPath $file -Name "$workFolder\delay" -Build 1.0 -Force -Configuration @{ ExecutionTimeout = 2 } } BeforeEach { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } It "should throw timeout error " { try { $null = Install-DBOPackage "$workFolder\delay.zip" -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent } catch { $testResults = $_ } $testResults | Should Not Be $null $testResults.Exception.Message | Should BeLike '*Timeout Expired.*' $output = Get-Content "$workFolder\log.txt" -Raw $output | Should BeLike '*Timeout Expired*' $output | Should Not BeLike '*Successful!*' } It "should successfully run within specified timeout" { $testResults = Install-DBOPackage "$workFolder\delay.zip" -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent -ExecutionTimeout 6 $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be '1.0\delay.sql' $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\delay.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterThan 3000 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterThan $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog $output = Get-Content "$workFolder\log.txt" -Raw $output | Should Not BeLike '*Timeout Expired*' $output | Should BeLike '*Successful!*' } It "should successfully run with infinite timeout" { $testResults = Install-DBOPackage "$workFolder\delay.zip" -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent -ExecutionTimeout 0 $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be '1.0\delay.sql' $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\delay.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog $output = Get-Content "$workFolder\log.txt" -Raw $output | Should Not BeLike '*Timeout Expired*' $output | Should BeLike '*Successful!*' } } Context "$commandName whatif tests" { BeforeAll { $null = New-DBOPackage -ScriptPath $v1scripts -Name $packageNamev1 -Build 1.0 $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } AfterAll { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript Remove-Item $packageNamev1 } It "should deploy nothing" { $testResults = Install-DBOPackage $packageNamev1 -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -SchemaVersionTable $logTable -Silent -WhatIf $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.Scripts.Name | Should Be $v1Journal $testResults.SourcePath | Should Be $packageNamev1 $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime "No deployment performed - WhatIf mode." | Should BeIn $testResults.DeploymentLog $v1Journal | ForEach-Object { "$_ would have been executed - WhatIf mode." } | Should BeIn $testResults.DeploymentLog #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should Not BeIn $testResults.name 'a' | Should Not BeIn $testResults.name 'b' | Should Not BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name } } Context "testing regular deployment with configuration overrides" { BeforeAll { $p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force -ConfigurationFile $fullConfig $p2 = New-DBOPackage -ScriptPath $v2scripts -Name "$workFolder\pv2" -Build 2.0 -Force -Configuration @{ SqlInstance = 'nonexistingServer' Database = 'nonexistingDB' SchemaVersionTable = 'nonexistingSchema.nonexistinTable' DeploymentMethod = "SingleTransaction" } $outputFile = "$workFolder\log.txt" $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } It "should deploy version 1.0 using -Configuration file override" { $configFile = "$workFolder\config.custom.json" @{ SqlInstance = $script:mssqlInstance Database = $newDbName SchemaVersionTable = $logTable Silent = $true DeploymentMethod = 'NoTransaction' } | ConvertTo-Json -Depth 2 | Out-File $configFile -Force $testResults = Install-DBOPackage "$workFolder\pv1.zip" -Configuration $configFile -OutputFile "$workFolder\log.txt" -Credential $script:mssqlCredential $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v1Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog $output = Get-Content "$workFolder\log.txt" | Select-Object -Skip 1 $output | Should Be (Get-Content "$here\etc\log1.txt") #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name } It "should deploy version 2.0 using -Configuration object override" { $testResults = Install-DBOPackage "$workFolder\pv2.zip" -Configuration @{ SqlInstance = $script:mssqlInstance Credential = $script:mssqlCredential Database = $newDbName SchemaVersionTable = $logTable Silent = $true DeploymentMethod = 'NoTransaction' } -OutputFile "$workFolder\log.txt" $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v2Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv2.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog $output = Get-Content "$workFolder\log.txt" | Select-Object -Skip 1 $output | Should Be (Get-Content "$here\etc\log2.txt") #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should BeIn $testResults.name 'd' | Should BeIn $testResults.name } } Context "testing deployment without specifying SchemaVersion table" { BeforeAll { $p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force $p2 = New-DBOPackage -ScriptPath $v2scripts -Name "$workFolder\pv2" -Build 2.0 -Force $outputFile = "$workFolder\log.txt" $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } AfterAll { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -Query "IF OBJECT_ID('SchemaVersions') IS NOT NULL DROP TABLE SchemaVersions" } It "should deploy version 1.0" { $before = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $rowsBefore = ($before | Measure-Object).Count $testResults = Install-DBOPackage "$workFolder\pv1.zip" -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v1Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be 'SchemaVersions' $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript 'SchemaVersions' | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name ($testResults | Measure-Object).Count | Should Be ($rowsBefore + 3) } It "should deploy version 2.0" { $before = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $rowsBefore = ($before | Measure-Object).Count $testResults = Install-DBOPackage "$workFolder\pv2.zip" -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v2Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv2.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be 'SchemaVersions' $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript 'SchemaVersions' | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should BeIn $testResults.name 'd' | Should BeIn $testResults.name ($testResults | Measure-Object).Count | Should Be ($rowsBefore + 2) } } Context "testing deployment with no history`: SchemaVersion is null" { BeforeEach { $null = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } AfterEach { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -Query "IF OBJECT_ID('SchemaVersions') IS NOT NULL DROP TABLE SchemaVersions" } It "should deploy version 1.0 without creating SchemaVersions" { $before = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $rowsBefore = ($before | Measure-Object).Count $testResults = Install-DBOPackage "$workFolder\pv1.zip" -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -Silent -SchemaVersionTable $null $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v1Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should BeNullOrEmpty $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript 'SchemaVersions' | Should Not BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name ($testResults | Measure-Object).Count | Should Be ($rowsBefore + 2) } } Context "testing deployment with defined schema" { BeforeEach { $null = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -Query "CREATE SCHEMA testschema" } AfterEach { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -Query "IF OBJECT_ID('SchemaVersions') IS NOT NULL DROP TABLE SchemaVersions" } It "should deploy version 1.0 into testschema" { $before = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $rowsBefore = ($before | Measure-Object).Count $testResults = Install-DBOPackage "$workFolder\pv1.zip" -SqlInstance $script:mssqlInstance -Credential $script:mssqlCredential -Database $newDbName -Silent -Schema testschema $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v1Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be 'SchemaVersions' $testResults.Configuration.Schema | Should Be 'testschema' $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog #Verifying objects $after = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $after | Where-Object name -eq 'SchemaVersions' | Select-Object -ExpandProperty schema | Should Be 'testschema' # disabling for SQL Server, but leaving for other rdbms in perspective # $testResults | Where-Object Name -eq 'a' | Select-Object -ExpandProperty schema | Should Be 'testschema' # $testResults | Where-Object Name -eq 'b' | Select-Object -ExpandProperty schema | Should Be 'testschema' ($after | Measure-Object).Count | Should Be ($rowsBefore + 3) } } Context "testing deployment using variables in config" { BeforeAll { $p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force -Configuration @{SqlInstance = '#{srv}'; Database = '#{db}'} $outputFile = "$workFolder\log.txt" $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } AfterAll { $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -Query "IF OBJECT_ID('SchemaVersions') IS NOT NULL DROP TABLE SchemaVersions" } It "should deploy version 1.0" { $before = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $rowsBefore = ($before | Measure-Object).Count $testResults = Install-DBOPackage "$workFolder\pv1.zip" -Credential $script:mssqlCredential -Variables @{srv = $script:mssqlInstance; db = $newDbName} -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v1Journal $testResults.SqlInstance | Should Be $script:mssqlInstance $testResults.Database | Should Be $newDbName $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog $output = Get-Content "$workFolder\log.txt" | Select-Object -Skip 1 $output | Should Be (Get-Content "$here\etc\log1.txt") #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name ($testResults | Measure-Object).Count | Should Be ($rowsBefore + 3) } } Context "testing deployment with custom connection string" { BeforeAll { $p1 = New-DBOPackage -ScriptPath $v1scripts -Name "$workFolder\pv1" -Build 1.0 -Force $null = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $cleanupScript } It "should deploy version 1.0" { $connectionString = "Server=$script:mssqlInstance;Database=$newDbName;" if ($script:mssqlCredential) { $connectionString += "User ID=$($script:mssqlCredential.UserName);Password=$($script:mssqlCredential.GetNetworkCredential().Password)" } else { $connectionString += "Trusted_Connection=True" } $testResults = Install-DBOPackage "$workFolder\pv1.zip" -ConnectionString $connectionString -SqlInstance willBeIgnored -Database IgnoredAsWell -SchemaVersionTable $logTable -OutputFile "$workFolder\log.txt" -Silent $testResults.Successful | Should Be $true $testResults.Scripts.Name | Should Be $v1Journal $testResults.SqlInstance | Should BeNullOrEmpty $testResults.Database | Should BeNullOrEmpty $testResults.SourcePath | Should Be (Join-PSFPath -Normalize "$workFolder\pv1.zip") $testResults.ConnectionType | Should Be 'SQLServer' $testResults.Configuration.SchemaVersionTable | Should Be $logTable $testResults.Error | Should BeNullOrEmpty $testResults.Duration.TotalMilliseconds | Should -BeGreaterOrEqual 0 $testResults.StartTime | Should Not BeNullOrEmpty $testResults.EndTime | Should Not BeNullOrEmpty $testResults.EndTime | Should -BeGreaterOrEqual $testResults.StartTime 'Upgrade successful' | Should BeIn $testResults.DeploymentLog $output = Get-Content "$workFolder\log.txt" | Select-Object -Skip 1 $output | Should Be (Get-Content "$here\etc\log1.txt") #Verifying objects $testResults = Invoke-DBOQuery -SqlInstance $script:mssqlInstance -Silent -Credential $script:mssqlCredential -Database $newDbName -InputFile $verificationScript $logTable | Should BeIn $testResults.name 'a' | Should BeIn $testResults.name 'b' | Should BeIn $testResults.name 'c' | Should Not BeIn $testResults.name 'd' | Should Not BeIn $testResults.name } } } |