Skip to content

Commit

Permalink
Merge pull request #1853 from ekohl/only-install-timesync-packages-if…
Browse files Browse the repository at this point in the history
…-needed

Only install timesync packages if needed
  • Loading branch information
bastelfreak authored Aug 27, 2024
2 parents 33f32b3 + 18e6e22 commit 0e788e0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 94 deletions.
25 changes: 1 addition & 24 deletions acceptance/tests/base/host/packages.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
test_name 'confirm packages on hosts behave correctly'
confine :except, :platform => %w(osx)

def get_host_pkg(host)
case host['platform']
when /sles-10/
Beaker::HostPrebuiltSteps::SLES10_PACKAGES
when /opensuse|sles-/
Beaker::HostPrebuiltSteps::SLES_PACKAGES
when /debian/
Beaker::HostPrebuiltSteps::DEBIAN_PACKAGES
when /windows/
host.is_cygwin? ? Beaker::HostPrebuiltSteps::WINDOWS_PACKAGES : Beaker::HostPrebuiltSteps::PSWINDOWS_PACKAGES
when /freebsd/
Beaker::HostPrebuiltSteps::FREEBSD_PACKAGES
when /openbsd/
Beaker::HostPrebuiltSteps::OPENBSD_PACKAGES
when /solaris-10/
Beaker::HostPrebuiltSteps::SOLARIS10_PACKAGES
when /el-[89]/
Beaker::HostPrebuiltSteps::RHEL8_PACKAGES
else
Beaker::HostPrebuiltSteps::UNIX_PACKAGES
end
end

step '#check_for_command : can determine where a command exists'
hosts.each do |host|
logger.debug "echo package should be installed on #{host}"
Expand All @@ -34,7 +11,7 @@ def get_host_pkg(host)

step '#check_for_package : can determine if a package is installed'
hosts.each do |host|
package = get_host_pkg(host)[0]
package = 'bash'

logger.debug "#{package} package should be installed on #{host}"
assert(host.check_for_package(package), "'#{package}' should be installed")
Expand Down
66 changes: 8 additions & 58 deletions lib/beaker/host_prebuilt_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ module HostPrebuiltSteps
NTPSERVER = 'pool.ntp.org'
SLEEPWAIT = 5
TRIES = 5
AMAZON2023_PACKAGES = %w[chrony]
RHEL8_PACKAGES = %w[chrony iputils] # iputils provides ping. beaker assumes that's present
FEDORA_PACKAGES = %w[chrony iputils]
UNIX_PACKAGES = %w[curl ntpdate]
FREEBSD_PACKAGES = ['curl', 'perl5|perl']
OPENBSD_PACKAGES = ['curl']
ARCHLINUX_PACKAGES = %w[curl ntp net-tools openssh]
WINDOWS_PACKAGES = ['curl']
PSWINDOWS_PACKAGES = []
SLES10_PACKAGES = ['curl']
SLES_PACKAGES = %w[curl ntp]
DEBIAN_PACKAGES = %w[curl ntpdate lsb-release apt-transport-https]
SOLARIS10_PACKAGES = %w[CSWcurl CSWntp wget]
SOLARIS11_PACKAGES = %w[curl ntp]
ETC_HOSTS_PATH = "/etc/hosts"
ETC_HOSTS_PATH_SOLARIS = "/etc/inet/hosts"
ROOT_KEYS_SCRIPT = "https://raw.githubusercontent.com/puppetlabs/puppetlabs-sshkeys/master/templates/scripts/manage_root_authorized_keys"
Expand All @@ -48,7 +34,7 @@ def timesync host, opts
host.exec(Command.new("w32tm /resync"))
logger.notify "NTP date succeeded on #{host}"
else
if /amazon|el-[89]|fedora/.match?(host['platform'])
if host['platform'].uses_chrony?
ntp_command = "chronyc add server #{ntp_server} prefer trust;chronyc makestep;chronyc burst 1/2"
elsif /opensuse-|sles-/.match?(host['platform'])
ntp_command = "sntp #{ntp_server}"
Expand Down Expand Up @@ -79,12 +65,6 @@ def timesync host, opts
# Validate that hosts are prepared to be used as SUTs, if packages are missing attempt to
# install them.
#
# Verifies the presence of #{HostPrebuiltSteps::UNIX_PACKAGES} on unix platform hosts,
# {HostPrebuiltSteps::SLES_PACKAGES} on SUSE platform hosts,
# {HostPrebuiltSteps::DEBIAN_PACKAGES} on debian platform hosts,
# {HostPrebuiltSteps::WINDOWS_PACKAGES} on cygwin-installed windows platform hosts,
# and {HostPrebuiltSteps::PSWINDOWS_PACKAGES} on non-cygwin windows platform hosts.
#
# @param [Host, Array<Host>, String, Symbol] host One or more hosts to act upon
# @param [Hash{Symbol=>String}] opts Options to alter execution.
# @option opts [Beaker::Logger] :logger A {Beaker::Logger} object
Expand All @@ -102,44 +82,14 @@ def validate_host host, opts
# @param [Host] host A host return the packages for
# @return [Array<String>] A list of packages to install
def host_packages(host)
case host['platform']
when /amazon/
AMAZON2023_PACKAGES
when /el-[89]/
RHEL8_PACKAGES
when /sles-10/
SLES10_PACKAGES
when /opensuse|sles-/
SLES_PACKAGES
when /debian/
DEBIAN_PACKAGES
when /windows/
if host.is_cygwin?
raise RuntimeError, "cygwin is not installed on #{host}" if !host.cygwin_installed?

