Skip to content

Commit

Permalink
Using service ssh restart for Ubuntu, Debian
Browse files Browse the repository at this point in the history
Fix Rubocop

Fixed failing test

Update RHELPLATFORMS

Fix Robocop
  • Loading branch information
skyamgarp committed May 24, 2024
1 parent 0d89e6a commit 1c686d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/beaker/host/unix/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ def clear_env_var key
# @return [Result] result of restarting the SSH service
def ssh_service_restart
case self['platform']
when /debian|ubuntu/
exec(Beaker::Command.new("systemctl restart ssh"))
when /(el|centos|redhat|oracle|scientific)-[0-6]/
exec(Beaker::Command.new("/sbin/service sshd restart"))
when /solaris/
Expand All @@ -302,7 +304,7 @@ def ssh_permit_user_environment
directory = tmpdir
exec(Beaker::Command.new("echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit"))
exec(Beaker::Command.new("mv #{directory}/sshd_config.permit /etc/ssh/sshd_config"))
exec(Beaker::Command.new("echo '' >/etc/environment")) if /ubuntu-2(0|2).04/.match?(self['platform'])
exec(Beaker::Command.new("echo '' >/etc/environment")) if self['platform'].include?('ubuntu-')
when /(free|open)bsd/
exec(Beaker::Command.new("sudo perl -pi -e 's/^#?PermitUserEnvironment no/PermitUserEnvironment yes/' /etc/ssh/sshd_config"), { :pty => true })
else
Expand Down
15 changes: 13 additions & 2 deletions spec/beaker/host/unix/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def to_s
let(:ssh_command) { "echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit" }
let(:ssh_move) { "mv #{directory}/sshd_config.permit /etc/ssh/sshd_config" }

PlatformHelpers::SYSTEMDPLATFORMS.each do |platform|
platforms = PlatformHelpers::RHELPLATFORMS + PlatformHelpers::DEBIANPLATFORMS

platforms.each do |platform|
it "calls the correct commands for #{platform}" do
opts['platform'] = platform
expect(instance).to receive(:exec).twice
Expand All @@ -104,14 +106,23 @@ def to_s
end

describe '#ssh_service_restart' do
PlatformHelpers::SYSTEMDPLATFORMS.each do |platform|
PlatformHelpers::RHELPLATFORMS.each do |platform|
it "calls the correct command for #{platform}" do
opts['platform'] = platform
expect(instance).to receive(:exec)
expect(Beaker::Command).to receive(:new).with("systemctl restart sshd.service")
expect { instance.ssh_service_restart }.not_to raise_error
end
end

PlatformHelpers::DEBIANPLATFORMS.each do |platform|
it "calls the ssh service restart command for #{platform}" do
opts['platform'] = platform
expect(instance).to receive(:exec)
expect(Beaker::Command).to receive(:new).with("systemctl restart ssh")
expect { instance.ssh_service_restart }.not_to raise_error
end
end
end

describe '#prepend_commands' do
Expand Down
18 changes: 10 additions & 8 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ def make_instance instance_data = {}
module PlatformHelpers
DEBIANPLATFORMS = %w[debian ubuntu]

SYSTEMDPLATFORMS = %w[amazon-2023
fedora
el-
centos
redhat
oracle
scientific
archlinux].concat(DEBIANPLATFORMS)
RHELPLATFORMS = %w[
amazon-2023
fedora
el-
centos
redhat
oracle
scientific
archlinux
]
end

0 comments on commit 1c686d2

Please sign in to comment.