Skip to content

Commit

Permalink
Add auto generated spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Gazeley committed Nov 19, 2015
1 parent 7e33cb4 commit 6c1f805
Show file tree
Hide file tree
Showing 32 changed files with 1,672 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .bundle/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
BUNDLE_DISABLE_SHARED_GEMS: '1'
8 changes: 8 additions & 0 deletions .fixtures.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fixtures:
symlinks:
freeradius: "#{source_dir}"
repositories:
stdlib:
repo: https://github.com/puppetlabs/puppetlabs-stdlib.git
ref: 4.3.2
forge_modules:
8 changes: 8 additions & 0 deletions .puppet-lint.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Use this file to instruct puppet-lint to ignore certain checks.
# For the complete list of checks and flags to disable them,
# refer to <http://puppet-lint.com/checks/>.

# Examples (uncomment before use):
#--no-80chars-check
#--no-class_inherits_from_params_class-check
#--no-inherits_across_namespaces-check
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
branches:
script: "bundle exec rake spec SPEC_OPTS='--format documentation'"
bundler_args: --without development

rvm:
- 1.9.3
- 2.0.0
- 2.1.5
- 2.2.0
env:
matrix:
- PUPPET_GEM_VERSION="~> 3.2"
- PUPPET_GEM_VERSION="~> 3.7"

27 changes: 27 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
source "https://rubygems.org"

group :test do
gem "rake"
gem "puppet", ENV['PUPPET_VERSION'] || '~> 3.8.3'
gem "rspec-puppet", :git => 'https://github.com/rodjek/rspec-puppet.git'
gem "puppetlabs_spec_helper"
gem 'rspec-puppet-utils', :git => 'https://github.com/Accuity/rspec-puppet-utils.git'
gem 'hiera-puppet-helper', :git => 'https://github.com/bobtfish/hiera-puppet-helper.git'
gem "metadata-json-lint"
gem 'puppet-syntax'
gem 'puppet-lint'
end

group :integration do
gem "beaker", :git => 'https://github.com/puppetlabs/beaker.git'
gem "beaker-rspec", :git => 'https://github.com/puppetlabs/beaker-rspec.git'
gem "vagrant-wrapper"
gem 'serverspec'
end

group :development do
gem "travis"
gem "travis-lint"
gem "puppet-blacksmith"
gem "guard-rake"
end
76 changes: 65 additions & 11 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,72 @@
require 'rubygems'
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'
PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]
require 'puppet-syntax/tasks/puppet-syntax'

desc "Validate manifests, templates, and ruby files"
task :validate do
Dir['manifests/**/*.pp'].each do |manifest|
sh "puppet parser validate --noop #{manifest}"
# These two gems aren't always present, for instance
# on Travis with --without development
begin
require 'puppet_blacksmith/rake_tasks'
Blacksmith::RakeTask.new do |t|
t.tag_pattern = "v%s" # Use a custom pattern with git tag. %s is replaced with the version number.
end
Dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|
sh "ruby -c #{ruby_file}" unless ruby_file =~ /spec\/fixtures/
rescue LoadError
end

PuppetLint.configuration.relative = true
PuppetLint.configuration.send("disable_80chars")
PuppetLint.configuration.log_format = "%{path}:%{linenumber}:%{check}:%{KIND}:%{message}"
PuppetLint.configuration.fail_on_warnings = true

# Forsake support for Puppet 2.6.2 for the benefit of cleaner code.
# http://puppet-lint.com/checks/class_parameter_defaults/
PuppetLint.configuration.send('disable_class_parameter_defaults')
# http://puppet-lint.com/checks/class_inherits_from_params_class/
PuppetLint.configuration.send('disable_class_inherits_from_params_class')

exclude_paths = [
"pkg/**/*",
"vendor/**/*",
"spec/**/*",
]
PuppetLint.configuration.ignore_paths = exclude_paths
PuppetSyntax.exclude_paths = exclude_paths

task :metadata do
sh "metadata-json-lint metadata.json"
end

desc "Run syntax, lint, and spec tests."
task :test => [
:syntax,
:lint,
:spec,
:metadata,
]
def io_popen(command)
IO.popen(command) do |io|
io.each do |line|
print line
yield line if block_given?
end
end
Dir['templates/**/*.erb'].each do |template|
sh "erb -P -x -T '-' #{template} | ruby -c"
end

desc 'Vagrant VM power up and provision'
task :vagrant_up, [:manifest, :hostname] do |t, args|
args.with_defaults(:manifest => 'init.pp', :hostname => '')
Rake::Task['spec_prep'].execute
ENV['VAGRANT_MANIFEST'] = args[:manifest]
provision = false
io_popen("vagrant up #{args[:hostname]}") do |line|
provision = true if line =~ /is already running./
end
io_popen("vagrant provision #{args[:hostname]}") if provision
end

# Cleanup vagrant environment
desc 'Vagrant VM shutdown and fixtures cleanup'
task :vagrant_destroy do
Rake::Task['spec_prep'].execute
`vagrant destroy -f`
Rake::Task['spec_clean'].execute
end
95 changes: 95 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
def vm(opt)
module_name = opt.fetch(:module).to_s || raise(ArgumentError, 'Must provide puppet module name')
hostname = opt.fetch(:hostname, module_name).to_s
memory = opt.fetch(:memory, 512)
cpu = opt.fetch(:cpu, 1)
box = opt.fetch(:box).to_s || raise(ArgumentError, 'Must provide box type.')
url = opt.fetch(:url, '').to_s
os_type = opt[:os_type] || opt[:type] || :linux
gui = opt.fetch(:gui, false)
ports = Array(opt.fetch(:port, []))
iso = opt.fetch(:iso, nil)
proj_root = File.expand_path(File.join(File.dirname(__FILE__)))
fixture_modules = File.join(proj_root, 'spec', 'fixtures', 'modules')

