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

def initialize(modulepath)
  @noop = false
  @run_as = nil
  @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

#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

Instance Method Details

#assert_call_expectationsObject



158
159
160
161
162
163
164
165
# File 'lib/bolt_spec/plans/mock_executor.rb', line 158

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



223
224
225
226
# File 'lib/bolt_spec/plans/mock_executor.rb', line 223

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

#log_action(*_args) ⇒ Object



185
186
187
# File 'lib/bolt_spec/plans/mock_executor.rb', line 185

def log_action(*_args)
  yield
end

#log_plan(_plan_name) ⇒ Object



189
190
191
# File 'lib/bolt_spec/plans/mock_executor.rb', line 189

def log_plan(_plan_name)
  yield
end

#module_file_id(file) ⇒ Object



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

def module_file_id(file)
  modpath = @modulepath.select { |path| file =~ /^#{path}/ }
  raise "Could not identify module path 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



203
204
205
206
207
208
209
210
211
# File 'lib/bolt_spec/plans/mock_executor.rb', line 203

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



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

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

#report_apply(_statements, _resources) ⇒ Object



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

def report_apply(_statements, _resources); end

#report_bundled_content(_mode, _name) ⇒ Object



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

def report_bundled_content(_mode, _name); end

#report_function_call(_function) ⇒ Object



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

def report_function_call(_function); end

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



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

def run_command(targets, command, options = {})
  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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
# File 'lib/bolt_spec/plans/mock_executor.rb', line 114

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]

  # 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
  result
end

#run_script(targets, script_path, arguments, options = {}) ⇒ Object



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

def run_script(targets, script_path, arguments, options = {})
  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 = {}) ⇒ Object



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

def run_task(targets, task, arguments, options = {})
  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

#stub_applyObject



177
178
179
# File 'lib/bolt_spec/plans/mock_executor.rb', line 177

def stub_apply
  @allow_apply = true
end

#stub_out_messageObject



173
174
175
# File 'lib/bolt_spec/plans/mock_executor.rb', line 173

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

#transport(_protocol) ⇒ Object

Mocked for apply_prep



230
231
232
233
234
235
236
237
# File 'lib/bolt_spec/plans/mock_executor.rb', line 230

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 = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bolt_spec/plans/mock_executor.rb', line 93

def upload_file(targets, source_path, destination, options = {})
  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



181
182
183
# File 'lib/bolt_spec/plans/mock_executor.rb', line 181

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.



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

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

#with_plan_allowed_exec(plan_name, params) ⇒ Object



107
108
109
110
111
112
# File 'lib/bolt_spec/plans/mock_executor.rb', line 107

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



193
194
195
# File 'lib/bolt_spec/plans/mock_executor.rb', line 193

def without_default_logging
  yield
end