Puppet Development Kit (PDK): Create Modules, Classes and Run Validation and Tests
What Is the PDK?
The Puppet Development Kit (PDK) is a command-line toolkit that creates modules with a standard layout and gives you the same tools for linting and tests that Puppet professionals use. It bundles Ruby, Puppet, rspec-puppet, puppet-lint, rubocop and more, so nothing else needs to be installed.
Install the PDK
bash
# Debian / Ubuntu (after adding the Puppet tools repo from the Puppet download page)
sudo apt-get install pdk
# RHEL family
sudo dnf install pdk
# macOS
brew install --cask pdk
# Windows
choco install pdk
pdk --versio
Check the install page: The package name and repository URL change between releases. Use the current PDK installation instructions from the official Puppet documentation for your OS.
Create a Module
bash
pdk new module ntp
# asks: module name, author, licence, supported operating systems
cd ntp
tree -L 2
text
tp/
metadata.json
.pdk-version, .sync.yml, .fixtures.yml, .gitignore
Gemfile, Rakefile
manifests/ files/ templates/
spec/ default_facts.yml spec_helper.rb classes/ defines/
.github/ or .gitlab-ci.yml CI templates (depending on settings)
Generate Code with Templates
bash
pdk new class ntp # manifests/init.pp + spec/classes/ntp_spec.rb
pdk new class ntp::config # manifests/config.pp + spec
pdk new defined_type vhost # manifests/vhost.pp + spec/defines/vhost_spec.rb
pdk new fact my_fact # lib/facter/my_fact.rb + spec
pdk new function my_func # functions/my_func.pp or lib/puppet/functions/
pdk new task restart_app # tasks/restart_app.json + .sh
pdk new provider / pdk new type # custom type and provider
pdk new test --unit ntp::install # add a test file for an existing class
Each generator creates the code file and a matching test skeleton, which encourages writing tests from the start.
Validate
bash
pdk validate # runs everything below
pdk validate puppet # puppet parser validate + puppet-lint
pdk validate metadata # metadata.json against the schema
pdk validate ruby # rubocop for Ruby code
pdk validate yaml
pdk validate tasks
pdk validate puppet --auto-correct # fix simple style issues automatically
| Check | Finds |
| Parser validation | Syntax errors in .pp files |
| puppet-lint | Style issues: double-quoted strings without variables, missing quotes on modes, arrow alignment |
| Metadata | Invalid metadata.json, missing fields |
| Rubocop | Style errors in Ruby facts and functions |
Unit Tests with rspec-puppet
ruby
# spec/classes/ntp_spec.rb
require 'spec_helper'
describe 'ntp' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_package('chrony').with_ensure('installed') }
it { is_expected.to contain_service('chronyd').with(ensure: 'running', enable: true) }
it { is_expected.to contain_file('/etc/chrony.conf').that_notifies('Service[chronyd]') }
context 'with custom servers' do
let(:params) { { servers: ['time.example.com'] } }
it { is_expected.to contain_file('/etc/chrony.conf').with_content(%r{time\.example\.com}) }
end
end
end
end
bash
pdk test unit # run all unit tests (downloads fixtures the first time)
pdk test unit --tests=spec/classes/ntp_spec.rb
pdk test unit --list
pdk test unit --verbose
pdk bundle exec rake spec
What compile.with_all_deps means: The catalog compiles, and every resource and relationship refers to something that exists. This one line catches most mistakes, such as a missing require target.
Fixtures
yaml
# .fixtures.yml: modules your module needs during tests
fixtures:
forge_modules:
stdlib: puppetlabs/stdlib
concat: puppetlabs/concat
symlinks:
ntp: "#{source_dir}"
Convert an Existing Module
bash
cd existing_module
pdk convert # add PDK files and templates, show a diff first
pdk update # update PDK template files to the newest versio
Build and Publish
bash
pdk build # creates pkg/myorg-ntp-1.2.0.tar.gz
pdk release prep # bump version, changelog, docs
pdk release publish --forge-token $TOKEN
VS Code Extension
The Puppet VS Code extension offers syntax highlighting, auto-complete, hover help, a puppet-lint integration, and pdk commands in the command palette. Install it from the VS Code marketplace.
Use the PDK in CI
yaml
# .gitlab-ci.yml snippet
validate:
image: puppet/pdk:latest
script:
- pdk validate
unit:
image: puppet/pdk:latest
script:
- pdk test unit
Practice Tasks
- Create a module with
pdk new module and generate a class, a defined type and a fact
- Run
pdk validate and fix every warning
- Write three rspec-puppet examples for your class and run
pdk test unit
- Add a failing test, see the error, and fix the code
- Add a CI job that runs
pdk validate and pdk test unit
Conclusion
The PDK gives every module the same layout, validation and test tools. Use pdk new to scaffold, pdk validate for style and syntax, and pdk test unit with compile.with_all_deps to catch problems before deployment.
Quick Summary: pdk new module/class; pdk validate; pdk test unit; rspec-puppet; .fixtures.yml; pdk build/release.