Vagrant.configure('2') do |conf|

# forward all the ports
ports.each do |p|
conf.vm.network(:forwarded_port, guest: p, host: p, auto_correct: true)
end

if os_type == :windows
conf.ssh.username = 'vagrant'
conf.winrm.username = 'vagrant'
conf.winrm.password = 'vagrant'
end

conf.vm.define hostname.to_sym do |mod|
mod.vm.box = box
mod.vm.box_url = url

if os_type == :windows
mod.vm.guest = :windows
mod.vm.communicator = 'winrm'
mod.vm.synced_folder './' , "/ProgramData/PuppetLabs/puppet/etc/modules/#{module_name}"
mod.vm.synced_folder 'spec/fixtures/modules' , '/temp/modules'
else
mod.vm.synced_folder './', "/etc/puppet/modules/#{module_name}"
mod.vm.synced_folder 'spec/fixtures/modules', '/tmp/puppet/modules'
end

mod.vm.hostname = hostname

mod.vm.provider :vmware_fusion do |f|
f.gui = gui
f.vmx['displayName'] = hostname
f.vmx['memsize'] = memory
f.vmx['numvcpus'] = cpu
if iso
f.vmx['ide1:0.devicetype'] = "cdrom-image"
f.vmx['ide1:0.filename'] = iso
end
end

mod.vm.provider :vmware_workstation do |f|
f.gui = gui
f.vmx['displayName'] = hostname
f.vmx['memsize'] = memory
f.vmx['numvcpus'] = cpu
if iso
f.vmx['ide1:0.devicetype'] = "cdrom-image"
f.vmx['ide1:0.filename'] = iso
end
end

mod.vm.provider :virtualbox do |v|
v.gui = gui
v.name = hostname
v.memory = memory
v.cpus = cpu
end

if os_type == :windows
manifest = ENV['VAGRANT_MANIFEST'] || 'init.pp'
#mod.vm.provision :shell, :inline => "@powershell -NoProfile -ExecutionPolicy Bypass -Command \"iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))\" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
#mod.vm.provision :shell, :inline => "choco install puppet"
mod.vm.provision :shell, :inline => "puppet apply --modulepath 'C:/ProgramData/PuppetLabs/puppet/etc/modules;C:/temp/modules' --verbose C:/ProgramData/PuppetLabs/puppet/etc/modules/#{module_name}/tests/#{manifest}"
else
mod.vm.provision :puppet do |p|
p.manifests_path = 'tests'
# p.hiera_config_path = File.join(fixture_modules, 'hieradata', 'hiera.yaml')
p.manifest_file = ENV['VAGRANT_MANIFEST'] || 'init.pp'
#p.module_path = fixture_modules
# because of how symlinks are handled via the spec_helper we are forced to mount the modules is different locations
# otherwise we could just use the above option
p.options = '--modulepath="/etc/puppet/modules:/tmp/puppet/modules"'
end
end
end
end
end
module_name = File.basename(File.expand_path(File.join(File.dirname(__FILE__))))
vm :hostname => 'win2012r2', :module => module_name, :box => 'opentable/win-2012r2-standard-amd64-nocm', :url => 'opentable/win-2012r2-standard-amd64-nocm', :os_type => :windows, :cpu => 1, :memory => 4096, :gui => true
vm :hostname => 'win2008r2', :module => module_name, :box => 'opentable/win-2008r2-standard-amd64-nocm', :url => 'opentable/win-2008r2-standard-amd64-nocm', :os_type => :windows, :cpu => 1, :memory => 4096, :gui => true
vm :hostname => 'centos6', :module => module_name, :box => 'puppetlabs/centos-6.6-64-puppet', :url => 'puppetlabs/centos-6.6-64-puppet', :cpu => 1, :memory => 2048, :gui => false
36 changes: 36 additions & 0 deletions spec/classes/control_socket_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'
require 'shared_contexts'

describe 'freeradius::control_socket' do
# by default the hiera integration uses hiera data from the shared_contexts.rb file
# but basically to mock hiera you first need to add a key/value pair
# to the specific context in the spec/shared_contexts.rb file
# Note: you can only use a single hiera context per describe/context block
# rspec-puppet does not allow you to swap out hiera data on a per test block
#include_context :hiera


# below is the facts hash that gives you the ability to mock
# facts on a per describe/context block. If you use a fact in your
# manifest you should mock the facts below.
let(:facts) do
{}
end
# below is a list of the resource parameters that you can override.
# By default all non-required parameters are commented out,
# while all required parameters will require you to add a value
let(:params) do
{
#:mode => "ro",
}
end
# add these two lines in a single test block to enable puppet and hiera debug mode
# Puppet::Util::Log.level = :debug
# Puppet::Util::Log.newdestination(:console)
it do
is_expected.to contain_freeradius__site('control-socket')
.with(
'content' => 'template(freeradius/sites-enabled/control-socket.erb)'
)
end
end
Loading

0 comments on commit 6c1f805

Please sign in to comment.