Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to be able to run them outside of AppVeyor #9406

Merged
merged 21 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/Get-DbaDbBackupHistory.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
}

It "Invalid type for -Since" {
$results = Get-DbaDbBackupHistory -SqlInstance $script:instance1 -Database $dbname -Since "-" -WarningVariable warning
($results = Get-DbaDbBackupHistory -SqlInstance $script:instance1 -Database $dbname -Since "-" -WarningVariable warning) *> $null
$results | Should -BeNullOrEmpty
$warning | Should -BeLike "*-Since must be either a DateTime or TimeSpan object*"
}
Expand Down
12 changes: 8 additions & 4 deletions tests/Get-DbaDbCompatibility.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
}

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$server = Connect-DbaInstance -SqlInstance $script:instance1
$compatibilityLevel = $server.Databases['master'].CompatibilityLevel
}
Context "Gets compatibility for multiple databases" {
$results = Get-DbaDbCompatibility -SqlInstance $script:instance1
It "Gets results" {
$results | Should Not Be $null
}
Foreach ($row in $results) {
It "Should return Compatiblity level of Version100 for $($row.database)" {
$row.Compatibility | Should Be "Version100"
It "Should return correct compatiblity level for $($row.database)" {
Copy link
Contributor

@niphlod niphlod Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compatIbility here. may sound nitpicky, but given we're reviewing, we may fix "formalities" as well.

$row.Compatibility | Should Be $compatibilityLevel
$row.DatabaseId | Should -Be (Get-DbaDatabase -SqlInstance $script:instance1 -Database $row.Database).Id
}
}
Expand All @@ -32,8 +36,8 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
It "Gets results" {
$results | Should Not Be $null
}
It "Should return Compatiblity level of Version100 for $($results.database)" {
$results.Compatibility | Should Be "Version100"
It "Should return correct compatiblity level for $($results.database)" {
andreasjordan marked this conversation as resolved.
Show resolved Hide resolved
$results.Compatibility | Should Be $compatibilityLevel
$results.DatabaseId | Should -Be (Get-DbaDatabase -SqlInstance $script:instance1 -Database master).Id
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Get-DbaDbDetachedFileInfo.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$versionName = $server.GetSqlServerVersionName()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't even know this existed, cool.

$random = Get-Random
$dbname = "dbatoolsci_detatch_$random"
$server.Query("CREATE DATABASE $dbname")
Expand All @@ -39,7 +40,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
$results.name | Should Be $dbname
}
It "Should be 2016" {
andreasjordan marked this conversation as resolved.
Show resolved Hide resolved
$results.version | Should Be 'SQL Server 2016'
$results.version | Should Be $versionName
}
It "Should have Data files" {
$results.DataFiles | Should Not Be $null
Expand Down
4 changes: 0 additions & 4 deletions tests/Get-DbaDbRestoreHistory.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Describe "$commandname Integration Tests" -Tags "IntegrationTests" {

BeforeAll {
$DestBackupDir = 'C:\Temp\backups'
if (-Not(Test-Path $DestBackupDir)) {
New-Item -Type Container -Path $DestBackupDir
}
$random = Get-Random
$dbname1 = "dbatoolsci_restorehistory1_$random"
$dbname2 = "dbatoolsci_restorehistory2_$random"
Expand Down
30 changes: 24 additions & 6 deletions tests/Get-DbaDbSharePoint.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,47 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {

Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
BeforeAll {
$skip = $false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never really got to use this function and I fear it spun up by potatoqualitee's period on working tirelessly on sharepoint farms. That being said I'm seeing multiple reports about either SQLPS loading DLLs that interfere with us and also DacFx-related issues (many cycling around core-noncore dilemma). That being said I trust you on this change, maybe @potatoqualitee can chime in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont remember anything and also trust the change 😅

$spdb = 'SharePoint_Admin_7c0c491d0e6f43858f75afa5399d49ab', 'WSS_Logging', 'SecureStoreService_20e1764876504335a6d8dd0b1937f4bf', 'DefaultWebApplicationDB', 'SharePoint_Config_4c524cb90be44c6f906290fe3e34f2e0', 'DefaultPowerPivotServiceApplicationDB-5b638361-c6fc-4ad9-b8ba-d05e63e48ac6', 'SharePoint_Config_4c524cb90be44c6f906290fe3e34f2e0'
Get-DbaProcess -SqlInstance $script:instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
Get-DbaProcess -SqlInstance $script:instance2 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
$server = Connect-DbaInstance -SqlInstance $script:instance2
foreach ($db in $spdb) {
try {
$null = $server.Query("Create Database [$db]")
} catch { continue }
}
# Andreas Jordan: We should try to get a backup working again or even better just a sql script to set this up.
# This takes a long time but I cannot figure out why every backup of this db is malformed
$bacpac = "$script:appveyorlabrepo\bacpac\sharepoint_config.bacpac"
$sqlpackage = (Get-Command sqlpackage -ErrorAction Ignore).Source
if (-not $sqlpackage) {
$sqlpackage = Join-DbaPath -Path (Split-Path -Path (Get-Module dbatools*library).Path) -ChildPath lib, sqlpackage, windows, sqlpackage.exe
if (Test-Path -Path $bacpac) {
$sqlpackage = (Get-Command sqlpackage -ErrorAction Ignore).Source
if (-not $sqlpackage) {
$libraryPath = Get-DbatoolsLibraryPath
if ($libraryPath -match 'desktop$') {
$sqlpackage = Join-DbaPath -Path (Get-DbatoolsLibraryPath) -ChildPath lib, sqlpackage.exe
} elseif ($isWindows) {
$sqlpackage = Join-DbaPath -Path (Get-DbatoolsLibraryPath) -ChildPath lib, win, sqlpackage.exe
} else {
# Not implemented
}
}
# On PowerShell 5.1 on Windows Server 2022, the following line throws:
# sqlpackage.exe : *** An unexpected failure occurred: Could not load type 'Microsoft.Data.Tools.Schema.Common.Telemetry.SqlPackageSource' from assembly 'Microsoft.Data.Tools.Utilities, Version=162.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
# On PowerShell 7.4.2 on Windows Server 2022, the following line throws:
# Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
. $sqlpackage /Action:Import /tsn:$script:instance2 /tdn:Sharepoint_Config /sf:$bacpac /p:Storage=File
} else {
Write-Warning -Message "No bacpac found in path [$bacpac], skipping tests."
$skip = $true
}
. $sqlpackage /Action:Import /tsn:$script:instance2 /tdn:Sharepoint_Config /sf:$bacpac /p:Storage=File
}
AfterAll {
Remove-DbaDatabase -SqlInstance $script:instance2 -Database $spdb -Confirm:$false
}
Context "Command gets SharePoint Databases" {
$results = Get-DbaDbSharePoint -SqlInstance $script:instance2
foreach ($db in $spdb) {
It "returns $db from in the SharePoint database list" {
It -Skip:$skip "returns $db from in the SharePoint database list" {
$db | Should -BeIn $results.Name
}
}
Expand Down
18 changes: 9 additions & 9 deletions tests/Get-DbaDbSpace.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'IncludeSystemDBs', 'InputObject', '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
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
}
}
}
Expand All @@ -24,30 +24,30 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
}
#Skipping these tests as internals of Get-DbaDbSpace seems to be unreliable in CI
Context "Gets DbSpace" {
$results = Get-DbaDbSpace -SqlInstance $script:instance2 | Where-Object {$_.Database -eq "$dbname"}
It -skip "Gets results" {
$results = Get-DbaDbSpace -SqlInstance $script:instance2 | Where-Object { $_.Database -eq "$dbname" }
It "Gets results" {
$results | Should Not Be $null
}
foreach ($row in $results) {
It -skip "Should retreive space for $dbname" {
It "Should retreive space for $dbname" {
$row.Database | Should Be $dbname
}
It -skip "Should have a physical path for $dbname" {
It "Should have a physical path for $dbname" {
$row.physicalname | Should Not Be $null
}
}
}
#Skipping these tests as internals of Get-DbaDbSpace seems to be unreliable in CI
Context "Gets DbSpace when using -Database" {
$results = Get-DbaDbSpace -SqlInstance $script:instance2 -Database $dbname
It -skip "Gets results" {
It "Gets results" {
$results | Should Not Be $null
}
Foreach ($row in $results) {
It -skip "Should retreive space for $dbname" {
It "Should retreive space for $dbname" {
$row.Database | Should Be $dbname
andreasjordan marked this conversation as resolved.
Show resolved Hide resolved
}
It -skip "Should have a physical path for $dbname" {
It "Should have a physical path for $dbname" {
$row.physicalname | Should Not Be $null
}
}
Expand Down
18 changes: 12 additions & 6 deletions tests/New-DbaDbAsymmetricKey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {

Context "Handles pre-existing key" {
$keyname = 'test1'
$key = New-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Name $keyname -Database master -WarningVariable warnvar
($key = New-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Name $keyname -Database master -WarningVariable warnvar) *> $null
$null = Remove-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Name $keyname -Database master -confirm:$false
It "Should Warn that they key $keyname already exists" {
$Warnvar | Should -BeLike '*already exists in master on*'
Expand Down Expand Up @@ -98,14 +98,20 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
$null = Remove-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Name $keyname -Database $database -confirm:$false
}

Context "Create new key in $database Loaded from a keyfile" {
Context "Create new key loaded from a keyfile" {
$skip = $false
$keyname = 'filekey'
$dbuser = 'keyowner'
$database = 'enctest'
$path = "$($script:appveyorlabrepo)\keytests\keypair.snk"
$key = New-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Database $database -Name $keyname -Owner keyowner -WarningVariable warnvar -KeySourceType File -KeySource $path
$results = Get-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Name $keyname -Database $database
It "Should Create new key in master called $keyname" {
if (Test-Path -Path $path) {
$key = New-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Database $database -Name $keyname -Owner keyowner -WarningVariable warnvar -KeySourceType File -KeySource $path
$results = Get-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Name $keyname -Database $database
} else {
Write-Warning -Message "No keypair found in path [$path], skipping tests."
$skip = $true
}
It -Skip:$skip "Should Create new key in master called $keyname" {
$warnvar | Should -BeNullOrEmpty
$results.database | Should -Be $database
$results.name | Should -Be $keyname
Expand All @@ -119,7 +125,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
$dbuser = 'keyowner'
$database = 'enctest'
$path = "$($script:appveyorlabrepo)\keytests\keypair.bad"
$key = New-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Database $database -Name $keyname -Owner keyowner -WarningVariable warnvar -KeySourceType File -KeySource $path
($key = New-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Database $database -Name $keyname -Owner keyowner -WarningVariable warnvar -KeySourceType File -KeySource $path) *> $null
$results = Get-DbaDbAsymmetricKey -SqlInstance $script:instance2 -Name $keyname -Database $database
It "Should not Create new key in $database called $keyname" {
$warnvar | Should -Not -BeNullOrEmpty
Expand Down
6 changes: 5 additions & 1 deletion tests/New-DbaDbEncryptionKey.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ Describe "$CommandName Integration Tests for Async" -Tags "IntegrationTests" {
}
}

# TODO: I think I need some background on this. Was the intention to create the key or not to creeate the key?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur with the "I don't know" bit

# Currently $warn is:
# [09:49:20][New-DbaDbEncryptionKey] Failed to create encryption key in random-1299050584 on localhost\sql2016 | Cannot decrypt or encrypt using the specified asymmetric key, either because it has no private key or because the password provided for the private key is incorrect.
# Will leave it skipped for now.
Context "Command does not work but warns" {
# this works on docker, not sure what's up
It -Skip "should warn that it cant create an encryption key" {
$null = $db | New-DbaDbEncryptionKey -Force -Type AsymmetricKey -EncryptorName $masterasym.Name -WarningVariable warn
($null = $db | New-DbaDbEncryptionKey -Force -Type AsymmetricKey -EncryptorName $masterasym.Name -WarningVariable warn) *> $null
$warn | Should -Match "n order to encrypt the database encryption key with an as"
}
}
Expand Down
9 changes: 7 additions & 2 deletions tests/New-DbaDbMailAccount.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
EmailAddress = $email_address
DisplayName = $display_name
ReplyToAddress = $replyto_address
# MailServer is not set, because we don't want to configure the mail server on the instance.
# MailServer = $mailserver_name
}
$results = New-DbaDbMailAccount @splat

Expand All @@ -54,12 +56,14 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
It "Should have Description of 'Mail account for email alerts' " {
$results.Description | Should Be 'Mail account for email alerts'
}
It "Should have EmailAddress of 'dbatoolssci@dbatools.net' " {
$results.EmailAddress | Should Be 'dbatoolssci@dbatools.net'
# TODO: If we set the variables then we should use them, don't we?
It "Should have EmailAddress of '$email_address' " {
$results.EmailAddress | Should Be $email_address
}
It "Should have ReplyToAddress of 'no-reply@dbatools.net' " {
$results.ReplyToAddress | Should Be 'no-reply@dbatools.net'
}
# Skipped, because we have not set the MailServer, because we don't want to configure the mail server on the instance.
It -Skip "Should have MailServer of '[smtp.dbatools.io]' " {
$results.MailServers | Should Be '[smtp.dbatools.io]'
}
Expand All @@ -81,6 +85,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
It "Should have ReplyToAddress of 'no-reply@dbatools.net' " {
$results.ReplyToAddress | Should Be 'no-reply@dbatools.net'
}
# Skipped, because we have not set the MailServer, because we don't want to configure the mail server on the instance.
It -Skip "Should have MailServer of '[smtp.dbatools.io]' " {
$results.MailServers | Should Be '[smtp.dbatools.io]'
}
Expand Down
Loading