Module: BoltSpec::Plans

Defined in:
lib/bolt_spec/plans.rb,
lib/bolt_spec/plans/action_stubs.rb,
lib/bolt_spec/plans/publish_stub.rb,
lib/bolt_spec/plans/mock_executor.rb,
lib/bolt_spec/plans/action_stubs/task_stub.rb,
lib/bolt_spec/plans/action_stubs/script_stub.rb,
lib/bolt_spec/plans/action_stubs/upload_stub.rb,
lib/bolt_spec/plans/action_stubs/command_stub.rb

Defined Under Namespace

Classes: ActionDouble, ActionStub, CommandStub, MockExecutor, MockPuppetDBClient, PublishStub, ScriptStub, TaskStub, UnexpectedInvocation, UploadStub

Constant Summary collapse

MOCKED_ACTIONS =
%i[command script task upload].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initObject



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

def self.init
  # Ensure tasks are enabled when rspec-puppet sets up an environment so we get task loaders.
  # Note that this is probably not safe to do in modules that also test Puppet manifest code.
  Bolt::PAL.load_puppet
  Puppet[:tasks] = true

  # Ensure logger is initialized with Puppet levels so 'notice' works when running plan specs.
  Logging.init :debug, :info, :notice, :warn, :error, :fatal, :any
end

Instance Method Details

#allow_applyObject



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

def allow_apply
  executor.stub_apply
  nil
end

#allow_apply_prepObject



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

def allow_apply_prep
  allow_task('apply_helpers::custom_facts')
  nil
end

#allow_get_resourcesObject



227
228
229
230
# File 'lib/bolt_spec/plans.rb', line 227

def allow_get_resources
  allow_task('apply_helpers::query_resources')
  nil
end

#allow_out_messageObject Also known as: allow_any_out_message



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

def allow_out_message
  executor.stub_out_message.add_stub
end

#configObject

Override in your tests



162
163
164
165
166
167
168
# File 'lib/bolt_spec/plans.rb', line 162

def config
  @config ||= begin
    conf = Bolt::Config.new(Bolt::Boltdir.new('.'), {})
    conf.modulepath = [modulepath].flatten
    conf
  end
end

#executorObject

intended to be private below here



266
267
268
# File 'lib/bolt_spec/plans.rb', line 266

def executor
  @executor ||= BoltSpec::Plans::MockExecutor.new(modulepath)
end

#expect_out_messageObject



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

def expect_out_message
  allow_out_message.expect_call
end

#inventoryObject

Override in your tests



171
172
173
# File 'lib/bolt_spec/plans.rb', line 171

def inventory
  @inventory ||= Bolt::Inventory.new({})
end

#modulepathObject

Override in your tests if needed



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

def modulepath
  [RSpec.configuration.module_path]
rescue NoMethodError
  raise "RSpec.configuration.module_path not defined set up rspec puppet or define modulepath for this test"
end

#puppetdb_clientObject



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

def puppetdb_client
  @puppetdb_client ||= MockPuppetDBClient.new
end

#run_plan(name, params) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/bolt_spec/plans.rb', line 182

def run_plan(name, params)
  pal = Bolt::PAL.new(config.modulepath, config.hiera_config, config.boltdir.resource_types)
  result = pal.run_plan(name, params, executor, inventory, puppetdb_client)

  if executor.error_message
    raise executor.error_message
  end

  begin
    executor.assert_call_expectations
  rescue StandardError => e
    raise "#{e.message}\nPlan result: #{result}"
  end

  result
end