Refactor spec helper

The spec helper file has been cargocult-copied from puppetlabs and
puppet-openstack modules, adjusted slightly for Infra's particular
needs. It is hard to read which makes adjusting it hard to do and hard
to review. This refactor moves code into descriptively-named functions,
and condenses the main setup code so that there is only one loop over
the host list, and passes the entire environment into host commands
instead of crafting long bash commands with required environment
variables.

Change-Id: I572a5dc0d9987e8c90a27f300c8b4979e20c43a2
This commit is contained in:
Colleen Murphy 2017-07-09 15:27:15 +02:00
parent 0861447aaa
commit 2cc8cce5cf
1 changed files with 53 additions and 54 deletions

View File

@ -1,7 +1,8 @@
require 'beaker-rspec'
hosts.each do |host|
SYSTEM_CONFIG='openstack-infra/system-config'
def install_infra_puppet(host)
# puppet 3 isn't available from apt.puppetlabs.com so install it from the Xenial repos
on host, "which apt-get && apt-get install puppet -y", { :acceptable_exit_codes => [0,1] }
# otherwise use the beaker helpers to install the yum.puppetlabs.com repo and puppet
@ -9,63 +10,61 @@ hosts.each do |host|
if r.exit_code == 0
install_puppet
end
add_platform_foss_defaults(host, 'unix')
end
def setup_host(host)
add_platform_foss_defaults(host, 'unix')
on host, "mkdir -p #{host['distmoduledir']}"
end
RSpec.configure do |c|
# Project root
proj_root = File.expand_path(File.join(Dir.getwd))
modname = JSON.parse(open('metadata.json').read)['name'].split('-')[1]
def install_system_config(host)
# install git
install_package host, 'git'
# Make sure proj_root is the real project root
unless File.exists?("#{proj_root}/metadata.json")
raise "bundle exec rspec spec/acceptance needs be run from module root."
end
# Readable test descriptions
c.formatter = :documentation
# Configure all nodes in nodeset
c.before :suite do
# Install module and dependencies
hosts.each do |host|
# Clean out any module cruft
shell('rm -fr /etc/puppet/modules/*')
# install git
install_package host, 'git'
zuul_ref = ENV['ZUUL_REF']
zuul_branch = ENV['ZUUL_BRANCH']
zuul_url = ENV['ZUUL_URL']
# Install dependent modules via git or zuul
r = on host, "test -e /usr/zuul-env/bin/zuul-cloner", { :acceptable_exit_codes => [0,1] }
repo = 'openstack-infra/system-config'
if r.exit_code == 0
zuul_clone_cmd = '/usr/zuul-env/bin/zuul-cloner '
zuul_clone_cmd += '--cache-dir /opt/git '
zuul_clone_cmd += "--zuul-ref #{zuul_ref} "
zuul_clone_cmd += "--zuul-branch #{zuul_branch} "
zuul_clone_cmd += "--zuul-url #{zuul_url} "
zuul_clone_cmd += "git://git.openstack.org #{repo}"
on host, zuul_clone_cmd
else
on host, "git clone https://git.openstack.org/#{repo} #{repo}"
end
on host, "ZUUL_REF=#{zuul_ref} ZUUL_BRANCH=#{zuul_branch} ZUUL_URL=#{zuul_url} bash #{repo}/tools/install_modules_acceptance.sh"
on host, "rm -fr /etc/puppet/modules/#{modname}"
# Install the module being tested
puppet_module_install(:source => proj_root, :module_name => modname)
on host, "rm -fr #{repo}"
# List modules installed to help with debugging
on hosts[0], puppet('module','list'), { :acceptable_exit_codes => 0 }
end
# Install dependent modules via git or zuul
on host, "rm -fr #{SYSTEM_CONFIG}"
if ENV['ZUUL_REF'] && ENV['ZUUL_BRANCH'] && ENV['ZUUL_URL']
zuul_clone_cmd = '/usr/zuul-env/bin/zuul-cloner '
zuul_clone_cmd += '--cache-dir /opt/git '
zuul_clone_cmd += "git://git.openstack.org #{SYSTEM_CONFIG}"
on host, zuul_clone_cmd, :environment => ENV.to_hash
else
on host, "git clone https://git.openstack.org/#{SYSTEM_CONFIG} #{SYSTEM_CONFIG}"
end
end
def install_infra_modules(host, proj_root)
install_system_config(host)
# Clean out any module cruft
shell('rm -fr /etc/puppet/modules/*')
# Install module and dependencies
modname = JSON.parse(open('metadata.json').read)['name'].split('-')[1]
module_install_cmd = "bash #{SYSTEM_CONFIG}/tools/install_modules_acceptance.sh"
on host, module_install_cmd, :environment => ENV.to_hash
on host, "rm -fr /etc/puppet/modules/#{modname}"
# Install the module being tested
puppet_module_install(:source => proj_root, :module_name => modname)
# List modules installed to help with debugging
on hosts[0], puppet('module','list'), { :acceptable_exit_codes => 0 }
end
proj_root = File.expand_path(File.join(Dir.getwd))
# Make sure proj_root is the real project root
unless File.exists?("#{proj_root}/metadata.json")
raise "bundle exec rspec spec/acceptance needs be run from module root."
end
# Readable test descriptions
RSpec.configure do |conf|
conf.formatter = :documentation
end
# Set up hosts, before running any tests
hosts.each do |host|
install_infra_puppet(host)
setup_host(host)
install_infra_modules(host, proj_root)
end