Skip to content

Commit 1137942

Browse files
authored
Refactored pester based E2E test setup (#384)
- Set up dedicated test endpoint instead of tweaking main service configuration. - Got rid of redundant custom test configuration files. - Cleaned up pester tests directory. Moved test filed to dedicated directory - Revised TestDaemon start/stop routines to use "netstat" for host process identification. This gets rid of slight flakiness in previous approach.
1 parent 3d35b91 commit 1137942

25 files changed

+203
-339
lines changed

contrib/win32/openssh/OpenSSHTestHelper.psm1

Lines changed: 82 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $PubKeyUser = "sshtest_pubkeyuser"
1515
$PasswdUser = "sshtest_passwduser"
1616
$OpenSSHTestAccountsPassword = "P@ssw0rd_1"
1717
$OpenSSHTestAccounts = $Script:SSOUser, $Script:PubKeyUser, $Script:PasswdUser
18-
$OpenSSHConfigPath = Join-Path $env:ProgramData "ssh"
18+
$SSHDTestSvcName = "sshdTestSvc"
1919

2020
$Script:TestDataPath = "$env:SystemDrive\OpenSSHTests"
2121
$Script:SetupTestResultsFile = Join-Path $TestDataPath $SetupTestResultsFileName
@@ -24,6 +24,7 @@ $Script:E2ETestResultsFile = Join-Path $TestDataPath $E2ETestResultsFileName
2424
$Script:UnitTestResultsFile = Join-Path $TestDataPath $UnitTestResultsFileName
2525
$Script:TestSetupLogFile = Join-Path $TestDataPath $TestSetupLogFileName
2626
$Script:E2ETestDirectory = Join-Path $repositoryRoot.FullName -ChildPath "regress\pesterTests"
27+
$Script:E2ETestDataDirectory = Join-Path $Script:E2ETestDirectory data
2728
$Script:WindowsInBox = $false
2829
$Script:NoLibreSSL = $false
2930
$Script:EnableAppVerifier = $true
@@ -81,19 +82,11 @@ function Set-OpenSSHTestEnvironment
8182
}
8283
$Global:OpenSSHTestInfo.Add("PostmortemDebugging", $Script:PostmortemDebugging)
8384

84-
#start service if not already started
85-
Start-Service -Name sshd
86-
8785
$description = @"
8886
WARNING: Following changes will be made to OpenSSH configuration
89-
- sshd_config will be backed up as sshd_config.ori
90-
- will be replaced with a test sshd_config
91-
- $HOME\.ssh\known_hosts will be backed up as known_hosts.ori
92-
- will be replaced with a test known_hosts
93-
- $HOME\.ssh\config will be backed up as config.ori
94-
- will be replaced with a test config
87+
- $HOME\.ssh\known_hosts will be amended with test endpoint entries
88+
- $HOME\.ssh\config will be amended with test endpoint entries
9589
- sshd test listener will be on port 47002
96-
- $HOME\.ssh\known_hosts will be modified with test host key entry
9790
- test accounts - ssouser, pubkeyuser, and passwduser will be added
9891
- Setup single signon for ssouser
9992
- To cleanup - Run Clear-OpenSSHTestEnvironment
@@ -109,60 +102,96 @@ WARNING: Following changes will be made to OpenSSH configuration
109102

110103
Install-OpenSSHTestDependencies
111104

