Class: BoltSpec::Plans::MockExecutor
- Inherits:
-
Object
- Object
- BoltSpec::Plans::MockExecutor
- Defined in:
- lib/bolt_spec/plans/mock_executor.rb
Overview
Nothing on the executor is ‘public’
Instance Attribute Summary collapse
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#execute_any_plan ⇒ Object
Returns the value of attribute execute_any_plan.
-
#in_parallel ⇒ Object
readonly
Returns the value of attribute in_parallel.
-
#noop ⇒ Object
readonly
Returns the value of attribute noop.
-
#run_as ⇒ Object
Returns the value of attribute run_as.
-
#transport_features ⇒ Object
Returns the value of attribute transport_features.
Instance Method Summary collapse
- #assert_call_expectations ⇒ Object
- #await_results(promises) ⇒ Object
- #download_file(targets, source, destination, options = {}, _position = []) ⇒ Object
-
#initialize(modulepath) ⇒ MockExecutor
constructor
A new instance of MockExecutor.
- #log_action(*_args) ⇒ Object
- #log_plan(_plan_name) ⇒ Object
- #module_file_id(file) ⇒ Object
- #publish_event(event) ⇒ Object
- #queue_execute(targets) ⇒ Object
- #report_apply(_statements, _resources) ⇒ Object
- #report_bundled_content(_mode, _name) ⇒ Object
- #report_function_call(_function) ⇒ Object
- #run_command(targets, command, options = {}, _position = []) ⇒ Object
- #run_plan(scope, plan_clj, params) ⇒ Object
- #run_script(targets, script_path, arguments, options = {}, _position = []) ⇒ Object
- #run_task(targets, task, arguments, options = {}, _position = []) ⇒ Object
- #stub_apply ⇒ Object
- #stub_out_message ⇒ Object
-
#transport(_protocol) ⇒ Object
Mocked for apply_prep.
- #upload_file(targets, source_path, destination, options = {}, _position = []) ⇒ Object
- #wait_until_available(targets, _options) ⇒ Object
-
#with_node_logging(_description, targets) ⇒ Object
Mocked for Apply so it does not compile and execute.
- #with_plan_allowed_exec(plan_name, params) ⇒ Object
- #without_default_logging ⇒ Object
Constructor Details
#initialize(modulepath) ⇒ MockExecutor
Returns a new instance of MockExecutor.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 23 def initialize(modulepath) @noop = false @run_as = nil @in_parallel = false @error_message = nil @allow_apply = false @modulepath = [modulepath].flatten.map { |path| File.absolute_path(path) } MOCKED_ACTIONS.each { |action| instance_variable_set(:"@#{action}_doubles", {}) } @stub_out_message = nil @transport_features = ['puppet-agent'] @executor_real = Bolt::Executor.new # by default, we want to execute any plan that we come across without error # or mocking. users can toggle this behavior so that plans will either need to # be mocked out, or an error will be thrown. @execute_any_plan = true # plans that are allowed to be executed by the @executor_real @allowed_exec_plans = {} end |
Instance Attribute Details
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
20 21 22 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 20 def @error_message end |
#execute_any_plan ⇒ Object
Returns the value of attribute execute_any_plan.
21 22 23 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 21 def execute_any_plan @execute_any_plan end |
#in_parallel ⇒ Object (readonly)
Returns the value of attribute in_parallel.
20 21 22 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 20 def in_parallel @in_parallel end |
#noop ⇒ Object (readonly)
Returns the value of attribute noop.
20 21 22 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 20 def noop @noop end |
#run_as ⇒ Object
Returns the value of attribute run_as.
21 22 23 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 21 def run_as @run_as end |
#transport_features ⇒ Object
Returns the value of attribute transport_features.
21 22 23 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 21 def transport_features @transport_features end |
Instance Method Details
#assert_call_expectations ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 174 def assert_call_expectations MOCKED_ACTIONS.each do |action| instance_variable_get(:"@#{action}_doubles").map do |object, doub| doub.assert_called(object) end end @stub_out_message.assert_called('out::message') if @stub_out_message end |
#await_results(promises) ⇒ Object
239 240 241 242 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 239 def await_results(promises) raise "Unexpected call to apply(#{targets})" unless @allow_apply Bolt::ResultSet.new(promises.map { |target| Bolt::ApplyResult.new(target) }) end |
#download_file(targets, source, destination, options = {}, _position = []) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 94 def download_file(targets, source, destination, = {}, _position = []) result = nil if (doub = @download_doubles[source] || @download_doubles[:default]) result = doub.process(targets, source, destination, ) end unless result targets = targets.map(&:name) @error_message = "Unexpected call to 'download_file(#{source}, #{destination}, #{targets}, #{})'" raise UnexpectedInvocation, @error_message end result end |
#log_action(*_args) ⇒ Object
201 202 203 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 201 def log_action(*_args) yield end |
#log_plan(_plan_name) ⇒ Object
205 206 207 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 205 def log_plan(_plan_name) yield end |
#module_file_id(file) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 42 def module_file_id(file) modpath = @modulepath.select { |path| file =~ /^#{path}/ } raise "Could not identify modulepath containing #{file}: #{modpath}" unless modpath.size == 1 path = Pathname.new(file) relative = path.relative_path_from(Pathname.new(modpath.first)) segments = relative.to_path.split('/') ([segments[0]] + segments[2..-1]).join('/') end |
#publish_event(event) ⇒ Object
219 220 221 222 223 224 225 226 227 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 219 def publish_event(event) if event[:type] == :message unless @stub_out_message @error_message = "Unexpected call to 'out::message(#{event[:message]})'" raise UnexpectedInvocation, @error_message end @stub_out_message.process(event[:message]) end end |
#queue_execute(targets) ⇒ Object
234 235 236 237 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 234 def queue_execute(targets) raise "Unexpected call to apply(#{targets})" unless @allow_apply targets end |
#report_apply(_statements, _resources) ⇒ Object
217 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 217 def report_apply(_statements, _resources); end |
#report_bundled_content(_mode, _name) ⇒ Object
215 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 215 def report_bundled_content(_mode, _name); end |
#report_function_call(_function) ⇒ Object
213 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 213 def report_function_call(_function); end |
#run_command(targets, command, options = {}, _position = []) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 52 def run_command(targets, command, = {}, _position = []) result = nil if (doub = @command_doubles[command] || @command_doubles[:default]) result = doub.process(targets, command, ) end unless result targets = targets.map(&:name) @error_message = "Unexpected call to 'run_command(#{command}, #{targets}, #{})'" raise UnexpectedInvocation, @error_message end result end |
#run_plan(scope, plan_clj, params) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 128 def run_plan(scope, plan_clj, params) result = nil plan_name = plan_clj.closure_name # get the mock object either by plan name, or the default in case allow_any_plan # was called, if both are nil / don't exist, then dub will be nil and we'll fall # through to another conditional statement doub = @plan_doubles[plan_name] || @plan_doubles[:default] # rubocop:disable Lint/DuplicateBranch # High level: # - If we've explicitly allowed execution of the plan (normally the main plan # passed into BoltSpec::Plan::run_plan()), then execute it # - If we've explicitly "allowed/expected" the plan (mocked), # then run it through the mock object # - If we're allowing "any" plan to be executed, # then execute it # - Otherwise we have an error if @allowed_exec_plans.key?(plan_name) && @allowed_exec_plans[plan_name] == params # This plan's name + parameters were explicitly allowed to be executed. # run it with the real executor. # We require this functionality so that the BoltSpec::Plans.run_plan() # function can kick off the initial plan. In reality, no other plans should # be in this hash. result = @executor_real.run_plan(scope, plan_clj, params) elsif doub result = doub.process(scope, plan_clj, params) # the throw here is how Puppet exits out of a closure and returns a result # it throws this special symbol with a result object that is captured by # the run_plan Puppet function throw :return, result elsif @execute_any_plan # if the plan wasn't allowed or mocked out, and we're allowing any plan to be # executed, then execute the plan result = @executor_real.run_plan(scope, plan_clj, params) else # convert to JSON and back so that we get the ruby representation with all keys and # values converted to a string .to_s instead of their ruby object notation params_str = JSON.parse(params.to_json) @error_message = "Unexpected call to 'run_plan(#{plan_name}, #{params_str})'" raise UnexpectedInvocation, @error_message end # rubocop:enable Lint/DuplicateBranch result end |
#run_script(targets, script_path, arguments, options = {}, _position = []) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 65 def run_script(targets, script_path, arguments, = {}, _position = []) script = module_file_id(script_path) result = nil if (doub = @script_doubles[script] || @script_doubles[:default]) result = doub.process(targets, script, arguments, ) end unless result targets = targets.map(&:name) params = .merge('arguments' => arguments) @error_message = "Unexpected call to 'run_script(#{script}, #{targets}, #{params})'" raise UnexpectedInvocation, @error_message end result end |
#run_task(targets, task, arguments, options = {}, _position = []) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 80 def run_task(targets, task, arguments, = {}, _position = []) result = nil if (doub = @task_doubles[task.name] || @task_doubles[:default]) result = doub.process(targets, task.name, arguments, ) end unless result targets = targets.map(&:name) params = arguments.merge() @error_message = "Unexpected call to 'run_task(#{task.name}, #{targets}, #{params})'" raise UnexpectedInvocation, @error_message end result end |
#stub_apply ⇒ Object
193 194 195 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 193 def stub_apply @allow_apply = true end |
#stub_out_message ⇒ Object
189 190 191 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 189 def @stub_out_message ||= ActionDouble.new(:PublishStub) end |
#transport(_protocol) ⇒ Object
Mocked for apply_prep
246 247 248 249 250 251 252 253 254 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 246 def transport(_protocol) Class.new do attr_reader :provided_features def initialize(features) @provided_features = features end end.new(transport_features) end |
#upload_file(targets, source_path, destination, options = {}, _position = []) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 107 def upload_file(targets, source_path, destination, = {}, _position = []) source = module_file_id(source_path) result = nil if (doub = @upload_doubles[source] || @upload_doubles[:default]) result = doub.process(targets, source, destination, ) end unless result targets = targets.map(&:name) @error_message = "Unexpected call to 'upload_file(#{source}, #{destination}, #{targets}, #{})'" raise UnexpectedInvocation, @error_message end result end |
#wait_until_available(targets, _options) ⇒ Object
197 198 199 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 197 def wait_until_available(targets, ) Bolt::ResultSet.new(targets.map { |target| Bolt::Result.new(target) }) end |
#with_node_logging(_description, targets) ⇒ Object
Mocked for Apply so it does not compile and execute.
230 231 232 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 230 def with_node_logging(_description, targets) raise "Unexpected call to apply(#{targets})" unless @allow_apply end |
#with_plan_allowed_exec(plan_name, params) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 121 def with_plan_allowed_exec(plan_name, params) @allowed_exec_plans[plan_name] = params result = yield @allowed_exec_plans.delete(plan_name) result end |
#without_default_logging ⇒ Object
209 210 211 |
# File 'lib/bolt_spec/plans/mock_executor.rb', line 209 def without_default_logging yield end |