Skip to content

Commit

Permalink
Fix test for Get-DbaEstimatedCompletionTime (#9522)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan authored Oct 20, 2024
1 parent 036abd7 commit 8d4f86f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 45 deletions.
97 changes: 53 additions & 44 deletions tests/Get-DbaEstimatedCompletionTime.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,66 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Describe "$commandname Integration Tests" -Tags "IntegrationTests" {

BeforeAll {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Get-DbaDatabase -SqlInstance $server -Database checkdbTestDatabase | Remove-DbaDatabase -Confirm:$false
$null = Restore-DbaDatabase -SqlInstance $server -Path $script:appveyorlabrepo\sql2008-backups\db1\SQL2008_db1_FULL_20170518_041738.bak -DatabaseName checkdbTestDatabase
$null = New-DbaAgentJob -SqlInstance $server -Job checkdbTestJob
$null = New-DbaAgentJobStep -SqlInstance $server -Job checkdbTestJob -StepName checkdb -Subsystem TransactSql -Command "DBCC CHECKDB('checkdbTestDatabase')"
$skip = $true
if ($script:bigDatabaseBackup) {
try {
if (-not (Test-Path -Path $script:bigDatabaseBackup) -and $script:bigDatabaseBackupSourceUrl) {
Invoke-WebRequest -Uri $script:bigDatabaseBackupSourceUrl -OutFile $script:bigDatabaseBackup -ErrorAction Stop
}
$null = Restore-DbaDatabase -SqlInstance $script:instance2 -Path $script:bigDatabaseBackup -DatabaseName checkdbTestDatabase -WithReplace -ReplaceDbNameInFile -EnableException
$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob -EnableException
$null = New-DbaAgentJobStep -SqlInstance $script:instance2 -Job checkdbTestJob -StepName checkdb -Subsystem TransactSql -Command "DBCC CHECKDB('checkdbTestDatabase')" -EnableException
$skip = $false
} catch {
Write-Host -Object "Test for $CommandName failed in BeforeAll because: $_" -ForegroundColor Cyan
}
}
}

AfterAll {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Remove-DbaAgentJob -SqlInstance $server -Job checkdbTestJob -Confirm:$false
$null = Get-DbaDatabase -SqlInstance $erver -Database checkdbTestDatabase | Remove-DbaDatabase -Confirm:$false
$null = Get-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob | Remove-DbaAgentJob -Confirm:$false
$null = Get-DbaDatabase -SqlInstance $script:instance2 -Database checkdbTestDatabase | Remove-DbaDatabase -Confirm:$false
}

Context "Gets Query Estimated Completion" {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Start-DbaAgentJob -SqlInstance $server -Job checkdbTestJob
$results = Get-DbaEstimatedCompletionTime -SqlInstance $server
$null = Remove-DbaAgentJob -SqlInstance $server -Job checkdb -Confirm:$false
Start-Sleep -Seconds 5
It "Gets results" {
$results | Should Not Be $null
}
It "Should be SELECT" {
$results.Command | Should Match 'DBCC'
}
It "Should be login dbo" {
$results.login | Should Be 'dbo'
}
}
Context "Gets Query Estimated Completion when using -Database" {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Start-DbaAgentJob -SqlInstance $server -Job checkdbTestJob
$results = Get-DbaEstimatedCompletionTime -SqlInstance $server -Database checkdbTestDatabase
Start-Sleep -Seconds 5
It "Gets results" {
$results | Should Not Be $null
}
It "Should be SELECT" {
$results.Command | Should Match 'DBCC'
Context "Gets correct results" {
It -Skip:$skip "Gets Query Estimated Completion" {
$job = Start-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob
Start-Sleep -Seconds 1
$results = Get-DbaEstimatedCompletionTime -SqlInstance $script:instance2
while ($job.CurrentRunStatus -eq 'Executing') {
Start-Sleep -Seconds 1
$job.Refresh()
}

$results | Should -Not -BeNullOrEmpty
$results.Command | Should -Match 'DBCC'
$results.Database | Should -Be checkdbTestDatabase
}
It "Should be login dbo" {
$results.login | Should Be 'dbo'

It -Skip:$skip "Gets Query Estimated Completion when using -Database" {
$job = Start-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob
Start-Sleep -Seconds 1
$results = Get-DbaEstimatedCompletionTime -SqlInstance $script:instance2 -Database checkdbTestDatabase
while ($job.CurrentRunStatus -eq 'Executing') {
Start-Sleep -Seconds 1
$job.Refresh()
}

$results | Should -Not -BeNullOrEmpty
$results.Command | Should -Match 'DBCC'
$results.Database | Should -Be checkdbTestDatabase
}
}
Context "Gets no Query Estimated Completion when using -ExcludeDatabase" {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Start-DbaAgentJob -SqlInstance $server -Job checkdbTestJob
$results = Get-DbaEstimatedCompletionTime -SqlInstance $server -ExcludeDatabase checkdbTestDatabase
Start-Sleep -Seconds 5
It "Gets no results" {
$results | Should Be $null

It -Skip:$skip "Gets no Query Estimated Completion when using -ExcludeDatabase" {
$job = Start-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob
Start-Sleep -Seconds 1
$results = Get-DbaEstimatedCompletionTime -SqlInstance $script:instance2 -ExcludeDatabase checkdbTestDatabase
while ($job.CurrentRunStatus -eq 'Executing') {
Start-Sleep -Seconds 1
$job.Refresh()
}

$results | Should -BeNullOrEmpty
}
}
}
2 changes: 2 additions & 0 deletions tests/constants.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ if (Test-Path "$PSScriptRoot\constants.local.ps1") {
$script:azureblobaccount = "dbatools"
$script:azureserver = 'psdbatools.database.windows.net'
$script:azuresqldblogin = "appveyor@clemairegmail.onmicrosoft.com"
$script:bigDatabaseBackup = 'C:\github\StackOverflowMini.bak'
$script:bigDatabaseBackupSourceUrl = 'https://github.com/BrentOzarULTD/Stack-Overflow-Database/releases/download/20230114/StackOverflowMini.bak'
}

if ($env:appveyor) {
Expand Down
4 changes: 3 additions & 1 deletion tests/pester.groups.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ $TestsRunGroups = @{
'Get-DbaExecutionPlan',
# Non-useful info from newly started sql servers
'Get-DbaCpuRingBuffer',
'Get-DbaLatchStatistic'
'Get-DbaLatchStatistic',
# uses a backup that only works on SQL Server 2022
'Get-DbaEstimatedCompletionTime'
)
# do not run everywhere
"disabled" = @()
Expand Down

0 comments on commit 8d4f86f

Please sign in to comment.