112-
$backupConfigPath = Join-Path $OpenSSHConfigPath sshd_config.ori
113-
$targetsshdConfig = Join-Path $OpenSSHConfigPath sshd_config
114-
#Backup existing OpenSSH configuration
115-
if ((Test-Path $targetsshdConfig -PathType Leaf) -and (-not (Test-Path $backupConfigPath -PathType Leaf))) {
116-
Copy-Item $targetsshdConfig $backupConfigPath -Force
117-
}
118-
# copy new sshd_config
119-
Copy-Item (Join-Path $Script:E2ETestDirectory sshd_config) $targetsshdConfig -Force
120-
if($DebugMode) {
121-
$con = (Get-Content $targetsshdConfig | Out-String).Replace("#SyslogFacility AUTH","SyslogFacility LOCAL0")
122-
Set-Content -Path $targetsshdConfig -Value "$con" -Force
123-
}
124-
$sshAgentSvc = Get-service ssh-agent
125-
if($sshAgentSvc.StartType -eq [System.ServiceProcess.ServiceStartMode]::Disabled)
105+
106+
##### START: install sshd test service
107+
#delete service if exists
108+
if (Get-Service $SSHDTestSvcName -ErrorAction SilentlyContinue)
126109
{
127-
Set-service ssh-agent -StartupType Manual
110+
Stop-Service $SSHDTestSvcName
111+
sc.exe delete $SSHDTestSvcName 1>$null
128112
}
129-
Start-Service ssh-agent
130113

