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 Aug 13, 2024
1 parent 998d412 commit 9d85e89
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 79 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
4 changes: 2 additions & 2 deletions spec/beaker/host/windows/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
103 changes: 49 additions & 54 deletions spec/beaker/host_prebuilt_steps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,87 +40,83 @@
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

subject.timesync(hosts, options)
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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -184,16 +179,16 @@
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

subject.timesync(hosts, options_ntp)
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
Expand Down Expand Up @@ -225,23 +220,23 @@
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

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 -qq").once

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

Expand All @@ -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)
Expand All @@ -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

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

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 @@ -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)
Expand Down
Loading

0 comments on commit 9d85e89

Please sign in to comment.