Add inital puppet rspec test for custom facts

This commit is contained in:
varac 2015-12-07 00:22:50 +01:00
parent d372562329
commit 6b27efb434
6 changed files with 155 additions and 0 deletions

12
.gitignore vendored Normal file
View file

@ -0,0 +1,12 @@
/pkg/
/Gemfile.lock
/vendor/
/spec/fixtures/manifests/*
/spec/fixtures/modules/*
!/spec/fixtures/modules/apt
!/spec/fixtures/modules/apt/*
/.vagrant/
/.bundle/
/coverage/
/.idea/
*.iml

37
Gemfile Normal file
View file

@ -0,0 +1,37 @@
source "https://rubygems.org"
def location_for(place, fake_version = nil)
if place =~ /^(git[:@][^#]*)#(.*)/
[fake_version, { :git => $1, :branch => $2, :require => false }].compact
elsif place =~ /^file:\/\/(.*)/
['>= 0', { :path => File.expand_path($1), :require => false }]
else
[place, { :require => false }]
end
end
group :test do
gem "rake"
gem "rspec", '< 3.2.0'
gem "rspec-puppet"
gem "puppetlabs_spec_helper"
gem "metadata-json-lint"
gem "rspec-puppet-facts"
gem "mocha"
end
facterversion = ENV['GEM_FACTER_VERSION'] || ENV['FACTER_GEM_VERSION']
if facterversion
gem 'facter', *location_for(facterversion)
else
gem 'facter', :require => false
end
puppetversion = ENV['GEM_PUPPET_VERSION'] || ENV['PUPPET_GEM_VERSION']
if puppetversion
gem 'puppet', *location_for(puppetversion)
else
gem 'puppet', :require => false
end

8
README
View file

@ -568,6 +568,14 @@ make sure APT indexes are up-to-date before a package upgrade is
attempted, but don't want "apt-get update" to happen on every Puppet
run.
Tests
=====
To run pupept rspec tests:
bundle install --path vendor/bundle
bundle exec rake spec
Licensing
=========

19
Rakefile Normal file
View file

@ -0,0 +1,19 @@
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"]
desc "Validate manifests, templates, and ruby files"
task :validate do
Dir['manifests/**/*.pp'].each do |manifest|
sh "puppet parser validate --noop #{manifest}"
end
Dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|
sh "ruby -c #{ruby_file}" unless ruby_file =~ /spec\/fixtures/
end
Dir['templates/**/*.erb'].each do |template|
sh "erb -P -x -T '-' #{template} | ruby -c"
end
end
task :test => [:lint, :syntax , :validate, :spec]

12
spec/spec_helper.rb Normal file
View file

@ -0,0 +1,12 @@
# https://puppetlabs.com/blog/testing-modules-in-the-puppet-forge
require 'rspec-puppet'
require 'mocha/api'
RSpec.configure do |c|
c.module_path = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
c.color = true
#Puppet.features.stubs(:root? => true)
end

View file

@ -0,0 +1,67 @@
require "spec_helper"
describe "Facter::Util::Fact" do
before {
Facter.clear
}
describe 'custom facts' do
context 'Debian 7' do
before do
Facter.fact(:operatingsystem).stubs(:value).returns("Debian")
Facter.fact(:operatingsystemrelease).stubs(:value).returns("7.8")
Facter.fact(:lsbdistcodename).stubs(:value).returns("wheezy")
end
it "debian_release = oldstable" do
expect(Facter.fact(:debian_release).value).to eq('oldstable')
end
it "debian_codename = wheezy" do
expect(Facter.fact(:debian_codename).value).to eq('wheezy')
end
end
context 'Debian 8' do
before do
Facter.fact(:operatingsystem).stubs(:value).returns("Debian")
Facter.fact(:operatingsystemrelease).stubs(:value).returns("8.0")
Facter.fact(:lsbdistcodename).stubs(:value).returns("jessie")
end
it "debian_release = stable" do
expect(Facter.fact(:debian_release).value).to eq('stable')
end
it "debian_codename = jessie" do
expect(Facter.fact(:debian_codename).value).to eq('jessie')
end
end
context 'Ubuntu 15.10' do
before do
Facter.fact(:operatingsystem).stubs(:value).returns("Ubuntu")
Facter.fact(:operatingsystemrelease).stubs(:value).returns("15.10")
Facter.fact(:lsbdistcodename).stubs(:value).returns("Vivid")
end
it "ubuntu_codename = Vivid" do
expect(Facter.fact(:ubuntu_codename).value).to eq('Vivid')
end
end
end
describe "Test 'apt_running' fact" do
it "should return true when apt-get is running" do
Facter::Util::Resolution.stubs(:exec).with("pgrep apt-get >/dev/null 2>&1 && echo true || echo false").returns("true")
expect(Facter.fact(:apt_running).value).to eq('true')
end
it "should return false when apt-get is not running" do
Facter::Util::Resolution.stubs(:exec).with("pgrep apt-get >/dev/null 2>&1 && echo true || echo false").returns("false")
expect(Facter.fact(:apt_running).value).to eq('false')
end
end
end