From 4a78cae878ee9aa4978f1b1e8843cbed71697de1 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Thu, 14 Mar 2024 15:49:49 +0100 Subject: [PATCH] Ensure platform is Beaker::Platform in tests The regular option parser guarantees this and it sort of works because Beaker::Platform can behave as a string (since it inherits from it). When you do want to use extended features, it fails. This enhances the helper to guarantee it's the correct class instance. Then there are many changes to the tests to ensure a valid platform is passed in. --- spec/beaker/command_spec.rb | 2 +- spec/beaker/host/windows/file_spec.rb | 4 +- spec/beaker/host_prebuilt_steps_spec.rb | 103 ++++++++++------------ spec/beaker/host_spec.rb | 34 ++++--- spec/beaker/hypervisor/hypervisor_spec.rb | 2 +- spec/beaker/perf_spec.rb | 2 +- spec/beaker/shared/host_manager_spec.rb | 2 +- spec/helpers.rb | 4 +- 8 files changed, 74 insertions(+), 79 deletions(-) diff --git a/spec/beaker/command_spec.rb b/spec/beaker/command_spec.rb index d62bcd2d0..bb723cb17 100644 --- a/spec/beaker/command_spec.rb +++ b/spec/beaker/command_spec.rb @@ -125,7 +125,7 @@ module Beaker allow(h).to receive(:append_commands).and_return('') h end - let(:platform) { @platform || 'unix' } + let(:platform) { @platform || 'el-9-64' } let(:expression) { @expression || 's/b/s/' } let(:filename) { @filename || '/fakefile' } let(:options) { @options || {} } diff --git a/spec/beaker/host/windows/file_spec.rb b/spec/beaker/host/windows/file_spec.rb index fca1c7dcc..f3c3135b1 100644 --- a/spec/beaker/host/windows/file_spec.rb +++ b/spec/beaker/host/windows/file_spec.rb @@ -6,7 +6,7 @@ module Beaker let(:group) { 'somegroup' } let(:path) { 'C:\Foo\Bar' } let(:newpath) { '/Foo/Bar' } - let(:host) { make_host('name', { :platform => 'windows' }) } + let(:host) { make_host('name', { :platform => 'windows-11-64' }) } describe '#chown' do it 'calls cygpath first' do @@ -60,7 +60,7 @@ module Beaker end it 'replaces backslashes with forward slashes when using BitVise and powershell' do - host = make_host('name', platform: 'windows', is_cygwin: false) + host = make_host('name', platform: 'windows-11-64', is_cygwin: false) allow(host).to receive(:determine_ssh_server).and_return(:bitvise) expect(host.scp_path(path)).to eq('C:/Windows') end diff --git a/spec/beaker/host_prebuilt_steps_spec.rb b/spec/beaker/host_prebuilt_steps_spec.rb index 0abbd1c29..428a8876c 100644 --- a/spec/beaker/host_prebuilt_steps_spec.rb +++ b/spec/beaker/host_prebuilt_steps_spec.rb @@ -7,12 +7,11 @@ 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(:platform) { @platform || 'el-9-64' } let(:ip) { "ip.address.0.0" } let(:stdout) { @stdout || ip } let(:hosts) do @@ -41,79 +40,75 @@ end # Non-cygwin Windows - it_behaves_like 'enables_root_login', 'pswindows', [], false + it_behaves_like 'enables_root_login', 'windows-11-64', [], false - # Non-cygwin Windows - it_behaves_like 'enables_root_login', 'windows', [ + # cygwin Windows + it_behaves_like 'enables_root_login', 'windows-11-64', [ "sed -ri 's/^#?PermitRootLogin /PermitRootLogin yes/' /etc/sshd_config", ], true # FreeBSD - it_behaves_like 'enables_root_login', 'freesbd', [ - "sudo su -c \"sed -ri 's/^#?PermitRootLogin no|^#?PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config\"", + it_behaves_like 'enables_root_login', 'freebsd-14-64', [ + "sudo sed -i -e 's/#PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config", + "sudo /etc/rc.d/sshd restart", ], true - it_behaves_like 'enables_root_login', 'osx-10.10', [ + it_behaves_like 'enables_root_login', 'osx-10.10-64', [ "sudo sed -i '' 's/#PermitRootLogin yes/PermitRootLogin Yes/g' /etc/sshd_config", "sudo sed -i '' 's/#PermitRootLogin no/PermitRootLogin Yes/g' /etc/sshd_config", ] - it_behaves_like 'enables_root_login', 'osx-10.11', [ + it_behaves_like 'enables_root_login', 'osx-10.11-64', [ "sudo sed -i '' 's/#PermitRootLogin yes/PermitRootLogin Yes/g' /private/etc/ssh/sshd_config", "sudo sed -i '' 's/#PermitRootLogin no/PermitRootLogin Yes/g' /private/etc/ssh/sshd_config", ] - it_behaves_like 'enables_root_login', 'osx-10.12', [ + it_behaves_like 'enables_root_login', 'osx-10.12-64', [ "sudo sed -i '' 's/#PermitRootLogin yes/PermitRootLogin Yes/g' /private/etc/ssh/sshd_config", "sudo sed -i '' 's/#PermitRootLogin no/PermitRootLogin Yes/g' /private/etc/ssh/sshd_config", ] - it_behaves_like 'enables_root_login', 'osx-10.13', [ + it_behaves_like 'enables_root_login', 'osx-10.13-64', [ "sudo sed -i '' 's/#PermitRootLogin yes/PermitRootLogin Yes/g' /private/etc/ssh/sshd_config", "sudo sed -i '' 's/#PermitRootLogin no/PermitRootLogin Yes/g' /private/etc/ssh/sshd_config", ] # Solaris - it_behaves_like 'enables_root_login', 'solaris-10', [ + it_behaves_like 'enables_root_login', 'solaris-10-64', [ "sudo -E svcadm restart network/ssh", "sudo gsed -i -e 's/#PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config", ], true - it_behaves_like 'enables_root_login', 'solaris-11', [ + it_behaves_like 'enables_root_login', 'solaris-11-64', [ "sudo -E svcadm restart network/ssh", "sudo gsed -i -e 's/PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config", "if grep \"root::::type=role\" /etc/user_attr; then sudo rolemod -K type=normal root; else echo \"root user already type=normal\"; fi", ], true - it_behaves_like 'enables_root_login', 'amazon-2023', [ - "sudo -E systemctl restart sshd.service", + it_behaves_like 'enables_root_login', 'amazon-2023-64', [ "sudo su -c \"sed -ri 's/^#?PermitRootLogin no|^#?PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config\"", + "sudo -E systemctl restart sshd.service", ] - %w[debian ubuntu].each do |deb_like| + %w[debian-12-64 ubuntu-2204-64].each do |deb_like| it_behaves_like 'enables_root_login', deb_like, [ "sudo su -c \"sed -ri 's/^#?PermitRootLogin no|^#?PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config\"", "sudo su -c \"service ssh restart\"", ] end - ['centos', 'el-', 'redhat'].each do |redhat_like| + ['centos-9-64', 'el-9-64', 'redhat-9-64', 'fedora-39-64'].each do |redhat_like| it_behaves_like 'enables_root_login', redhat_like, [ "sudo su -c \"sed -ri 's/^#?PermitRootLogin no|^#?PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config\"", - "sudo -E /sbin/service sshd reload", + "sudo -E systemctl restart sshd.service", ] end - it_behaves_like 'enables_root_login', 'fedora', [ - "sudo su -c \"sed -ri 's/^#?PermitRootLogin no|^#?PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config\"", - "sudo -E systemctl restart sshd.service", - ] - context 'timesync' do subject { dummy_class.new } - it "can sync time on unix hosts" do - hosts = make_hosts({ :platform => 'unix' }) + it "can sync time on el-7 hosts" do + hosts = make_hosts({ :platform => 'el-7-64' }) expect(Beaker::Command).to receive(:new).with("ntpdate -u -t 20 #{ntpserver}").exactly(3).times @@ -121,7 +116,7 @@ end it "can retry on failure on unix hosts" do - hosts = make_hosts({ :platform => 'unix', :exit_code => [1, 0] }) + hosts = make_hosts({ :platform => 'el-7-64', :exit_code => [1, 0] }) allow(subject).to receive(:sleep).and_return(true) expect(Beaker::Command).to receive(:new).with("ntpdate -u -t 20 #{ntpserver}").exactly(6).times @@ -130,7 +125,7 @@ end it "eventually gives up and raises an error when unix hosts can't be synched" do - hosts = make_hosts({ :platform => 'unix', :exit_code => 1 }) + hosts = make_hosts({ :platform => 'el-7-64', :exit_code => 1 }) allow(subject).to receive(:sleep).and_return(true) expect(Beaker::Command).to receive(:new).with("ntpdate -u -t 20 #{ntpserver}").exactly(5).times @@ -139,7 +134,7 @@ end it "can sync time on windows hosts" do - hosts = make_hosts({ :platform => 'windows' }) + hosts = make_hosts({ :platform => 'windows-11-64' }) expect(Beaker::Command).to receive(:new).with("w32tm /register").exactly(3).times expect(Beaker::Command).to receive(:new).with("net start w32time").exactly(3).times @@ -184,8 +179,8 @@ subject.timesync(hosts, options) end - it "can set time server on unix hosts" do - hosts = make_hosts({ :platform => 'unix' }) + it "can set time server on el-7 hosts" do + hosts = make_hosts({ :platform => 'el-7-64' }) expect(Beaker::Command).to receive(:new).with("ntpdate -u -t 20 #{ntpserver_set}").exactly(3).times @@ -193,7 +188,7 @@ end it "can set time server on windows hosts" do - hosts = make_hosts({ :platform => 'windows' }) + hosts = make_hosts({ :platform => 'windows-11-64' }) expect(Beaker::Command).to receive(:new).with("w32tm /register").exactly(3).times expect(Beaker::Command).to receive(:new).with("net start w32time").exactly(3).times @@ -225,7 +220,7 @@ subject { dummy_class.new } it "can perform apt-get on ubuntu hosts" do - host = make_host('testhost', { :platform => 'ubuntu' }) + host = make_host('testhost', { :platform => 'ubuntu-2204-64' }) expect(Beaker::Command).to receive(:new).with("apt-get update -qq").once @@ -233,7 +228,7 @@ end it "can perform apt-get on debian hosts" do - host = make_host('testhost', { :platform => 'debian' }) + host = make_host('testhost', { :platform => 'debian-12-64' }) expect(Beaker::Command).to receive(:new).with("apt-get update -qq").once @@ -241,7 +236,7 @@ end it "does nothing on non debian/ubuntu hosts" do - host = make_host('testhost', { :platform => 'windows' }) + host = make_host('testhost', { :platform => 'windows-11-64' }) expect(Beaker::Command).not_to receive(:new) @@ -256,7 +251,7 @@ content = "this is the content" tempfilepath = "/path/to/tempfile" filepath = "/path/to/file" - host = make_host('testhost', { :platform => 'windows' }) + host = make_host('testhost', { :platform => 'windows-11-64' }) tempfile = double('tempfile') allow(tempfile).to receive(:path).and_return(tempfilepath) allow(Tempfile).to receive(:open).and_yield(tempfile) @@ -274,7 +269,7 @@ subject { dummy_class.new } it "can sync keys on a solaris host" do - @platform = 'solaris' + @platform = 'solaris-11-64' expect(Beaker::Command).to receive(:new).with(sync_cmd % "bash").exactly(3).times @@ -291,9 +286,9 @@ context "validate_host" do subject { dummy_class.new } - it "can validate unix hosts" do + it "can validate el-9 hosts" do hosts.each do |host| - unix_only_pkgs.each do |pkg| + 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 @@ -303,7 +298,7 @@ end it "can validate windows hosts" do - @platform = 'windows' + @platform = 'windows-11-64' hosts.each do |host| windows_pkgs.each do |pkg| @@ -395,7 +390,7 @@ context "on windows" do let(:host) do make_host('name', { - :platform => 'windows', + :platform => 'windows-11-64', :is_cygwin => cygwin, :stdout => "domain labs.lan d.labs.net dc1.labs.net labs.com\nnameserver 10.16.22.10\nnameserver 10.16.22.11", }) @@ -422,7 +417,7 @@ end end - %w[amazon centos redhat].each do |platform| + %w[amazon-2023-64 centos-9-64 redhat-9-64].each do |platform| context "on platform '#{platform}'" do let(:host) do make_host('name', { @@ -480,7 +475,7 @@ subject { dummy_class.new } it "can copy ssh to root in windows hosts with no cygwin" do - host = make_host('testhost', { :platform => 'windows', :is_cygwin => false }) + host = make_host('testhost', { :platform => 'windows-11-64', :is_cygwin => false }) expect(Beaker::Command).to receive(:new).with("if exist .ssh (xcopy .ssh C:\\Users\\Administrator\\.ssh /s /e /y /i)").once subject.copy_ssh_to_root(host, options) @@ -493,7 +488,7 @@ proxyurl = "http://192.168.2.100:3128" it "can set proxy config on a debian/ubuntu host" do - host = make_host('name', { :platform => 'debian' }) + host = make_host('name', { :platform => 'debian-12-64' }) expect(Beaker::Command).to receive(:new).with("echo 'Acquire::http::Proxy \"#{proxyurl}/\";' >> /etc/apt/apt.conf.d/10proxy").once expect(host).to receive(:exec).once @@ -501,7 +496,7 @@ subject.package_proxy(host, options.merge({ 'package_proxy' => proxyurl })) end - %w[amazon centos redhat].each do |platform| + %w[amazon-2023-64 el-9-64].each do |platform| it "can set proxy config on a '#{platform}' host" do host = make_host('name', { :platform => platform }) @@ -517,43 +512,43 @@ subject { dummy_class.new } it "sets user ssh environment on an OS X 10.10 host" do - test_host_ssh_calls('osx-10.10') + test_host_ssh_calls('osx-10.10-64') end it "sets user ssh environment on an OS X 10.11 host" do - test_host_ssh_calls('osx-10.11') + test_host_ssh_calls('osx-10.11-64') end it "sets user ssh environment on an OS X 10.12 host" do - test_host_ssh_calls('osx-10.12') + test_host_ssh_calls('osx-10.12-64') end it "sets user ssh environment on an OS X 10.13 host" do - test_host_ssh_calls('osx-10.13') + test_host_ssh_calls('osx-10.13-64') end it "sets user ssh environment on an ssh-based linux host" do - test_host_ssh_calls('ubuntu') + test_host_ssh_calls('ubuntu-2204-64') end it "sets user ssh environment on an sles host" do - test_host_ssh_calls('sles') + test_host_ssh_calls('sles-15-64') end it "sets user ssh environment on a solaris host" do - test_host_ssh_calls('solaris') + test_host_ssh_calls('solaris-11-64') end it "sets user ssh environment on an aix host" do - test_host_ssh_calls('aix') + test_host_ssh_calls('aix-7.2-power') end it "sets user ssh environment on a FreeBSD host" do - test_host_ssh_calls('freebsd') + test_host_ssh_calls('freebsd-14-64') end it "sets user ssh environment on a windows host" do - test_host_ssh_calls('windows') + test_host_ssh_calls('windows-11-64') end def test_host_ssh_calls(platform_name) diff --git a/spec/beaker/host_spec.rb b/spec/beaker/host_spec.rb index 0a4af7e34..fbfa6c102 100644 --- a/spec/beaker/host_spec.rb +++ b/spec/beaker/host_spec.rb @@ -7,7 +7,7 @@ module Beaker let(:host) { make_host('name', options.merge(platform)) } it 'creates a windows host given a windows config' do - @platform = 'windows' + @platform = 'windows-11-64' expect(host).to be_a Windows::Host end @@ -109,16 +109,16 @@ module Beaker result.exit_code = 0 expect(Beaker::Command).to receive(:new).with(/grep \^key= ~\/\.ssh\/environment/) expect(host).to receive(:exec).and_return(result) - expect(Beaker::SedCommand).to receive(:new).with('unix', 's/^key=/key=\\/my\\/first\\/value:/', '~/.ssh/environment') + expect(Beaker::SedCommand).to receive(:new).with('el-9-64', 's/^key=/key=\\/my\\/first\\/value:/', '~/.ssh/environment') host.add_env_var('key', '/my/first/value') end end describe "#delete_env_var" do it "deletes env var" do - expect(Beaker::SedCommand).to receive(:new).with('unix', '/key=\\/my\\/first\\/value$/d', '~/.ssh/environment') - expect(Beaker::SedCommand).to receive(:new).with("unix", "s/key=\\(.*\\)[;:]\\/my\\/first\\/value/key=\\1/", "~/.ssh/environment") - expect(Beaker::SedCommand).to receive(:new).with("unix", "s/key=\\/my\\/first\\/value[;:]/key=/", "~/.ssh/environment") + expect(Beaker::SedCommand).to receive(:new).with('el-9-64', '/key=\\/my\\/first\\/value$/d', '~/.ssh/environment') + expect(Beaker::SedCommand).to receive(:new).with("el-9-64", "s/key=\\(.*\\)[;:]\\/my\\/first\\/value/key=\\1/", "~/.ssh/environment") + expect(Beaker::SedCommand).to receive(:new).with("el-9-64", "s/key=\\/my\\/first\\/value[;:]/key=/", "~/.ssh/environment") host.delete_env_var('key', '/my/first/value') end end @@ -250,7 +250,7 @@ module Beaker describe "#mkdir_p" do it "does the right thing on a bash host, identified as is_cygwin=true" do @options = { :is_cygwin => true } - @platform = 'windows' + @platform = 'windows-11-64' result = double allow(result).to receive(:exit_code).and_return(0) allow(host).to receive(:exec).and_return(result) @@ -261,7 +261,7 @@ module Beaker it "does the right thing on a bash host, identified as is_cygwin=nil" do @options = { :is_cygwin => nil } - @platform = 'windows' + @platform = 'windows-11-64' result = double allow(result).to receive(:exit_code).and_return(0) allow(host).to receive(:exec).and_return(result) @@ -272,7 +272,7 @@ module Beaker it "does the right thing on a non-bash host, identified as is_cygwin=false (powershell)" do @options = { :is_cygwin => false } - @platform = 'windows' + @platform = 'windows-11-64' result = double allow(result).to receive(:exit_code).and_return(0) allow(host).to receive(:exec).and_return(result) @@ -290,19 +290,17 @@ module Beaker describe "#touch" do it "generates the right absolute command for a windows host" do - @platform = 'windows' + @platform = 'windows-11-64' expect(host.touch('touched_file')).to eq "c:\\\\windows\\\\system32\\\\cmd.exe /c echo. 2> touched_file" end - %w[centos redhat].each do |platform| - it "generates the right absolute command for a #{platform} host" do - @platform = platform - expect(host.touch('touched_file')).to eq "/bin/touch touched_file" - end + it "generates the right absolute command for an el-9-64 host" do + @platform = 'el-9-64' + expect(host.touch('touched_file')).to eq "/bin/touch touched_file" end - it "generates the right absolute command for an osx host" do - @platform = 'osx' + it "generates the right absolute command for an osx-12-64 host" do + @platform = 'osx-12-64' expect(host.touch('touched_file')).to eq "/usr/bin/touch touched_file" end end @@ -762,12 +760,12 @@ module Beaker describe "#fips_mode?" do it 'returns false on non-linux hosts' do - @platform = 'windows' + @platform = 'windows-11-64' expect(host).to receive(:file_exist?).with('/proc/sys/crypto/fips_enabled').and_return(false) expect(host.fips_mode?).to be false end - platforms = %w[el-7 el-8 centos] + platforms = %w[el-7-64 el-8-64 centos-9-64] platforms.each do |platform| context "on #{platform}" do diff --git a/spec/beaker/hypervisor/hypervisor_spec.rb b/spec/beaker/hypervisor/hypervisor_spec.rb index 0de108537..bc0e31eff 100644 --- a/spec/beaker/hypervisor/hypervisor_spec.rb +++ b/spec/beaker/hypervisor/hypervisor_spec.rb @@ -2,7 +2,7 @@ module Beaker describe Hypervisor do - let(:hosts) { make_hosts({ :platform => 'el-5' }) } + let(:hosts) { make_hosts({ :platform => 'el-9-64' }) } describe "#create" do let(:hypervisor) { described_class } diff --git a/spec/beaker/perf_spec.rb b/spec/beaker/perf_spec.rb index 1d8bb1d02..7ff2b7515 100644 --- a/spec/beaker/perf_spec.rb +++ b/spec/beaker/perf_spec.rb @@ -80,7 +80,7 @@ module Beaker it "Does the Right Thing on non-Linux hosts" do hosts = [ make_host("myHost", @options.merge('platform' => 'windows-11-64')), - make_host("myOtherHost", @options), + make_host("myOtherHost", @options.merge('platform' => 'solaris-11-64')), ] @my_logger.remove_destination(STDOUT) perf = described_class.new(hosts, @options) diff --git a/spec/beaker/shared/host_manager_spec.rb b/spec/beaker/shared/host_manager_spec.rb index dede8f86c..705e852ff 100644 --- a/spec/beaker/shared/host_manager_spec.rb +++ b/spec/beaker/shared/host_manager_spec.rb @@ -11,7 +11,7 @@ module Shared let(:logger) { double('logger') } let(:host_handler) { described_class } let(:spec_block) { Proc.new { |arr| arr } } - let(:platform) { @platform || 'unix' } + let(:platform) { @platform || 'el-9-64' } let(:role0) { "role0" } let(:role1) { :role1 } let(:role2) { :role2 } diff --git a/spec/helpers.rb b/spec/helpers.rb index af4b33cdc..e36a81f6a 100644 --- a/spec/helpers.rb +++ b/spec/helpers.rb @@ -24,7 +24,7 @@ def fog_file_contents end module HostHelpers - HOST_DEFAULTS = { :platform => 'unix', + HOST_DEFAULTS = { :platform => 'el-9-64', :roles => ['agent'], :snapshot => 'snap', :ip => 'default.ip.address', @@ -91,6 +91,8 @@ def make_host_opts name, opts def make_host name, host_hash host_hash = Beaker::Options::OptionsHash.new.merge(HOST_DEFAULTS.merge(host_hash)) + host_hash['platform'] = Beaker::Platform.new(host_hash['platform']) unless host_hash['platform'].is_a?(Beaker::Platform) + host = Beaker::Host.create(name, host_hash, make_opts) allow(host).to receive(:exec).and_return(generate_result(name, host_hash))