131-
#copy sshtest keys
132-
Copy-Item "$($Script:E2ETestDirectory)\sshtest*hostkey*" $OpenSSHConfigPath -Force
133-
Get-ChildItem "$($OpenSSHConfigPath)\sshtest*hostkey*" -Exclude *.pub| % {
134-
Repair-SshdHostKeyPermission -FilePath $_.FullName -confirm:$false
114+
#prepare config directory
115+
$testSvcConfigDir = Join-Path $Global:OpenSSHTestInfo["TestDataPath"] "serviceconfig"
116+
Remove-Item $testSvcConfigDir -Force -Recurse -ErrorAction SilentlyContinue
117+
New-Item -ItemType Directory -Path $testSvcConfigDir
118+
$Global:OpenSSHTestInfo["ServiceConfigDir"] = $testSvcConfigDir
119+
120+
#copy sshd_config
121+
$testSshdConfig = Join-Path $testSvcConfigDir sshd_config
122+
Copy-Item (Join-Path $Script:E2ETestDataDirectory sshd_config) $testSshdConfig -Force
123+
$con = (Get-Content $testSshdConfig | Out-String).Replace("___TEST_SERVICE_CONFIG_DIR___", $testSvcConfigDir)
124+
Set-Content -Path $testSshdConfig -Value "$con" -Force
125+
if($DebugMode) {
126+
$con = (Get-Content $testSshdConfig | Out-String).Replace("#SyslogFacility AUTH","SyslogFacility LOCAL0")
127+
Set-Content -Path $testSshdConfig -Value "$con" -Force
135128
}
136129

130+
#copy sshtest keys
131+
Copy-Item "$($Script:E2ETestDataDirectory)\sshtest*hostkey*" $testSvcConfigDir -Force
132+
137133
#copy ca pubkey to ssh config path
138-
Copy-Item "$($Script:E2ETestDirectory)\sshtest_ca_userkeys.pub" $OpenSSHConfigPath -Force
134+
Copy-Item "$($Script:E2ETestDataDirectory)\sshtest_ca_userkeys.pub" $testSvcConfigDir -Force
135+
136+
$acl = New-Object System.Security.AccessControl.DirectorySecurity
137+
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Administrators","FullControl","Allow")
138+
$acl.AddAccessRule($rule)
139+
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("System","FullControl","Allow")
140+
$acl.AddAccessRule($rule)
141+
$acl.SetAccessRuleProtection($true, $true)
142+
143+
Get-ChildItem $testSvcConfigDir | foreach {$acl | set-acl $_.FullName}
144+
145+
146+
$SSHDTestSvcNameCmdLine = (Join-Path $script:OpenSSHBinPath sshd) + " -f " + $testSshdConfig
147+
New-Service -Name $SSHDTestSvcName -DisplayName "OpenSSH SSH Test Server for E2E tests" -BinaryPathName $SSHDTestSvcNameCmdLine -StartupType Manual | Out-Null
148+
sc.exe privs $SSHDTestSvcName SeAssignPrimaryTokenPrivilege/SeTcbPrivilege/SeBackupPrivilege/SeRestorePrivilege/SeImpersonatePrivilege
149+
150+
Start-Service $SSHDTestSvcName
151+
##### END: install sshd test service
152+
139153

140154
#copy ca private key to test dir
141155
$ca_priv_key = (Join-Path $Global:OpenSSHTestInfo["TestDataPath"] sshtest_ca_userkeys)
142-
Copy-Item (Join-Path $Script:E2ETestDirectory sshtest_ca_userkeys) $ca_priv_key -Force
156+
Copy-Item (Join-Path $Script:E2ETestDataDirectory sshtest_ca_userkeys) $ca_priv_key -Force
143157
Repair-UserSshConfigPermission -FilePath $ca_priv_key -confirm:$false
144158
$Global:OpenSSHTestInfo["CA_Private_Key"] = $ca_priv_key
145159

146-
Restart-Service sshd -Force
147-
148-
#Backup existing known_hosts and replace with test version
149-
#TODO - account for custom known_hosts locations
160+
$sshAgentSvc = Get-service ssh-agent
161+
if($sshAgentSvc.StartType -eq [System.ServiceProcess.ServiceStartMode]::Disabled)
162+
{
163+
Set-service ssh-agent -StartupType Manual
164+
}
165+
Start-Service ssh-agent
166+
167+
168+
169+
#Prepare user config - known_hosts and ssh_config
150170
$dotSshDirectoryPath = Join-Path $home .ssh
151-
$knowHostsFilePath = Join-Path $dotSshDirectoryPath known_hosts
152171
if(-not (Test-Path $dotSshDirectoryPath -PathType Container))
153172
{
154173
New-Item -ItemType Directory -Path $dotSshDirectoryPath -Force -ErrorAction SilentlyContinue | out-null
155174
}
156-
if ((Test-Path $knowHostsFilePath -PathType Leaf) -and (-not (Test-Path (Join-Path $dotSshDirectoryPath known_hosts.ori) -PathType Leaf))) {
157-
Copy-Item $knowHostsFilePath (Join-Path $dotSshDirectoryPath known_hosts.ori) -Force
175+
176+
$knowHostsFilePath = Join-Path $dotSshDirectoryPath known_hosts
177+
if (-not (Test-Path $knowHostsFilePath -PathType Leaf)) {
178+
Copy-Item (Join-Path $Script:E2ETestDataDirectory known_hosts) $knowHostsFilePath -Force
179+
}
180+
$con = Get-Content $knowHostsFilePath
181+
if (($con -eq $null) -or (-not($con.Contains("###OpenSSHE2ETests")))) {
182+
Get-Content (Join-Path $Script:E2ETestDataDirectory known_hosts) | Add-Content $knowHostsFilePath
158183
}
159-
Copy-Item (Join-Path $Script:E2ETestDirectory known_hosts) $knowHostsFilePath -Force
160184

161185
$sshConfigFilePath = Join-Path $dotSshDirectoryPath config
162-
if ((Test-Path $sshConfigFilePath -PathType Leaf) -and (-not (Test-Path (Join-Path $dotSshDirectoryPath config.ori) -PathType Leaf))) {
163-
Copy-Item $sshConfigFilePath (Join-Path $dotSshDirectoryPath config.ori) -Force
186+
if (-not (Test-Path (Join-Path $dotSshDirectoryPath config) -PathType Leaf)) {
187+
Copy-Item (Join-Path $Script:E2ETestDataDirectory ssh_config) $sshConfigFilePath -Force
188+
}
189+
$con = Get-Content $sshConfigFilePath
190+
if (($con -eq $null) -or (-not($con.Contains("###OpenSSHE2ETests")))) {
191+
Get-Content (Join-Path $Script:E2ETestDataDirectory ssh_config) | Add-Content $sshConfigFilePath
164192
}
165-
Copy-Item (Join-Path $Script:E2ETestDirectory ssh_config) $sshConfigFilePath -Force
193+
194+
Copy-Item (Join-Path $Script:E2ETestDataDirectory ssh_config) $sshConfigFilePath -Force
166195
Repair-UserSshConfigPermission -FilePath $sshConfigFilePath -confirm:$false
167196

168197
# create test accounts
@@ -188,11 +217,11 @@ WARNING: Following changes will be made to OpenSSH configuration
188217

189218
New-Item -ItemType Directory -Path (Join-Path $ssouserProfile .ssh) -Force -ErrorAction SilentlyContinue | out-null
190219
$authorizedKeyPath = Join-Path $ssouserProfile .ssh\authorized_keys
191-
$testPubKeyPath = Join-Path $Script:E2ETestDirectory sshtest_userssokey_ed25519.pub
220+
$testPubKeyPath = Join-Path $Script:E2ETestDataDirectory sshtest_userssokey_ed25519.pub
192221
Copy-Item $testPubKeyPath $authorizedKeyPath -Force -ErrorAction SilentlyContinue
193222
Repair-AuthorizedKeyPermission -FilePath $authorizedKeyPath -confirm:$false
194223

195-
copy-item (Join-Path $Script:E2ETestDirectory sshtest_userssokey_ed25519) $Global:OpenSSHTestInfo["TestDataPath"]
224+
copy-item (Join-Path $Script:E2ETestDataDirectory sshtest_userssokey_ed25519) $Global:OpenSSHTestInfo["TestDataPath"]
196225
$testPriKeypath = Join-Path $Global:OpenSSHTestInfo["TestDataPath"] sshtest_userssokey_ed25519
197226
cmd /c "ssh-add -D 2>&1 >> $Script:TestSetupLogFile"
198227
Repair-UserKeyPermission -FilePath $testPriKeypath -confirm:$false
@@ -214,8 +243,6 @@ WARNING: Following changes will be made to OpenSSH configuration
214243
New-ItemProperty "HKLM:Software\Microsoft\Windows NT\CurrentVersion\AeDebug" -Name Auto -Type String -Value "1" -Force -ErrorAction SilentlyContinue | Out-Null
215244
}
216245
}
217-
218-
Backup-OpenSSHTestInfo
219246
}
220247