WINDOWS_PACKAGES
else
PSWINDOWS_PACKAGES
end
when /freebsd/
FREEBSD_PACKAGES
when /openbsd/
OPENBSD_PACKAGES
when /solaris-10/
SOLARIS10_PACKAGES
when /solaris-1[1-9]/
SOLARIS11_PACKAGES
when /archlinux/
ARCHLINUX_PACKAGES
when /fedora/
FEDORA_PACKAGES
else
if !/aix|solaris|osx-/.match?(host['platform'])
UNIX_PACKAGES
else
[]
end
packages = host['platform'].base_packages
if host.is_cygwin?
raise RuntimeError, "cygwin is not installed on #{host}" if !host.cygwin_installed?

packages << 'curl'
end
packages += host['platform'].timesync_packages if host[:timesync]
packages
end

# Installs the given packages if they aren't already on a host
Expand Down
55 changes: 55 additions & 0 deletions lib/beaker/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,60 @@ def with_version_codename
def with_version_number
[@variant, @version, @arch].join('-')
end

def uses_chrony?
case @variant
when 'amazon', 'fedora'
true
when 'el'
@version.to_i >= 8
else
false
end
end

# Return a list of packages that should always be present.
#
# @return [Array<String>] A list of packages to install
def base_packages
case @variant
when 'el'
@version.to_i >= 8 ? ['curl-minimal', 'iputils'] : %w[curl]
when 'debian'
%w[curl lsb-release apt-transport-https]
when 'freebsd'
%w[curl perl5|perl]
when 'solaris'
@version.to_i >= 11 ? %w[curl] : %w[CSWcurl wget]
when 'archlinux'
%w[curl net-tools openssh]
when 'amazon', 'fedora'
['curl-minimal', 'iputils']
when 'aix', 'osx', 'windows'
[]
else
%w[curl]
end
end

# Return a list of packages that are needed for timesync
#
# @return [Array<String>] A list of packages to install for timesync
def timesync_packages
return ['chrony'] if uses_chrony?

case @variant
when 'freebsd', 'openbsd', 'windows', 'aix', 'osx'
[]
when 'archlinux', 'opensuse'
['ntp']
when 'sles'
@version.to_i >= 11 ? %w[ntp] : []
when 'solaris'
@version.to_i >= 11 ? %w[ntp] : %w[CSWntp]
else
%w[ntpdate]
end
end
end
end
19 changes: 7 additions & 12 deletions spec/beaker/host_prebuilt_steps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
let(:options_ntp) { make_opts.merge({ 'ntp_server' => ntpserver_set }) }
let(:ntpserver) { Beaker::HostPrebuiltSteps::NTPSERVER }
let(:sync_cmd) { Beaker::HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD }
let(:windows_pkgs) { Beaker::HostPrebuiltSteps::WINDOWS_PACKAGES }
let(:sles_only_pkgs) { Beaker::HostPrebuiltSteps::SLES_PACKAGES }
let(:rhel8_packages) { Beaker::HostPrebuiltSteps::RHEL8_PACKAGES }
let(:fedora_packages) { Beaker::HostPrebuiltSteps::FEDORA_PACKAGES }
let(:amazon2023_packages) { Beaker::HostPrebuiltSteps::AMAZON2023_PACKAGES }
let(:dummy_class) { Class.new { include Beaker::HostPrebuiltSteps } }

shared_examples 'enables_root_login' do |platform, commands, non_cygwin|
Expand Down Expand Up @@ -281,7 +276,7 @@
it "can validate el-9 hosts" do
host = make_host('host', { :platform => 'el-9-64' })

rhel8_packages.each do |pkg|
['curl-minimal', 'iputils'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -293,7 +288,7 @@
host = make_host('host', { :platform => 'windows-11-64', :is_cygwin => true })
allow(host).to receive(:cygwin_installed?).and_return(true)

windows_pkgs.each do |pkg|
['curl'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -304,7 +299,7 @@
it "can validate SLES hosts" do
host = make_host('host', { :platform => 'sles-13.1-x86_64' })

sles_only_pkgs.each do |pkg|
['curl'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -315,7 +310,7 @@
it "can validate opensuse hosts" do
host = make_host('host', { :platform => 'opensuse-15-x86_x64' })

sles_only_pkgs.each do |pkg|
['curl'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -326,7 +321,7 @@
it "can validate RHEL8 hosts" do
host = make_host('host', { :platform => 'el-8-64' })

rhel8_packages.each do |pkg|
['curl-minimal', 'iputils'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -337,7 +332,7 @@
it "can validate Fedora hosts" do
host = make_host('host', { :platform => 'fedora-32-x86_64' })

fedora_packages.each do |pkg|
['curl-minimal', 'iputils'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand All @@ -348,7 +343,7 @@
it "can validate Amazon hosts" do
host = make_host('host', { :platform => 'amazon-2023-x86_64' })

amazon2023_packages.each do |pkg|
['curl-minimal', 'iputils'].each do |pkg|
expect(host).to receive(:check_for_package).with(pkg).once.and_return(false)
expect(host).to receive(:install_package).with(pkg).once
end
Expand Down

0 comments on commit 0e788e0

Please sign in to comment.