• Stars
    star
    121
  • Rank 284,425 (Top 6 %)
  • Language
    Ruby
  • License
    Other
  • Created over 12 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A set of shared spec helpers specific to Puppetlabs projects

Puppet Labs Spec Helper

The Short Version

This repository is meant to provide a single source of truth for how to initialize different Puppet versions for spec testing.

The common use case is a module such as stdlib that works with many versions of Puppet. The stdlib module should require the spec helper in this repository, which will in turn automatically figure out the version of Puppet being tested against and perform version specific initialization.

Other "customers" that should use this module are:

Usage

When developing or testing modules, simply clone this repository and install the gem it contains. The recommended way to do this is using bundler.

Example Gemfile:

source 'https://rubygems.org'
gem 'puppetlabs_spec_helper'

Add this to your project's spec_helper.rb:

require 'puppetlabs_spec_helper/module_spec_helper'

Add this to your project's Rakefile:

require 'puppetlabs_spec_helper/rake_tasks'

And run the spec tests:

$ cd $modulename
$ rake spec

Parallel Fixture Downloads

Fixture downloads will now execute in parallel to speed up the testing process. Which can represent >= 600% speed increase (depending on number of threads). You can control the amount of threads by setting the MAX_FIXTURE_THREAD_COUNT environment variable to a positive integer, the default is currently 10. We don't suggest going higher than 25 as the gains are marginal due to some repos taking a long time to download. Please be aware that your internal VCS system may not be able to handle a high load in which case the server would fail to clone the repository. Because of this issue, this setting is tunable via MAX_FIXTURE_THREAD_COUNT.

Additionally, you can also speed up cloning when using the ssh protocol by multiplexing ssh sessions. Add something similar to your ssh config. Note: you may need to change the host if you're using an internal git server.

Host github.com
  ControlMaster auto
  ControlPath ~/.ssh/ssh-%r@%h:%p
  ControlPersist yes

Note: parallel downloads are available for repositories and forge modules.

Parallel tests

It is also possible to use the parallel_tests Gem via the :parallel_spec Rake task to run rspec commands in parallel on groups of spec files.

Use of parallelization at this level can result in large performance benefits when the Rspec examples tend to cause a number of large, CPU-intensive catalog compilations to occur. An example of where this might be the case is in a complex module with a lot of tests or a control repo with many hosts.

Be aware however that in other circumstances this parallelization can result in the tests actually taking longer to run. The best thing to do is time rspec spec and rspec parallel_spec and use the parallelization only when there is a clear benefit.

To enable this feature, add the parallel_tests Gem to your project's Gemfile:

gem 'parallel_tests'

And then to run spec tests in parallel:

$ rake parallel_spec

Issues

Please file issues against this project at the Puppet Labs Issue Tracker

The Long Version

Purpose of this Project

This project is intended to serve two purposes:

  1. To serve as a bridge between external projects and multiple versions of puppet; in other words, if your project has a dependency on puppet, you shouldn't need to need to worry about the details of how to initialize puppet's state for testing, no matter what version of puppet you are testing against.
  2. To provide some convenience classes / methods for doing things like creating tempfiles, common rspec matchers, etc. These classes are in the puppetlabs_spec directory.
  3. To provide a common set of Rake tasks so that the procedure for testing modules is unified.

To Use this Project

The most common usage scenario is that you will check out the 'main' branch of this project from github, and install it as a rubygem. There should be few or no cases where you would want to have any other branch of this project besides main/HEAD.

Running on non-current ruby versions

Since gem and bundler, ruby's package management tools, do not take the target ruby version into account when downloading packages, the puppetlabs_spec_helper gem can only depend on gems that are available for all supported ruby versions. If you can/want to use features from other packages, install those additional packages manually, or have a look at the Gemfile, which provides code to specify those dependencies in a more "friendly" way. This currently affects the following gems:

  • puppet
  • rubocop
  • rubocop-rspec
  • json_pure
  • rack

Initializing Puppet for Testing

In most cases, your project should be able to define a spec_helper.rb that includes just this one simple line:

require 'puppetlabs_spec_helper/puppet_spec_helper'

