When testing modules with beaker, you need to choose up-front whether to drive the tests using puppet apply or a master/agent setup. While choosing the apply approach is tempting due to the reduced resource usage, everyone is running master/agent setups, and those have subtle differences to puppet apply that might trip up your code. To solve this dilemma, use this gem to choose the test mode at runtime!
Beaker::TestmodeSwitcher supports running tests in master/agent mode, or using puppet apply or locally without any setup.
Set up you module for beaker testing as usual. Additionally add
gem 'beaker-testmode_switcher'to the :system_tests group in your module's Gemfile. Add
require 'beaker/testmode_switcher/dsl'to your spec/spec_helper_acceptance.rb to enable the DSL extensions. Instead of using #apply_manifest_on or #run_agent_on you can now use #execute_manifest_on and - depending on the test mode - will upload and execute the manifest on the right node(s).
The BEAKER_TESTMODE environment variable determines how the tests are run:
local: No VMs are provisioned and tests are run withpuppet applyusing the context of your test runner. This mode uses the least resources and is great for development, but may require running the tests as root and could trash the system.apply: VMs are provisioned as normal (determined by the nodeset) and tests are run withpuppet applyon the specified node. This mode only requires a single VM and is great for running the tests in an isolated environment. When the nodeset has more than one node, exactly one has to have the 'default' role assigned. This will be the node to execute the manifests.agent: VMs are provisioned as normal (determined by the nodeset). When running tests, the manifest is uploaded to the master and a fullpuppet agentrun is kicked off on the specified node. This mode requires multiple VMs and a more involved provisioning step, but the tests run in a very production-like environment to ensure highest fidelity of the test results. The nodeset needs to contain one node with the 'master' role assigned. This will be the node to receive the manifest. When the nodeset has more than one node, exactly one has to have the 'default' role assigned. This will be the node to execute the puppet agent.
Beaker and Beaker-rspec frameworks are supported. The environment variable TEST_FRAMEWORK MUST be set before the gem/bundle install.
The TEST_FRAMEWORK environment variable determines the gem support for the test run:
beakerBeaker only support.beaker-rspecBeaker-rspec support.
The acceptance tests will immediately fail if the environment variable is not set.
To run the tests bundle exec rake
This experimental version supports only a minimal set of functionality from the beaker DSL:
-
create_remote_file_ex(file_path, file_content, opts = {}): Creates a file atfile_pathwith the content specified infile_contenton the default node.optscan have the keys:mode,:user, and:groupto specify the permissions, owner, and group respectively. -
execute_manifest_on(hosts, manifest, opts = {}): Execute the manifest on all nodes. This will only work whenBEAKER_TESTMODE, is set toagentand it will run apuppet agentrun.optskeys::debug,:trace,:noop: set to true to enable the puppet option of the same name.:dry_run: set to true to skip executing the actual command.:environment: pass environment variables for the command as a hash.:catch_failuresenables detailed exit codes and causes a test failure if the puppet run indicates there was a failure during its execution.:catch_changesenables detailed exit codes and causes a test failure if the puppet run indicates that there were changes or failures during its execution.:expect_changesenables detailed exit codes and causes a test failure if the puppet run indicates that there were no resource changes during its execution.:expect_failuresenables detailed exit codes and causes a test failure if the puppet run indicates there were no failure during its execution.
-
execute_manifest(manifest, opts = {}): Execute the manifest on the default node. Depending on theBEAKER_TESTMODEenvironment variable, this may usepuppet agentorpuppet apply. Calls execute_manifest_on whenBEAKER_TESTMODE=agentoptskeys::debug,:trace,:noop: set to true to enable the puppet option of the same name.:dry_run: set to true to skip executing the actual command.:environment: pass environment variables for the command as a hash.:catch_failuresenables detailed exit codes and causes a test failure if the puppet run indicates there was a failure during its execution.:catch_changesenables detailed exit codes and causes a test failure if the puppet run indicates that there were changes or failures during its execution.:expect_changesenables detailed exit codes and causes a test failure if the puppet run indicates that there were no resource changes during its execution.:expect_failuresenables detailed exit codes and causes a test failure if the puppet run indicates there were no failure during its execution.
-
resource(type, name, opts = {}): Runspuppet resourcewith the specifiedtypeandnamearguments.optskeys::debug,:trace,:noop: set to true to enable the puppet option of the same name.:values: pass a hash of key/value pairs which is passed on the commandline topuppet resourceto influence the specified resource.:dry_run: set to true to skip executing the actual command.:environment: pass environment variables for the command as a hash.
-
scp_to_ex(from, to): Copies the filefromto the locationtoon all nodes. -
shell_ex(cmd, opts = {}): Execute a shell command on the default node.optskeys::dry_run: set to true to skip executing the actual command.:environment: pass environment variables for the command as a hash.
Other helpful methods:
Beaker::TestmodeSwitcher.mode: Returns the currently configured test mode.Beaker::TestmodeSwitcher.runner: Returns the currently configured runner.
After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request