Skip to content

Commit 8770610

Browse files
Enhance tests (#10131)
1 parent ddb031a commit 8770610

7 files changed

+72
-39
lines changed

tests/Find-DbaInstance.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Describe $CommandName -Tag UnitTests {
3030
Describe $CommandName -Tag IntegrationTests {
3131
Context "Command finds SQL Server instances" {
3232
BeforeAll {
33-
$results = Find-DbaInstance -ComputerName $TestConfig.InstanceSingle -ScanType Browser, SqlConnect | Select-Object -First 1
33+
$results = Find-DbaInstance -ComputerName $TestConfig.InstanceSingle -ScanType Browser, SqlConnect | Where-Object SqlInstance -eq $TestConfig.InstanceSingle
3434
}
3535

3636
It "Returns an object type of [Dataplat.Dbatools.Discovery.DbaInstanceReport]" {

tests/Get-DbaExternalProcess.Tests.ps1

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Describe $CommandName -Tag IntegrationTests {
2525
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
2626
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
2727

28+
$computerName = Resolve-DbaComputerName -ComputerName $TestConfig.InstanceSingle -Property ComputerName
29+
2830
# Setup xp_cmdshell to create external processes for testing
2931
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceSingle -Query "
3032
-- To allow advanced options to be changed.
@@ -40,10 +42,12 @@ Describe $CommandName -Tag IntegrationTests {
4042
RECONFIGURE;
4143
GO"
4244

43-
$query = @"
44-
xp_cmdshell 'powershell -command ""sleep 20""'
45-
"@
46-
Start-Process -FilePath sqlcmd -ArgumentList "-S $($TestConfig.InstanceSingle) -Q `"$query`"" -NoNewWindow -RedirectStandardOutput null
45+
# Create sql file with code to start an external process
46+
$sqlFile = "$($TestConfig.Temp)\sleep.sql"
47+
Set-Content -Path $sqlFile -Value "xp_cmdshell 'powershell -command ""sleep 5""'"
48+
49+
# Run sql file to start external process
50+
Start-Process -FilePath sqlcmd -ArgumentList "-S $($TestConfig.InstanceSingle) -i $sqlFile" -NoNewWindow -RedirectStandardOutput null
4751

4852
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
4953
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
@@ -62,16 +66,20 @@ Describe $CommandName -Tag IntegrationTests {
6266
EXECUTE sp_configure 'show advanced options', 0;
6367
GO
6468
RECONFIGURE;
65-
GO" -ErrorAction SilentlyContinue
69+
GO"
70+
71+
# remove sql file
72+
Remove-Item -Path $sqlFile
6673

6774
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
6875
}
6976

7077
Context "Can get an external process" {
7178
It "returns a process" {
7279
Start-Sleep -Seconds 1
73-
$results = Get-DbaExternalProcess -ComputerName localhost | Where-Object Name -eq "cmd.exe"
74-
$results.ComputerName | Should -Be "localhost"
80+
$results = Get-DbaExternalProcess -ComputerName $computerName | Where-Object Name -eq "cmd.exe"
81+
Start-Sleep -Seconds 5
82+
$results.ComputerName | Should -Be $computerName
7583
$results.Name | Should -Be "cmd.exe"
7684
$results.ProcessId | Should -Not -Be $null
7785
}

tests/Get-DbaSchemaChangeHistory.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Describe $CommandName -Tag IntegrationTests {
4040
}
4141

4242
It "notices dbatoolsci_schemachange changed" {
43-
$schemaResults.Object -match "dbatoolsci_schemachange" | Should -Be $true
43+
$schemaResults.Object | Should -Contain "dbatoolsci_schemachange"
4444
}
4545
}
4646
}

tests/New-DbaFirewallRule.Tests.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Describe $CommandName -Tag UnitTests {
2525
}
2626

2727
Describe $CommandName -Tag IntegrationTests {
28+
# The context "RuleType Port (traditional port-based rules)" does not work with dynamic ports.
29+
# So we test at discovery time if dynamic ports are used and skip the tests if so.
30+
$isUsingDynamicPort = (Get-DbaNetworkConfiguration -SqlInstance $TestConfig.InstanceSingle -OutputType TcpIpAddresses).TcpDynamicPorts -ne ''
31+
2832
Context "RuleType Program (default - executable-based rules)" {
2933
BeforeAll {
3034
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
@@ -102,7 +106,7 @@ Describe $CommandName -Tag IntegrationTests {
102106
}
103107
}
104108

105-
Context "RuleType Port (traditional port-based rules)" {
109+
Context "RuleType Port (traditional port-based rules)" -Skip:$isUsingDynamicPort {
106110
BeforeAll {
107111
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
108112

tests/Stop-DbaExternalProcess.Tests.ps1

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,29 @@ Describe $CommandName -Tag IntegrationTests {
2626
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
2727
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
2828

29+
$computerName = Resolve-DbaComputerName -ComputerName $TestConfig.InstanceSingle -Property ComputerName
30+
2931
# Enable xp_cmdshell for test process creation
3032
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceSingle -Query "
31-
-- To allow advanced options to be changed.
32-
EXECUTE sp_configure 'show advanced options', 1;
33-
GO
34-
-- To update the currently configured value for advanced options.
35-
RECONFIGURE;
36-
GO
37-
-- To enable the feature.
38-
EXECUTE sp_configure 'xp_cmdshell', 1;
39-
GO
40-
-- To update the currently configured value for this feature.
41-
RECONFIGURE;
42-
GO"
33+
-- To allow advanced options to be changed.
34+
EXECUTE sp_configure 'show advanced options', 1;
35+
GO
36+
-- To update the currently configured value for advanced options.
37+
RECONFIGURE;
38+
GO
39+
-- To enable the feature.
40+
EXECUTE sp_configure 'xp_cmdshell', 1;
41+
GO
42+
-- To update the currently configured value for this feature.
43+
RECONFIGURE;
44+
GO"
45+
46+
# Create sql file with code to start an external process
47+
$sqlFile = "$($TestConfig.Temp)\sleep.sql"
48+
Set-Content -Path $sqlFile -Value "xp_cmdshell 'powershell -command ""sleep 5""'"
4349

44-
# Create a test process to stop
45-
$query = @"
46-
xp_cmdshell 'powershell -command ""sleep 20""'
47-
"@
48-
Start-Process -FilePath sqlcmd -ArgumentList "-S $($TestConfig.InstanceSingle) -Q `"$query`"" -NoNewWindow -RedirectStandardOutput null
50+
# Run sql file to start external process
51+
Start-Process -FilePath sqlcmd -ArgumentList "-S $($TestConfig.InstanceSingle) -i $sqlFile" -NoNewWindow -RedirectStandardOutput null
4952

5053
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
5154
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
@@ -57,24 +60,29 @@ Describe $CommandName -Tag IntegrationTests {
5760

5861
# Disable xp_cmdshell after tests
5962
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceSingle -Query "
60-
EXECUTE sp_configure 'xp_cmdshell', 0;
61-
GO
62-
RECONFIGURE;
63-
GO
64-
EXECUTE sp_configure 'show advanced options', 0;
65-
GO
66-
RECONFIGURE;
67-
GO" -ErrorAction SilentlyContinue
63+
EXECUTE sp_configure 'xp_cmdshell', 0;
64+
GO
65+
RECONFIGURE;
66+
GO
67+
EXECUTE sp_configure 'show advanced options', 0;
68+
GO
69+
RECONFIGURE;
70+
GO"
71+
72+
# remove sql file
73+
Remove-Item -Path $sqlFile
6874

6975
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
7076
}
7177

7278
Context "Can stop an external process" {
7379
It "returns results" {
74-
$results = Get-DbaExternalProcess -ComputerName localhost | Select-Object -First 1 | Stop-DbaExternalProcess
75-
$results.ComputerName | Should -Be "localhost"
80+
Start-Sleep -Seconds 1
81+
$results = Get-DbaExternalProcess -ComputerName $computerName | Select-Object -First 1 | Stop-DbaExternalProcess
82+
Start-Sleep -Seconds 5
83+
$results.ComputerName | Should -Be $computerName
7684
$results.Name | Should -Be "cmd.exe"
77-
$results.ProcessId | Should -Not -Be $null
85+
$results.ProcessId | Should -Not -BeNullOrEmpty
7886
$results.Status | Should -Be "Stopped"
7987
}
8088
}

tests/Test-DbaInstanceName.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Describe $CommandName -Tag UnitTests {
2424
Describe $CommandName -Tag IntegrationTests {
2525
Context "Command tests servername" {
2626
BeforeAll {
27-
$results = Test-DbaInstanceName -SqlInstance $TestConfig.InstanceSingle
27+
$results = Test-DbaInstanceName -SqlInstance $TestConfig.InstanceSingle -WarningAction SilentlyContinue
28+
# "-WarningAction SilentlyContinue" because on a cluster we get this warning:
29+
# "$instance is a cluster. Renaming clusters is not supported by Microsoft."
2830
}
2931

3032
It "should say rename is not required" {

tests/Test-DbaLinkedServerConnection.Tests.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Describe $CommandName -Tag UnitTests {
2222

2323
Describe $CommandName -Tag IntegrationTests {
2424
BeforeAll {
25+
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
26+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
27+
2528
$target = $TestConfig.InstanceSingle
2629

2730
$server = Connect-DbaInstance -SqlInstance $TestConfig.InstanceSingle
@@ -34,10 +37,18 @@ Describe $CommandName -Tag IntegrationTests {
3437
# AppVeyor images do not have the MSOLEDBSQL provider installed, so we use SQLNCLI11 instead
3538
$server.Query("EXEC master.dbo.sp_addlinkedserver @server=N'$target', @srvproduct=N'SQL Server'")
3639
}
40+
41+
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
42+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
3743
}
3844

3945
AfterAll {
46+
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
47+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
48+
4049
$server.Query("EXEC master.dbo.sp_dropserver @server=N'$target'")
50+
51+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
4152
}
4253

4354
Context "Function works" {

0 commit comments

Comments
 (0)