Then, as long as the gem is installed, you should be all set.

If you are using rspec-puppet for module testing, you will want to include a different library:

require 'puppetlabs_spec_helper/module_spec_helper'

NOTE that this is specifically for initializing Puppet's core. If your project does not have any dependencies on puppet and you just want to use the utility classes, see the next section.

A number of the Puppet parser features, controlled via configuration during a normal puppet run, can be controlled by exporting specific environment variables for the spec run. These are:

  • STRICT_VARIABLES - Set to "no" to disable strict variable checking. See strict_variables in puppet.conf for details.

  • ORDERING - set to the desired ordering method ("title-hash", "manifest", or "random") to set the order of unrelated resources when applying a catalog. Leave unset for the default behavior, currently "random". This is equivalent to setting ordering in puppet.conf.

    As an example, to run spec tests with the future parser, strict variable checking, and manifest ordering, you would:

    ORDERING=manifest rake spec

Using Utility Classes

If you'd like to use the Utility classes (PuppetlabsSpec::Files, PuppetlabsSpec::Fixtures), you just need to add this to your project's spec_helper.rb:

require 'puppetlabs_spec_helper/puppetlabs_spec_helper'

NOTE that the above line happens automatically if you've required 'puppetlabs_spec_helper/puppet_spec_helper', so you don't need to do both.

In either case, you'll have all of the functionality of Puppetlabs::Files, Puppetlabs::Fixtures, etc., mixed-in to your rspec context.

Using Fixtures

puppetlabs_spec_helper has the ability to populate the spec/fixtures/modules directory with dependent modules when rake spec or rake spec_prep is run. To do so, all required modules should be listed in a file named .fixtures.yml in the root of the project. You can specify a alternate location for that file in the FIXTURES_YML environment variable.

You can use the MODULE_WORKING_DIR environment variable to specify a diffent location when installing module fixtures via the forge. By default the working directory is <module directory>/spec/fixtures/work-dir.

When specifying the repo source of the fixture you have a few options as to which revision of the codebase you wish to use, and optionally, the puppet versions where the fixture is needed.

  • repo - the url to the repo

  • scm - options include git or hg. This is an optional step as the helper code will figure out which scm is used.

    scm: git
    scm: hg
  • target - the directory name to clone the repo into ie. target: mymodule defaults to the repo name (Optional)

  • subdir - directory to be removed from the cloned repo. Its contents will be moved to the root directory (Optional)

  • ref - used to specify the tag name (like version) or commit hash to be checked out (Optional). Branch names should use the branch option instead.

    ref: 1.0.0
    ref: 880fca52c
  • branch - used to specify the branch name you want to use ie. branch: development

  • flags - additional flags passed to the module installer (both puppet and scm)

    flags: --verbose
  • puppet_version - versions of puppet for which the fixture should be installed. Ruby version constraints are supported. Only works when the semantic_puppet gem is available (shipped with puppet 4.0 and up, by default).

    puppet_version: '>= 6.0.0'

Notes:

  • ref and branch can be used together to get a specific revision on a specific branch
  • Top level defaults option could be used to set global options

Fixtures Examples

Basic fixtures that will symlink spec/fixtures/modules/my_modules to the project root:

fixtures:
  symlinks:
    my_module: "#{source_dir}"

This is the same as specifying no symlinks fixtures at all.

Add firewall and stdlib as required module fixtures:

fixtures:
  repositories:
    firewall: "git://github.com/puppetlabs/puppetlabs-firewall"
    stdlib: "git://github.com/puppetlabs/puppetlabs-stdlib"

Put a supplementary repository at a different location

fixtures:
  repositories:
    firewall: "git://github.com/puppetlabs/puppetlabs-firewall"
    stdlib: "git://github.com/puppetlabs/puppetlabs-stdlib"
    control_repo:
      repo: "https://github.com/puppetlabs/control-repo"
      target: "spec/fixtures/control_repos"

Specify that the git tag 2.4.2 of stdlib should be checked out:

