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, #allow_out_verbose, #config, #config_data, #executor, #expect_out_message, #expect_out_verbose, #in_bolt_context, #inventory, #inventory_data, #modulepath, #pal, #plugins, #setup

Class Method Details

.initObject



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

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 :trace, :debug, :info, :notice, :warn, :error, :fatal
end

Instance Method Details

#allow_applyObject



150
151
152
153
# File 'lib/bolt_spec/plans.rb', line 150

def allow_apply
  executor.stub_apply
  nil
end

#allow_apply_prepObject



145
146
147
148
# File 'lib/bolt_spec/plans.rb', line 145

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

#allow_get_resourcesObject



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

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.



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

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.



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

def execute_no_plan
  executor.execute_any_plan = false
end

#puppetdb_clientObject



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

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

#run_plan(name, params) ⇒ Object



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

def run_plan(name, params)
  pal = Bolt::PAL.new(
    Bolt::Config::Modulepath.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