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

Defined Under Namespace

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

Constant Summary collapse

MOCKED_ACTIONS =
%i[command 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



198
199
200
201
202
203
204
205
206
# File 'lib/bolt_spec/plans.rb', line 198

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



245
246
247
248
# File 'lib/bolt_spec/plans.rb', line 245

def allow_apply
  executor.stub_apply
  nil
end

#allow_apply_prepObject



240
241
242
243
# File 'lib/bolt_spec/plans.rb', line 240

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

#allow_get_resourcesObject



250
251
252
253
# File 'lib/bolt_spec/plans.rb', line 250

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.



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

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.



269
270
271
# File 'lib/bolt_spec/plans.rb', line 269

def execute_no_plan
  executor.execute_any_plan = false
end

#puppetdb_clientObject



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

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

#run_plan(name, params) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/bolt_spec/plans.rb', line 221

def run_plan(name, params)
  pal = Bolt::PAL.new(config.modulepath, config.hiera_config, config.project.resource_types)
  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