fixtures:
  repositories:
    firewall: "git://github.com/puppetlabs/puppetlabs-firewall"
    stdlib:
      repo: "git://github.com/puppetlabs/puppetlabs-stdlib"
      ref: "2.6.0"

Only install the yumrepo_core module when testing against Puppet 7 or greater:

fixtures:
  repositories:
    yumrepo_core:
      repo: "git://github.com/puppetlabs/puppetlabs-yumrepo_core"
      puppet_version: ">= 6.0.0"

Move manifests and siblings to root directory when they are inside a code directory:

fixtures:
  repositories:
    stdlib:
      repo: "git://github.com/puppetlabs/puppetlabs-extradirectory"
      subdir: "code"

Install modules from Puppet Forge:

fixtures:
  forge_modules:
    firewall: "puppetlabs/firewall"
    stdlib:
      repo: "puppetlabs/stdlib"
      ref: "2.6.0"

Pass additional flags to module installation:

fixtures:
  forge_modules:
    stdlib:
      repo: "puppetlabs/stdlib"
      ref: "2.6.0"
      flags: "--module_repository https://my_repo.com"
    repositories:
      firewall:
        repo: "git://github.com/puppetlabs/puppetlabs-firewall"
        ref: "2.6.0"
        flags: "--verbose"

Use defaults to define global parameters:

defaults:
  forge_modules:
    flags: "--module_repository https://my_repo.com"
fixtures:
  forge_modules:
    stdlib:
      repo: "puppetlabs/stdlib"
      ref: "2.6.0"
    repositories:
      firewall:
        repo: "git://github.com/puppetlabs/puppetlabs-firewall"
        ref: "2.6.0"

Fixture Loading

Any module that has a spec/lib directory will be available on the ruby LOAD_PATH for tests to consume. This allows modules to provide additional helper code to be supplied. The augeasprovider_core module has some examples.

Testing Parser Functions

This whole section is superseded by improved support of accessing the scope in rspec-puppet.

Modify rspec behavior

Running Rspec with additional settings

You can add command line options to rspec using the CI_SPEC_OPTIONS environment variable. Any text in the CI_SPEC_OPTIONS environment variable is added as an rspec option. For example:

If you wanted to output JUnit test reports for a Jenkins CI server you could use;

Bash

export CI_SPEC_OPTIONS = "-r yarjuf -f JUnit -o result.xml"

PowerShell

$ENV:CI_SPEC_OPTIONS = '-r yarjuf -f JUnit -o result.xml'

And then run

bundle exec rake spec

This would cause rspec to load the yarjuf gem and output the results in JUnit format to the file result.xml

Running specs in parallel

When executing tests in a matrix CI environment, tests can be split up to run a share of specs per CI node in parallel. Set the CI_NODE_TOTAL environment variable to the total number of nodes, and the CI_NODE_INDEX to a number between 1 and the CI_NODE_TOTAL.

If using Travis CI, add new lines to the "env" section of .travis.yml per node, remembering to duplicate any existing environment variables:

env:
  - FUTURE_PARSER=yes CI_NODE_TOTAL=2 CI_NODE_INDEX=1
  - FUTURE_PARSER=yes CI_NODE_TOTAL=2 CI_NODE_INDEX=2

Running tests tagged with test tiers

To run tests tagged with risk levels set the TEST_TIERS environment variable to a comma-separated list of the appropriate tiers.

For example: to run tests marked tier_high => true and tier_medium => true in the same test run set the environment variableTEST_TIERS=high,medium

By default TEST_TIERS only accepts low, medium and high as valid tiers. If you would like to use your own keywords to set the environment variable TEST_TIERS_ALLOWED.

For example: to use the keywords dev, rnd, staging and production you can set TEST_TIERS_ALLOWED=dev,rnd,staging,production. Then you would be able to run tests marked tier_dev => true, tier_production => true with TEST_TIERS=dev,production

Note, if the TEST_TIERS environment variable is set to empty string or nil, all tiers will be executed.

Generating code coverage reports

This section describes how to add code coverage reports for Ruby files (types, providers, ...). See the documentation of RSpec-Puppet for Puppet manifest coverage reports.

