Class: BoltSpec::Plans::MockExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt_spec/plans/mock_executor.rb

Overview

Nothing on the executor is ‘public’

Instance Attribute Summary collapse

Instance Method Summary collapse

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
41
# File 'lib/bolt_spec/plans/mock_executor.rb', line 23

def initialize(modulepath)
  @noop = false
  @run_as = nil
  @in_parallel = false
  @future = {}
  @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_messageObject (readonly)

Returns the value of attribute error_message.



20
21
22
# File 'lib/bolt_spec/plans/mock_executor.rb', line 20

def error_message
  @error_message
end

#execute_any_planObject

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

#futureObject (readonly)

Returns the value of attribute future.



20
21
22
# File 'lib/bolt_spec/plans/mock_executor.rb', line 20

def future
  @future
end

#in_parallelObject (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

#noopObject (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_asObject

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_featuresObject

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

#transportsObject (readonly)

Returns the value of attribute transports.



20
21
22
# File 'lib/bolt_spec/plans/mock_executor.rb', line 20

def transports
  @transports
end

Instance Method Details

#assert_call_expectationsObject



183
184
185
186
187
188
189
190
# File 'lib/bolt_spec/plans/mock_executor.rb', line 183

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



242
243
244
245
# File 'lib/bolt_spec/plans/mock_executor.rb', line 242

def await_results(promises)
  raise "Unexpected call to apply(#{targets})" unless @allow_apply
  Bolt::ResultSet.new(promises.map { |target| Bolt::ApplyResult.new(target) })
end

#batch_execute(_targets) ⇒ Object

Public methods on Bolt::Executor that need to be mocked so there aren’t “undefined method” errors.



315
# File 'lib/bolt_spec/plans/mock_executor.rb', line 315

def batch_execute(_targets); end

#create_yarn(scope, block, object, _index) ⇒ Object

Evaluates a ‘parallelize()` block and returns the result. Normally, Bolt’s executor wraps this in a Yarn for each object passed to the ‘parallelize()` function, and then executes them in parallel before returning the result from the block. However, in BoltSpec the block is executed for each object sequentially, and this function returns the result itself.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/bolt_spec/plans/mock_executor.rb', line 267

def create_yarn(scope, block, object, _index)
  # Create the new scope
  newscope = Puppet::Parser::Scope.new(scope.compiler)
  local = Puppet::Parser::Scope::LocalScope.new

  # Compress the current scopes into a single vars hash to add to the new scope
  current_scope = scope.effective_symtable(true)
  until current_scope.nil?
    current_scope.instance_variable_get(:@symbols)&.each_pair { |k, v| local[k] = v }
    current_scope = current_scope.parent
  end
  newscope.push_ephemerals([local])

  begin
    result = catch(:return) do
      args = { block.parameters[0][1].to_s => object }
      block.closure.call_by_name_with_scope(newscope, args, true)
    end

    # If we got a return from the block, get it's value
    # Otherwise the result is the last line from the block
    result = result.value if result.is_a?(Puppet::Pops::Evaluator::Return)

    # Validate the result is a PlanResult
    unless Puppet::Pops::Types::TypeParser.singleton.parse('Boltlib::PlanResult').instance?(result)
      raise Bolt::InvalidParallelResult.new(result.to_s, *Puppet::Pops::PuppetStack.top_of_stack)
    end

    result
  rescue Puppet::PreformattedError => e
    if e.cause.is_a?(Bolt::Error)
      e.cause
    else
      raise e
    end
  end
end

#download_file(targets, source, destination, options = {}, _position = []) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bolt_spec/plans/mock_executor.rb', line 103

def download_file(targets, source, destination, options = {}, _position = [])
  result = nil
  if (doub = @download_doubles[source] || @download_doubles[:default])
    result = doub.process(targets, source, destination, options)
  end
  unless result
    targets = targets.map(&:name)
    @error_message = "Unexpected call to 'download_file(#{source}, #{destination}, #{targets}, #{options})'"
    raise UnexpectedInvocation, @error_message
  end
  result
end

#finish_plan(_plan_result) ⇒ Object



317
# File 'lib/bolt_spec/plans/mock_executor.rb', line 317

def finish_plan(_plan_result); end

#handle_event(_event) ⇒ Object



319
# File 'lib/bolt_spec/plans/mock_executor.rb', line 319

def handle_event(_event); end

#log_action(*_args) ⇒ Object



210
211
212
# File 'lib/bolt_spec/plans/mock_executor.rb', line 210

def log_action(*_args)
  yield
end

#log_plan(_plan_name) ⇒ Object



214
215
216
# File 'lib/bolt_spec/plans/mock_executor.rb', line 214

def log_plan(_plan_name)
  yield
end

#module_file_id(file) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/bolt_spec/plans/mock_executor.rb', line 43

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

#prompt(_prompt, _options) ⇒ Object



321
# File 'lib/bolt_spec/plans/mock_executor.rb', line 321

def prompt(_prompt, _options); end

#publish_event(event) ⇒ Object



222
223
224
225
226
227
228
229
230
# File 'lib/bolt_spec/plans/mock_executor.rb', line 222

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



237
238
239
240
# File 'lib/bolt_spec/plans/mock_executor.rb', line 237

def queue_execute(targets)
  raise "Unexpected call to apply(#{targets})" unless @allow_apply
  targets
end

#report_apply(_statements, _resources) ⇒ Object



329
# File 'lib/bolt_spec/plans/mock_executor.rb', line 329

def report_apply(_statements, _resources); end

#report_bundled_content(_mode, _name) ⇒ Object



