Skip to content

Commit

Permalink
Update tests to be able to run them outside of AppVeyor (part 2) (#9413)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan authored Jul 16, 2024
1 parent 61eed4d commit 1556754
Show file tree
Hide file tree
Showing 29 changed files with 184 additions and 5,430 deletions.
21 changes: 17 additions & 4 deletions public/Backup-DbaDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,15 @@ function Backup-DbaDatabase {

if (Test-Bound 'EncryptionAlgorithm') {
if (!((Test-Bound 'EncryptionCertificate') -xor (Test-Bound 'EncryptionKey'))) {
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message 'EncryptionCertifcate and EncryptionKey are mutually exclusive, only provide on of them'
return
} else {
$encryptionOptions = New-Object Microsoft.SqlServer.Management.Smo.BackupEncryptionOptions
if (Test-Bound 'EncryptionCertificate') {
$tCertCheck = Get-DbaDbCertificate -SqlInstance $server -Database master -Certificate $EncryptionCertificate
if ($null -eq $tCertCheck) {
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "Certificate $EncryptionCertificate does not exist on $server so cannot be used for backups"
return
} else {
Expand All @@ -415,6 +417,7 @@ function Backup-DbaDatabase {
# Should not end up here until Key encryption in implemented
$tKeyCheck = Get-DbaDbAsymmetricKey -SqlInstance $server -Database master -Name $EncrytptionKey
if ($null -eq $tKeyCheck) {
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "AsymmetricKey $Encryptionkey does not exist on $server so cannot be used for backups"
return
} else {
Expand All @@ -441,13 +444,14 @@ function Backup-DbaDatabase {
}

if (($MaxTransferSize % 64kb) -ne 0 -or $MaxTransferSize -gt 4mb) {
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "MaxTransferSize value must be a multiple of 64kb and no greater than 4MB"
return
}

if ($BlockSize) {
if ($BlockSize -notin (0.5kb, 1kb, 2kb, 4kb, 8kb, 16kb, 32kb, 64kb)) {

Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "Block size must be one of 0.5kb,1kb,2kb,4kb,8kb,16kb,32kb,64kb"
return
}
Expand All @@ -469,6 +473,7 @@ function Backup-DbaDatabase {
if (Get-DbaCredential -SqlInstance $server -Name $credentialName) {
Write-Message -Message "Found a SAS backup credential" -Level Verbose
} else {
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "You must provide the credential name for the Azure Storage Account"
return
}
Expand All @@ -488,14 +493,17 @@ function Backup-DbaDatabase {
}

if ($dbName -eq "tempdb") {
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "Backing up tempdb not supported" -Continue
}

if ('Normal' -notin ($db.Status -split ',')) {
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "Database status not Normal. $dbName skipped." -Continue
}

if ($db.DatabaseSnapshotBaseName) {
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "Backing up snapshots not supported. $dbName skipped." -Continue
}

Expand All @@ -511,6 +519,7 @@ function Backup-DbaDatabase {
if ($dbRecovery -eq 'Simple' -and $Type -eq 'Log') {
$failreason = "$db is in simple recovery mode, cannot take log backup"
$failures += $failreason
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "$failreason" -Continue -Target $db
}

Expand All @@ -520,6 +529,7 @@ function Backup-DbaDatabase {
if ($Type -notin @("Database", "Full") -and $lastfull -eq 1) {
$failreason = "$db does not have an existing full backup, cannot take log or differentialbackup"
$failures += $failreason
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "$failreason" -Continue -Target $db
}

Expand Down Expand Up @@ -562,6 +572,7 @@ function Backup-DbaDatabase {
$backup.CompressionOption = [Microsoft.SqlServer.Management.Smo.BackupCompressionOptions]::Off
}
} elseif ($server.Edition -like 'Express*' -or ($server.VersionMajor -eq 10 -and $server.VersionMinor -eq 0 -and $server.Edition -notlike '*enterprise*') -or $server.VersionMajor -lt 10) {
Write-Progress -Id $topProgressId -Activity 'Backup' -Completed
Stop-Function -Message "Compression is not supported with this version/edition of Sql Server" -Continue -Target $db
} else {
Write-Message -Level Verbose -Message "Compression enabled"
Expand Down Expand Up @@ -758,7 +769,7 @@ function Backup-DbaDatabase {
if ($OutputScriptOnly -ne $True) {
$backup.SqlBackup($server)
$script = $backup.Script($server)
Write-Progress -Id $ProgressId -Activity "Backing up database $dbName to $backupfile" -Status "Complete" -Completed
Write-Progress -Id $ProgressId -Activity "Backing up database $dbName to $backupfile" -Completed
$BackupComplete = $true
if ($server.VersionMajor -eq '8') {
$HeaderInfo = Get-BackupAncientHistory -SqlInstance $server -Database $dbName
Expand Down Expand Up @@ -826,13 +837,15 @@ function Backup-DbaDatabase {
$HeaderInfo | Add-Member -Type NoteProperty -Name Verified -Value $Verified
} else {
$backup.Script($server)
Write-Progress -Id $ProgressId -Activity "Backing up database $dbName to $backupfile" -Completed
}
}
} catch {
if ($NoRecovery -and ($_.Exception.InnerException.InnerException.InnerException -like '*cannot be opened. It is in the middle of a restore.')) {
Write-Message -Message "Exception thrown by db going into restoring mode due to recovery" -Leve Verbose
Write-Message -Message "Exception thrown by db going into restoring mode due to recovery" -Level Verbose
} else {
Write-Progress -Id $ProgressId -Activity "Backup" -Status "Failed" -Completed
Write-Progress -Id $ProgressId -Activity "Backup" -Completed
Write-Progress -Id $topProgressId -Activity "Backup" -Completed
Stop-Function -message "Backup of [$dbName] failed" -ErrorRecord $_ -Target $dbName -Continue
$BackupComplete = $false
}
Expand Down
1 change: 1 addition & 0 deletions public/Connect-DbaInstance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ function Connect-DbaInstance {
if (-not $server.IsAzure) {
Add-Member -InputObject $server -NotePropertyName IsAzure -NotePropertyValue $isAzure -Force
Add-Member -InputObject $server -NotePropertyName DbaInstanceName -NotePropertyValue $instance.InstanceName -Force
Add-Member -InputObject $server -NotePropertyName SqlInstance -NotePropertyValue $server.DomainInstanceName -Force
Add-Member -InputObject $server -NotePropertyName NetPort -NotePropertyValue $instance.Port -Force
Add-Member -InputObject $server -NotePropertyName ConnectedAs -NotePropertyValue $server.ConnectionContext.TrueLogin -Force
Write-Message -Level Debug -Message "We added IsAzure = '$($server.IsAzure)', DbaInstanceName = instance.InstanceName = '$($server.DbaInstanceName)', SqlInstance = server.DomainInstanceName = '$($server.SqlInstance)', NetPort = instance.Port = '$($server.NetPort)', ConnectedAs = server.ConnectionContext.TrueLogin = '$($server.ConnectedAs)'"
Expand Down
2 changes: 1 addition & 1 deletion public/Get-DbaExternalProcess.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function Get-DbaExternalProcess {
foreach ($computer in $ComputerName) {
try {
$sqlpid = (Get-DbaCmObject -ComputerName $computer -Credential $Credential -ClassName win32_process | Where-Object ProcessName -eq "sqlservr.exe").ProcessId
$processes = Get-DbaCmObject -ComputerName $computer -Credential $Credential -ClassName win32_process | Where-Object ParentProcessId -eq $sqlpid
$processes = Get-DbaCmObject -ComputerName $computer -Credential $Credential -ClassName win32_process | Where-Object ParentProcessId -in $sqlpid

foreach ($process in $processes) {
[PSCustomObject]@{
Expand Down
1 change: 1 addition & 0 deletions public/Invoke-DbaDbPiiScan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ function Invoke-DbaDbPiiScan {

} # End for each table
} # End for each database
Write-Progress -Id $progressId -Activity $progressActivity -Completed
} # End for each instance

$piiScanResults
Expand Down
20 changes: 20 additions & 0 deletions public/New-DbaAvailabilityGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ function New-DbaAvailabilityGroup {
$second = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SecondarySqlCredential
$secondaries += $second
} catch {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}

Expand All @@ -430,50 +431,58 @@ function New-DbaAvailabilityGroup {
}

if ($requirementsFailed) {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Prerequisites are not completly met, so stopping here. See warning messages for details."
return
}

# Don't reuse $server here, it fails
if (Get-DbaAvailabilityGroup -SqlInstance $Primary -SqlCredential $PrimarySqlCredential -AvailabilityGroup $Name) {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Availability group named $Name already exists on $Primary"
return
}

if ($Certificate) {
$cert = Get-DbaDbCertificate -SqlInstance $Primary -SqlCredential $PrimarySqlCredential -Certificate $Certificate
if (-not $cert) {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Certificate $Certificate does not exist on $Primary" -Target $Primary
return
}
}

if (($SharedPath)) {
if (-not (Test-DbaPath -SqlInstance $Primary -SqlCredential $PrimarySqlCredential -Path $SharedPath)) {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Continue -Message "Cannot access $SharedPath from $Primary"
return
}
}

if ($Database -and -not $UseLastBackup -and -not $SharedPath -and $Secondary -and $SeedingMode -ne 'Automatic') {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Continue -Message "You must specify a SharedPath when adding databases to a manually seeded availability group"
return
}

if ($server.HostPlatform -eq "Linux") {
# New to SQL Server 2017 (14.x) is the introduction of a cluster type for AGs. For Linux, there are two valid values: External and None.
if ($ClusterType -notin "External", "None") {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Continue -Message "Linux only supports ClusterType of External or None"
return
}
# Microsoft Distributed Transaction Coordinator (DTC) is not supported under Linux in SQL Server 2017
if ($DtcSupport) {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Continue -Message "Microsoft Distributed Transaction Coordinator (DTC) is not supported under Linux"
return
}
}

if ($ClusterType -eq "None" -and $server.VersionMajor -lt 14) {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "ClusterType of None only supported in SQL Server 2017 and above"
return
}
Expand All @@ -485,17 +494,20 @@ function New-DbaAvailabilityGroup {

foreach ($primarydb in $dbs) {
if ($primarydb.MirroringStatus -ne "None") {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Cannot setup mirroring on database ($($primarydb.Name)) due to its current mirroring state: $($primarydb.MirroringStatus)"
return
}

if ($primarydb.Status -ne "Normal") {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Cannot setup mirroring on database ($($primarydb.Name)) due to its current state: $($primarydb.Status)"
return
}

if ($primarydb.RecoveryModel -ne "Full") {
if ((Test-Bound -ParameterName UseLastBackup)) {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "$($primarydb.Name) not set to full recovery. UseLastBackup cannot be used."
return
} else {
Expand Down Expand Up @@ -531,6 +543,7 @@ function New-DbaAvailabilityGroup {

if ($PassThru) {
$defaults = 'LocalReplicaRole', 'Name as AvailabilityGroup', 'PrimaryReplicaServerName as PrimaryReplica', 'AutomatedBackupPreference', 'AvailabilityReplicas', 'AvailabilityDatabases', 'AvailabilityGroupListeners'
Write-Progress -Activity "Adding new availability group" -Completed
return (Select-DefaultView -InputObject $ag -Property $defaults)
}

Expand Down Expand Up @@ -562,6 +575,7 @@ function New-DbaAvailabilityGroup {
if (-not $msg) {
$msg = $_
}
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message $msg -ErrorRecord $_ -Target $Primary
return
}
Expand All @@ -581,6 +595,7 @@ function New-DbaAvailabilityGroup {

$null = Add-DbaAgReplica @replicaparams -EnableException -SqlInstance $second
} catch {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Failure" -ErrorRecord $_ -Target $second -Continue
}
}
Expand All @@ -594,6 +609,7 @@ function New-DbaAvailabilityGroup {
if (-not $msg) {
$msg = $_
}
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message $msg -ErrorRecord $_ -Target $Primary
return
}
Expand Down Expand Up @@ -624,6 +640,7 @@ function New-DbaAvailabilityGroup {
# join replicas to ag
Join-DbaAvailabilityGroup -SqlInstance $second -InputObject $ag -EnableException
} catch {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Failure" -ErrorRecord $_ -Target $second -Continue
}
$second.AvailabilityGroups.Refresh()
Expand Down Expand Up @@ -659,6 +676,7 @@ function New-DbaAvailabilityGroup {
$null = Grant-DbaAgPermission -SqlInstance $second -Type AvailabilityGroup -AvailabilityGroup $Name -Permission CreateAnyDatabase -EnableException
}
} catch {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Failure" -ErrorRecord $_
}
}
Expand All @@ -672,6 +690,7 @@ function New-DbaAvailabilityGroup {
try {
Get-DbaDatabase -SqlInstance $secondaries -Database $Database -EnableException | Remove-DbaDatabase -EnableException
} catch {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Failed to remove databases from secondary replicas." -ErrorRecord $_
}
}
Expand All @@ -689,6 +708,7 @@ function New-DbaAvailabilityGroup {
try {
$null = Add-DbaAgDatabase @addDatabaseParams
} catch {
Write-Progress -Activity "Adding new availability group" -Completed
Stop-Function -Message "Failed to add databases to Availability Group." -ErrorRecord $_
}
}
Expand Down
5 changes: 2 additions & 3 deletions tests/Disconnect-DbaInstance.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ Describe "$CommandName Unit Tests" -Tag "UnitTests" {

Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
BeforeAll {
$null = Get-DbaDatabase -SqlInstance $script:instance1
$null = Connect-DbaInstance -SqlInstance $env:COMPUTERNAME -SqlConnectionOnly
$null = Connect-DbaInstance -SqlInstance $script:instance1
}
Context "disconnets a server" {
It "disconnects and returns some results" {
$results = Get-DbaConnectedInstance | Disconnect-DbaInstance
$results.Count | Should -BeGreaterThan 1
$results | Should -Not -BeNullOrEmpty
}
}
}
12 changes: 5 additions & 7 deletions tests/Find-DbaInstance.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
Context "Command finds SQL Server instances" {
BeforeAll {
if ($env:APPVEYOR) {
$results = Find-DbaInstance -ComputerName $script:instance3 -ScanType Browser, SqlConnect | Select-Object -First 1
} else {
$results = Find-DbaInstance -ComputerName $TestServer -ScanType Browser, SqlConnect | Select-Object -First 1
}
$results = Find-DbaInstance -ComputerName $script:instance3 -ScanType Browser, SqlConnect | Select-Object -First 1
}
It "Returns an object type of [Dataplat.Dbatools.Discovery.DbaInstanceReport]" {
$results | Should -BeOfType [Dataplat.Dbatools.Discovery.DbaInstanceReport]
}
It "FullName is populated" {
$results.FullName | Should -Not -BeNullOrEmpty
}
It "TcpConnected is true" {
$results.TcpConnected | Should -Be $true
if (([DbaInstanceParameter]$script:instance3).IsLocalHost -eq $false) {
It "TcpConnected is true" {
$results.TcpConnected | Should -Be $true
}
}
It "successfully connects" {
$results.SqlConnected | Should -Be $true
Expand Down
Loading

0 comments on commit 1556754

Please sign in to comment.