Skip to content

Commit 18806d2

Browse files
committed
finally have a working solution
1 parent 0428fe3 commit 18806d2

2 files changed

Lines changed: 36 additions & 24 deletions

File tree

tests/Get-DbaExternalProcess.Tests.ps1

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ 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.InstanceRestart -Property ComputerName
28+
$computerName = Resolve-DbaComputerName -ComputerName $TestConfig.InstanceSingle -Property ComputerName
2929

3030
# Setup xp_cmdshell to create external processes for testing
31-
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceRestart -Query "
31+
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceSingle -Query "
3232
-- To allow advanced options to be changed.
3333
EXECUTE sp_configure 'show advanced options', 1;
3434
GO
@@ -47,7 +47,7 @@ Describe $CommandName -Tag IntegrationTests {
4747
Set-Content -Path $sqlFile -Value "xp_cmdshell 'powershell -command ""sleep 5""'"
4848

4949
# Run sql file to start external process
50-
Start-Process -FilePath sqlcmd -ArgumentList "-S $($TestConfig.InstanceRestart) -i $sqlFile" -NoNewWindow -RedirectStandardOutput null
50+
Start-Process -FilePath sqlcmd -ArgumentList "-S $($TestConfig.InstanceSingle) -i $sqlFile" -NoNewWindow -RedirectStandardOutput null
5151

5252
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
5353
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
@@ -58,7 +58,7 @@ Describe $CommandName -Tag IntegrationTests {
5858
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
5959

6060
# Cleanup: Disable xp_cmdshell for security
61-
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceRestart -Query "
61+
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceSingle -Query "
6262
EXECUTE sp_configure 'xp_cmdshell', 0;
6363
GO
6464
RECONFIGURE;
@@ -68,20 +68,26 @@ Describe $CommandName -Tag IntegrationTests {
6868
RECONFIGURE;
6969
GO"
7070

71-
# We need to restart the instance to ensure that the xp_cmdshell process has ended before we can remove the sql file.
72-
$null = Restart-DbaService -SqlInstance $TestConfig.InstanceRestart -Type Engine -Force
73-
74-
# Remove sql file
75-
Remove-Item -Path $sqlFile
71+
1..20 | ForEach-Object {
72+
try {
73+
Remove-Item -Path $sqlFile
74+
break
75+
} catch {
76+
Start-Sleep -Seconds 1
77+
}
78+
}
79+
if (Test-Path -Path $sqlFile) {
80+
Write-Warning -Message "Could not delete temporary SQL file at $sqlFile. Please check and delete manually."
81+
}
7682

7783
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
7884
}
7985

8086
Context "Can get an external process" {
8187
It "returns a process" {
8288
1..10 | ForEach-Object {
83-
$results = Get-DbaExternalProcess -ComputerName $computerName | Where-Object Name -eq "cmd.exe"
84-
if ($results) { return }
89+
$results = Get-DbaExternalProcess -ComputerName $computerName
90+
if ($results) { break }
8591
Start-Sleep -Milliseconds 500
8692
}
8793
$results.ComputerName | Should -Be $computerName

tests/Stop-DbaExternalProcess.Tests.ps1

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ 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.InstanceRestart -Property ComputerName
29+
$computerName = Resolve-DbaComputerName -ComputerName $TestConfig.InstanceSingle -Property ComputerName
3030

31-
# Enable xp_cmdshell for test process creation
32-
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceRestart -Query "
31+
# Setup xp_cmdshell to create external processes for testing
32+
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceSingle -Query "
3333
-- To allow advanced options to be changed.
3434
EXECUTE sp_configure 'show advanced options', 1;
3535
GO
@@ -48,7 +48,7 @@ Describe $CommandName -Tag IntegrationTests {
4848
Set-Content -Path $sqlFile -Value "xp_cmdshell 'powershell -command ""sleep 5""'"
4949

5050
# Run sql file to start external process
51-
Start-Process -FilePath sqlcmd -ArgumentList "-S $($TestConfig.InstanceRestart) -i $sqlFile" -NoNewWindow -RedirectStandardOutput null
51+
Start-Process -FilePath sqlcmd -ArgumentList "-S $($TestConfig.InstanceSingle) -i $sqlFile" -NoNewWindow -RedirectStandardOutput null
5252

5353
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
5454
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
@@ -58,8 +58,8 @@ Describe $CommandName -Tag IntegrationTests {
5858
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
5959
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
6060

61-
# Disable xp_cmdshell after tests
62-
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceRestart -Query "
61+
# Cleanup: Disable xp_cmdshell for security
62+
$null = Invoke-DbaQuery -SqlInstance $TestConfig.InstanceSingle -Query "
6363
EXECUTE sp_configure 'xp_cmdshell', 0;
6464
GO
6565
RECONFIGURE;
@@ -69,20 +69,26 @@ Describe $CommandName -Tag IntegrationTests {
6969
RECONFIGURE;
7070
GO"
7171

72-
# We need to restart the instance to ensure that the xp_cmdshell process has ended before we can remove the sql file.
73-
$null = Restart-DbaService -SqlInstance $TestConfig.InstanceRestart -Type Engine -Force
74-
75-
# Remove sql file
76-
Remove-Item -Path $sqlFile
72+
1..20 | ForEach-Object {
73+
try {
74+
Remove-Item -Path $sqlFile
75+
break
76+
} catch {
77+
Start-Sleep -Seconds 1
78+
}
79+
}
80+
if (Test-Path -Path $sqlFile) {
81+
Write-Warning -Message "Could not delete temporary SQL file at $sqlFile. Please check and delete manually."
82+
}
7783

7884
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
7985
}
8086

8187
Context "Can stop an external process" {
8288
It "returns results" {
8389
1..10 | ForEach-Object {
84-
$results = Get-DbaExternalProcess -ComputerName $computerName | Where-Object Name -eq "cmd.exe" | Stop-DbaExternalProcess
85-
if ($results) { return }
90+
$results = Get-DbaExternalProcess -ComputerName $computerName | Stop-DbaExternalProcess
91+
if ($results) { break }
8692
Start-Sleep -Milliseconds 500
8793
}
8894
$results.ComputerName | Should -Be $computerName

0 commit comments

Comments
 (0)