325
# File 'lib/bolt_spec/plans/mock_executor.rb', line 325

def report_bundled_content(_mode, _name); end

#report_file_source(_plan_function, _source) ⇒ Object



327
# File 'lib/bolt_spec/plans/mock_executor.rb', line 327

def report_file_source(_plan_function, _source); end

#report_function_call(_function) ⇒ Object



323
# File 'lib/bolt_spec/plans/mock_executor.rb', line 323

def report_function_call(_function); end

#report_yaml_plan(_plan) ⇒ Object



331
# File 'lib/bolt_spec/plans/mock_executor.rb', line 331

def report_yaml_plan(_plan); end

#round_robin(results) ⇒ Object

BoltSpec already evaluated the ‘parallelize()` block for each object passed to the function, so these results can be returned as-is.



308
309
310
# File 'lib/bolt_spec/plans/mock_executor.rb', line 308

def round_robin(results)
  results
end

#run_command(targets, command, options = {}, _position = []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bolt_spec/plans/mock_executor.rb', line 53

def run_command(targets, command, options = {}, _position = [])
  result = nil
  if (doub = @command_doubles[command] || @command_doubles[:default])
    result = doub.process(targets, command, options)
  end
  unless result
    targets = targets.map(&:name)
    @error_message = "Unexpected call to 'run_command(#{command}, #{targets}, #{options})'"
    raise UnexpectedInvocation, @error_message
  end
  result
end

#run_plan(scope, plan_clj, params) ⇒ Object



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
173
174
175
176
177
178
179
180
181
# File 'lib/bolt_spec/plans/mock_executor.rb', line 137

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bolt_spec/plans/mock_executor.rb', line 66

def run_script(targets, script_path, arguments, options = {}, _position = [])
  script = module_file_id(script_path)
  result = nil
  if (doub = @script_doubles[script] || @script_doubles[:default])
    result = doub.process(targets, script, arguments, options)
  end
  unless result
    targets = targets.map(&:name)
    params = options.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



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bolt_spec/plans/mock_executor.rb', line 81

def run_task(targets, task, arguments, options = {}, _position = [])
  result = nil
  if (doub = @task_doubles[task.name] || @task_doubles[:default])
    result = doub.process(targets, task.name, arguments, options)
  end
  unless result
    targets = targets.map(&:name)
    params = arguments.merge(options)
    @error_message = "Unexpected call to 'run_task(#{task.name}, #{targets}, #{params})'"
    raise UnexpectedInvocation, @error_message
  end
  result
end

#run_task_with(target_mapping, task, options = {}, _position = []) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/bolt_spec/plans/mock_executor.rb', line 95

def run_task_with(target_mapping, task, options = {}, _position = [])
  resultsets = target_mapping.map do |target, arguments|
    run_task([target], task, arguments, options)
  end.compact

  Bolt::ResultSet.new(resultsets.map(&:results).flatten)
end

#shutdownObject



333
# File 'lib/bolt_spec/plans/mock_executor.rb', line 333

def shutdown; end

#start_plan(_plan_context) ⇒ Object



335
# File 'lib/bolt_spec/plans/mock_executor.rb', line 335

def start_plan(_plan_context); end

#stub_applyObject



202
203
204
# File 'lib/bolt_spec/plans/mock_executor.rb', line 202

def stub_apply
  @allow_apply = true
end

#stub_out_messageObject



198
199
200
# File 'lib/bolt_spec/plans/mock_executor.rb', line 198

def stub_out_message
  @stub_out_message ||= ActionDouble.new(:PublishStub)
end

#subscribe(_subscriber, _types = nil) ⇒ Object



337
# File 'lib/bolt_spec/plans/mock_executor.rb', line 337

def subscribe(_subscriber, _types = nil); end

#transport(_protocol) ⇒ Object

Mocked for apply_prep



249
250
251
252
253
254
255
256
257
# File 'lib/bolt_spec/plans/mock_executor.rb', line 249

def transport(_protocol)
  Class.new do
    attr_reader :provided_features

    def initialize(features)
      @provided_features = features
    end
  end.new(transport_features)
end

#unsubscribe(_subscriber, _types = nil) ⇒ Object



339
# File 'lib/bolt_spec/plans/mock_executor.rb', line 339

def unsubscribe(_subscriber, _types = nil); end

#upload_file(targets, source_path, destination, options = {}, _position = []) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bolt_spec/plans/mock_executor.rb', line 116

def upload_file(targets, source_path, destination, options = {}, _position = [])
  source = module_file_id(source_path)
  result = nil
  if (doub = @upload_doubles[source] || @upload_doubles[:default])
    result = doub.process(targets, source, destination, options)
  end
  unless result
    targets = targets.map(&:name)
    @error_message = "Unexpected call to 'upload_file(#{source}, #{destination}, #{targets}, #{options})'"
    raise UnexpectedInvocation, @error_message
  end
  result
end

#wait_until_available(targets, _options) ⇒ Object



206
207
208
# File 'lib/bolt_spec/plans/mock_executor.rb', line 206

def wait_until_available(targets, _options)
  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.



233
234
235
# File 'lib/bolt_spec/plans/mock_executor.rb', line 233

def with_node_logging(_description, targets)
  raise "Unexpected call to apply(#{targets})" unless @allow_apply
end

#with_plan_allowed_exec(plan_name, params) ⇒ Object



130
131
132
133
134
135
# File 'lib/bolt_spec/plans/mock_executor.rb', line 130

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_loggingObject



218
219
220
# File 'lib/bolt_spec/plans/mock_executor.rb', line 218

def without_default_logging
  yield
end