summaryrefslogtreecommitdiff
path: root/test/integration/common.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/integration/common.rb')
-rw-r--r--test/integration/common.rb66
1 files changed, 55 insertions, 11 deletions
diff --git a/test/integration/common.rb b/test/integration/common.rb
index 6295ada..b897ece 100644
--- a/test/integration/common.rb
+++ b/test/integration/common.rb
@@ -64,11 +64,13 @@ module IntegrationTestHelpers
pid, status = Process.wait2 pid
end
raise "Command: #{command} failed:#{status.exitstatus}" unless status
+
status.exitstatus
end
def with_sshd_config(sshd_config, &block)
raise "Failed to copy config" unless system("sudo cp -f /etc/ssh/sshd_config /etc/ssh/sshd_config.original")
+
begin
Tempfile.open('sshd_config') do |f|
f.write(sshd_config)
@@ -77,6 +79,7 @@ module IntegrationTestHelpers
end
system("sudo chmod 0644 /etc/ssh/sshd_config")
raise "Failed to restart sshd" unless system("sudo service ssh restart")
+
yield
ensure
system("sudo cp -f /etc/ssh/sshd_config.original /etc/ssh/sshd_config")
@@ -84,33 +87,74 @@ module IntegrationTestHelpers
end
end
- def with_lines_as_tempfile(lines = [], &block)
+ def with_lines_as_tempfile(lines = [], add_pid: true, debug: false, &block)
Tempfile.open('sshd_config') do |f|
- f.write(lines)
+ f.write(lines.join("\n"))
+ pidpath = nil
+ if add_pid
+ pidpath = f.path + '.pid'
+ f.write("\nPidFile #{pidpath}\n")
+ end
+ f.write("\nLogLevel DEBUG3\n") if debug
f.close
- yield(f.path)
+ puts "CONFIG: #{f.path} PID: #{pidpath}" if debug
+ yield(f.path, pidpath)
end
end
+ def port_open?(path)
+ Socket.tcp("localhost", 10567, connect_timeout: 1) { true } rescue false # rubocop:disable Style/RescueModifier
+ end
+
# @yield [pid, port]
- def start_sshd_7_or_later(port = '2200', config: nil)
+ def start_sshd_7_or_later(port = '2200', config: nil, debug: false)
pid = nil
+ sshpidfile = nil
if config
- with_lines_as_tempfile(config) do |path|
- pid = spawn('sudo', '/opt/net-ssh-openssh/sbin/sshd', '-D', '-f', path, '-p', port)
+ with_lines_as_tempfile(config, debug: debug) do |path, pidpath|
+ puts "DEBUG - SSH LOG: #{path}-log.txt config: #{path}" if debug
+ raise "A leftover sshd is already running" if port_open?(port)
+
+ extra_params = []
+ extra_params = ['-E', "#{path}-log.txt"] if debug
+ pid = spawn('sudo', '/opt/net-ssh-openssh/sbin/sshd', '-D', '-f', path, '-p', port, *extra_params)
+ sshpidfile = pidpath
yield pid, port
end
else
- pid = spawn('sudo', '/opt/net-ssh-openssh/sbin/sshd', '-D', '-p', port)
- yield pid, port
+ with_lines_as_tempfile(['']) do |path, pidpath|
+ pid = spawn('sudo', '/opt/net-ssh-openssh/sbin/sshd', '-D', '-f', path, '-p', port)
+ sshpidfile = pidpath
+ yield pid, port
+ end
end
ensure
- # Our pid is sudo, -9 (KILL) on sudo will not clean up its children
+ # Our pid is sudo and not sshd, -9 (KILL) on sudo will not clean up its children
# properly, so we just have to hope that -15 (TERM) will manage to bring
# down sshd.
- if pid
+ if sshpidfile
+ sshpid = File.read(sshpidfile).strip
+ system('sudo', 'kill', '-15', sshpid.to_s)
+ begin
+ Timeout.timeout(20) do
+ Process.wait(pid)
+ end
+ rescue Timeout::Error
+ warn "Failed to kill openssh process: #{sshpid}"
+ system('sudo', 'kill', '-9', sshpid.to_s)
+ raise
+ end
+ elsif pid
system('sudo', 'kill', '-15', pid.to_s)
- Process.wait(pid)
+ begin
+ Timeout.timeout(20) do
+ Process.wait(pid)
+ end
+ rescue Timeout::Error
+ warn "Failed to kill openssh process: #{pid}"
+ system('sudo', 'kill', '-9', pid.to_s)
+ raise
+ end
end
end