Skip to content

Commit

Permalink
Merge pull request #1725 from tphoney/task_improvements
Browse files Browse the repository at this point in the history
Improvements for tasks and testing
  • Loading branch information
pmcmaw committed Nov 21, 2017
2 parents 7fcad90 + 5f0f28e commit 16f8332
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 49 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ group :system_tests do
gem "beaker-hostgenerator", *location_for(ENV['BEAKER_HOSTGENERATOR_VERSION'])
gem "beaker-abs", *location_for(ENV['BEAKER_ABS_VERSION'] || '~> 0.1')
gem "puppet-blacksmith", '~> 3.4', :require => false
gem "beaker-task_helper"
end

gem 'puppet', *location_for(ENV['PUPPET_GEM_VERSION'])
Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/init_task_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# run a test task
require 'spec_helper_acceptance'

describe 'apache tasks', if: pe_install? && puppet_version =~ %r{(5\.\d\.\d)} do
describe 'apache tasks', if: puppet_version =~ %r{(5\.\d\.\d)} && fact('operatingsystem') != 'SLES' do
describe 'reload' do
it 'execute reload' do
pp = <<-EOS
Expand All @@ -14,7 +14,7 @@ class { 'apache':
apply_manifest(pp, :catch_failures => true)

result = run_task(task_name: 'apache', params: 'action=reload')
expect_multiple_regexes(result: result, regexes: [%r{reload successful}, %r{Job completed. 1/1 nodes succeeded}])
expect_multiple_regexes(result: result, regexes: [%r{reload successful}, %r{Job completed. 1/1 nodes succeeded|Ran on 1 node}])
end
end
end
45 changes: 1 addition & 44 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,13 @@
require 'beaker-rspec/helpers/serverspec'
require 'beaker/puppet_install_helper'
require 'beaker/module_install_helper'

def install_bolt_on(hosts)
on(hosts, "/opt/puppetlabs/puppet/bin/gem install --source http://rubygems.delivery.puppetlabs.net bolt -v '> 0.0.1'", acceptable_exit_codes: [0, 1]).stdout
end

def pe_install?
ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i
end

def puppet_version
(on default, puppet('--version')).output.chomp
end
require 'beaker/task_helper'

run_puppet_install_helper
install_bolt_on(hosts) unless pe_install?
install_module_on(hosts)
install_module_dependencies_on(hosts)

DEFAULT_PASSWORD = if default[:hypervisor] == 'vagrant'
'vagrant'
elsif default[:hypervisor] == 'vcloud'
'Qu@lity!'
end

def run_puppet_access_login(user:, password: '~!@#$%^*-/ aZ', lifetime: '5y')
on(master, puppet('access', 'login', '--username', user, '--lifetime', lifetime), stdin: password)
end

def run_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
if pe_install?
run_puppet_task(task_name: task_name, params: params)
else
run_bolt_task(task_name: task_name, params: params, password: password)
end
end

def run_bolt_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
on(master, "/opt/puppetlabs/puppet/bin/bolt task run #{task_name} --modulepath /etc/puppetlabs/code/modules/service --nodes localhost --password #{password} #{params}", acceptable_exit_codes: [0, 1]).stdout # rubocop:disable Metrics/LineLength
end

def run_puppet_task(task_name:, params: nil)
on(master, puppet('task', 'run', task_name, '--nodes', fact_on(master, 'fqdn'), params.to_s), acceptable_exit_codes: [0, 1]).stdout
end

def expect_multiple_regexes(result:, regexes:)
regexes.each do |regex|
expect(result).to match(regex)
end
end

RSpec.configure do |c|
c.filter_run :focus => true
c.run_all_when_everything_filtered = true
Expand Down
4 changes: 4 additions & 0 deletions tasks/init.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"action": {
"description": "Action to perform ",
"type": "Enum[reload]"
},
"service_name": {
"description": "The name of the apache service ",
"type": "Optional[String[1]]"
}
}
}
19 changes: 16 additions & 3 deletions tasks/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@
require 'open3'
require 'puppet'

def service(action)
cmd_string = "service apache2 #{action}"
def service(action, service_name)
if service_name.nil?
cmd_string = "facter -p osfamily"
stdout, stderr, status = Open3.capture3(cmd_string)
osfamily = stdout.strip
if osfamily == 'RedHat'
service_name = 'httpd'
elsif osfamily == 'FreeBSD'
service_name = 'apache24'
else
service_name = 'apache2'
end
end
cmd_string = "service #{service_name} #{action}"
stdout, stderr, status = Open3.capture3(cmd_string)
raise Puppet::Error, stderr if status != 0
{ status: "#{action} successful" }
end

params = JSON.parse(STDIN.read)
action = params['action']
service_name = params['service_name']

begin
result = service(action)
result = service(action, service_name)
puts result.to_json
exit 0
rescue Puppet::Error => e
Expand Down

0 comments on commit 16f8332

Please sign in to comment.