Skip to content

Commit

Permalink
(maint) acceptance-fix previous merge-up
Browse files Browse the repository at this point in the history
The previous merge-up from stable had a few conflicts improperly
resolved.  There were at least two refactors that conflicted.  This
change takes the pure stable changes, as intended.

[skip ci]
  • Loading branch information
Eric Thompson committed Jan 13, 2016
1 parent fb51b8d commit 82bafca
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 66 deletions.
161 changes: 101 additions & 60 deletions acceptance/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ require 'securerandom'
require 'fileutils'
require 'beaker-hostgenerator'

$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
require 'puppet/acceptance/git_utils'
extend Puppet::Acceptance::GitUtils

ONE_DAY_IN_SECS = 24 * 60 * 60
PRESERVED_HOSTS_FILENAME = 'hosts_preserved.yml'
SUTS_FILENAME = 'sut.log'
CLEAN.include('*.tar', 'merged_options.rb')
REPO_CONFIGS_DIR = "repo-configs"
CLEAN.include('*.tar', REPO_CONFIGS_DIR, 'merged_options.rb')

module HarnessOptions

DEFAULTS = {
:type => 'git',
:helper => ['lib/helper.rb'],
:tests => ['tests'],
:log_level => 'debug',
:color => false,
:root_keys => true,
:ssh => {
:keys => ["~/.ssh/id_rsa-acceptance"],
},
:xml => true,
:timesync => false,
:repo_proxy => true,
:add_el_extras => true,
:preserve_hosts => 'onfail',
}

Expand All @@ -28,7 +40,7 @@ module HarnessOptions
end

def get_options(file_path)
puts "Attempting to merge config file: #{file_path}"
puts file_path
if File.exists? file_path
options = eval(File.read(file_path), binding)
else
Expand Down Expand Up @@ -68,27 +80,6 @@ def beaker_test(mode = :aio, options = {})
final_options[:install] ||= []
final_options[:install] << "#{build_giturl('facter')}##{sha}"
end
end

def tests
ENV['TESTS'] || ENV['TEST']
end

def generate_beaker_cli_flags(options_file)
# the options file (including the default options might also have tests
# they'll get merged with the below by beaker
tests_opt = "--tests=#{tests}" if tests

hosts_opt = "--hosts=#{hosts}" if hosts

overriding_options = ENV['OPTIONS']

# compact to remove the nil elements
["--options-file", options_file, hosts_opt, tests_opt, overriding_options].compact
end

def write_merged_options_file(mode,options)
final_options = HarnessOptions.options(mode, options)

options_file = 'merged_options.rb'
File.open(options_file, 'w') do |merged|
Expand All @@ -98,12 +89,7 @@ def write_merged_options_file(mode,options)
EOS
merged.puts(final_options.pretty_inspect)
end
return options_file
end

def beaker_test(mode = :aio, options = {})
options_file = write_merged_options_file(mode,options)
args = generate_beaker_cli_flags(options_file)
tests = ENV['TESTS'] || ENV['TEST']
tests_opt = "--tests=#{tests}" if tests

Expand All @@ -127,30 +113,75 @@ def beaker_test(mode = :aio, options = {})
begin
sh("beaker", *args)
ensure
if (hosts_file = config || final_options[:hosts_file]) && hosts_file !~ /preserved_config/
cp(hosts_file, "log/latest/config.yml")
generate_config_for_latest_hosts if final_options[:preserve_hosts] || overriding_options =~ /--preserve-hosts/
end
mv(options_file, "log/latest")
puts "\n\n"
end
end

def generate_config_for_latest_hosts
preserved_config_hash = { 'HOSTS' => {} }

config_hash = YAML.load_file('log/latest/config.yml').to_hash
nodes = config_hash['HOSTS'].map do |node_label,hash|
{ :node_label => node_label, :platform => hash['platform'] }
end

