assert

Puppet 4 functions for making assertions, to support in-language testing

Daniel Dreier

danieldreier

7,389 downloads

5,401 latest version

4.3 quality score

Version information

  • 0.1.2 (latest)
  • 0.1.1
  • 0.1.0
released Aug 1st 2016
This version is compatible with:
  • Puppet Enterprise 2017.2.x, 2017.1.x, 2016.5.x, 2016.4.x
  • Puppet 4.x
  • Debian
    ,
    RedHat

Start using this module

Documentation

danieldreier/assert — version 0.1.2 Aug 1st 2016

Puppet Assert Functions

Build Status

  1. Overview
  2. Description
  3. Getting Started
  4. Usage
  5. Development

Overview

Puppet functions to make assertions that fail the compile if they're not met.

Description

This module provides several functions intended for testing other puppet functions. If you're writing puppet functions without side effects, this is a simpler way to write tests than using rspec-puppet. The intent is to provide what amounts to syntactic sugar for writing simple tests in Puppet.

Getting Started

To get started, you can test simple puppet expressions. Put the following in a file and run it using puppet apply:

assert::true(true)
assert::true(1 + 1 == 2)
assert::false(false)
assert::false(2 + 2 == 1)

This will run without errors, or any output at all:

puppet apply test.pp
Notice: Compiled catalog for macbook in environment production in 0.03 seconds
Notice: Applied catalog in 0.01 seconds

Change one of the assertions so that it fails:

assert:true(false)
puppet apply test.pp
Error: Evaluation Error: Error while evaluating a Function Call, assert::true failed because argument is false instead of true at /tmp/example.pp:1:1 on node macbook

A trivial test of custom function would look something like:

# our custom function
function is_odd($num) {
  case $num % 2 {
    0: { false }
    default: { true }
  }
}

# using dot notation
is_odd(1).assert::true
is_odd(2).assert::false

# passing values to a block
[1,3,5].each |$x| {
  $x.assert::true |$n| {
    is_odd($n)
  }
}

Usage

Note that you can compile catalogs without applying them, for the sake of testing:

puppet master --compile --manifest example.pp

assert::equal(Any, Any)

Return true if both parameters are equal, or return an error if the values are not equal.

example: using assert::equal:

assert::equal('example', 'example')
'example'.assert::equal('example')
assert::equal((2 + 2),4)

assert::not_equal(Any, Any)

Return true if both parameters are not equal, and return an error if the values are equal.

example: using assert::not_equal:

assert::not_equal(1, 2)
assert::not_equal(true, false)
true.assert::not_equal(false)
'fizz'.assert::not_equal('buzz')

assert::true

If given a single boolean parameter, assert::true will raise an error if the parameter value is false, and will return true if the parameter value is true.

example: using assert::true with a boolean parameter:

assert::true(2 + 2 == 4)

Given a block and any number of parameters, assert::true will raise an error if the block returns any value other than a Boolean true value.

example: using assert::true with a block:

'hunter2'.assert::true |$amazing_password| {
  $amazing_password =~ /[\w]+/
}

assert::false

assert::false is the inverse of assert::true. example: using assert::false with a boolean parameter:

assert::false(1 + 1 == 3)

example: using assert::false with a block:

'daniel'.assert::false |$username| {
  $username == 'joe'
}

Development

Please contribute if there's functionality you'd like to see, and please file issues if you run into problems. Testing inside the puppet language isn't much of a thing, so the tooling around it is minimal.

The goal for this particular module is to provide a very simple way to test functions based on assertions, because I want to write more puppet functions and I enjoy writing puppet more than writing rspec-puppet.

More sophisticated in-language testing tools may be needed, so the goal here is to have something simple that can bootstrap a larger ecosystem of functional puppet tools.