Skip to content

Commit

Permalink
Merge pull request #81 from chorankates/did-exec-fire
Browse files Browse the repository at this point in the history
adding did_exec_fire? method
  • Loading branch information
chorankates authored Aug 23, 2016
2 parents 37177fa + 599ec6b commit 2dcbf2e
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/rouster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RemoteExecutionError < StandardError; end # thrown by run()
class PassthroughError < StandardError; end # thrown by anything Passthrough related (mostly vagrant.rb)
class SSHConnectionError < StandardError; end # thrown by available_via_ssh() -- and potentially _run()

attr_accessor :facts
attr_accessor :facts, :last_puppet_run
attr_reader :cache, :cache_timeout, :deltas, :exitcode, :logger, :name, :output, :passthrough, :retries, :sshkey, :unittest, :vagrantbinary, :vagrantfile

##
Expand Down
25 changes: 23 additions & 2 deletions lib/rouster/puppet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ def facter(cache=true, custom_facts=true)
res
end

##
# did_exec_fire?
#
# given the name of an Exec resource, parse the output from the most recent puppet run
# and return true/false based on whether the exec in question was fired
def did_exec_fire?(resource_name, puppet_run = self.last_puppet_run)
# Notice: /Stage[main]//Exec[foo]/returns: executed successfully
# Error: /Stage[main]//Exec[bar]/returns: change from notrun to 0 failed: Could not find command '/bin/bar'
matchers = [
'Notice: /Stage\[.*\]//Exec\[%s\]/returns: executed successfully',
'Error: /Stage\[.*\]//Exec\[%s\]/returns: change from notrun to 0 failed'
]

matchers.each do |m|
matcher = sprintf(m, resource_name)
return true if puppet_run.match(matcher)
end

false
end

##
# get_catalog
#
Expand Down Expand Up @@ -468,7 +489,7 @@ def run_puppet(mode='master', passed_opts={})
cmd << sprintf(' %s', opts[:additional_options]) unless opts[:additional_options].nil?
cmd << sprintf(' %s', file)

self.run(cmd, opts[:expected_exitcode])
self.last_puppet_run = self.run(cmd, opts[:expected_exitcode])
end
end

Expand All @@ -490,7 +511,7 @@ def run_puppet(mode='master', passed_opts={})
cmd << sprintf(' %s', opts[:additional_options]) unless opts[:additional_options].nil?
cmd << sprintf(' %s', m)

self.run(cmd, opts[:expected_exitcode])
self.last_puppet_run = self.run(cmd, opts[:expected_exitcode])
end

