Module: BoltSpec::Plans

Includes:
BoltContext
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/plan_stub.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,
lib/bolt_spec/plans/action_stubs/download_stub.rb

Defined Under Namespace

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BoltContext

#allow_out_message, #config, #executor, #expect_out_message, #in_bolt_context, #inventory, #inventory_data, #modulepath, #pal, #plugins, #setup

Class Method Details

.initObject



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

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



258
259
260
261
# File 'lib/bolt_spec/plans.rb', line 258

def allow_apply
  executor.stub_apply
  nil
end

#allow_apply_prepObject



253
254
255
256
# File 'lib/bolt_spec/plans.rb', line 253

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

#allow_get_resourcesObject



263
264
265
266
# File 'lib/bolt_spec/plans.rb', line 263

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

#execute_any_planObject

Flag for the default behavior of executing sub-plans during testing By default we allow any sub-plan to be executed, no mocking required. Users can still mock out plans in this mode and the mocks will check for parameters and return values like normal. However, if a plan isn’t explicitly mocked out, it will be executed.



273
274
275
# File 'lib/bolt_spec/plans.rb', line 273

def execute_any_plan
  executor.execute_any_plan = true
end

#execute_no_planObject

If you want to explicitly mock out all of the sub-plan calls, then call this prior to calling ‘run_plan()` along with setting up any mocks that you require. In this mode, any plan that is not explicitly mocked out will not be executed and an error will be thrown.



282
283
284
# File 'lib/bolt_spec/plans.rb', line 282

def execute_no_plan
  executor.execute_any_plan = false
end

#puppetdb_clientObject



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

def puppetdb_client
  @puppetdb_client ||= MockPuppetDBClient.new(Bolt::PuppetDB::Config.new({}))
end

#run_plan(name, params) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/bolt_spec/plans.rb', line 225

def run_plan(name, params)
  pal = Bolt::PAL.new(
    config.modulepath,
    config.hiera_config,
    config.project.resource_types,
    config.compile_concurrency,
    config.trusted_external,
    config.apply_settings,
    config.project
  )

  result = executor.with_plan_allowed_exec(name, params) do
    pal.run_plan(name, params, executor, inventory, puppetdb_client)
  end

  if executor.error_message
    raise executor.error_message
  end

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

  result
end