### Install rspec-puppet using gem
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/documentation/setup/index.md
Install the rspec-puppet gem directly using the gem command. This is an alternative to using Bundler for installation.
```bash
$ gem install rspec-puppet
```
--------------------------------
### Manual rspec-puppet setup on Unix
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/documentation/setup/index.md
Manually set up rspec-puppet by creating directories and configuration files. This includes creating a 'spec' directory, a default manifest file, and configuring 'spec/spec_helper.rb'.
```bash
cd path/to/your/module
mkdir spec
mkdir -p spec/fixtures/{manifests,modules}
touch spec/fixtures/manifests/site.pp
```
--------------------------------
### Automatic rspec-puppet setup
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/documentation/setup/index.md
Automate the creation of necessary files and directories for rspec and rspec-puppet. This involves navigating to your module directory and running the rspec-puppet-init script.
```bash
cd path/to/your/module
touch metadata.json
rspec-puppet-init
```
--------------------------------
### Install rspec-puppet using Bundler
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/documentation/setup/index.md
Add the rspec-puppet gem to your Gemfile for dependency management. This is the recommended approach for managing gems in modules or control repositories.
```ruby
gem 'rspec-puppet', '~> 2.0'
```
--------------------------------
### Test Puppet Applications with rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Provides an example of testing a single-component Puppet application. It highlights the necessary setup, including enabling `app_management`, defining `:node` and `:title`, and using `ref` for node and resource mappings within `:params`. Cross-node support is currently unavailable.
```ruby
require 'spec_helper'
describe 'orch_app' do
let(:node) { 'my_node' }
let(:title) { 'my_awesome_app' }
let(:params) do
{
'nodes' => {
ref('Node', node) => ref('Orch_app::Db', title),
}
}
end
it { should compile }
it { should contain_orch_app(title) }
end
```
--------------------------------
### Install jQuery.scrollTo via Package Managers
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/jquery-scrollTo/README.md
Instructions for installing the jQuery.scrollTo plugin using popular package managers like bower, npm, and composer.
```bash
bower install jquery.scrollTo
```
```bash
npm install jquery.scrollto
```
```php
php composer.phar require --prefer-dist flesler/jquery.scrollTo "*"
```
--------------------------------
### Install and Configure rspec-puppet
Source: https://context7.com/rodjek/rspec-puppet/llms.txt
Instructions for installing rspec-puppet using RubyGems or Bundler and configuring the RSpec helper file. This includes setting module paths, Hiera configuration, default facts, and coverage reporting.
```ruby
# Gemfile
gem 'rspec-puppet', '~> 2.0'
gem 'puppet', ENV.fetch('PUPPET_GEM_VERSION', '>= 0')
# spec/spec_helper.rb
require 'rspec-puppet'
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
RSpec.configure do |c|
c.module_path = File.join(fixture_path, 'modules')
c.manifest_dir = File.join(fixture_path, 'manifests')
c.hiera_config = 'spec/fixtures/hiera/hiera.yaml'
c.default_facts = { 'os' => { 'family' => 'RedHat' } }
c.after(:suite) do
RSpec::Puppet::Coverage.report!(95) # Require 95% coverage
end
end
```
--------------------------------
### Hiera Configuration File Example
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
An example of a Hiera configuration file (hiera.yaml) used with rspec-puppet. It specifies the backends, hierarchy, and data directory for Hiera lookups.
```yaml
---
:backends:
- yaml
:hierarchy:
- test
:yaml:
:datadir: 'spec/fixtures/hiera'
```
--------------------------------
### Documentation for Hiera Integration (rspec-puppet)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
The documentation for Hiera integration has been improved, including the addition of an example spec. This provides clearer guidance on how to set up and use Hiera with rspec-puppet for more effective testing. The example implies that documentation and examples are available.
```ruby
# Refer to the rspec-puppet documentation for detailed examples
# on Hiera integration and an example spec file.
```
--------------------------------
### Install rspec-puppet Gem
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Installs the rspec-puppet gem using the RubyGems package manager. This is the primary method for installing the testing framework.
```bash
gem install rspec-puppet
```
--------------------------------
### Configure rspec-puppet in spec_helper.rb
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/documentation/setup/index.md
Configure RSpec to use rspec-puppet by setting the module path and manifest directory in the 'spec/spec_helper.rb' file. This ensures RSpec can find your module's files.
```ruby
require 'rspec-puppet'
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
RSpec.configure do |c|
c.module_path = File.join(fixture_path, 'modules')
c.manifest_dir = File.join(fixture_path, 'manifests')
end
```
--------------------------------
### Basic Scrolling Examples with jQuery.scrollTo
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/jquery-scrollTo/demo/index.html
Demonstrates fundamental scrolling actions using jQuery.scrollTo. This includes scrolling to the top of a pane, scrolling the entire window to a hash, and basic element scrolling.
```javascript
jQuery(function( $ ){
// Demo binding and preparation, no need to read this part
$('a.back').click(function() {
$(this).parents('div.pane').scrollTo(0, 800, { queue:true });
$(this).parents('div.section').find('span.message').text( this.title);
return false;
});
// Reset all scrollable panes to (0,0)
$('div.pane').scrollTo(0);
// Reset the screen to (0,0)
$.scrollTo(0);
// TOC, shows how to scroll the whole window
$('#toc a').click(function() {
//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
$.scrollTo(this.hash, 1500, { easing:'elasout' });
$(this.hash).find('span.message').text(this.title);
return false;
});
});
```
--------------------------------
### Example: Single Image Lightbox with Options (HTML)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/lightbox/examples/index.html
An HTML example showcasing a single image displayed within the lightbox. It utilizes data attributes to pass options directly to the lightbox, such as 'data-title' for the modal's title and 'data-footer' for a custom footer element.
```html
```
--------------------------------
### matchHeight Callback Events: Before and After Update
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/jquery-match-height/README.md
Provides examples of how to define global callback functions that execute before and after matchHeight updates the layout. These callbacks receive the event object and a reference to the internal groups object.
```javascript
$.fn.matchHeight._beforeUpdate = function(event, groups) {
// do something before any updates are applied
}
$.fn.matchHeight._afterUpdate = function(event, groups) {
// do something after all updates are applied
}
```
--------------------------------
### Hiera Data File Example
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
An example of a Hiera data file (test.yaml) containing key-value pairs used in rspec-puppet tests. This demonstrates how to structure data for different types, like arrays and nested hashes.
```yaml
---
ntpserver: ['ntp1.domain.com','ntpXX.domain.com']
user:
oneuser:
shell: '/bin/bash'
twouser:
shell: '/sbin/nologin'
```
--------------------------------
### Alternative Hiera Configuration
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
An alternative Hiera configuration example for `spec_helper.rb`, specifying a different data directory and hierarchy. This configuration is used when Hiera data is organized differently.
```yaml
---
:backends:
- yaml
:yaml:
:datadir: spec/fixtures/hieradata
:hierarchy:
- common
```
--------------------------------
### rspec-puppet Basic Test Structure
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Provides a basic example of an rspec-puppet test case. It demonstrates how to test that a Puppet resource, like 'sysctl', when applied with specific parameters, results in another resource, like 'exec', being included in the catalogue.
```ruby
describe 'sysctl' do
let(:title) { 'baz' }
let(:params) { { 'value' => 'foo' } }
it { is_expected.to contain_exec('sysctl/reload').with_command("/sbin/sysctl -p /etc/sysctl.conf") }
end
```
--------------------------------
### rspec-puppet Specifying Resource Parameters
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Shows how to pass parameters to a Puppet resource, class, or application in rspec-puppet tests. It includes examples for standard parameters, Puppet's `undef` value, and references to nodes or resources using the `ref` helper.
```ruby
let(:params) { {'ensure' => 'present', ...} }
let(:params) { {'user' => :undef, ...} }
let(:params) { 'require' => ref('Package', 'sudoku') }
let(:params) { { 'nodes' => { ref('Node', 'dbnode') => ref('Myapp::Mycomponent', 'myapp') } } }
```
--------------------------------
### Add Puppet dependency using Bundler
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/documentation/setup/index.md
Include the Puppet gem in your Gemfile, allowing for version specification via the PUPPET_GEM_VERSION environment variable. This pattern supports multiple Puppet versions.
```ruby
gem 'puppet', ENV.fetch('PUPPET_GEM_VERSION', '>= 0')
```
--------------------------------
### Create Mixed Media Gallery
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/lightbox/examples/index.html
Shows how to combine different types of media (images, YouTube videos, Vimeo videos) within a single gallery. This example demonstrates the flexibility of the rspec-puppet library in handling diverse content sources.
```html
```
--------------------------------
### Test Resource Relationships (Puppet Manifest Example)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/_includes/catalogue_matchers.md
Demonstrates testing resource relationships using rspec-puppet matchers based on a provided Puppet manifest. It shows how to assert 'before' and 'require' relationships between notify resources.
```puppet
notify { 'a': }
notify { 'b':
before => Notify['a'],
}
```
```ruby
it { is_expected.to contain_notify('b').that_comes_before('Notify[a]') }
```
```ruby
it { is_expected.to contain_notify('a').that_requires('Notify[b]') }
```
--------------------------------
### Programmatic Navigation with Ekko Lightbox
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/lightbox/examples/index.html
Shows how to programmatically control navigation within Ekko Lightbox. This example sets up event delegation for elements with data-gallery="navigateTo" and allows for manual navigation calls within the modal's onShown callback.
```javascript
$(document).delegate('*[data-gallery="navigateTo"]', 'click', function (event) {
event.preventDefault();
var lb;
return $(this).ekkoLightbox({
onShown: function () {
lb = this;
$(lb.modal_content).on('click', '.modal-footer a', function (e) {
e.preventDefault();
lb.navigateTo(2);
});
}
});
});
```
--------------------------------
### Test Puppet Classes with rspec-puppet
Source: https://context7.com/rodjek/rspec-puppet/llms.txt
Example of testing Puppet classes using rspec-puppet. Demonstrates how to use matchers like `compile`, `contain_`, `with_ensure`, `with_content`, `that_notifies`, `have_resource_count`, and context-specific parameter testing.
```ruby
# spec/classes/apache_spec.rb
require 'spec_helper'
describe 'apache' do
let(:params) { { 'port' => 8080, 'docroot' => '/var/www/html' } }
let(:facts) { { 'osfamily' => 'RedHat', 'operatingsystem' => 'CentOS' } }
let(:node) { 'webserver.example.com' }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_package('httpd').with_ensure('present') }
it { is_expected.to contain_service('httpd').with(
'ensure' => 'running',
'enable' => true
)}
it { is_expected.to contain_file('/etc/httpd/conf/httpd.conf')
.with_content(/Listen 8080/)
.that_notifies('Service[httpd]')
}
it { is_expected.to have_resource_count(5) }
it { is_expected.to have_package_resource_count(2) }
context 'with ssl enabled' do
let(:params) { super().merge('ssl' => true) }
it { is_expected.to contain_package('mod_ssl') }
it { is_expected.to contain_file('/etc/httpd/conf.d/ssl.conf') }
end
end
```
--------------------------------
### Setting Environment with Let Block (rspec-puppet)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
This feature allows you to set the Puppet environment for your tests using a `let` block. This provides fine-grained control over the environment in which your Puppet code is compiled and tested. The example shows how to define the environment.
```ruby
describe 'environment_setting' do
let(:environment) { 'production' } # Set the environment
it 'should compile with the production environment'
# Test your code within the 'production' environment context
end
end
```
--------------------------------
### Embed Vimeo Videos
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/lightbox/examples/index.html
Explains how to embed Vimeo videos, noting the requirement to use the embed source URL (e.g., `player.vimeo.com/video/ID`) instead of the standard URL. It provides examples for direct embedding and for using a `data-remote` attribute to control the lightbox behavior.
```html
City Lights - from Colin Rich (using embed link)
City Lights - from Colin Rich (with reccommended `data-remote` setting)
```
--------------------------------
### Updating README for RSpec 3 Expect Syntax
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
The README file has been updated to use the RSpec 3 `expect` syntax and includes additional explanations. This ensures that the documentation aligns with the latest RSpec conventions and provides clearer guidance for users. The example implies updated documentation content.
```markdown
```markdown
# rspec-puppet README
## Testing Custom Types
```ruby
describe 'my_type' do
it { is_expected.to contain_my_type('resource_name') }
end
```
## ... additional explanations ...
```
```
--------------------------------
### Using RSpec 3 Syntax with rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
rspec-puppet now fully supports the RSpec 3 syntax, including the `expect(...).to ...` syntax. This allows you to leverage the latest RSpec features and improvements in your Puppet tests. The example demonstrates the RSpec 3 expect syntax.
```ruby
describe 'rspec3_syntax' do
it 'should use expect syntax'
expect(true).to be(true)
end
end
```
--------------------------------
### Embed YouTube Videos
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/lightbox/examples/index.html
Shows how to embed YouTube videos using various URL formats. It specifies the regular expression used to extract the video ID from different YouTube URL structures. The example also demonstrates how to force specific widths for the video player.
```html
Ghostpoet - Cash and Carry Me Home
Tame Impala - Elephant (using youtu.be link)
Ghostpoet - Cash and Carry Me Home (640 x 360)
Ghostpoet - Cash and Carry Me Home (1280 x 780)
```
--------------------------------
### Add Rake task for rspec
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/documentation/setup/index.md
Integrate rspec testing into your Rake tasks by adding a 'spec' task. This allows you to run your RSpec tests using the Rake build tool.
```ruby
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/**/*_spec.rb'
end
```
--------------------------------
### Puppet Recursive Dependencies: Horizontal
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Demonstrates horizontal recursion in rspec-puppet's relationship matchers, which follows indirect dependencies. This example shows a chain of dependencies starting from 'Yumrepo['foo']' to 'File['/foo']'.
```puppet
class { 'foo::repo': } ->
class { 'foo::install': } ->
class { 'foo::config': }
class foo::repo {
yumrepo { 'foo': }
}
class foo::install {
package { 'foo': }
}
class foo::config {
file { '/foo': }
}
```
--------------------------------
### Initialize Lightbox via JavaScript (jQuery)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/lightbox/examples/index.html
Shows how to programmatically open the lightbox using jQuery. An element with a specific ID is selected, and the '.ekkoLightbox()' method is called on it, optionally accepting configuration options.
```javascript
Open lightbox
$('#mylink').ekkoLightbox(options);
```
--------------------------------
### Restructuring TravisCI Matrix (rspec-puppet)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
The TravisCI matrix has been restructured to optimize build times and testing configurations. Notably, Puppet 2.6 is no longer tested. This change affects the continuous integration setup for projects using rspec-puppet. The example shows a typical TravisCI configuration snippet.
```yaml
matrix:
exclude:
- rvm: 2.6.0
env: PUPPET_VERSION=3.8.0
- rvm: 2.7.0
env: PUPPET_VERSION=2.7.26
```
--------------------------------
### Exposing Scope in Function Example Groups (rspec-puppet)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
rspec-puppet now exposes the `scope` object within function example groups. This allows you to directly interact with and inspect the scope during function testing, providing greater control and insight. The example shows how to access the scope.
```ruby
describe 'my_function' do
it 'should access the scope'
# 'scope' is now available here
expect(scope.lookupvar('ntp::servers')).to eq(['pool.ntp.org'])
end
end
```
--------------------------------
### Test Puppet Functions with rspec-puppet Matchers
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Illustrates testing Puppet functions using rspec-puppet. It shows how to use standard RSpec matchers with the `execute` method and the convenience `run` matcher with methods like `with_params`, `and_return`, `with_lambda`, and `and_raise_error` for comprehensive function testing.
```ruby
it 'should be able to do something' do
subject.execute('foo') == 'bar'
end
```
```ruby
it { is_expected.to run.with_params('foo').and_return('bar') }
```
```ruby
describe '' do
...
end
```
```ruby
describe 'split' do
```
```ruby
it { is_expected.to run.with_params('foo', 'bar', ['baz']) }
```
```ruby
it 'something' do
subject.execute('foo', 'bar', ['baz'])
end
```
```ruby
it { is_expected.to run.with_lambda { |x| x * 2 }
```
```ruby
it { is_expected.to run.with_params('foo').and_return('bar') }
```
```ruby
it 'something' do
subject.execute('foo') == 'bar'
subject.execute('baz').should be_an Array
end
```
```ruby
it { is_expected.to run.with_params('a', 'b').and_raise_error(Puppet::ParseError) }
it { is_expected.not_to run.with_params('a').and_raise_error(Puppet::ParseError) }
```
```ruby
it 'something' do
expect { subject.execute('a', 'b') }.should raise_error(Puppet::ParseError)
expect { subject.execute('a') }.should_not raise_error(Puppet::ParseError)
end
```
```ruby
before(:each) { scope.expects(:lookupvar).with('some_variable').returns('some_value') }
it { is_expected.to run.with_params('...').and_return('...') }
```
```ruby
before(:each) do
Puppet::Parser::Functions.newfunction(:custom_function, :type => :rvalue) { |args|
raise ArgumentError, 'expected foobar' unless args[0] == 'foobar'
'expected value'
}
end
```
--------------------------------
### jQuery scrollTo with Step Function
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/jquery-scrollTo/demo/index.html
Provides an example of using a step function with scrollTo. This function is called for each step of the animation.
```javascript
$(...).scrollTo('li:eq(15)', 2500, {step:function() { }});
```
--------------------------------
### Basic rspec-puppet Test File Structure (Ruby)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/tutorial/index.md
This snippet demonstrates the fundamental structure of an rspec-puppet test file. It includes the necessary require statement for the helper file and the basic 'describe' block where test cases are defined. This structure is consistent across testing classes, defined types, hosts, and functions.
```ruby
require 'spec_helper'
describe '' do
# Your tests go in here
end
```
--------------------------------
### jQuery.scrollTo Default Settings Modification
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/jquery-scrollTo/README.md
Shows how to change the default settings for jQuery.scrollTo globally using $.extend. This example modifies the default axis to 'y' and duration to 800ms.
```javascript
$.extend($.scrollTo.defaults, {
axis: 'y',
duration: 800
});
```
--------------------------------
### Display Image Gallery with Custom Wrapper
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/lightbox/examples/index.html
Demonstrates how to create a gallery of images, where each image is wrapped in a specific HTML tag (e.g., ``). It highlights the use of a custom selector for the parent element to control layout, such as creating columns and rows. The example also shows how to exclude specific images from the gallery.
```html
```
--------------------------------
### Manually Applying matchHeight
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/jquery-match-height/README.md
Illustrates how to directly use the apply function to match heights for specific elements, bypassing the automatic update functionality.
```javascript
$.fn.matchHeight._apply(elements, byRow)
```
--------------------------------
### Fixes and Enhancements for `run` Matcher (rspec-puppet)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
Several fixes and enhancements have been made to the `run` matcher in rspec-puppet. These improvements ensure more reliable and accurate testing of Puppet runs. The example implies that the `run` matcher is now more robust.
```ruby
describe 'run_matcher_enhancements' do
it 'should work correctly with the updated run matcher'
# expect(run).to ... # Use the enhanced run matcher
end
end
```
--------------------------------
### Puppet Resource Matching Syntax
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Demonstrates the correct and incorrect syntax for matching Puppet resources in rspec-puppet tests. It highlights limitations such as the lack of support for quoted titles, inline arrays, and fully qualified class names with leading '::'.
```puppet
Notify[foo]
Class[profile::apache]
```
--------------------------------
### Fixing Newline Issue in pre_condition (rspec-puppet)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
This fix addresses a bug in rspec-puppet where a newline might not be added after a user-specified `pre_condition`. This could cause catalogue compilation to fail. The example shows how the `pre_condition` is now correctly handled.
```ruby
describe 'pre_condition_newline' do
let(:pre_condition) { 'ensure_packages(apache2)' } # Pre-condition string
it 'should ensure newline after pre_condition'
# Test that the catalogue compiles correctly with the pre_condition
end
end
```
--------------------------------
### Testing Custom Types in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
This snippet demonstrates how to test custom Puppet types using rspec-puppet. It includes an example group and matcher specifically designed for this purpose. No external dependencies are required beyond rspec-puppet itself.
```ruby
describe 'custom_type' do
let(:title) { 'my_resource' }
let(:params) { { 'param1' => 'value1' } }
it { is_expected.to contain_custom_type(title).with(params) }
end
```
--------------------------------
### Display Gallery of YouTube Videos
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/lightbox/examples/index.html
Illustrates how to create a gallery specifically for YouTube videos. It shows different ways to link to YouTube videos, including standard URLs, youtu.be short links, and embed URLs, all within a gallery format.
```html
```
--------------------------------
### Puppet Recursive Dependencies: Vertical
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Illustrates vertical recursion in rspec-puppet's relationship matchers, where dependencies with parent resources are checked. This example shows a class dependency where 'Package['foo']' must come before 'File['/foo']'.
```puppet
class { 'foo::install': } ->
class { 'foo::config': }
class foo::install {
package { 'foo': }
}
class foo::config {
file { '/foo': }
}
```
--------------------------------
### Fixing `compile.and_raise_error` in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
The `compile.and_raise_error` matcher has been fixed to correctly consider successful compilation as an error when this matcher is used. This ensures accurate testing of scenarios where compilation is expected to fail. The example implies correct behavior for this matcher.
```ruby
describe 'compilation_error_matcher' do
let(:manifest) { '...' } # Malformed Puppet code
it 'should raise an error when using and_raise_error'
expect(compile(manifest)).to raise_error(Puppet::Error)
end
end
```
--------------------------------
### Configure Hiera in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Demonstrates how to set up Hiera configuration within rspec-puppet tests. This involves defining the Hiera configuration file path and initializing Hiera. It's crucial for tests that rely on Hiera data.
```ruby
let(:hiera_config) { 'spec/fixtures/hiera/hiera.yaml' }
hiera = Hiera.new(:config => 'spec/fixtures/hiera/hiera.yaml')
```
--------------------------------
### Configure Test Environment with Facts and Node Parameters in rspec-puppet
Source: https://context7.com/rodjek/rspec-puppet/llms.txt
Shows how to configure the test environment in rspec-puppet using facts, node parameters, trusted facts, and pre/post conditions. This allows for more realistic testing scenarios by simulating different node states and configurations.
```ruby
require 'spec_helper'
describe 'mymodule' do
# Basic facts
let(:facts) do
{
'os' => {
'family' => 'RedHat',
'release' => { 'major' => '7', 'full' => '7.9' }
},
'networking' => { 'fqdn' => 'server.example.com' },
'kernel' => 'Linux'
}
end
# Node name
let(:node) { 'server.example.com' }
# Node parameters (top-scope variables like from ENC)
let(:node_params) do
{
'role' => 'webserver',
'environment' => 'production',
'datacenter' => 'us-east-1'
}
end
# Trusted facts (certificate extensions)
let(:trusted_facts) do
{
'pp_role' => 'webserver',
'pp_datacenter' => 'us-east-1'
}
end
# Trusted external data (Puppet 6.14+)
let(:trusted_external_data) do
{
'vault' => { 'secret_key' => 'abc123' }
}
end
# Pre-condition: code evaluated before the tested class
let(:pre_condition) do
<<-PUPPET
class { 'stdlib': }
$global_var = 'test_value'
PUPPET
end
# Post-condition: code evaluated after the tested class
let(:post_condition) { 'include mymodule::cleanup' }
# Environment name
let(:environment) { 'production' }
it { is_expected.to compile }
it { is_expected.to contain_class('mymodule').with_role('webserver') }
end
```
--------------------------------
### Coverage for Non-Class Resources in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
Coverage reports in rspec-puppet now correctly include non-class resources. This ensures a more accurate and comprehensive understanding of your code coverage, as all resource types are accounted for. The example implies that coverage is now calculated for all resource types.
```ruby
# In your RSpec configuration (e.g., spec/spec_helper.rb)
require 'rspec/puppet/coverage'
# Coverage reports will now include non-class resources.
```
--------------------------------
### Basic rspec-puppet Test Structure
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/documentation/applications/index.md
Defines the fundamental structure for an rspec-puppet test case for an application. It includes setting up the node, title, and parameters, which are essential for defining the application's context and configuration within the test. The `ref` method is crucial for creating valid node and resource references.
```ruby
describe '
' do
let(:node) { '' }
let(:title) { '' }
let(:params) do
{
'nodes' => {
ref('Node', node) => ref('', title)
}
# any additional app parameters
}
end
# tests go here
end
```
--------------------------------
### Basic Host Test Structure (Ruby)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/documentation/hosts/index.md
Defines the basic structure for testing a host using RSpec-Puppet. It requires the spec_helper and uses a describe block for the host name. This sets up the environment to compile the catalogue for the specified host and run tests against it.
```ruby
require 'spec_helper'
describe '' do
# your tests go here
end
```
--------------------------------
### Test Puppet Catalog Compilation with RSpec-Puppet
Source: https://context7.com/rodjek/rspec-puppet/llms.txt
Demonstrates using the `compile` matcher in RSpec-Puppet to test catalog compilation. Covers basic compilation, compilation with all dependencies, testing for expected compilation errors, and detecting circular dependencies.
```ruby
require 'spec_helper'
describe 'mymodule' do
# Basic compilation
it { is_expected.to compile }
# Compile with all dependencies resolved
it { is_expected.to compile.with_all_deps }
# Test compilation failure with error matching
context 'with invalid parameters' do
let(:params) { { 'port' => 'invalid' } }
it { is_expected.to compile.and_raise_error(/expects an Integer/) }
it { is_expected.to compile.and_raise_error(Puppet::PreformattedError) }
end
# Test for dependency cycles
context 'with circular dependencies' do
let(:pre_condition) do
<<-PUPPET
file { '/a': require => File['/b'] }
file { '/b': require => File['/a'] }
PUPPET
end
it { is_expected.not_to compile }
end
end
```
--------------------------------
### Better Function Failure Messages
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
Function failure messages in rspec-puppet have been improved, providing more clarity and detail when functions encounter errors during testing. This aids in quicker debugging and resolution of function-related issues. The example implies more informative error output.
```ruby
describe 'function_failure_messages' do
it 'should provide clear failure messages for functions'
# expect { my_function_that_fails }.to raise_error(...)
# The error message will be more descriptive.
end
end
```
--------------------------------
### Configuring Resize Update Throttling for matchHeight
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/jquery-match-height/README.md
Explains how to adjust the `_throttle` property to control the rate at which matchHeight updates its layout during window resize events. Lower values offer smoother updates at the cost of performance, while higher values improve performance at the cost of responsiveness.
```javascript
$.fn.matchHeight._throttle = 80;
```
--------------------------------
### Documenting the `scope` Property in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
The `scope` property within rspec-puppet has been documented. This clarifies its purpose and how it can be used, particularly in function testing, allowing developers to better understand and leverage this aspect of the framework. The example implies that documentation for `scope` is now available.
```ruby
# Refer to the rspec-puppet documentation for details on the 'scope' property.
```
--------------------------------
### Verify Resource Parameters with with_ and only_with_
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Allows testing of specific parameters passed to a resource. `with_` checks individual parameters, while `only_with_` ensures those are the only parameters set. The `with` and `only_with` methods can verify multiple parameters simultaneously.
```ruby
it { is_expected.to contain_package('mysql-server').with_ensure('present') }
it { is_expected.to contain_package('httpd').only_with_ensure('latest') }
it do
is_expected.to contain_service('keystone').with(
'ensure' => 'running',
'enable' => 'true',
'hasstatus' => 'true',
'hasrestart' => 'true'
)
end
it do
is_expected.to contain_user('luke').only_with(
'ensure' => 'present',
'uid' => '501'
)
end
```
--------------------------------
### Puppet's module_path Configuration in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
rspec-puppet now correctly respects and configures Puppet's `module_path`. It can handle multiple entries in `module_path`, ensuring that Puppet loads code from all specified directories. The example shows how multiple paths are handled.
```ruby
describe 'module_path_support' do
let(:module_path) { ['/etc/puppet/modules', '/usr/share/puppet/modules'] } # Multiple paths
it 'should load modules from all specified paths'
# Assert that modules from both paths are available for compilation
end
end
```
--------------------------------
### Delegate Event for Lightbox Initialization (jQuery)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/assets/plugins/lightbox/examples/index.html
This JavaScript code sets up a delegated event listener for any element with the 'data-toggle="lightbox"' attribute. When clicked, it prevents the default action and initializes the Ekko Lightbox, making it efficient for dynamically added content.
```javascript
$(document).delegate('*[data-toggle="lightbox"]', 'click', function(event) {
event.preventDefault();
$(this).ekkoLightbox();
});
```
--------------------------------
### Specify Trusted Facts for Manifests in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Allows you to define custom trusted facts, particularly for Puppet versions 4.3 and later. This includes simulating certificate extensions that are typically found in the `$trusted['extensions']` hash. These can be set per example or as defaults.
```ruby
let(:trusted_facts) { {'pp_uuid' => 'ED803750-E3C7-44F5-BB08-41A04433FE2E', '1.3.6.1.4.1.34380.1.2.1' => 'ssl-termination'} }
```
```ruby
RSpec.configure do |c|
c.default_trusted_facts = {
'pp_uuid' => 'ED803750-E3C7-44F5-BB08-41A04433FE2E',
'1.3.6.1.4.1.34380.1.2.1' => 'ssl-termination'
}
end
```
--------------------------------
### Asserting File Resource Parameters in Puppet Defined Type with RSpec
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/tutorial/index.md
This RSpec snippet demonstrates how to assert specific parameters and their values for a file resource created by a Puppet defined type. It uses the `with` method to chain parameter checks.
```ruby
it do
is_expected.to contain_file('/etc/logrotate.d/nginx').with({
'ensure' => 'present',
'owner' => 'root',
'group' => 'root',
'mode' => '0444',
})
end
```
--------------------------------
### Munging Parameter Values in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
This fix ensures that when comparing parameter values, rspec-puppet will munge the actual value into an array if the expected value is an array with a single item. This provides more consistent and predictable parameter comparisons. The example illustrates this behavior.
```ruby
describe 'parameter_munging' do
let(:params) { { 'users' => ['alice'] } } # Actual value is an array
it 'should handle single-item array expectations'
expect(params['users']).to eq('alice') # Expected value is a string
# rspec-puppet will now correctly compare ['alice'] with 'alice'
end
end
```
--------------------------------
### RSpec Describe Blocks for Puppet Resource Types
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Demonstrates how to use RSpec's 'describe' blocks with type metadata to group tests for various Puppet resource types. This is useful when not adhering to the recommended directory structure for automatic grouping.
```ruby
describe 'myclass', :type => :class do
...
end
describe 'mydefine', :type => :define do
...
end
describe 'myapplication', :type => :application do
...
end
describe 'myfunction', :type => :puppet_function do
...
end
describe 'mytype', :type => :type do
...
end
describe 'My::TypeAlias', :type => :type_alias do
...
end
describe 'myhost.example.com', :type => :host do
...
end
```
--------------------------------
### Accessing Catalogue in Function Tests (rspec-puppet)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
Function tests in rspec-puppet now have access to the compiled catalogue. This allows you to inspect the catalogue generated by your functions and perform more in-depth assertions. The example demonstrates how to access and use the catalogue within a function test.
```ruby
describe 'my_function' do
let(:scope) { ... }
let(:catalogue) { compile_catalogue(scope) } # Access the catalogue
it 'should produce the correct resources'
expect(catalogue.resource('Notify[hello]')).to be_present
end
end
```
--------------------------------
### Testing File Resource Creation in Puppet Defined Type with RSpec
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/tutorial/index.md
Shows how to test if a Puppet defined type creates a specific file resource. This snippet asserts the existence of a file in `/etc/logrotate.d/`.
```ruby
it { is_expected.to contain_file('/etc/logrotate.d/nginx') }
```
--------------------------------
### Testing with Future Parser in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
rspec-puppet allows you to test your Puppet code against the future parser. This is essential for ensuring compatibility with upcoming Puppet versions and for adopting new syntax features. The example shows how to enable the future parser for your tests.
```ruby
describe 'future_parser_tests' do
let(:parser) { 'future' } # Enable future parser
it 'should work with the future parser'
# Test your code using future parser syntax
end
end
```
--------------------------------
### Coverage Reports in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
This feature provides coverage reports for your Puppet code, helping you identify untested parts of your manifests and modules. It integrates with RSpec's coverage reporting mechanisms. The example shows how to enable and view coverage reports.
```ruby
# In your RSpec configuration (e.g., spec/spec_helper.rb)
require 'rspec/puppet/coverage'
RSpec.configure do |c|
c.before :each do
# Optionally clear coverage before each test
Puppet::RSpec::Coverage.instance.reset
end
end
# Run with: rspec --format progress --format RspecPuppet::CoverageFormatter
```
--------------------------------
### Specify Post-Conditions for Manifest Loading in rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Enables you to define Puppet code that will be evaluated after the manifest under test. This is useful for ensuring that certain code runs after the tested class, for example, when testing modular components. The post-condition can be a single string or an array of strings.
```ruby
let(:post_condition) { 'include other_class' }
```
--------------------------------
### Test Puppet Function Execution with RSpec-Puppet
Source: https://context7.com/rodjek/rspec-puppet/llms.txt
Demonstrates testing Puppet functions using RSpec-Puppet's `run` matcher. Covers successful execution, regex matching, error handling, and lambda/block usage. It also shows how to test functions that modify the catalogue and interact with scope lookups.
```ruby
require 'spec_helper'
describe 'my_split' do
# Test successful execution with return value
it { is_expected.to run.with_params('hello-world', '-').and_return(['hello', 'world']) }
# Test with regex pattern matching on return
it { is_expected.to run.with_params('test123', /\d+/).and_return(/test/) }
# Test error handling
it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
it { is_expected.to run.with_params('single').and_raise_error(ArgumentError, /expects 2 arguments/) }
# Test that function runs without error (no return value check)
it { is_expected.to run.with_params('foo', 'o') }
# Test with lambda/block
it { is_expected.to run.with_params([1, 2, 3]).with_lambda { |x| x * 2 }.and_return([2, 4, 6]) }
# Test function that modifies catalogue
context 'ensure_resource function' do
it 'adds resources to the catalogue' do
is_expected.to run.with_params('user', 'testuser', { 'ensure' => 'present' })
expect(catalogue).to contain_user('testuser').with_ensure('present')
end
end
# Access parser scope for stubbing
context 'with scope lookups' do
let(:pre_condition) { 'include ::mymodule' }
before do
allow(scope).to receive(:lookupvar).with('mymodule::config_dir').and_return('/etc/myapp')
end
it { is_expected.to run.with_params('config').and_return('/etc/myapp/config') }
end
end
```
--------------------------------
### Testing Puppet Catalog Compilation with rspec-puppet
Source: https://github.com/rodjek/rspec-puppet/blob/master/README.md
Shows how to use the 'compile' matcher in rspec-puppet to verify that a Puppet catalog compiles without errors. It also demonstrates how to assert specific error messages during compilation.
```ruby
it { is_expected.to compile }
```
```ruby
it { is_expected.to compile.and_raise_error(/error message match/) }
```
--------------------------------
### Adding Settings for Ordering, Strict Variables, etc. (rspec-puppet)
Source: https://github.com/rodjek/rspec-puppet/blob/master/docs/changelog/index.md
New settings have been added to rspec-puppet, including `ordering`, `strict_variables`, `stringify_facts`, and `trusted_node_data`. These allow for more granular control over Puppet's behavior during testing. The example shows how to set one of these options.
```ruby
describe 'puppet_settings' do
let(:strict_variables) { true } # Enable strict variables
it 'should compile with strict variables enabled'
# Test your code to ensure it adheres to strict variable rules
end
end
```