Skip to content

Commit

Permalink
Ensure platform is Beaker::Platform in tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ekohl committed Mar 14, 2024
1 parent 9e10133 commit bc2c7f0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 53 deletions.
2 changes: 1 addition & 1 deletion spec/beaker/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {} }
Expand Down
62 changes: 31 additions & 31 deletions spec/beaker/host_prebuilt_steps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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', [
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -221,31 +221,31 @@
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

subject.apt_get_update(host)
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

subject.apt_get_update(host)
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

subject.apt_get_update(host)
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)

Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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",
})
Expand All @@ -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', {
Expand Down Expand Up @@ -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)
Expand All @@ -503,15 +503,15 @@
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

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 })

Expand All @@ -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,
})
Expand Down
34 changes: 16 additions & 18 deletions spec/beaker/host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/beaker/hypervisor/hypervisor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion spec/beaker/shared/host_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit bc2c7f0

Please sign in to comment.