From 27c5b04ea1ced0fa47e74be825ae8127bd754708 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Wed, 13 Mar 2024 20:17:14 +0100 Subject: [PATCH] Only install timesync packages if needed Chrony/NTP is only needed if timesync is requested. In some cases (like containers) it never makes sense. Which packages are installed is really platform specific logic, so it should live in the platform class. --- lib/beaker/host_prebuilt_steps.rb | 63 ++----------------------- lib/beaker/platform.rb | 63 +++++++++++++++++++++++++ spec/beaker/host_prebuilt_steps_spec.rb | 59 +++++++++-------------- 3 files changed, 90 insertions(+), 95 deletions(-) diff --git a/lib/beaker/host_prebuilt_steps.rb b/lib/beaker/host_prebuilt_steps.rb index acb2e60b3..dcf9635bb 100644 --- a/lib/beaker/host_prebuilt_steps.rb +++ b/lib/beaker/host_prebuilt_steps.rb @@ -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" @@ -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}" @@ -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, 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 @@ -102,44 +82,9 @@ def validate_host host, opts # @param [Host] host A host return the packages for # @return [Array] 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 - end + packages = host['platform'].base_packages + packages += host['platform'].timesync_packages if host[:timesync] + packages end # Installs the given packages if they aren't already on a host diff --git a/lib/beaker/platform.rb b/lib/beaker/platform.rb index 5289d2c62..9a90fbcf6 100644 --- a/lib/beaker/platform.rb +++ b/lib/beaker/platform.rb @@ -100,5 +100,68 @@ 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] A list of packages to install + def base_packages + case @variant + when 'el' + @version.to_i >= 8 ? ['curl-minmal', 'iputils'] : %w[curl] + when 'debian' + %w[curl lsb-release apt-transport-https] + when 'windows' + if host.is_cygwin? + raise RuntimeError, "cygwin is not installed on #{host}" if !host.cygwin_installed? + + %w[curl] + else + [] + end + 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' + [] + else + %w[curl] + end + end + + # Return a list of packages that are needed for timesync + # + # @return [Array] 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 diff --git a/spec/beaker/host_prebuilt_steps_spec.rb b/spec/beaker/host_prebuilt_steps_spec.rb index a96da9928..c26a3d0ed 100644 --- a/spec/beaker/host_prebuilt_steps_spec.rb +++ b/spec/beaker/host_prebuilt_steps_spec.rb @@ -6,12 +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(:unix_only_pkgs) { Beaker::HostPrebuiltSteps::UNIX_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(:platform) { @platform || 'unix' } let(:ip) { "ip.address.0.0" } let(:stdout) { @stdout || ip } @@ -292,12 +286,12 @@ subject { dummy_class.new } it "can validate unix hosts" do + # rubocop:disable RSpec/IteratedExpectation hosts.each do |host| - unix_only_pkgs.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 + expect(host).to receive(:check_for_package).with('curl').once.and_return(false) + expect(host).to receive(:install_package).with('curl').once end + # rubocop:enable RSpec/IteratedExpectation subject.validate_host(hosts, options) end @@ -306,12 +300,11 @@ @platform = 'windows' hosts.each do |host| - windows_pkgs.each do |pkg| - allow(host).to receive(:cygwin_installed?).and_return(true) - allow(host).to receive(:is_cygwin?).and_return(true) - expect(host).to receive(:check_for_package).with(pkg).once.and_return(false) - expect(host).to receive(:install_package).with(pkg).once - end + pkg = 'curl' + allow(host).to receive(:cygwin_installed?).and_return(true) + allow(host).to receive(:is_cygwin?).and_return(true) + expect(host).to receive(:check_for_package).with(pkg).once.and_return(false) + expect(host).to receive(:install_package).with(pkg).once end subject.validate_host(hosts, options) @@ -320,12 +313,12 @@ it "can validate SLES hosts" do @platform = 'sles-13.1-x64' + # rubocop:disable RSpec/IteratedExpectation hosts.each do |host| - sles_only_pkgs.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 + expect(host).to receive(:check_for_package).with('curl').once.and_return(false) + expect(host).to receive(:install_package).with('curl').once end + # rubocop:enable RSpec/IteratedExpectation subject.validate_host(hosts, options) end @@ -333,12 +326,12 @@ it "can validate opensuse hosts" do @platform = 'opensuse-15-x86_x64' + # rubocop:disable RSpec/IteratedExpectation hosts.each do |host| - sles_only_pkgs.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 + expect(host).to receive(:check_for_package).with('curl').once.and_return(false) + expect(host).to receive(:install_package).with('curl').once end + # rubocop:enable RSpec/IteratedExpectation subject.validate_host(hosts, options) end @@ -347,10 +340,8 @@ @platform = 'el-8-x86_x64' hosts.each do |host| - rhel8_packages.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 + expect(host).not_to receive(:check_for_package) + expect(host).not_to receive(:install_package) end subject.validate_host(hosts, options) @@ -360,10 +351,8 @@ @platform = 'fedora-32-x86_64' hosts.each do |host| - fedora_packages.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 + expect(host).not_to receive(:check_for_package) + expect(host).not_to receive(:install_package) end subject.validate_host(hosts, options) @@ -373,10 +362,8 @@ @platform = 'amazon-2023-x86_64' hosts.each do |host| - amazon2023_packages.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 + expect(host).not_to receive(:check_for_package) + expect(host).not_to receive(:install_package) end subject.validate_host(hosts, options)