Skip to content

Commit

Permalink
Update tests to be able to run them outside of AppVeyor (part 3) (#9426)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan authored Oct 5, 2024
1 parent 49f7514 commit 90ec329
Show file tree
Hide file tree
Showing 25 changed files with 81 additions and 72 deletions.
3 changes: 3 additions & 0 deletions tests/Add-DbaDbMirrorMonitor.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
BeforeAll {
$null = Remove-DbaDbMirrorMonitor -SqlInstance $script:instance2 -WarningAction SilentlyContinue
}
AfterAll {
$null = Remove-DbaDbMirrorMonitor -SqlInstance $script:instance2 -WarningAction SilentlyContinue
}

It "adds the mirror monitor" {
$results = Add-DbaDbMirrorMonitor -SqlInstance $script:instance2 -WarningAction SilentlyContinue
Expand Down
11 changes: 7 additions & 4 deletions tests/Copy-DbaAgentJob.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
$destjobs = Get-DbaAgentJob -SqlInstance $script:instance3
}
AfterAll {
$null = Remove-DbaAgentJob -SqlInstance $script:instance2, $script:instance3 -Job dbatoolsci_copyjob, dbatoolsci_copyjob_disabled -Confirm:$false
$null = Remove-DbaAgentJob -SqlInstance $script:instance2 -Job dbatoolsci_copyjob, dbatoolsci_copyjob_disabled -Confirm:$false
$null = Remove-DbaAgentJob -SqlInstance $script:instance3 -Job dbatoolsci_copyjob, dbatoolsci_copyjob_disabled -Confirm:$false
}

Context "Command copies jobs properly" {
$results = Copy-DbaAgentJob -Source $script:instance2 -Destination $script:instance3 -Job dbatoolsci_copyjob

It "returns one success" {
$results.Name -eq "dbatoolsci_copyjob"
$results.Status -eq "Successful"
$results.Name | Should -Be "dbatoolsci_copyjob"
$results.Status | Should -Be "Successful"
}

It "did not copy dbatoolsci_copyjob_disabled" {
Expand All @@ -38,7 +39,9 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" {

It "disables jobs when requested" {
(Get-DbaAgentJob -SqlInstance $script:instance2 -Job dbatoolsci_copyjob_disabled).Enabled
$results = Copy-DbaAgentJob -Source $script:instance2 -Destination $script:instance3 -Job dbatoolsci_copyjob_disabled -DisableOnSource -DisableOnDestination
$results = Copy-DbaAgentJob -Source $script:instance2 -Destination $script:instance3 -Job dbatoolsci_copyjob_disabled -DisableOnSource -DisableOnDestination -Force
$results.Name | Should -Be "dbatoolsci_copyjob_disabled"
$results.Status | Should -Be "Successful"
(Get-DbaAgentJob -SqlInstance $script:instance2 -Job dbatoolsci_copyjob_disabled).Enabled | Should -Be $false
(Get-DbaAgentJob -SqlInstance $script:instance3 -Job dbatoolsci_copyjob_disabled).Enabled | Should -Be $false
}
Expand Down
1 change: 1 addition & 0 deletions tests/Copy-DbaBackupDevice.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ if (-not $env:appveyor) {
} catch {
# don't care
}
Get-ChildItem -Path $backupfilename | Remove-Item
}

$results = Copy-DbaBackupDevice -Source $script:instance1 -Destination $script:instance2 -WarningVariable warn -WarningAction SilentlyContinue 3> $null
Expand Down
3 changes: 3 additions & 0 deletions tests/Copy-DbaDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
$results[0].DestinationDatabase | Should -Be $newname
$files = Get-DbaDbFile -Sqlinstance $script:instance3 -Database $newname
($files.PhysicalName -like "*$newname*").count | Should -Be $files.count
$null = Remove-DbaDatabase -SqlInstance $script:instance3 -Database $newname
}

It "Should prefix databasename and files" {
Expand All @@ -214,12 +215,14 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
$results[0].DestinationDatabase | Should -Be "$prefix$backuprestoredb"
$files = Get-DbaDbFile -Sqlinstance $script:instance3 -Database "$prefix$backuprestoredb"
($files.PhysicalName -like "*$prefix$backuprestoredb*").count | Should -Be $files.count
$null = Remove-DbaDatabase -SqlInstance $script:instance3 -Database "$prefix$backuprestoredb"
}

$null = Restore-DbaDatabase -SqlInstance $script:instance2 -path $script:appveyorlabrepo\RestoreTimeClean2016 -useDestinationDefaultDirectories
It "Should warn and exit if newname and >1 db specified" {
$null = Copy-DbaDatabase -Source $script:instance2 -Destination $script:instance3 -Database $backuprestoredb, RestoreTimeClean -DetachAttach -Reattach -NewName warn -WarningVariable warnvar 3> $null
$warnvar | Should -BeLike "*Cannot use NewName when copying multiple databases"
$null = Remove-DbaDatabase -SqlInstance $script:instance2 -Database RestoreTimeClean
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Export-DbaBinaryFile.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
BeforeEach {
$db = Get-DbaDatabase -SqlInstance $script:instance2 -Database tempdb
$null = $db.Query("CREATE TABLE [dbo].[BunchOFilezz]([FileName123] [nvarchar](50) NULL, [TheFile123] [image] NULL)")
$null = Import-DbaBinaryFile -SqlInstance $script:instance2 -Database tempdb -Table BunchOFilezz -FilePath $script:appveyorlabrepo\azure\adalsql.msi -Verbose
$null = Import-DbaBinaryFile -SqlInstance $script:instance2 -Database tempdb -Table BunchOFilezz -FilePath $script:appveyorlabrepo\azure\adalsql.msi
$null = Get-ChildItem $script:appveyorlabrepo\certificates | Import-DbaBinaryFile -SqlInstance $script:instance2 -Database tempdb -Table BunchOFilezz
}
AfterEach {
Expand Down
5 changes: 4 additions & 1 deletion tests/Export-DbaXESessionTemplate.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
}

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$null = Get-DbaXESession -SqlInstance $script:instance2 -Session 'Profiler TSQL Duration' | Remove-DbaXESession
}
AfterAll {
$null = Get-DbaXESession -SqlInstance $script:instance2 -Session db_ola_health | Remove-DbaXESession
$null = Get-DbaXESession -SqlInstance $script:instance2 -Session 'Profiler TSQL Duration' | Remove-DbaXESession
Remove-Item -Path 'C:\windows\temp\Profiler TSQL Duration.xml' -ErrorAction SilentlyContinue
}
Context "Test Importing Session Template" {
Expand Down
42 changes: 26 additions & 16 deletions tests/Find-DbaAgentJob.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,28 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
}
}

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob' -OwnerLogin 'sa'
$null = New-DbaAgentJobStep -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob' -StepId 1 -StepName 'dbatoolsci Failed' -Subsystem TransactSql -SubsystemServer $script:instance2 -Command "RAISERROR (15600,-1,-1, 'dbatools_error');" -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -DatabaseUser sa -RetryAttempts 1 -RetryInterval 2
$null = Start-DbaAgentJob -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob'
$null = New-DbaAgentJobCategory -SqlInstance $script:instance2 -Category 'dbatoolsci_job_category' -CategoryType LocalJob
$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob_disabled' -Category 'dbatoolsci_job_category' -Disabled
$null = New-DbaAgentJobStep -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob_disabled' -StepId 1 -StepName 'dbatoolsci Test Step' -Subsystem TransactSql -SubsystemServer $script:instance2 -Command 'SELECT * FROM master.sys.all_columns' -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -DatabaseUser sa -RetryAttempts 1 -RetryInterval 2
}
AfterAll {
$null = Remove-DbaAgentJob -SqlInstance $script:instance2 -Job dbatoolsci_testjob, dbatoolsci_testjob_disabled -Confirm:$false
$null = Remove-DbaAgentJobCategory -SqlInstance $script:instance2 -Category 'dbatoolsci_job_category' -Confirm:$false
}
Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {

Context "Command finds jobs using all parameters" {
BeforeAll {
#subsystemServer needs the real underlying name, and it doesn't work if targeting something like localhost\namedinstance
# the typical error would be WARNING: [17:19:26][New-DbaAgentJobStep] Something went wrong creating the job step | The specified '@server' is
#invalid (valid values are returned by sp_helpserver).
$srvName = Invoke-DbaQuery -SqlInstance $script:instance2 -Query "select @@servername as sn" -as PSObject
$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob' -OwnerLogin 'sa'
$null = New-DbaAgentJobStep -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob' -StepId 1 -StepName 'dbatoolsci Failed' -Subsystem TransactSql -SubsystemServer $srvName.sn -Command "RAISERROR (15600,-1,-1, 'dbatools_error');" -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -RetryAttempts 1 -RetryInterval 2
$null = Start-DbaAgentJob -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob'
$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob' -OwnerLogin 'sa'
$null = New-DbaAgentJobStep -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob' -StepId 1 -StepName 'dbatoolsci Failed' -Subsystem TransactSql -SubsystemServer $srvName.sn -Command "RAISERROR (15600,-1,-1, 'dbatools_error');" -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -RetryAttempts 1 -RetryInterval 2
$null = New-DbaAgentJobCategory -SqlInstance $script:instance2 -Category 'dbatoolsci_job_category' -CategoryType LocalJob
$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob_disabled' -Category 'dbatoolsci_job_category' -Disabled
$null = New-DbaAgentJobStep -SqlInstance $script:instance2 -Job 'dbatoolsci_testjob_disabled' -StepId 1 -StepName 'dbatoolsci Test Step' -Subsystem TransactSql -SubsystemServer $srvName.sn -Command 'SELECT * FROM master.sys.all_columns' -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -RetryAttempts 1 -RetryInterval 2
}
AfterAll {
$null = Remove-DbaAgentJob -SqlInstance $script:instance2 -Job dbatoolsci_testjob, dbatoolsci_testjob_disabled -Confirm:$false
$null = Remove-DbaAgentJobCategory -SqlInstance $script:instance2 -Category 'dbatoolsci_job_category' -Confirm:$false
}

$results = Find-DbaAgentJob -SqlInstance $script:instance2 -Job dbatoolsci_testjob
It "Should find a specific job" {
$results.name | Should Be "dbatoolsci_testjob"
Expand Down Expand Up @@ -55,9 +63,9 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
It "Should find jobs that have not been scheduled" {
$results | Should not be null
}
$results = Find-DbaAgentJob -SqlInstance $script:instance2 -IsNotScheduled
It "Should find 11 jobs that have no schedule" {
$results.count | Should be 13
$results = Find-DbaAgentJob -SqlInstance $script:instance2 -IsNotScheduled -Job *dbatoolsci*
It "Should find 2 jobs that have no schedule" {
$results.count | Should be 2
}
$results = Find-DbaAgentJob -SqlInstance $script:instance2 -IsNoEmailNotification
It "Should find jobs that have no email notification" {
Expand All @@ -71,6 +79,8 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
It "Should find jobs that are owned by sa" {
$results | Should not be null
}


$results = Find-DbaAgentJob -SqlInstance $script:instance2 -IsFailed -Since '2016-07-01 10:47:00'
It "Should find jobs that have been failed since July of 2016" {
$results | Should not be null
Expand Down
14 changes: 0 additions & 14 deletions tests/Find-DbaTrigger.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ $CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'Pattern', 'TriggerLevel', 'IncludeSystemObjects', 'IncludeSystemDatabases', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
}
}
}
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
Expand Down
3 changes: 1 addition & 2 deletions tests/Get-DbaBackupInformation.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
}

AfterAll {
$null = Get-DbaDatabase -SqlInstance $script:instance1 -Database $dbname | Remove-DbaDatabase -Confirm:$false
$null = Get-DbaDatabase -SqlInstance $script:instance1 -Database $dbname2 | Remove-DbaDatabase -Confirm:$false
$null = Get-DbaDatabase -SqlInstance $script:instance1 -Database $dbname, $dbname2, $dbname3 | Remove-DbaDatabase -Confirm:$false
}

Context "Get history for all database" {
Expand Down
5 changes: 4 additions & 1 deletion tests/Get-DbaDbCompatibility.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
}
Foreach ($row in $results) {
It "Should return correct compatibility level for $($row.database)" {
$row.Compatibility | Should Be $compatibilityLevel
# Only test system databases as there might be leftover databases from other tests
if ($row.DatabaseId -le 4) {
$row.Compatibility | Should Be $compatibilityLevel
}
$row.DatabaseId | Should -Be (Get-DbaDatabase -SqlInstance $script:instance1 -Database $row.Database).Id
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Get-DbaDump.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if (-not $env:appveyor) {
BeforeAll {
$server = Connect-DbaInstance -SqlInstance $script:instance1
$server.Query("DBCC STACKDUMP")
Start-Sleep -Seconds 5
}

$results = Get-DbaDump -SqlInstance $script:instance1
Expand Down
2 changes: 1 addition & 1 deletion tests/Get-DbaLastGoodCheckDb.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
$db.Query("DBCC CHECKDB")
}
AfterAll {
$null = Remove-DbaDatabase -SqlInstance $script:instance2 -Database $dbname -confirm:$false
$null = Remove-DbaDatabase -SqlInstance $script:instance1 -Database $dbname -confirm:$false
}
Context "Command actually works" {
$results = Get-DbaLastGoodCheckDb -SqlInstance $script:instance1 -Database master
Expand Down
3 changes: 0 additions & 3 deletions tests/Get-DbaPermission.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
$null = Invoke-DbaQuery -SqlInstance $server -Database $dbName -Query "GRANT CONTROL ON Schema::$schemaNameForTable2 TO $loginNameUser2"

$table2 = New-DbaDbTable -SqlInstance $server -Database $dbName -Name $tableName2 -Schema $schemaNameForTable2 -ColumnMap $tableSpec2

# debugging errors seen only in AppVeyor
Write-Host "Get-DbaPermission: Server=$server, dbName=$dbName, loginDBO=$($loginDBO.Name), loginDBOwner=$($loginDBOwner.Name), loginUser1=$($loginUser1.Name), newUserDBOwner=$($newUserDBOwner.Name), newUser1=$($newUser1.Name), table1=$($table1.Name), loginUser2=$($loginUser2.Name), newUser2=$($newUser2.Name), table2=$($table2.Name), table2Schema=$($table2.Schema)"
}

AfterAll {
Expand Down
3 changes: 0 additions & 3 deletions tests/Get-DbaPfDataCollectorCounterSample.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
}
}
}
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
Context "Verifying command works" {
Expand Down
4 changes: 1 addition & 3 deletions tests/Install-DbaAgentAdminAlert.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
BeforeEach {
if ($Env:appveyor) {
Get-DbaAgentAlert -SqlInstance $script:instance2, $script:instance3 | Remove-DbaAgentAlert -Confirm:$false
}
Get-DbaAgentAlert -SqlInstance $script:instance2, $script:instance3 | Remove-DbaAgentAlert -Confirm:$false
}
Context 'Creating a new SQL Server Agent alert' {
$parms = @{
Expand Down
6 changes: 3 additions & 3 deletions tests/Invoke-DbaBalanceDataFiles.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
$db = Get-DbaDatabase -SqlInstance $server -Database $dbname

# Create the tables
$db.Query("CREATE TABLE table1 (ID1 INT IDENTITY PRIMARY KEY, Name1 varchar(100))")
$db.Query("CREATE TABLE table2 (ID1 INT IDENTITY PRIMARY KEY, Name2 varchar(100))")
$db.Query("CREATE TABLE table1 (ID1 INT IDENTITY PRIMARY KEY, Name1 char(100))")
$db.Query("CREATE TABLE table2 (ID1 INT IDENTITY PRIMARY KEY, Name2 char(100))")

# Generate the values
$sqlvalues = New-Object System.Collections.ArrayList
1 .. 1000 | ForEach-Object { $null = $sqlvalues.Add("('some value to test the balance command $_')") }
1 .. 1000 | ForEach-Object { $null = $sqlvalues.Add("('some value')") }

$db.Query("insert into table1 (Name1) Values $($sqlvalues -join ',')")
$db.Query("insert into table1 (Name1) Values $($sqlvalues -join ',')")
Expand Down
4 changes: 2 additions & 2 deletions tests/Invoke-DbaDiagnosticQuery.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
@($results | Where-Object {$_.Database -eq $Database2}).Count | Should -Be 0
}
It "Correctly excludes queries when QueryName and ExcludeQuery parameters are used" {
$results = Invoke-DbaDiagnosticQuery -SqlInstance $script:instance2 -QueryName 'Version Info', 'Core Counts', 'Server Properties' -ExcludeQuery 'Core Counts' -WhatIf
$results = Invoke-DbaDiagnosticQuery -SqlInstance $script:instance2 -QueryName 'Version Info', 'Core Counts', 'Server Properties' -ExcludeQuery 'Core Counts'
@($results).Count | Should be 2
}
It "Correctly excludes queries when only ExcludeQuery parameter is used" {
$results = Invoke-DbaDiagnosticQuery -SqlInstance $script:instance2 -ExcludeQuery "Missing Index Warnings", "Buffer Usage" -whatif
$results = Invoke-DbaDiagnosticQuery -SqlInstance $script:instance2 -ExcludeQuery "Missing Index Warnings", "Buffer Usage"
@($results).Count | Should -BeGreaterThan 0
@($results | Where-Object Name -eq "Missing Index Warnings").Count | Should be 0
@($results | Where-Object Name -eq "Buffer Usage").Count | Should be 0
Expand Down
Loading

0 comments on commit 90ec329

Please sign in to comment.