BoltSpec reference
The BoltSpec helper library includes several functions to help write unit
tests for plans. For more information about writing unit tests for plans, and
how to use these functions, see Testing plans.
On this page:
Execution modes
Plans often execute sub-plans with the run_plan function to build complex
workflows. When testing a plan with sub-plans, it might be helpful to execute the
sub-plans without needing to separately stub or mock each one. To support this,
BoltSpec offers two different execution modes:
execute_any_plan
Default mode. When running in this mode, BoltSpec runs any plan invoked
with therun_plan function as long as that plan is not stubbed or mocked. If
a plan is stubbed or mocked while running in this mode, BoltSpec honors the
stub or mock and does not execute the plan.
it 'executes a task' do
execute_any_plan
# Test code . . .
end
execute_no_plan
If a test is run in execute_no_plan mode, BoltSpec does not run any plan
that is invoked with the run_plan function. If BoltSpec encounters a
run_plan function and it is not stubbed or mocked, the test fails. This mode
is useful for ensuring that your plan is not running any unexpected sub-plans.
Test authors should stub or mock all sub-plans that might be invoked during a
test.
it 'executes a task' do
execute_no_plan
# Test code . . .
end
Mocks
Mocks serve two purposes when you are writing tests for your plans: they allow
assertions about plan functions invoked during a plan run and also allow you
to set their return values. For example, you might use a mock to make an
assertion that the run_task function is invoked exactly one time during a test
and have the mocked function return { 'stdout' => 'Task was successful!' }.
expect_command
The expect_command function mocks the run_command
function. It accepts a single parameter: a
command.
expect_command('whoami')
The expect_command function accepts the following modifiers:
-
be_called_times(number)The test fails if the command is not run exactly number times.
Copyexpect_command('whoami').be_called_times(1) -
not_be_calledThe test fails if the command is run.
Copyexpect_command('whoami').not_be_called -
with_targets(targets)The target or list of targets that the command must be run on. The test fails if the command is not run on the list of targets.
Copyexpect_command('whoami').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that must be passed to the
run_commandfunction. The test fails if the command is not run with the set of options.Copyexpect_command('whoami').with_params({ '_run_as' => 'root' }) -
always_return(value)Sets the value for each target's
Resultwhen the command is run. Returns aBolt::ResultSetobject. Only acceptsstderrandstdoutkeys.Copyexpect_command('whoami').always_return({ 'stdout' => 'BoltyMcBoltface' }) -
return_for_targets(targets_to_values)Sets the value for each target's
Resultwhen the command is run. Accepts a hash of key-value pairs where each key is a target and the value is the value for that target'sResult. Values can only havestderrandstdoutkeys.Copyexpect_command('whoami').return_for_targets(
'target1' => { 'stdout' => 'BoltyMcBoltFace' },
'target2' => { 'stdout' => 'Robert' },
'target3' => { 'stdout' => 'Bobert' }
) -
return(&block) { |targets, command, params| ... }Invokes a block to construct the
ResultSetreturned by therun_commandfunction.Copyexpect_command('whoami').return do |targets, command, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'stdout' => 'BoltyMcBoltface' })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the command is run. Returns aBolt::ResultSetobject.Copyexpect_command('whoami').error_with('msg' => 'sh: command not found: whoami')
expect_download
The expect_download function mocks the download_file
function. It accepts a single parameter: the
path to a remote file to download.
expect_download('/var/log/kern.log')
The expect_download function accepts the following modifiers:
-
be_called_times(number)The test fails if the file is not downloaded exactly number times.
Copyexpect_download('/var/log/kern.log').be_called_times(1) -
not_be_calledThe test fails if the file is downloaded.
Copyexpect_download('/var/log/kern.log').not_be_called -
with_targets(targets)The target or list of targets that the file must be downloaded from. The test fails if the file is not downloaded from the list of targets.
Copyexpect_download('/var/log/kern.log').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that must be passed to the
download_filefunction. The test fails if the file is not downloaded with the set of options.Copyexpect_download('/var/log/kern.log').with_params({ '_run_as' => 'root' }) -
with_destination(destination)The destination path that the file is downloaded to. The test fails if the file is not downloaded to the location.
Copyexpect_download('/var/log/kern.log').with_destination('kernel') -
return(&block) { |targets, source, destination, params| ... }Invokes a block to construct the
ResultSetreturned by thedownload_filefunction.Copyexpect_download('/var/log/kern.log').return do |targets, source, destination, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'path' => File.join(destination, source) })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the file is downloaded. Returns aBolt::ResultSetobject.Copyexpect_download('/var/log/kern.log').error_with('msg' => 'File not found')
expect_out_message
The expect_out_message function mocks the out::message function.
It does not accept parameters.
expect_out_message
The expect_out_message function accepts the following modifiers:
-
be_called_times(number)The test fails if
out::messageis not invoked exactly number times.Copyexpect_out_message.be_called_times(3) -
not_be_calledThe test fails if
out::messageis invoked.Copyexpect_out_message.not_be_called -
with_params(params)The message that must be passed to the
out::messagefunction. The test fails if the function is not invoked with the message.Copyexpect_out_message.with_params('This is not the example you are looking for.')
expect_out_verbose
The expect_out_verbose function mocks the out::verbose function. It does not accept parameters.
expect_out_verbose
The expect_out_verbose function accepts the following modifiers:
-
be_called_times(number)The test fails if
out::verboseis not invoked exactly number times.Copyexpect_out_verbose.be_called_times(3) -
not_be_calledThe test fails if
out::verboseis invoked.Copyexpect_out_verbose.not_be_called -
with_params(params)The message that must be passed to the
out::verbosefunction. The test fails if the function is not invoked with the message.Copyexpect_out_verbose.with_params('This is not the example you are looking for.')
expect_plan
The expect_plan function mocks the run_plan function.
It accepts a single parameter: the name of a plan.
expect_plan('count')
The expect_plan function accepts the following modifiers:
-
be_called_times(number)The test fails if the plan is not run exactly number times.
Copyexpect_plan('count').be_called_times(3) -
not_be_calledThe test fails if the plan is run.
Copyexpect_plan('count').not_be_called -
with_params(parameters)The parameters and options that must be passed to the
run_planfunction. The test fails if the plan is not run with the set of parameters and options.Copyexpect_plan('count').with_params({ 'fruit' => 'apple', '_run_as' => 'root' }) -
always_return(value)Sets the value for the
PlanResultobject returned by the plan. ThePlanResultobject returned by this modifier always has asuccessstatus.Copyexpect_plan('count').always_return(222) -
return(&block) { |plan, params| ... }Invokes a block to construct the
PlanResultreturned by therun_planfunction.Copyexpect_plan('count').return do |plan, params|
Bolt::PlanResult.new(100, 'success')
end -
error_with(error)Sets the value for the
PlanResultobject returned by the plan. ThePlanResultobject returned by this modifier always has afailurestatus.Copyexpect_plan('count').error_with('Too many apples, buffer overflow!')
expect_script
The expect_script function mocks the run_script
function. It accepts a single parameter: the path
to a script.
expect_script('configure.sh')
The expect_script function accepts the following modifiers:
-
be_called_times(number)The test fails if the script is not run exactly number times.
Copyexpect_script('configure.sh').be_called_times(1) -
not_be_calledThe test fails if the script is run.
Copyexpect_script('configure.sh').not_be_called -
with_targets(targets)The target or list of targets that the script must be run on. The test fails if the script is not run on the list of targets.
Copyexpect_script('configure.sh').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that must be passed to the
run_scriptfunction. The test fails if the script is not run with the set of options.Copyexpect_script('configure.sh').with_params({ 'arguments' => ['/u', 'Administrator'] }) -
always_return(value)Sets the value for each target's
Resultwhen the script is run. Returns aBolt::ResultSetobject. Values only acceptstderrandstdoutkeys.Copyexpect_script('configure.sh').always_return({ 'stdout' => 'success' }) -
return_for_targets(targets_to_values)Sets the value for each target's
Resultwhen the script is run. Accepts a hash of key-value pairs where each key is a target and the value is the value for that target'sResult. Values only acceptstderrandstdoutkeys.Copyexpect_script('configure.sh').return_for_targets(
'target1' => { 'stdout' => 'success' },
'target2' => { 'stdout' => 'failure' }
) -
return(&block) { |targets, script, params| ... }Invokes a block to construct the
ResultSetreturned by therun_scriptfunction.Copyexpect_script('configure.sh').return do |targets, script, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'stdout' => 'success' })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the script is run. Returns aBolt::ResultSetobject.Copyexpect_script('configure.sh').error_with('msg' => 'sh: command not found: apt-get')
expect_task
The expect_task functions mocks the run_task
function. It accepts a single parameter: the name
of a task.
expect_task('pet_dog')
The expect_task function accepts the following modifiers:
-
be_called_times(number)The test fails if the task is not run exactly number times.
Copyexpect_task('pet_dog').be_called_times(3) -
not_be_calledThe test fails if the task is run.
Copyexpect_task('pet_dog').not_be_called -
with_targets(targets)The target or list of targets that the task must be run on. The test fails if the task is not run on the list of targets.
Copyexpect_task('pet_dog').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The parameters and options that must be passed to the
run_taskfunction. The test fails if the task is not run with the set of parameters and options.Copyexpect_task('pet_dog').with_params({ 'breed' => 'german shepherd' }) -
always_return(value)Sets the value for each target's
Resultwhen the task is run. Returns aBolt::ResultSetobject.Copyexpect_task('pet_dog').always_return({ 'happy' => true }) -
return_for_targets(targets_to_values)Sets the value for each target's
Resultwhen the task is run. Accepts a hash of key-value pairs where each key is a target and the value is the value for that target'sResult.Copyexpect_task('pet_dog').return_for_targets(
'target1' => { 'happy' => true },
'target2' => { 'happy' => false }
) -
return(&block) { |targets, task, params| ... }Invokes a block to construct the
ResultSetreturned by therun_taskfunction.Copyexpect_task('pet_dog').return do |targets, task, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'happy' => true })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the task is run. Returns aBolt::ResultSetobject.Copyexpect_task('pet_dog').error_with('msg' => 'There are no German Shepherds to pet.')
expect_upload
The expect_upload function mocks the upload_file
function. It accepts a single parameter: the
path to a local file to upload.
expect_upload('sshd_config')
The expect_upload function accepts the following modifiers:
-
be_called_times(number)The test fails if the file is not uploaded exactly number times.
Copyexpect_upload('sshd_config').be_called_times(1) -
not_be_calledThe test fails if the file is uploaded.
Copyexpect_upload('sshd_config').not_be_called -
with_targets(targets)The target or list of targets that the file must be uploaded to. The test fails if the file is not uploaded to the list of targets.
Copyexpect_upload('sshd_config').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that must be passed to the
upload_filefunction. The test fails if the file is not uploaded with the set of options.Copyexpect_upload('sshd_config').with_params({ '_run_as' => 'root' }) -
with_destination(destination)The destination path that the file must be uploaded to. The test fails if the file is not uploaded to the location.
Copyexpect_upload('sshd_config').with_destination('/etc/ssh/sshd_config') -
return(&block) { |targets, source, destination, params| ... }Invokes a block to construct the
ResultSetreturned by theupload_filefunction.Copyexpect_upload('sshd_config').return do |targets, source, destination, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'path' => File.join(destination, source) })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the file is uploaded. Returns aBolt::ResultSetobject.Copyexpect_upload('sshd_config').error_with('msg' => 'Not authorized')
Stubs
Stubs serve two purposes when you are writing tests for your plans: they allow
plan functions to be invoked during a plan run and also allow you to set
their return values. For example, you might use a stub to allow the run_task
function to be invoked any number of times during a test.
allow_apply
The allow_apply function stubs the apply
function. It does not accept parameters or
modifiers. Using the allow_apply stub only allows you to invoke the apply
function in a plan.
allow_apply_prep
The allow_apply_prep function stubs the apply_prep
function. It does not accept parameters or
modifiers. Using the allow_apply_prep stub only allows you to invoke the
apply_prep function in a plan.
allow_command
The allow_command function stubs the run_command
function. It accepts a single parameter: a
command.
allow_command('whoami')
The allow_command function accepts the following modifiers:
-
be_called_times(number)The test fails if the command is run more than number of times.
Copyallow_command('whoami').be_called_times(3) -
not_be_calledThe test fails if the command is run.
Copyallow_command('whoami').not_be_called -
with_targets(targets)The target or list of targets that the command can be run on. The test fails if the command is run on a different list of targets.
Copyallow_command('whoami').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that can be passed to the
run_commandfunction. The test fails if the command is run with a different set of options.Copyallow_command('whoami').with_params({ '_run_as' => 'root' }) -
always_return(value)Sets the value for each target's
Resultwhen the command is run. Returns aBolt::ResultSetobject. Only acceptsstderrandstdoutkeys.Copyallow_command('whoami').always_return({ 'stdout' => 'BoltyMcBoltface' }) -
return_for_targets(targets_to_values)Sets the value for each target's
Resultwhen the command is run. Accepts a hash of key-value pairs where each key is a target and the value is the value for that target'sResult. Values can only havestderrandstdoutkeys.Copyallow_command('whoami').return_for_targets(
'target1' => { 'stdout' => 'BoltyMcBoltFace' },
'target2' => { 'stdout' => 'Robert' },
'target3' => { 'stdout' => 'Bobert' }
) -
return(&block) { |targets, command, params| ... }Invokes a block to construct the
ResultSetreturned by therun_commandfunction.Copyallow_command('whoami').return do |targets, command, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'stdout' => 'BoltyMcBoltface' })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the command is run. Returns aBolt::ResultSetobject.Copyallow_command('whoami').error_with('msg' => 'sh: command not found: whoami')
allow_download
The allow_download function stubs the download_file
function. It accepts a single parameter: the
path to a remote file to download.
allow_download('/var/log/kern.log')
The allow_download function accepts the following modifiers:
-
be_called_times(number)The test fails if the file is downloaded more than number times.
Copyallow_download('/var/log/kern.log').be_called_times(1) -
not_be_calledThe test fails if the file is downloaded.
Copyallow_download('/var/log/kern.log').not_be_called -
with_targets(targets)The target or list of targets that the file can be downloaded from. The test fails if the file is downloaded from a different list of targets.
Copyallow_download('/var/log/kern.log').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that can be passed to the
download_filefunction. The test fails if the file is downloaded with a different set of options.Copyallow_download('/var/log/kern.log').with_params({ '_run_as' => 'root' }) -
with_destination(destination)The destination path that the file is downloaded to. The test fails if the file is downloaded to a different location.
Copyallow_download('/var/log/kern.log').with_destination('kernel') -
return(&block) { |targets, source, destination, params| ... }Invokes a block to construct the
ResultSetreturned by thedownload_filefunction.Copyallow_download('/var/log/kern.log').return do |targets, source, destination, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'path' => File.join(destination, source) })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the file is downloaded. Returns aBolt::ResultSetobject.Copyallow_download('/var/log/kern.log').error_with('msg' => 'File not found')
allow_out_message
The allow_out_message function stubs the out::message function.
It does not accept parameters.
allow_out_message
The allow_out_message function accepts the following stub modifiers:
-
be_called_times(number)The test fails if
out::messageis invoked more than number times.Copyallow_out_message.be_called_times(3) -
not_be_calledThe test fails if
out::messageis invoked.Copyallow_out_message.not_be_called -
with_params(params)The message that can be passed to the
out::messagefunction. The test fails if the function is invoked with a different message.Copyallow_out_message.with_params('This is not the example you are looking for.')
allow_out_verbose
The allow_out_verbose function stubs the out::verbose function.
It does not accept parameters.
allow_out_verbose
The allow_out_verbose function accepts the following stub modifiers:
-
be_called_times(number)The test fails if
out::verboseis invoked more than number times.Copyallow_out_verbose.be_called_times(3) -
not_be_calledThe test fails if
out::verboseis invoked.Copyallow_out_verbose.not_be_called -
with_params(params)The message that can be passed to the
out::verbosefunction. The test fails if the function is invoked with a different message.Copyallow_out_verbose.with_params('This is not the example you are looking for.')
allow_plan
The allow_plan function stubs the run_plan
function. It accepts a single parameter: the name
of a plan.
allow_plan('count')
The allow_plan function accepts the following modifiers:
-
be_called_times(number)The test fails if the plan is run more than number times.
Copyallow_plan('count').be_called_times(3) -
not_be_calledThe test fails if the plan is run.
Copyallow_plan('count').not_be_called -
with_params(parameters)The parameters and options that can be passed to the
run_planfunction. The test fails if the plan is run with a different set of parameters and options.Copyallow_plan('count').with_params({ 'fruit' => 'apple', '_run_as' => 'root' }) -
always_return(value)Sets the value for the
PlanResultobject returned by the plan. ThePlanResultobject returned by this modifier always has asuccessstatus.Copyallow_plan('count').always_return(222) -
return(&block) { |plan, params| ... }Invokes a block to construct the
PlanResultreturned by therun_planfunction.Copyallow_plan('count').return do |plan, params|
Bolt::PlanResult.new(100, 'success')
end -
error_with(error)Sets the value for the
PlanResultobject returned by the plan. ThePlanResultobject returned by this modifier always has afailurestatus.Copyallow_plan('count').error_with('Too many apples, buffer overflow!')
allow_script
The allow_script function stubs the run_script
function. It accepts a single parameter: the path
to a script.
allow_script('configure.sh')
The allow_script function accepts the following modifiers:
-
be_called_times(number)The test fails if the script is run more than number times.
Copyallow_script('configure.sh').be_called_times(3) -
not_be_calledThe test fails if the script is run.
Copyallow_script('configure.sh').not_be_called -
with_targets(targets)The target or list of targets that the script can be run on. The test fails if the script is run on a different list of targets.
Copyallow_script('configure.sh').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that can be passed to the
run_scriptfunction. The test fails if the script is run with a different set of options.Copyallow_script('configure.sh').with_params({ 'arguments' => ['/u', 'Administrator'] }) -
always_return(value)Sets the value for each target's
Resultwhen the script is run. Returns aBolt::ResultSetobject. Values only acceptstderrandstdoutkeys.Copyallow_script('configure.sh').always_return({ 'stdout' => 'success' }) -
return_for_targets(targets_to_values)Sets the value for each target's
Resultwhen the script is run. Accepts a hash of key-value pairs where each key is a target and the value is the value for that target'sResult. Values only acceptstderrandstdoutkeys.Copyallow_script('configure.sh').return_for_targets(
'target1' => { 'stdout' => 'success' },
'target2' => { 'stdout' => 'failure' }
) -
return(&block) { |targets, script, params| ... }Invokes a block to construct the
ResultSetreturned by therun_scriptfunction.Copyallow_script('configure.sh').return do |targets, script, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'stdout' => 'success' })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the script is run. Returns aBolt::ResultSetobject.Copyallow_script('configure.sh').error_with('msg' => 'sh: command not found: apt-get')
allow_task
The allow_task function stubs the run_task
function. It accepts a single parameter: the name
of a task.
allow_task('pet_dog')
The allow_task function accepts the following modifiers:
-
be_called_times(number)The test fails if the task is run more than number times.
Copyallow_task('pet_dog').be_called_times(3) -
not_be_calledThe test fails if the task is run.
Copyallow_task('pet_dog').not_be_called -
with_targets(targets)The target or list of targets that the task can be run on. The test fails if the task is run on a different list of targets.
Copyallow_task('pet_dog').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The parameters and options that can be passed to the
run_taskfunction. The test fails if the task is run with a different set of parameters and options.Copyallow_task('pet_dog').with_params({ 'breed' => 'german shepherd' }) -
always_return(value)Sets the value for each target's
Resultwhen the task is run. Returns aBolt::ResultSetobject.Copyallow_task('pet_dog').always_return({ 'happy' => true }) -
return_for_targets(targets_to_values)Sets the value for each target's
Resultwhen the task is run. Accepts a hash of key-value pairs where each key is a target and the value is the value for that target'sResult.Copyallow_task('pet_dog').return_for_targets(
'target1' => { 'happy' => true },
'target2' => { 'happy' => false }
) -
return(&block) { |targets, task, params| ... }Invokes a block to construct the
ResultSetreturned by therun_taskfunction.Copyallow_task('pet_dog').return do |targets, task, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'happy' => true })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the task is run. Returns aBolt::ResultSetobject.Copyallow_task('pet_dog').error_with('msg' => 'There are no German Shepherds to pet.')
allow_upload
The allow_upload function stubs the upload_file
function. It accepts a single parameter: the
path to a local file to upload.
allow_upload('sshd_config')
The allow_upload function accepts the following modifiers:
-
be_called_times(number)The test fails if the file is uploaded more than number times.
Copyallow_upload('sshd_config').be_called_times(1) -
not_be_calledThe test fails if the file is uploaded.
Copyallow_upload('sshd_config').not_be_called -
with_targets(targets)The target or list of targets that the file can be uploaded to. The test fails if the file is uploaded to a different list of targets.
Copyallow_upload('sshd_config').with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that can be passed to the
upload_filefunction. The test fails if the file is uploaded with a different set of options.Copyallow_upload('sshd_config').with_params({ '_run_as' => 'root' }) -
with_destination(destination)The destination path that the file is uploaded to. The test fails if the file is uploaded to a different location.
Copyallow_upload('sshd_config').with_destination('/etc/ssh/sshd_config') -
return(&block) { |targets, source, destination, params| ... }Invokes a block to construct the
ResultSetreturned by theupload_filefunction.Copyallow_upload('sshd_config').return do |targets, source, destination, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'path' => File.join(destination, source) })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the file is uploaded. Returns aBolt::ResultSetobject.Copyallow_upload('sshd_config').error_with('msg' => 'Not authorized')
Global stubs
Global stubs allow you to stub all related function invocations. This can be useful in
larger plans. For example, allow_any_task can be used in place of invoking allow_task
for each individual task. All global stub functions follow the allow_any_* naming
scheme.
allow_any_command
The allow_any_command function stubs all invocations of the run_command
function. It does not accept parameters.
allow_any_command
The allow_any_command function accepts the following modifiers:
-
be_called_times(number)The test fails if any one command is run more than number of times.
Copyallow_any_command.be_called_times(3) -
not_be_calledThe test fails if any command is run.
Copyallow_any_command.not_be_called -
with_targets(targets)The target or list of targets that a command can be run on. The test fails if any command is run on a different list of targets.
Copyallow_any_command.with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that can be passed to the
run_commandfunction. The test fails if any command is run with a different set of options.Copyallow_any_command.with_params({ '_run_as' => 'root' }) -
always_return(value)Sets the value for each target's
Resultwhen any command is run. Returns aBolt::ResultSetobject. Only acceptsstderrandstdoutkeys.Copyallow_any_command.always_return({ 'stdout' => 'BoltyMcBoltface' }) -
return_for_targets(targets_to_values)Sets the value for each target's
Resultwhen any command is run. Accepts a hash of key-value pairs where each key is a target and the value is the value for that target'sResult. Values can only havestderrandstdoutkeys.Copyallow_any_command.return_for_targets(
'target1' => { 'stdout' => 'BoltyMcBoltFace' },
'target2' => { 'stdout' => 'Robert' },
'target3' => { 'stdout' => 'Bobert' }
) -
return(&block) { |targets, command, params| ... }Invokes a block to construct the
ResultSetreturned by therun_commandfunction.Copyallow_any_command.return do |targets, command, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'stdout' => 'BoltyMcBoltface' })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultfor any command that is run. Returns aBolt::ResultSetobject.Copyallow_any_command.error_with('msg' => 'sh: command not found: whoami')
allow_any_download
The allow_any_download function stubs all invocations of the download_file
function. It does not accept parameters.
allow_any_download
The allow_any_download function accepts the following modifiers:
-
be_called_times(number)The test fails if any one file is downloaded more than number times.
Copyallow_any_download.be_called_times(1) -
not_be_calledThe test fails if any file is downloaded.
Copyallow_any_download.not_be_called -
with_targets(targets)The target or list of targets that the file can be downloaded from. The test fails if any file is downloaded from a different list of targets.
Copyallow_any_download.with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that can be passed to the
download_filefunction. The test fails if any file is downloaded with a different set of options.Copyallow_any_download.with_params({ '_run_as' => 'root' }) -
with_destination(destination)The destination path that the file is downloaded to. The test fails if any file is downloaded to a different location.
Copyallow_any_download.with_destination('kernel') -
return(&block) { |targets, source, destination, params| ... }Invokes a block to construct the
ResultSetreturned by thedownload_filefunction.Copyallow_any_download.return do |targets, source, destination, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'path' => File.join(destination, source) })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen the file is downloaded. Returns aBolt::ResultSetobject.Copyallow_any_download.error_with('msg' => 'File not found')
allow_any_out_message
The allow_any_out_message function stubs all invocations of the out::message
function. It does not accept parameters.
allow_any_out_message
The allow_any_out_message function accepts the following modifiers:
-
be_called_times(number)The test fails if
out::messageis run more than number times.Copyallow_any_out_message.be_called_times(3) -
not_be_calledThe test fails if
out::messageis called.Copyallow_any_out_message.not_be_called -
with_params(parameters)The parameters that can be passed to the
out::messagefunction. The test fails ifout::mesageis called with a different set of parameters.Copyallow_any_out_message.with_params("hello world")
allow_any_out_verbose
The allow_any_out_verbose function stubs all invocations of the out::verbose
function. It does not accept parameters.
allow_any_out_verbose
The allow_any_out_verbose function accepts the following modifiers:
-
be_called_times(number)The test fails if
out::verboseis run more than number times.Copyallow_any_out_verbose.be_called_times(3) -
not_be_calledThe test fails if
out::verboseis called.Copyallow_any_out_verbose.not_be_called -
with_params(parameters)The parameters that can be passed to the
out::verbosefunction. The test fails ifout::verboseis called with a different set of parameters.Copyallow_any_out_verbose.with_params("hello world")
allow_any_plan
The allow_any_plan function stubs all invocations of the run_plan
function. It does not accept parameters.
allow_any_plan
The allow_any_plan function accepts the following modifiers:
-
be_called_times(number)The test fails if any plan is run more than number times.
Copyallow_any_plan.be_called_times(3) -
not_be_calledThe test fails if any plan is run.
Copyallow_any_plan.not_be_called -
with_params(parameters)The parameters and options that can be passed to the
run_planfunction. The test fails if any plan is run with a different set of parameters and options.Copyallow_any_plan.with_params({ 'fruit' => 'apple', '_run_as' => 'root' }) -
always_return(value)Sets the value for the
PlanResultobject returned by every plan. ThePlanResultobject returned by this modifier always has asuccessstatus.Copyallow_any_plan.always_return(318) -
return(&block) { |plan, params| ... }Invokes a block to construct the
PlanResultreturned by therun_planfunction.Copyallow_any_plan.return do |plan, params|
Bolt::PlanResult.new(100, 'success')
end -
error_with(error)Sets the value for the
PlanResultobject returned by each plan. ThePlanResultobject returned by this modifier always has afailurestatus.Copyallow_any_plan.error_with('Too many apples, buffer overflow!')
allow_any_script
The allow_script function stubs all invocations of the run_script
function. It does not accept parameters.
allow_any_script
The allow_any_script function accepts the following modifiers:
-
be_called_times(number)The test fails if any one script is run more than number times.
Copyallow_any_script.be_called_times(3) -
not_be_calledThe test fails if any script is run.
Copyallow_any_script.not_be_called -
with_targets(targets)The target or list of targets that a script can be run on. The test fails if any script is run on a different list of targets.
Copyallow_any_script.with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that can be passed to the
run_scriptfunction. The test fails if any script is run with a different set of options.Copyallow_any_script.with_params({ 'arguments' => ['/u', 'Administrator'] }) -
always_return(value)Sets the value for each target's
Resultwhen any script is run. Returns aBolt::ResultSetobject. Values only acceptstderrandstdoutkeys.Copyallow_any_script.always_return({ 'stdout' => 'success' }) -
return_for_targets(targets_to_values)Sets the value for each target's
Resultwhen any script is run. Accepts a hash of key-value pairs where each key is a target and the value is the value for that target'sResult. Values only acceptstderrandstdoutkeys.Copyallow_any_script.return_for_targets(
'target1' => { 'stdout' => 'success' },
'target2' => { 'stdout' => 'failure' }
) -
return(&block) { |targets, script, params| ... }Invokes a block to construct the
ResultSetreturned by therun_scriptfunction.Copyallow_any_script.return do |targets, script, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'stdout' => 'success' })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen any script is run. Returns aBolt::ResultSetobject.Copyallow_any_script.error_with('msg' => 'sh: command not found: apt-get')
allow_any_task
The allow_any_task function stubs the run_task
function. It does not accept parameters.
allow_any_task
The allow_any_task function accepts the following modifiers:
-
be_called_times(number)The test fails if the any one task is run more than number times.
Copyallow_any_task.be_called_times(3) -
not_be_calledThe test fails if any task is run.
Copyallow_any_task.not_be_called -
with_targets(targets)The target or list of targets that a task can be run on. The test fails if any task is run on a different list of targets.
Copyallow_any_task.with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The parameters and options that can be passed to the
run_taskfunction. The test fails if any task is run with a different set of parameters and options.Copyallow_any_task.with_params({ 'breed' => 'german shepherd' }) -
always_return(value)Sets the value for each target's
Resultfor any task that is run. Returns aBolt::ResultSetobject.Copyallow_any_task.always_return({ 'happy' => true }) -
return_for_targets(targets_to_values)Sets the value for each target's
Resultwhen any task is run. Accepts a hash of key-value pairs where each key is a target and the value is the value for that target'sResult.Copyallow_any_task('pet_dog').return_for_targets(
'target1' => { 'happy' => true },
'target2' => { 'happy' => false }
) -
return(&block) { |targets, task, params| ... }Invokes a block to construct the
ResultSetreturned by therun_taskfunction.Copyallow_any_task.return do |targets, task, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'happy' => true })
end
Bolt::ResultSet.new(results)
end
allow_any_upload
The allow_any_upload function stubs any invocation of the upload_file
function. It does not accept parameters.
allow_any_upload
The allow_any_upload function accepts the following modifiers:
-
be_called_times(number)The test fails if any one file is uploaded more than number times.
Copyallow_any_upload.be_called_times(1) -
not_be_calledThe test fails if any file is uploaded.
Copyallow_any_upload.not_be_called -
with_targets(targets)The target or list of targets that a file can be uploaded to. The test fails if any file is uploaded to a different list of targets.
Copyallow_any_upload.with_targets(['target1', 'target2', 'target3']) -
with_params(parameters)The options that can be passed to the
upload_filefunction. The test fails if any file is uploaded with a different set of options.Copyallow_any_upload.with_params({ '_run_as' => 'root' }) -
with_destination(destination)The destination path that a file is uploaded to. The test fails if any file is uploaded to a different location.
Copyallow_any_upload.with_destination('/etc/ssh/sshd_config') -
return(&block) { |targets, source, destination, params| ... }Invokes a block to construct the
ResultSetreturned by theupload_filefunction.Copyallow_any_upload.return do |targets, source, destination, params|
results = targets.map do |target|
Bolt::Result.new(target, value: { 'path' => File.join(destination, source) })
end
Bolt::ResultSet.new(results)
end -
error_with(error)Sets the error hash for each target's
Resultwhen any file is uploaded. Returns aBolt::ResultSetobject.Copyallow_any_upload.error_with('msg' => 'Not authorized')
Related information
- For more information on using
BoltSpec, see Testing plans.