Starting with Ruby 1.9, the de facto standard for Ruby code coverage is SimpleCov. It is implemented as a Rake task in this gem.

To run code coverage:

$ rake spec:simplecov

Reports are written to /coverage/, which you should add to .gitignore.

The reports can be generated every time you invoke RSpec, e.g. via rake spec, You can enable it, set the following environment variable:s

SIMPLECOV=yes

Remember to add the simplecov-console and codecov gems to your Gemfile. If you run spec:simplecov on Travis-CI or any of the other supported CI services, reports gets automatically uploaded to https://codecov.io/ .

Some Notes for Windows Users

A windows users may need to do one of two things to execute rake spec.

Although things may appear to work, the init.pp may not transfer to the fixtures folder as needed or may transfer as an empty file.

This is related to a registry security setting requiring elevated privileges to create symbolic links.

Currently, there are two known approaches to get around this problem.

  • run your windows shell (cmd) as an Administrator or
  • modify the registry entry settings to allow symbolic links to be created.

The following links may give you some insight into why...

Server Fault Post

Stack Overflow Post

Microsoft TechNet

mock_with

There are two major mocking frameworks in modules test suites today: mocha and rspec-mocks. We recommend that you choose rspec-mocks explicitly by specifying mock_with, either in your spec_helper.rb like so:

RSpec.configure do |c|
  c.mock_with :rspec
end
require 'puppetlabs_spec_helper/module_spec_helper'

or by using Puppet Development Kit's mock_with option in .sync.yml and pdk update.

You can also continue to use mocha by explicitly specifying :mocha, following the mocha documentation.

Migration

To migrate from mocha to rspec-mocks, in many simple cases the following two kinds of changes are all you need:

Translate all stubs:

context.stubs(:some_method).with(arguments).returns('value')

to this:

allow(context).to receive(:some_method).with(arguments).and_return('value')

Translate all expectations:

context.expects(:some_method).with(arguments).returns('value')

to this:

expect(context).to receive(:some_method).with(arguments).and_return('value')

Rationale

  • As a part of the RSpec project, rspec-mocks integration is better.
  • mocha is extending base ruby objects with its stubs, and expects methods, polluting the global namespace, with a potential for subtle and gnarly errors.
  • Currently both rspec-mocks and mocha get loaded, leading to test suites that use both.
  • puppetlabs_spec_helper loads and configures mocha unconditionally, causing friction when a different mocking framework is wanted.
  • mocha is an additional dependency to carry.

EOF

More Repositories

1

puppet

Server automation framework and application
Ruby
7,082
star
2

showoff

Don't just present; interact with your audience!
JavaScript
932
star
3

r10k

Smarter Puppet deployment
Ruby
798
star
4

facter

Collect and display system facts
Ruby
603
star
5

trapperkeeper

A services framework for Clojure / JVM applications.
Clojure
586
star
6

bolt

Bolt is an open source orchestration tool that automates the manual work it takes to maintain your infrastructure on an as-needed basis or as part of a greater orchestration workflow. It can be installed on your local workstation and connects directly to remote nodes with SSH or WinRM, so you are not required to install any agent software.
Ruby
459
star
7

puppetlabs-mysql

MySQL Puppet Module / Manifests + Types & Providers
Ruby
385
star
8

puppetlabs-apache

Puppet module for the Apache httpd server, maintained by Puppet, Inc.
Ruby
365
star
9

puppetlabs-stdlib

Puppet Labs Standard Library module
Ruby
355
star
10

hiera

Lightweight Pluggable Hierarchical Database
Ruby
295
star
11

puppetdb

Centralized Puppet Storage
Clojure
290
star
12

puppetserver

Server automation framework and application
Clojure
280
star
13

puppetlabs-firewall

Puppet Firewall Module
Ruby
269
star
14

puppetlabs-postgresql

Puppet module for managing PostgreSQL
Ruby
229
star
15

puppet-docs

Curated Puppet Documentation
HTML
223
star
16

pdk

The shortest path to better modules: Puppet Development Kit; Download:
Ruby
217
star
17

control-repo

A control repository template
Ruby
197
star
18

pupperware

