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.



22
23
24
25
26
27
28
29
30
31
# File 'lib/bolt_spec/plans/mock_executor.rb', line 22

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']
end

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



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

def error_message
  @error_message
end

#noopObject (readonly)

Returns the value of attribute noop.



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

def noop
  @noop
end

#run_asObject

Returns the value of attribute run_as.



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

def run_as
  @run_as
end

#transport_featuresObject

Returns the value of attribute transport_features.



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

def transport_features
  @transport_features
end

Instance Method Details

#assert_call_expectationsObject



99
100
101
102
103
104
105
106
# File 'lib/bolt_spec/plans/mock_executor.rb', line 99

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



164
165
166
167
# File 'lib/bolt_spec/plans/mock_executor.rb', line 164

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



126
127
128
# File 'lib/bolt_spec/plans/mock_executor.rb', line 126

def log_action(*_args)
  yield
end

#log_plan(_plan_name) ⇒ Object



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

def log_plan(_plan_name)
  yield
end

#module_file_id(file) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/bolt_spec/plans/mock_executor.rb', line 33

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



144
145
146
147
148
149
150
151
152
# File 'lib/bolt_spec/plans/mock_executor.rb', line 144

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



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

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

#report_apply(_statements, _resources) ⇒ Object



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

def report_apply(_statements, _resources); end

#report_bundled_content(_mode, _name) ⇒ Object



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

def report_bundled_content(_mode, _name); end

#report_function_call(_function) ⇒ Object



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

def report_function_call(_function); end

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



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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bolt_spec/plans/mock_executor.rb', line 56

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



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bolt_spec/plans/mock_executor.rb', line 71

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



118
119
120
# File 'lib/bolt_spec/plans/mock_executor.rb', line 118

def stub_apply
  @allow_apply = true
end

#stub_out_messageObject



114
115
116
# File 'lib/bolt_spec/plans/mock_executor.rb', line 114

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

#transport(_protocol) ⇒ Object

Mocked for apply_prep



171
172
173
174
175
176
177
178
# File 'lib/bolt_spec/plans/mock_executor.rb', line 171

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



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bolt_spec/plans/mock_executor.rb', line 85

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



122
123
124
# File 'lib/bolt_spec/plans/mock_executor.rb', line 122

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.



155
156
157
# File 'lib/bolt_spec/plans/mock_executor.rb', line 155

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

#without_default_loggingObject



134
135
136
# File 'lib/bolt_spec/plans/mock_executor.rb', line 134

def without_default_logging
  yield
end