Merge pull request #29 from bodepd/finsh_pr_test_code

Finsh pr test code
This commit is contained in:
Dan Bode
2012-12-10 22:02:48 -08:00
7 changed files with 150 additions and 41 deletions

1
.gitignore vendored
View File

@@ -7,3 +7,4 @@ Puppetfile.lock
.current_testing .current_testing
my.log my.log
*swp *swp
logs

View File

@@ -17,7 +17,7 @@ mod 'puppetlabs/git', :git => 'git://github.com/puppetlabs/puppetlabs-git'
mod 'puppetlabs/vcsrepo', :git => 'git://github.com/puppetlabs/puppetlabs-vcsrepo' mod 'puppetlabs/vcsrepo', :git => 'git://github.com/puppetlabs/puppetlabs-vcsrepo'
mod 'saz/memcached', :git => 'git://github.com/saz/puppet-memcached' mod 'saz/memcached', :git => 'git://github.com/saz/puppet-memcached'
mod 'puppetlabs/rsync', :git => 'git://github.com/puppetlabs/puppetlabs-rsync' mod 'puppetlabs/rsync', :git => 'git://github.com/puppetlabs/puppetlabs-rsync'
mod 'puppetlabs/apache', :git => 'git://github.com/puppetlabs/puppetlabs-apache' mod 'puppetlabs/apache', :git => 'git://github.com/puppetlabs/puppetlabs-apache', :ref => '94ebca3aaaf2144a7b9ce7ca6a13837ec48a7e2a'
# other deps # other deps
mod 'puppetlabs/xinetd', :git => 'git://github.com/puppetlabs/puppetlabs-xinetd' mod 'puppetlabs/xinetd', :git => 'git://github.com/puppetlabs/puppetlabs-xinetd'
mod 'saz/ssh', :git => 'git://github.com/saz/puppet-ssh' mod 'saz/ssh', :git => 'git://github.com/saz/puppet-ssh'

View File