pre_suite_log = File.read('log/latest/pre_suite-run.log')
nodes.each do |node_info|
possible_hostname_match = /^(\w+) \(#{node_info[:node_label]}\)/.match(pre_suite_log)
hostname = (possible_hostname_match || Array.new)[1]
fqdn = hostname ? "#{hostname}.delivery.puppetlabs.net" : "unknown"
preserved_config_hash['HOSTS'][fqdn] = {
'roles' => [ 'agent'],
'platform' => node_info[:platform],
}
preserved_config_hash['HOSTS'][fqdn]['roles'].unshift('master') if node_info[:node_label] =~ /master/
end

puts "\n\n"
pp preserved_config_hash
puts "\n\n"

File.open('log/latest/preserved_config.yaml', 'w') do |config_file|
YAML.dump(preserved_config_hash, config_file)
end
rescue Errno::ENOENT => e
puts "Couldn't generate log #{e}"
end

def list_preserved_configurations(secs_ago = ONE_DAY_IN_SECS)
preserved = {}
Dir.glob('log/*_*').each do |dir|
preserved_config_path = "#{dir}/preserved_config.yaml"
yesterday = Time.now - secs_ago.to_i
if preserved_config = File.exists?(preserved_config_path)
directory = File.new(dir)
if directory.ctime > yesterday
hosts = []
preserved_config = YAML.load_file(preserved_config_path).to_hash
preserved_config['HOSTS'].each do |hostname,values|
hosts << "#{hostname}: #{values['platform']}, #{values['roles']}"
end
preserved[hosts] = directory.to_path
end
end
end
preserved.map { |k,v| [v,k] }.sort { |a,b| a[0] <=> b[0] }.reverse
end

def list_preserved_hosts(secs_ago = ONE_DAY_IN_SECS)
hosts = Set.new
Dir.glob('log/*').each do |dir|
unless dir =~ /^log\/latest$/
Dir.glob("#{dir}/*").each do |date_dir|
preserved_config_path = "#{date_dir}/#{SUTS_FILENAME}"
if preserved_config = File.exists?(preserved_config_path)
directory = File.new(date_dir)
yesterday = Time.now - secs_ago.to_i
if directory.ctime > yesterday
File.open(preserved_config_path, 'r') do |file|
if file.ctime > yesterday
file.each_line do |line|
matchdata = /(\w+\.delivery\.puppetlabs\.net) \(.*?\)$/.match(line)
hosts.add(matchdata[1]) if matchdata
end
end
end
end
Dir.glob('log/**/pre*suite*run.log').each do |log|
yesterday = Time.now - secs_ago.to_i
File.open(log, 'r') do |file|
if file.ctime > yesterday
file.each_line do |line|
matchdata = /^(\w+) \(.*?\) \$/.match(line.encode!('UTF-8', 'UTF-8', :invalid => :replace))
hosts.add(matchdata[1]) if matchdata
end
end
end
Expand Down Expand Up @@ -196,8 +227,9 @@ def destroy_preserved_hosts(hosts = nil, secs_ago = ONE_DAY_IN_SECS)
end

def print_preserved(preserved)
preserved.each do |entry|
puts entry
preserved.each_with_index do |entry,i|
puts "##{i}: #{entry[0]}"
entry[1].each { |h| puts " #{h}" }
end
end

Expand All @@ -216,6 +248,10 @@ end

namespace :ci do

task :check_env do
raise(USAGE) unless sha
end

namespace :test do

USAGE = <<-EOS
Expand All @@ -233,34 +269,36 @@ EOS
Run the acceptance tests through Beaker and install packages as part of the AIO puppet-agent installation.
#{USAGE}
EOS
task :aio do
task :aio => 'ci:check_env' do
beaker_test(:aio)
end

desc <<-EOS
DEPRECATED: use the aio method for now. Run the acceptance tests through Beaker and install from git on the configuration targets.
Run the acceptance tests through Beaker and install from git on the configuration targets.
#{USAGE}
EOS
task :git do
task :git => 'ci:check_env' do
beaker_test(:git)
end
end

desc "DEPRECATED: Beaker now does this automatically."
task :extract_preserved_config
desc "Capture the master and agent hostname from the latest log and construct a preserved_config.yaml for re-running against preserved hosts without provisioning."
task :extract_preserved_config do
generate_config_for_latest_hosts
end

desc <<-EOS
Run an acceptance test for a given node configuration and preserve the hosts.
Defaults to a packages run, but you can set it to 'git' with TYPE='git'.
#{USAGE}
EOS
task :test_and_preserve_hosts do
beaker_test(beaker_run_type, :preserve_hosts => 'always')
task :test_and_preserve_hosts => 'ci:check_env' do
beaker_test(beaker_run_type, :preserve_hosts => 'always', :__preserve_config__ => true)
end

desc "List acceptance runs from the past day which had hosts preserved."
task :list_preserved do
preserved = list_preserved_hosts
preserved = list_preserved_configurations
print_preserved(preserved)
end

Expand All @@ -276,7 +314,7 @@ You can go back through the last SECS_AGO logs. Default is one day ago in secs.
end

desc <<-EOS
Rerun an acceptance test using hosts_preserved.yaml to skip provisioning.
Rerun an acceptance test using the last captured preserved_config.yaml to skip provisioning.
Or specify a CONFIG_NUMBER from `rake ci:list_preserved`.
Uses the setup/rsync/pre-suite to rsync the local puppet source onto master and agent.
You may specify an RSYNC_FILTER_FILE as well.
Expand All @@ -285,13 +323,16 @@ You may skip rsyncing local puppet files over to the tests hosts by including SK
Defaults to an aio run, but you can set it to 'git' with TYPE='git'.
EOS
task :test_against_preserved_hosts do
config_number = (ENV['CONFIG_NUMBER'] || 0).to_i
preserved = list_preserved_configurations
print_preserved(preserved)
config_number = (ENV['CONFIG_NUMBER'] || 0).to_i
config_path = preserved[config_number][0]
puts "Using ##{config_number}: #{config_path}"
beaker_test(beaker_run_type,
:hosts_file => "log/latest/#{PRESERVED_HOSTS_FILENAME}"
:hosts_file => "#{config_path}/preserved_config.yaml",
:no_provision => true,
:preserve_hosts => 'always',
:pre_suite => ['setup/rsync/pre-suite']
)
end
end
Expand Down
8 changes: 2 additions & 6 deletions acceptance/config/aio/options.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
:type => 'aio',
:forge_host => 'forge-aio01-petest.puppetlabs.com',
:load_path => './lib/',
:repo_proxy => true,
:add_el_extras => true,
:pre_suite => [
:type => 'aio',
:pre_suite => [
'setup/common/pre-suite/000-delete-puppet-when-none.rb',
'setup/aio/pre-suite/010_Install.rb',
'setup/aio/pre-suite/021_InstallAristaModule.rb',
Expand Down
7 changes: 7 additions & 0 deletions acceptance/config/git/options.rbgit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
:pre_suite => [
'setup/common/pre-suite/000-delete-puppet-when-none.rb',
'setup/common/00_EnvSetup.rb',
'setup/git/pre-suite/01_TestSetup.rb',
],
}

0 comments on commit 82bafca

Please sign in to comment.