end
Expand Down
59 changes: 59 additions & 0 deletions test/unit/puppet/resources/puppet_run_with_failed_exec
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Debug: Creating default schedules
Debug: Using settings: adding file resource 'clientyamldir': 'File[/home/vagrant/.puppet/var/client_yaml]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/client_yaml"}'
Debug: Using settings: adding file resource 'lastrunreport': 'File[/home/vagrant/.puppet/var/state/last_run_report.yaml]{:loglevel=>:debug, :links=>:follow, :ensure=>:file, :backup=>false, :mode=>"640", :path=>"/home/vagrant/.puppet/var/state/last_run_report.yaml"}'
Debug: Using settings: adding file resource 'confdir': 'File[/home/vagrant/.puppet]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet"}'
Debug: Using settings: adding file resource 'ssldir': 'File[/home/vagrant/.puppet/ssl]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"771", :path=>"/home/vagrant/.puppet/ssl"}'
Debug: Using settings: adding file resource 'privatekeydir': 'File[/home/vagrant/.puppet/ssl/private_keys]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/ssl/private_keys"}'
Debug: Using settings: adding file resource 'client_datadir': 'File[/home/vagrant/.puppet/var/client_data]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/client_data"}'
Debug: Using settings: adding file resource 'statedir': 'File[/home/vagrant/.puppet/var/state]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"1755", :path=>"/home/vagrant/.puppet/var/state"}'
Debug: Using settings: adding file resource 'vardir': 'File[/home/vagrant/.puppet/var]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/var"}'
Debug: Using settings: adding file resource 'libdir': 'File[/home/vagrant/.puppet/var/lib]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/var/lib"}'
Debug: Using settings: adding file resource 'publickeydir': 'File[/home/vagrant/.puppet/ssl/public_keys]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/ssl/public_keys"}'
Debug: Using settings: adding file resource 'rundir': 'File[/home/vagrant/.puppet/var/run]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"755", :path=>"/home/vagrant/.puppet/var/run"}'
Debug: Using settings: adding file resource 'privatedir': 'File[/home/vagrant/.puppet/ssl/private]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/ssl/private"}'
Debug: Using settings: adding file resource 'statefile': 'File[/home/vagrant/.puppet/var/state/state.yaml]{:loglevel=>:debug, :links=>:follow, :ensure=>:file, :backup=>false, :mode=>"660", :path=>"/home/vagrant/.puppet/var/state/state.yaml"}'
Debug: Using settings: adding file resource 'clientbucketdir': 'File[/home/vagrant/.puppet/var/clientbucket]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/clientbucket"}'
Debug: Using settings: adding file resource 'lastrunfile': 'File[/home/vagrant/.puppet/var/state/last_run_summary.yaml]{:loglevel=>:debug, :links=>:follow, :ensure=>:file, :backup=>false, :mode=>"644", :path=>"/home/vagrant/.puppet/var/state/last_run_summary.yaml"}'
Debug: Using settings: adding file resource 'logdir': 'File[/home/vagrant/.puppet/var/log]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/log"}'
Debug: Using settings: adding file resource 'certdir': 'File[/home/vagrant/.puppet/ssl/certs]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/ssl/certs"}'
Debug: Using settings: adding file resource 'graphdir': 'File[/home/vagrant/.puppet/var/state/graphs]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/var/state/graphs"}'
Debug: Using settings: adding file resource 'requestdir': 'File[/home/vagrant/.puppet/ssl/certificate_requests]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/ssl/certificate_requests"}'
Debug: /File[/home/vagrant/.puppet/var]: Autorequiring File[/home/vagrant/.puppet]
Debug: /File[/home/vagrant/.puppet/ssl/certs]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/var/state]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/var/state/graphs]: Autorequiring File[/home/vagrant/.puppet/var/state]
Debug: /File[/home/vagrant/.puppet/var/state/last_run_report.yaml]: Autorequiring File[/home/vagrant/.puppet/var/state]
Debug: /File[/home/vagrant/.puppet/var/run]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/ssl/certificate_requests]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/ssl]: Autorequiring File[/home/vagrant/.puppet]
Debug: /File[/home/vagrant/.puppet/var/state/last_run_summary.yaml]: Autorequiring File[/home/vagrant/.puppet/var/state]
Debug: /File[/home/vagrant/.puppet/ssl/private_keys]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/var/client_data]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/var/client_yaml]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/var/clientbucket]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/ssl/private]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/ssl/public_keys]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/var/log]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/var/state/state.yaml]: Autorequiring File[/home/vagrant/.puppet/var/state]
Debug: /File[/home/vagrant/.puppet/var/lib]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: Finishing transaction 69859761088440
Debug: Loaded state in 0.00 seconds
Debug: Loaded state in 0.00 seconds
Info: Applying configuration version '1427151219'
Debug: /Schedule[daily]: Skipping device resources because running on a host
Debug: /Schedule[monthly]: Skipping device resources because running on a host
Debug: /Schedule[hourly]: Skipping device resources because running on a host
Error: Could not find command '/bin/bar'
Error: /Stage[main]//Exec[bar]/returns: change from notrun to 0 failed: Could not find command '/bin/bar'
Debug: /Schedule[never]: Skipping device resources because running on a host
Debug: /Schedule[weekly]: Skipping device resources because running on a host
Debug: /Schedule[puppet]: Skipping device resources because running on a host
Debug: Class[Main]: The container Stage[main] will propagate my refresh event
Debug: Finishing transaction 69859760392420
Debug: Storing state
Debug: Stored state in 0.01 seconds
Notice: Finished catalog run in 0.10 seconds
Debug: Using settings: adding file resource 'rrddir': 'File[/home/vagrant/.puppet/var/rrd]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/rrd"}'
Debug: Finishing transaction 69859760156920
Debug: Received report to process from centos6.internal.salesforce.com
Debug: Processing report from centos6.internal.salesforce.com with processor Puppet::Reports::Store
61 changes: 61 additions & 0 deletions test/unit/puppet/resources/puppet_run_with_successful_exec
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Debug: Creating default schedules
Debug: Using settings: adding file resource 'clientyamldir': 'File[/home/vagrant/.puppet/var/client_yaml]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/client_yaml"}'
Debug: Using settings: adding file resource 'lastrunreport': 'File[/home/vagrant/.puppet/var/state/last_run_report.yaml]{:loglevel=>:debug, :links=>:follow, :ensure=>:file, :backup=>false, :mode=>"640", :path=>"/home/vagrant/.puppet/var/state/last_run_report.yaml"}'
Debug: Using settings: adding file resource 'confdir': 'File[/home/vagrant/.puppet]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet"}'
Debug: Using settings: adding file resource 'ssldir': 'File[/home/vagrant/.puppet/ssl]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"771", :path=>"/home/vagrant/.puppet/ssl"}'
Debug: Using settings: adding file resource 'privatekeydir': 'File[/home/vagrant/.puppet/ssl/private_keys]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/ssl/private_keys"}'
Debug: Using settings: adding file resource 'client_datadir': 'File[/home/vagrant/.puppet/var/client_data]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/client_data"}'
Debug: Using settings: adding file resource 'statedir': 'File[/home/vagrant/.puppet/var/state]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"1755", :path=>"/home/vagrant/.puppet/var/state"}'
Debug: Using settings: adding file resource 'vardir': 'File[/home/vagrant/.puppet/var]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/var"}'
Debug: Using settings: adding file resource 'libdir': 'File[/home/vagrant/.puppet/var/lib]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/var/lib"}'
Debug: Using settings: adding file resource 'publickeydir': 'File[/home/vagrant/.puppet/ssl/public_keys]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/ssl/public_keys"}'
Debug: Using settings: adding file resource 'rundir': 'File[/home/vagrant/.puppet/var/run]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"755", :path=>"/home/vagrant/.puppet/var/run"}'
Debug: Using settings: adding file resource 'privatedir': 'File[/home/vagrant/.puppet/ssl/private]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/ssl/private"}'
Debug: Using settings: adding file resource 'statefile': 'File[/home/vagrant/.puppet/var/state/state.yaml]{:loglevel=>:debug, :links=>:follow, :ensure=>:file, :backup=>false, :mode=>"660", :path=>"/home/vagrant/.puppet/var/state/state.yaml"}'
Debug: Using settings: adding file resource 'clientbucketdir': 'File[/home/vagrant/.puppet/var/clientbucket]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/clientbucket"}'
Debug: Using settings: adding file resource 'lastrunfile': 'File[/home/vagrant/.puppet/var/state/last_run_summary.yaml]{:loglevel=>:debug, :links=>:follow, :ensure=>:file, :backup=>false, :mode=>"644", :path=>"/home/vagrant/.puppet/var/state/last_run_summary.yaml"}'
Debug: Using settings: adding file resource 'logdir': 'File[/home/vagrant/.puppet/var/log]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/log"}'
Debug: Using settings: adding file resource 'certdir': 'File[/home/vagrant/.puppet/ssl/certs]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/ssl/certs"}'
Debug: Using settings: adding file resource 'graphdir': 'File[/home/vagrant/.puppet/var/state/graphs]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/var/state/graphs"}'
Debug: Using settings: adding file resource 'requestdir': 'File[/home/vagrant/.puppet/ssl/certificate_requests]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :path=>"/home/vagrant/.puppet/ssl/certificate_requests"}'
Debug: /File[/home/vagrant/.puppet/var]: Autorequiring File[/home/vagrant/.puppet]
Debug: /File[/home/vagrant/.puppet/ssl/certs]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/var/state]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/var/state/graphs]: Autorequiring File[/home/vagrant/.puppet/var/state]
Debug: /File[/home/vagrant/.puppet/var/state/last_run_report.yaml]: Autorequiring File[/home/vagrant/.puppet/var/state]
Debug: /File[/home/vagrant/.puppet/var/run]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/ssl/certificate_requests]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/ssl]: Autorequiring File[/home/vagrant/.puppet]
Debug: /File[/home/vagrant/.puppet/var/state/last_run_summary.yaml]: Autorequiring File[/home/vagrant/.puppet/var/state]
Debug: /File[/home/vagrant/.puppet/ssl/private_keys]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/var/client_data]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/var/client_yaml]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/var/clientbucket]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/ssl/private]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/ssl/public_keys]: Autorequiring File[/home/vagrant/.puppet/ssl]
Debug: /File[/home/vagrant/.puppet/var/log]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: /File[/home/vagrant/.puppet/var/state/state.yaml]: Autorequiring File[/home/vagrant/.puppet/var/state]
Debug: /File[/home/vagrant/.puppet/var/lib]: Autorequiring File[/home/vagrant/.puppet/var]
Debug: Finishing transaction 69859761088440
Debug: Loaded state in 0.00 seconds
Debug: Loaded state in 0.00 seconds
Info: Applying configuration version '1427151219'
Debug: /Schedule[daily]: Skipping device resources because running on a host
Debug: /Schedule[monthly]: Skipping device resources because running on a host
Debug: /Schedule[hourly]: Skipping device resources because running on a host
Debug: Exec[foo](provider=posix): Executing '/bin/echo foo'
Debug: Executing '/bin/echo foo'
Notice: /Stage[main]//Exec[foo]/returns: executed successfully
Debug: /Stage[main]//Exec[foo]: The container Class[Main] will propagate my refresh event
Debug: /Schedule[never]: Skipping device resources because running on a host
Debug: /Schedule[weekly]: Skipping device resources because running on a host
Debug: /Schedule[puppet]: Skipping device resources because running on a host
Debug: Class[Main]: The container Stage[main] will propagate my refresh event
Debug: Finishing transaction 69859760392420
Debug: Storing state
Debug: Stored state in 0.01 seconds
Notice: Finished catalog run in 0.10 seconds
Debug: Using settings: adding file resource 'rrddir': 'File[/home/vagrant/.puppet/var/rrd]{:loglevel=>:debug, :links=>:follow, :ensure=>:directory, :backup=>false, :mode=>"750", :path=>"/home/vagrant/.puppet/var/rrd"}'
Debug: Finishing transaction 69859760156920
Debug: Received report to process from centos6.internal.salesforce.com
Debug: Processing report from centos6.internal.salesforce.com with processor Puppet::Reports::Store
44 changes: 44 additions & 0 deletions test/unit/puppet/test_puppet_parsing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require sprintf('%s/../../../path_helper', File.dirname(File.expand_path(__FILE__)))

require 'rouster'
require 'rouster/puppet'
require 'test/unit'

# this is a unit test, no need for a real Rouster VM

class TestGetPuppetStar < Test::Unit::TestCase

def setup
assert_nothing_raised do
@app = Rouster.new(:name => 'app', :unittest => true)
end

# expose private methods
Rouster.send(:public, *Rouster.protected_instance_methods)
end

def test_with_successful_exec
title = 'foo'
input = File.read(sprintf('%s/../../../test/unit/puppet/resources/puppet_run_with_successful_exec', File.dirname(File.expand_path(__FILE__))))


assert(@app.did_exec_fire?(title, input))
end

def test_with_failed_exec
title = 'bar'
input = File.read(sprintf('%s/../../../test/unit/puppet/resources/puppet_run_with_failed_exec', File.dirname(File.expand_path(__FILE__))))

assert(@app.did_exec_fire?(title, input))
end

def test_looking_for_nonexistent_exec
title = 'fizzbang'
input = File.read(sprintf('%s/../../../test/unit/puppet/resources/puppet_run_with_successful_exec', File.dirname(File.expand_path(__FILE__))))

assert_false(@app.did_exec_fire?(title, input))
end


end

0 comments on commit 2dcbf2e

Please sign in to comment.