@@ -112,13 +112,17 @@ end
namespace :test do namespace :test do
desc 'reset test environment'
task :reset do
refresh_modules
destroy_all_vms
end
desc 'checkout and test a pull request, publish the results' desc 'checkout and test a pull request, publish the results'
task 'pull_request', [:project_name, :number] do |t, args| task 'pull_request', [:project_name, :number] do |t, args|
require 'vagrant' log_dir = File.join(base_dir, 'logs')
require 'github_api' log_file = File.join(log_dir, "#{Time.now.to_i.to_s}.log")
$stdout.reopen("my.log", "w") FileUtils.mkdir(log_dir) unless File.exists?(log_dir)
$stdout.sync = true
$stderr.reopen($stdout)
refresh_modules refresh_modules
checkout_pr( checkout_pr(
args.project_name, args.project_name,
@@ -130,11 +134,12 @@ namespace :test do
:password => github_password :password => github_password
} }
) )
test_two_node(['redhat', 'ubuntu']) system "bash -c 'rspec spec/test_two_node.rb;echo $?' 2>&1 | tee #{log_file}"
results = File.read('my.log') results = File.read(log_file)
publish_results( publish_results(
args.project_name, args.project_name,
args.number, args.number,
results.split("\n").last == '0' ? 'passed' : 'failed',
results, results,
{ {
:login => github_login, :login => github_login,
@@ -145,7 +150,7 @@ namespace :test do
desc 'test openstack with basic test script on redhat and ubuntu' desc 'test openstack with basic test script on redhat and ubuntu'
task 'two_node' do task 'two_node' do
test_two_node(['redhat', 'ubunut']) test_two_node(['redhat', 'ubuntu'])
end end
desc 'test all in one deployment on redhat/ubuntu (not yet implemented)' desc 'test all in one deployment on redhat/ubuntu (not yet implemented)'
@@ -153,8 +158,8 @@ namespace :test do
end end
desc 'test that openstack can boot an image from the vagrant bog' desc 'test that openstack can boot an image from the vagrant box'
task :controller_test do task :controller do
on_box('openstack_controller', 'sudo bash /tmp/foo.sh') on_box('openstack_controller', 'sudo bash /tmp/test_nova.sh;exit $?')
end end
end end

2
Vagrantfile vendored
View File

@@ -5,7 +5,7 @@ def parse_vagrant_config(
config = {'gui_mode' => "false", 'operatingsystem' => 'ubuntu'} config = {'gui_mode' => "false", 'operatingsystem' => 'ubuntu'}
if File.exists?(config_file) if File.exists?(config_file)
overrides = YAML.load_file(config_file) overrides = YAML.load_file(config_file)
config.merge(overrides) config.merge!(overrides)
end end
config config
end end

View File

@@ -4,28 +4,53 @@
module Puppetlabs module Puppetlabs
module OsTester module OsTester
def cmd_system (cmd) require 'yaml'
result = system cmd require 'github_api'
raise(RuntimeError, $?) unless $?.success? require 'open3'
result
class TestException < Exception
end end
def git_cmd(cmd) def cmd_system (cmd, print=true)
command = 'git ' + cmd puts "Running cmd: #{Array(cmd).join(' ')}" if print
Open3.popen3(*command) do |i, o, e, t| output = `#{cmd}`.split("\n")
raise StandardError, e.read unless (t ? t.value : $?).success? puts output.join("\n") if print
o.read.split("\n") raise(StandardError, "Cmd #{cmd} failed") unless $?.success?
end #Open3.popen3(*cmd) do |i, o, e, t|
# output = o.read.split("\n")
# raise StandardError, e.read unless (t ? t.value : $?).success?
#end
output
end
def git_cmd(cmd, print=true)
cmd_system('git ' + cmd, print)
end
def vagrant_command(cmd, box='')
require 'vagrant'
env = Vagrant::Environment.new(:ui_class => Vagrant::UI::Colored)
env.cli(cmd, box)
end end
def on_box (box, cmd) def on_box (box, cmd)
cmd_system("vagrant ssh #{box} -c '#{cmd}'") require 'vagrant'
env = Vagrant::Environment.new(:ui_class => Vagrant::UI::Colored)
raise("Invalid VM: #{box}") unless vm = env.vms[box.to_sym]
raise("VM: #{box} was not already created") unless vm.created?
ssh_data = ''
#vm.channel.sudo(cmd) do |type, data|
vm.channel.execute(cmd) do |type, data|
ssh_data = data
env.ui.info(ssh_data.chomp, :prefix => false)
end
ssh_data
end end
# destroy all vagrant instances # destroy all vagrant instances
def destroy_all_vms def destroy_all_vms
puts "About to destroy all vms..." puts "About to destroy all vms..."
cmd_system('vagrant destroy -f') vagrant_command('destroy -f')
puts "Destroyed all vms" puts "Destroyed all vms"
end end
@@ -37,7 +62,7 @@ module Puppetlabs
if remotes.include?(remote_name) if remotes.include?(remote_name)
puts "Did not have to add remote #{remote_name} to #{module_name}" puts "Did not have to add remote #{remote_name} to #{module_name}"
elsif ! remotes.include?('origin') elsif ! remotes.include?('origin')
raise(Exception, "Repo #{module_name} has no remote called origin, failing") raise(TestException, "Repo #{module_name} has no remote called origin, failing")
else else
remote_url = git_cmd('remote show origin').detect {|x| x =~ /\s+Push\s+URL: / } remote_url = git_cmd('remote show origin').detect {|x| x =~ /\s+Push\s+URL: / }
if remote_url =~ /(git|https?):\/\/(.+)\/(.+)?\/(.+)/ if remote_url =~ /(git|https?):\/\/(.+)\/(.+)?\/(.+)/
@@ -101,14 +126,18 @@ module Puppetlabs
# this means that is can be merged, and has a comment where one of the admin users # this means that is can be merged, and has a comment where one of the admin users
def deploy_two_node def deploy_two_node
cmd_system('vagrant up openstack_controller') vagrant_command('up', 'openstack_controller')
cmd_system('vagrant up compute1') vagrant_command('up', 'compute1')
end end
def refresh_modules def refresh_modules
cmd_system('librarian-puppet clean') ['modules', '.librarian', 'Puppetfile.lock', '.tmp', checkedoutfile_name].each do |dir|
if File.exists?(File.join(base_dir, dir ))
FileUtils.rm_rf(File.join(base_dir, dir))
end
end
FileUtils.rm(checkedout_file) if File.exists?(checkedout_file)
cmd_system('librarian-puppet install') cmd_system('librarian-puppet install')
FileUtils.rm(checkedout_file)
end end
def each_repo(&block) def each_repo(&block)
@@ -143,7 +172,7 @@ module Puppetlabs
contributors = {} contributors = {}
each_repo do |module_name| each_repo do |module_name|
if repos_i_care_about.include?(module_name) if repos_i_care_about.include?(module_name)
logs = git_cmd('log --format=short') logs = git_cmd('log --format=short', print=false)
user_lines = logs.select {|x| x =~ /^Author:\s+(.*)$/ } user_lines = logs.select {|x| x =~ /^Author:\s+(.*)$/ }
user_lines.collect do |x| user_lines.collect do |x|
if x =~ /^Author:\s+(.*)?\s+<((\S+)@(\S+))>$/ if x =~ /^Author:\s+(.*)?\s+<((\S+)@(\S+))>$/
@@ -200,11 +229,16 @@ module Puppetlabs
else else
puts "PR: #{pr['number']} from #{pr['base']['repo']['name']} was already merged, will not test" puts "PR: #{pr['number']} from #{pr['base']['repo']['name']} was already merged, will not test"
end end
puts "Did not find comment matching #{expected_body}"
return false return false
end end
def checkedoutfile_name
'.current_testing'
end
def checkedout_file def checkedout_file
File.join(base_dir, '.current_testing') File.join(base_dir, checkedoutfile_name)
end end
def checkedout_branch def checkedout_branch
@@ -238,15 +272,26 @@ module Puppetlabs
if checkedout_branch[:project] if checkedout_branch[:project]
if checkedout_branch[:project] == project_name and checkedout_branch[:number] == number if checkedout_branch[:project] == project_name and checkedout_branch[:number] == number
puts "#{project_name}/#{number} already checkout out, not doing it again" puts "#{project_name}/#{number} already checkout out, not doing it again"
return
else else
raise(Exception, "Wanted to checkout: #{project_name}/#{number}, but #{checkedout_branch[:project]}/#{checkedout_branch[:number]} was already checked out") raise(TestException, "Wanted to checkout: #{project_name}/#{number}, but #{checkedout_branch[:project]}/#{checkedout_branch[:number]} was already checked out")
end end
end end
if testable_pull_request?(pr, admin_users, options) if testable_pull_request?(pr, admin_users, expected_body, options)
clone_url = pr['head']['repo']['clone_url'] clone_url = pr['head']['repo']['clone_url']
remote_name = pr['head']['user']['login'] remote_name = pr['head']['user']['login']
sha = pr['head']['sha'] sha = pr['head']['sha']
base_ref = pr['base']['ref']
if base_ref != 'master'
raise(TestException, "At the moment, I do not support non-master base refs")
end
unless (diffs = git_cmd("diff origin/master")) == []
raise(TestException, "There are differences between the current checked out branch and master, you need to clean up these branhces before running any tests\n#{diffs.join("\n")}")
end
write_checkedout_file(project_name, number) write_checkedout_file(project_name, number)
puts 'found one that we should test' puts 'found one that we should test'
# TODO I am not sure how reliable all of this is going # TODO I am not sure how reliable all of this is going
@@ -257,7 +302,9 @@ module Puppetlabs
end end
git_cmd("fetch #{remote_name}") git_cmd("fetch #{remote_name}")
# TODO does that work if master has been updated? # TODO does that work if master has been updated?
git_cmd("checkout #{sha}") git_cmd("merge #{sha}")
else
raise("pull request #{project_name}/#{number} is not testable")
end end
end end
end end
@@ -265,7 +312,7 @@ module Puppetlabs
# publish a string as a gist. # publish a string as a gist.
# publish a link to that gist as a issue comment. # publish a link to that gist as a issue comment.
def publish_results(project_name, number, body, options) def publish_results(project_name, number, outcome, body, options)
require 'github_api' require 'github_api'
github = Github.new(options) github = Github.new(options)
gist_response = github.gists.create( gist_response = github.gists.create(
@@ -279,7 +326,7 @@ module Puppetlabs
'puppetlabs', 'puppetlabs',
"puppetlabs-#{project_name}", "puppetlabs-#{project_name}",
number, number,
'body' => "Test results can be found here: #{gist_response.html_url}" 'body' => "Test #{outcome}. Results can be found here: #{gist_response.html_url}"
) )
end end
@@ -288,9 +335,7 @@ module Puppetlabs
require 'yaml' require 'yaml'
#Rake::Task['openstack:setup'.to_sym].invoke #Rake::Task['openstack:setup'.to_sym].invoke
oses.each do |os| oses.each do |os|
cfg = File.join(base_dir, 'config.yaml') update_vagrant_os(os)
yml = YAML.load_file(cfg).merge({'operatingsystem' => os})
File.open(cfg, 'w') {|f| f.write(yml.to_yaml) }
cmd_system('vagrant destroy -f') cmd_system('vagrant destroy -f')
deploy_two_node deploy_two_node
# I should check this to see if the last line is cirros # I should check this to see if the last line is cirros
@@ -298,6 +343,12 @@ module Puppetlabs
end end
end end
def update_vagrant_os(os)
cfg = File.join(base_dir, 'config.yaml')
yml = YAML.load_file(cfg).merge({'operatingsystem' => os})
File.open(cfg, 'w') {|f| f.write(yml.to_yaml) }
end
# iterate through each testable pull request # iterate through each testable pull request
def each_testable_pull_request(&block) def each_testable_pull_request(&block)
end end

View File

@@ -70,7 +70,8 @@ node /openstack-controller/ {
# deploy a script that can be used to test nova # deploy a script that can be used to test nova
class { 'openstack::test_file': class { 'openstack::test_file':
quantum => $use_quantum, quantum => $use_quantum,
sleep_time => 30,
} }
if $::osfamily == 'Debian' { if $::osfamily == 'Debian' {

51
spec/test_two_node.rb Normal file
View File

@@ -0,0 +1,51 @@
require File.join(
File.dirname(__FILE__),
'..',
'lib',
'puppetlabs',
'os_tester'
)
describe 'test various two node configurations' do
def base_dir
File.join(File.dirname(__FILE__), '..')
end
include Puppetlabs::OsTester
before :each do
cmd_system('vagrant destroy -f')
end
describe 'test redhat' do
before :each do
update_vagrant_os('redhat')
end
it 'should be able to build out a two node environment' do
deploy_two_node
result = on_box('openstack_controller', 'sudo bash /tmp/test_nova.sh;exit $?')
result.split("\n").last.should == 'cirros'
end
end
describe 'test ubuntu' do
before :each do
update_vagrant_os('ubuntu')
end
it 'should be able to build out a two node environment' do
deploy_two_node
result = on_box('openstack_controller', 'sudo bash /tmp/test_nova.sh;exit $?')
result.split("\n").last.should == 'cirros'
end
end
after :all do
end
end