Container fun time lives here.
Ruby
184
star
19

puppetlabs-concat

File concatenation system for Puppet
Ruby
171
star
20

puppet-vagrant-boxes

Veewee definitions for a set of generic vagrant boxes
Shell
153
star
21

puppetlabs-ntp

Puppet module to manage the NTP service
Ruby
145
star
22

puppetlabs-lvm

Puppet Module to manage LVM
Ruby
126
star
23

best-practices

Best practice docs created by the Puppet Customer Success team
CSS
120
star
24

puppetlabs-packer

Packer templates to build images for vSphere
PowerShell
119
star
25

puppetlabs-java

Puppet Module to manage Java
Ruby
103
star
26

puppet-syntax-vim

Puppet language syntax highlighting for Vim
Vim Script
102
star
27

puppet-specifications

Specification of the Puppet Language, Catalog, Extension points
Ruby
97
star
28

vault-plugin-secrets-oauthapp

OAuth 2.0 secrets plugin for HashiCorp Vault supporting a variety of grant types
Go
92
star
29

puppetlabs-kubernetes

This module install and configures a Kubernetes cluster
Ruby
91
star
30

puppet_litmus

Providing a simple command line tool for puppet content creators, to enable simple and complex test deployments.
Ruby
89
star
31

puppet-strings

The next generation Puppet documentation extraction and presentation tool.
Ruby
89
star
32

puppetlabs-docker

The Puppet Docker repository
Ruby
85
star
33

cpp-hocon

A C++ port of the Typesafe Config library.
C++
83
star
34

education-builds

Bootstrap CentOS training VMs from scratch. Now with true versioning!
Ruby
82
star
35

puppet-vscode

Puppet Editing. Redefined.
TypeScript
79
star
36

vmpooler

Provide configurable 'pools' of instantly-available (running) virtual machines
Ruby
75
star
37

tasks-hands-on-lab

Deprecated: Please see http://bolt.guide to follow our Bolt tutorial!
Shell
73
star
38

puppetlabs-inifile

Resource types for managing settings in INI files
Ruby
70
star
39

hiera-puppet

Puppet function and data backend for Hiera
Ruby
60
star
40

leatherman

A collection of C++ and CMake utility libraries.
C++
57
star
41

puppet-rfc

Puppet RFC Repository
Ruby
55
star
42

puppetlabs-f5

Puppet Management of F5 Network Devices.
53
star
43

puppetlabs-puppetdb

A puppet module for installing and managing puppetdb
Ruby
52
star
44

relay

Event-driven workflows for DevOps automation
Go
52
star
45

puppetlabs-powershell

powershell provider for the Puppet exec resource type
Ruby
50
star
46

puppetlabs-rsync

rsync module
Ruby
49
star
47

puppet-agent

All of the directions for building a puppet agent package.
Ruby
48
star
48

puppetserver-helm-chart

The Helm Chart for Puppet Server
Mustache
46
star
49

homebrew-puppet

A tap for Puppet macOS package distribution
Ruby
45
star
50

pdk-templates

The main template repo for the Puppet Development Kit https://github.com/puppetlabs/pdk
HTML
43
star
51

docs-archive

An archive of old documentation for Puppet, PE, CD4PE, Pipelines, and their related components. No longer updated, for reference only.
HTML
43
star
52

puppet-editor-services

Puppet Language Server for editors
Ruby
41
star
53

puppetlabs-puppet_agent

Module for managing Puppet-Agent
Ruby
40
star
54

packaging

Packaging automation for Puppet software
Ruby
38
star
55

puppetlabs-tomcat

PuppetLabs Tomcat module
Ruby
38
star
56

kream

Kubernetes Rules Everything Around Me. A development environment for the Puppet/kubernetes module
Ruby
38
star
57

rubocop-i18n

RuboCop rules for detecting and autocorrecting undecorated strings for i18n (gettext and rails-i18n)
Ruby
36
star
58

nssm

Puppet fork of the NSSM source code from https://git.nssm.cc/nssm/nssm.git
C++
36
star
59

puppetlabs-java_ks