221248
function Set-BasicTestInfo
@@ -504,42 +531,22 @@ function Clear-OpenSSHTestEnvironment
504531
Remove-ItemProperty "HKLM:Software\Microsoft\Windows NT\CurrentVersion\AeDebug" -Name Debugger -ErrorAction SilentlyContinue -Force | Out-Null
505532
Remove-ItemProperty "HKLM:Software\Microsoft\Windows NT\CurrentVersion\AeDebug" -Name Auto -ErrorAction SilentlyContinue -Force | Out-Null
506533
}
507-
508-
Remove-Item "$OpenSSHConfigPath\sshtest*hostkey*" -Force -ErrorAction SilentlyContinue
509-
Remove-Item "$OpenSSHConfigPath\sshtest*ca_userkeys*" -Force -ErrorAction SilentlyContinue
510-
511-
#Restore sshd_config
512-
$backupConfigPath = Join-Path $OpenSSHConfigPath sshd_config.ori
513-
if (Test-Path $backupConfigPath -PathType Leaf) {
514-
Copy-Item $backupConfigPath (Join-Path $OpenSSHConfigPath sshd_config) -Force -ErrorAction SilentlyContinue
515-
Remove-Item (Join-Path $OpenSSHConfigPath sshd_config.ori) -Force -ErrorAction SilentlyContinue
516-
Restart-Service sshd
517-
}
518-
519-
#Restore known_hosts
520-
$originKnowHostsPath = Join-Path $home .ssh\known_hosts.ori
521-
if (Test-Path $originKnowHostsPath)
522-
{
523-
Copy-Item $originKnowHostsPath (Join-Path $home .ssh\known_hosts) -Force -ErrorAction SilentlyContinue
524-
Remove-Item $originKnowHostsPath -Force -ErrorAction SilentlyContinue
525-
}
526534

