Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beaker 5 compatibility #240

Merged
merged 7 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion acceptance/tests/stub_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_hosts_file(host)
hosts.each do |host|
hosts_file = get_hosts_file(host)
result = on host, "cat #{hosts_file}"
assert_no_match(/sleepy/, result.stdout)
refute_match(/sleepy/, result.stdout)
end
end
end
4 changes: 2 additions & 2 deletions beaker-puppet.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.description = 'For use for the Beaker acceptance testing tool'
s.license = 'Apache-2.0'

s.required_ruby_version = '>= 2.7'
s.required_ruby_version = '>= 2.7', '< 3.2'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand All @@ -29,6 +29,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'beaker-vmpooler', '~> 1.4'

# Run time dependencies
s.add_runtime_dependency 'beaker', '~> 4.1'
s.add_runtime_dependency 'beaker', '~> 5.0'
s.add_runtime_dependency 'oga', '~> 3.4'
end
115 changes: 20 additions & 95 deletions lib/beaker-puppet/helpers/puppet_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,13 @@ def puppet_group(host)
# Test Puppet running in a certain run mode with specific options.
# This ensures the following steps are performed:
# 1. The pre-test Puppet configuration is backed up
# 2. A new Puppet configuraton file is layed down
# 2. Lay down a new Puppet configuraton file
# 3. Puppet is started or restarted in the specified run mode
# 4. Ensure Puppet has started correctly
# 5. Further tests are yielded to
# 6. Revert Puppet to the pre-test state
# 7. Testing artifacts are saved in a folder named for the test
#
# @note Whether Puppet is started or restarted depends on what kind of
# server you're running. Passenger and puppetserver are restarted before.
# Webrick is started before and stopped after yielding, unless you're using
# service scripts, then it'll behave like passenger & puppetserver.
# Passenger and puppetserver (or webrick using service scripts)
# restart after yielding by default. You can stop this from happening
# by setting the :restart_when_done flag of the conf_opts argument.
#
# @param [Host] host One object that act like Host
#
# @param [Hash{Symbol=>String}] conf_opts Represents puppet settings.
Expand All @@ -112,22 +104,11 @@ def puppet_group(host)
#
# These will only be applied when starting a FOSS
# master, as a pe master is just bounced.
# @option conf_opts [Hash] :__service_args__ A special setting of options
# for controlling how the puppet master service is
# handled. The only setting currently is
# :bypass_service_script, which if set true will
# force stopping and starting a webrick master
# using the start_puppet_from_source_* methods,
# even if it seems the host has passenger.
# This is needed in FOSS tests to initialize
# SSL.
# @option conf_opts [Boolean] :restart_when_done determines whether a restart
# should be run after the test has been yielded to.
# Will stop puppet if false. Default behavior
# is to restart, but you can override this on the
# host or with this option.
# (Note: only works for passenger & puppetserver
# masters (or webrick using the service scripts))
# @param [File] testdir The temporary directory which will hold backup
# configuration, and other test artifacts.
#
Expand Down Expand Up @@ -156,12 +137,11 @@ def with_puppet_running_on(host, conf_opts, testdir = host.tmpdir(File.basename(
end

cmdline_args = conf_opts[:__commandline_args__]
service_args = conf_opts[:__service_args__] || {}
restart_when_done = true
restart_when_done = host[:restart_when_done] if host.has_key?(:restart_when_done)
restart_when_done = conf_opts.fetch(:restart_when_done, restart_when_done)
conf_opts = conf_opts.reject do |k, v|
%i[__commandline_args__ __service_args__ restart_when_done].include?(k)
%i[__commandline_args__ restart_when_done].include?(k)
end

curl_retries = host['master-start-curl-retries'] || options['master-start-curl-retries']
Expand Down Expand Up @@ -202,13 +182,8 @@ def with_puppet_running_on(host, conf_opts, testdir = host.tmpdir(File.basename(
puppet_config(host, 'confdir', section: 'master'),
testdir,
'puppet.conf')
lay_down_new_puppet_conf host, conf_opts, testdir

if host.use_service_scripts? && !service_args[:bypass_service_script]
bounce_service(host, host['puppetservice'], curl_retries)
else
puppet_master_started = start_puppet_from_source_on!(host, cmdline_args)
end
lay_down_new_puppet_conf(host, conf_opts, testdir)
bounce_service(host, host['puppetservice'], curl_retries)

yield self if block_given?

Expand All @@ -229,20 +204,11 @@ def with_puppet_running_on(host, conf_opts, testdir = host.tmpdir(File.basename(
raise(original_exception)
ensure
begin
if host.use_service_scripts? && !service_args[:bypass_service_script]
restore_puppet_conf_from_backup(host, backup_file)
if restart_when_done
bounce_service(host, host['puppetservice'], curl_retries)
else
host.exec puppet_resource('service', host['puppetservice'], 'ensure=stopped')
end
restore_puppet_conf_from_backup(host, backup_file)
if restart_when_done
bounce_service(host, host['puppetservice'], curl_retries)
else
if puppet_master_started
stop_puppet_from_source_on(host)
else
dump_puppet_log(host)
end
restore_puppet_conf_from_backup(host, backup_file)
host.exec puppet_resource('service', host['puppetservice'], 'ensure=stopped')
end
rescue Exception => teardown_exception
begin
Expand Down Expand Up @@ -281,19 +247,6 @@ def restore_puppet_conf_from_backup(host, backup_file)
end
end

# @!visibility private
def start_puppet_from_source_on!(host, args = '')
host.exec(puppet('master', args))

logger.debug 'Waiting for the puppet master to start'
raise Beaker::DSL::FailTest, 'Puppet master did not start in a timely fashion' unless port_open_within?(
host, 8140, 10
)

logger.debug 'The puppet master has started'
true
end

# @!visibility private
def stop_puppet_from_source_on(host)
pid = host.exec(Command.new('cat `puppet config print --section master pidfile`')).stdout.chomp
Expand Down Expand Up @@ -355,19 +308,12 @@ def puppet_conf_for(host, conf_opts)
def bounce_service(host, service, curl_retries = nil, port = nil)
curl_retries = 120 if curl_retries.nil?
port = options[:puppetserver_port] if port.nil?
if host.graceful_restarts?
service = host.check_for_command('apache2ctl') ? 'apache2ctl' : 'apachectl'
apachectl_path = host.is_pe? ? "#{host['puppetsbindir']}/#{service}" : service
host.exec(Command.new("#{apachectl_path} graceful"))
else
result = host.exec(Command.new("service #{service} reload"),
acceptable_exit_codes: [0, 1, 3])
return result if result.exit_code == 0
result = host.exec(Command.new("service #{service} reload"), acceptable_exit_codes: [0, 1, 3])
return result if result.exit_code == 0

host.exec puppet_resource('service', service, 'ensure=stopped')
host.exec puppet_resource('service', service, 'ensure=running')
host.exec puppet_resource('service', service, 'ensure=stopped')
host.exec puppet_resource('service', service, 'ensure=running')

end
curl_with_retries(" #{service} ", host, "https://localhost:#{port}", [35, 60], curl_retries)
end

Expand Down Expand Up @@ -819,28 +765,17 @@ def wait_for_host_in_dashboard(host)
def sign_certificate_for(host = [])
hostnames = []
hosts = host.is_a?(Array) ? host : [host]
puppet_version = on(master, puppet('--version')).stdout.chomp
hosts.each do |current_host|
if [master, dashboard, database].include? current_host
on current_host, puppet('agent -t'), acceptable_exit_codes: [0, 1, 2]

if version_is_less(puppet_version, '5.99')
on master, puppet("cert --allow-dns-alt-names sign #{current_host}"), acceptable_exit_codes: [0, 24]
else
on master, "puppetserver ca sign --certname #{current_host}"
end
on(current_host, puppet('agent -t'), acceptable_exit_codes: [0, 1, 2])
on(master, "puppetserver ca sign --certname #{current_host}")
else
hostnames << Regexp.escape(current_host.node_name)
end
end

if hostnames.size < 1
if version_is_less(puppet_version, '5.99')
on master, puppet('cert --sign --all --allow-dns-alt-names'),
acceptable_exit_codes: [0, 24]
else
on master, 'puppetserver ca sign --all', acceptable_exit_codes: [0, 24]
end
on(master, 'puppetserver ca sign --all', acceptable_exit_codes: [0, 24])
return
end

Expand All @@ -852,21 +787,11 @@ def sign_certificate_for(host = [])
fail_test("Failed to sign cert for #{hostnames}")
hostnames.clear
end

if version_is_less(puppet_version, '5.99')
on master, puppet('cert --sign --all --allow-dns-alt-names'), acceptable_exit_codes: [0, 24]
out = on(master, puppet('cert --list --all')).stdout
if hostnames.all? { |hostname| out =~ /\+ "?#{hostname}"?/ }
hostnames.clear
break
end
else
on master, 'puppetserver ca sign --all', acceptable_exit_codes: [0, 24]
out = on(master, 'puppetserver ca list --all').stdout
if out !~ /.*Requested.*/ && hostnames.all? { |hostname| out =~ /\b#{hostname}\b/ }
hostnames.clear
break
end
on(master, 'puppetserver ca sign --all', acceptable_exit_codes: [0, 24])
out = on(master, 'puppetserver ca list --all').stdout
if out !~ /.*Requested.*/ && hostnames.all? { |hostname| out =~ /\b#{hostname}\b/ }
hostnames.clear
break
end

sleep next_sleep
Expand Down
Loading