From bc2c7f0038b481de61f3a6b3bfd9889d5800ec81 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_prebuilt_steps_spec.rb | 62 +++++++++++------------ spec/beaker/host_spec.rb | 34 ++++++------- spec/beaker/hypervisor/hypervisor_spec.rb | 2 +- spec/beaker/shared/host_manager_spec.rb | 2 +- spec/helpers.rb | 4 +- 6 files changed, 53 insertions(+), 53 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_prebuilt_steps_spec.rb b/spec/beaker/host_prebuilt_steps_spec.rb index 4e7d47702..f6833102f 100644 --- a/spec/beaker/host_prebuilt_steps_spec.rb +++ b/spec/beaker/host_prebuilt_steps_spec.rb @@ -12,6 +12,7 @@ let(:rhel8_packages) { Beaker::HostPrebuiltSteps::RHEL8_PACKAGES } let(:fedora_packages) { Beaker::HostPrebuiltSteps::FEDORA_PACKAGES } let(:amazon2023_packages) { Beaker::HostPrebuiltSteps::AMAZON2023_PACKAGES } + # TODO: unix is unsupported let(:platform) { @platform || 'unix' } let(:ip) { "ip.address.0.0" } let(:stdout) { @stdout || ip } @@ -40,18 +41,17 @@ end end - it_behaves_like 'enables_root_login', 'f5', [] - # Non-cygwin Windows - it_behaves_like 'enables_root_login', 'pswindows', [], false + it_behaves_like 'enables_root_login', 'f5-stuff-64', [] # Non-cygwin Windows - it_behaves_like 'enables_root_login', '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', [ @@ -91,14 +91,14 @@ "sudo su -c \"sed -ri 's/^#?PermitRootLogin no|^#?PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config\"", ] - %w[debian ubuntu cumulus].each do |deb_like| + %w[debian-12-64 ubuntu-2204-64 cumulus-x-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', 'fedora', 'eos'].each do |redhat_like| + ['centos-9-64', 'el-9-64', 'redhat-9-64', 'fedora-39-64', 'eos-x-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", @@ -135,7 +135,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 @@ -221,7 +221,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").once @@ -229,7 +229,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").once @@ -237,7 +237,7 @@ end it "can perform apt-get on cumulus hosts" do - host = make_host('testhost', { :platform => 'cumulus' }) + host = make_host('testhost', { :platform => 'cumulus-x-64' }) expect(Beaker::Command).to receive(:new).with("apt-get update").once @@ -245,7 +245,7 @@ end it "does nothing on non debian/ubuntu/cumulus hosts" do - host = make_host('testhost', { :platform => 'windows' }) + host = make_host('testhost', { :platform => 'windows-11-64' }) expect(Beaker::Command).not_to receive(:new) @@ -307,7 +307,7 @@ end it "can validate windows hosts" do - @platform = 'windows' + @platform = 'windows-11-64' hosts.each do |host| windows_pkgs.each do |pkg| @@ -405,7 +405,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", }) @@ -432,7 +432,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', { @@ -490,7 +490,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) @@ -503,7 +503,7 @@ proxyurl = "http://192.168.2.100:3128" it "can set proxy config on a debian/ubuntu/cumulus host" do - host = make_host('name', { :platform => 'cumulus' }) + 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 @@ -511,7 +511,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 }) @@ -527,52 +527,52 @@ 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 sshd-based linux host" do - test_host_ssh_calls('eos') + test_host_ssh_calls('eos-x-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 it "skips an f5 host correctly" do host = make_host('name', { - :platform => 'f5-stuff', + :platform => 'f5-stuff-64', :ssh_env_file => 'ssh_env_file', :is_cygwin => true, }) 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/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 438056c11..fbbb038f4 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))