527-
#Restore ssh_config
528-
$originConfigPath = Join-Path $home .ssh\config.ori
529-
if (Test-Path $originConfigPath)
535+
#delete service if exists
536+
if (Get-Service $SSHDTestSvcName -ErrorAction SilentlyContinue)
530537
{
531-
Copy-Item $originConfigPath (Join-Path $home .ssh\config) -Force -ErrorAction SilentlyContinue
532-
Remove-Item $originConfigPath -Force -ErrorAction SilentlyContinue
538+
Stop-Service $SSHDTestSvcName
539+
sc.exe delete $SSHDTestSvcName 1>$null
533540
}
534-
541+
535542
#Delete accounts
536543
foreach ($user in $OpenSSHTestAccounts)
537544
{
538545
net user $user /delete
539546
}
540547

541548
# remove registered keys
542-
cmd /c "ssh-add -d (Join-Path $Script:E2ETestDirectory sshtest_userssokey_ed25519) 2>&1 >> $Script:TestSetupLogFile"
549+
cmd /c "ssh-add -d (Join-Path $Script:E2ETestDataDirectory sshtest_userssokey_ed25519) 2>&1 >> $Script:TestSetupLogFile"
543550

544551
if($Global:OpenSSHTestInfo -ne $null)
545552
{
@@ -728,56 +735,6 @@ function Invoke-OpenSSHUnitTest
728735
$testfailed
729736
}
730737

731-
function Backup-OpenSSHTestInfo
732-
{
733-
param
734-
(
735-
[string] $BackupFile = $null
736-
)
737-
738-
if ($Global:OpenSSHTestInfo -eq $null) {
739-
Throw "`$OpenSSHTestInfo is null. Did you run Set-OpenSSHTestEnvironment yet?"
740-
}
741-
742-
$testInfo = $Global:OpenSSHTestInfo
743-
744-
if ([String]::IsNullOrEmpty($BackupFile)) {
745-
$BackupFile = Join-Path $testInfo["TestDataPath"] "OpenSSHTestInfo_backup.txt"
746-
}
747-
748-
$null | Set-Content $BackupFile
749-
750-
foreach ($key in $testInfo.Keys) {
751-
$value = $testInfo[$key]
752-
Add-Content $BackupFile "$key,$value"
753-
}
754-
}
755-
756-
function Restore-OpenSSHTestInfo
757-
{
758-
param
759-
(
760-
[Parameter(Mandatory=$true)]
761-
[ValidateNotNullOrEmpty()]
762-
[string] $BackupFile
763-
)
764-
765-
if($Global:OpenSSHTestInfo -ne $null)
766-
{
767-
$Global:OpenSSHTestInfo.Clear()
768-
$Global:OpenSSHTestInfo = $null
769-
}
770-
771-
$Global:OpenSSHTestInfo = @{}
772-
773-
$entries = Get-Content $BackupFile
774-
775-
foreach ($entry in $entries) {
776-
$data = $entry.Split(",")
777-
$Global:OpenSSHTestInfo[$data[0]] = $data[1]
778-
}
779-
}
780-
781738
<#
782739
Write-Log
783740
#>
@@ -799,4 +756,4 @@ function Write-Log
799756
}
800757
}
801758

802-
Export-ModuleMember -Function Set-BasicTestInfo, Set-OpenSSHTestEnvironment, Clear-OpenSSHTestEnvironment, Invoke-OpenSSHSetupTest, Invoke-OpenSSHUnitTest, Invoke-OpenSSHE2ETest, Invoke-OpenSSHUninstallTest, Backup-OpenSSHTestInfo, Restore-OpenSSHTestInfo
759+
Export-ModuleMember -Function Set-BasicTestInfo, Set-OpenSSHTestEnvironment, Clear-OpenSSHTestEnvironment, Invoke-OpenSSHSetupTest, Invoke-OpenSSHUnitTest, Invoke-OpenSSHE2ETest, Invoke-OpenSSHUninstallTest

0 commit comments

Comments
 (0)