Uses a combination of keytool and openssl to manage entries in a Java keystore
Ruby
35
star
60

gatling-puppet-load-test

Scala
34
star
61

ruby-hocon

A Ruby port of the Typesafe Config library.
Ruby
34
star
62

netdev_stdlib

Netdev is a vendor-neutral network abstraction framework maintained by Puppet, Inc
Ruby
30
star
63

puppetlabs-sshkeys

Puppet Labs SSH Public Keys
Shell
30
star
64

puppetlabs-peadm

A Puppet module defining Bolt plans used to automate Puppet Enterprise deployments
Puppet
30
star
65

tasks-playground

Deprecated: Please check out https://bolt.guide to learn about Bolt, or see the project at https://github.com/puppetlabs/bolt
Shell
27
star
66

puppetlabs-node_encrypt

Encrypt secrets inside Puppet catalogs and reports
Ruby
27
star
67

structured-logging

Write data structures to your logs from clojure
Clojure
27
star
68

puppet-resource_api

This library provides a simple way to write new native resources for https://puppet.com.
Ruby
27
star
69

vanagon

All of your packages will fit into this van with this one simple trick.
Ruby
26
star
70

puppetlabs-reboot

Reboot type and provider
Ruby
26
star
71

vmfloaty

A CLI helper tool for Puppet vmpooler to help you stay afloat
Ruby
25
star
72

puppetlabs-registry

Puppet Module for managing the Windows Registry through custom types and providers
Ruby
25
star
73

puppet-syntax-emacs

Puppet language syntax highlighting for Emacs
Emacs Lisp
25
star
74

pxp-agent

PCP eXecution Protocol Agent
C++
22
star
75

puppetlabs-acl

ACL (Access Control List) module
Ruby
20
star
76

clj-i18n

Clojure i18n library and utilities
Clojure
20
star
77

puppetlabs-transition

Transition module
Ruby
20
star
78

puppetlabs-sslcertificate

Puppet module to manage SSL Certificates on WIndows Server 2008 and upwards
Ruby
20
star
79

puppetlabs-accounts

Account management module
Ruby
19
star
80

cppbestpractices

Collection of C++ Best Practices at Puppet Labs
C++
19
star
81

clj-kitchensink

Library of utility functions for clojure
Clojure
19
star
82

jvm-ssl-utils

SSL certificate management on the JVM
Clojure
18
star
83

provision

Simple tasks to provision and tear_down containers / instances and virtual machines.
Ruby
18
star
84

net_http_unix

AF_UNIX domain socket support on top of Ruby's Net::HTTP libraries
Ruby
18
star
85

design-system

A resource for creating user interfaces based on brand, UX, and dev principles
JavaScript
18
star
86

puppet-eucalyptus

Install and management tools for Eucalyptus built with Puppet
Puppet
17
star
87

puppet-classify

A ruby library to interface with the classifier service
Ruby
17
star
88

puppetdb-cli

PuppetDB CLI Tooling
Go
16
star
89

puppetlabs-rcfiles

Customizations for vim, shell, screen, ruby, etc... The goal is to quickly provide an efficient working environment.
Vim Script
16
star
90

puppetlabs-motd

Simple motd module
Ruby
16
star
91

relay-core

Kubernetes-based execution engine
Go
16
star
92

trapperkeeper-webserver-jetty9

Trapperkeeper webservice service (jetty9 implementation).
Clojure
16
star
93

clj-http-client

HTTP client library wrapping Apache HttpAsyncClient
Clojure
15
star
94

bolt-examples

Puppet
15
star
95

puppet-vro-starter_content

Shell
15
star
96

facter-ng

Collect and display system facts
Ruby
15
star
97

ruby-pwsh

A ruby gem for interacting with PowerShell
Ruby
14
star
98

puppet-gatling-jenkins-plugin

A Jenkins plugin that extends the gatling library
HTML
14
star
99

cisco_ios

Cisco IOS Catalyst module
Ruby
14
star
100

learn-to-be-a-puppet-engineer

In this repository we map out skills that our PSE should have, we try link to existing documentation or blog posts, or if they don't exist, create it.
CSS
14
star