summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Bode <dan@puppetlabs.com>2011-11-14 22:24:01 -0800
committerDan Bode <dan@puppetlabs.com>2011-11-15 22:23:47 -0800
commitb78ee29465a20300aaa675a5867508c958c3896c (patch)
tree1efb8c2d5eb2d6b1290c5aa403e5c23bc7efee87
parentbaae368767b386cbc3e9b2d3fba7c4ea47db081a (diff)
(#10846) add spec tests for ntp class
This commit adds some unit tests for the ntp class. Also adds a rakefile for running the spec tests
-rw-r--r--Rakefile22
-rw-r--r--spec/classes/ntp_spec.rb76
-rw-r--r--spec/spec_helper.rb19
3 files changed, 102 insertions, 15 deletions
diff --git a/Rakefile b/Rakefile
new file mode 100644
index 0000000..cac9471
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rspec/core/rake_task'
+
+task :default do
+ sh %{rake -T}
+end
+
+# Aliases for spec. The (s) versions are used by rvm specs/tests.
+task :test => [:spec]
+task :tests => [:spec]
+task :specs => [:spec]
+
+desc 'Run all RSpec tests'
+RSpec::Core::RakeTask.new(:spec) do |t|
+ t.rspec_opts = ['--color']
+end
+
+desc 'Generate code coverage'
+RSpec::Core::RakeTask.new(:coverage) do |t|
+ t.rcov = true
+ t.rcov_opts = ['--exclude', 'spec']
+end
diff --git a/spec/classes/ntp_spec.rb b/spec/classes/ntp_spec.rb
new file mode 100644
index 0000000..cae253d
--- /dev/null
+++ b/spec/classes/ntp_spec.rb
@@ -0,0 +1,76 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+
+describe 'ntp' do
+
+ def param_value(subject, type, title, param)
+ catalogue.resource(type, title).send(:parameters)[param.to_sym]
+ end
+
+ let(:params) { {:servers => 'fake.pool.ntp.org'} }
+
+ describe 'test platform specific resources' do
+
+ debianish = ['debian', 'ubuntu']
+ redhatish = ['centos', 'redhat', 'oel', 'linux']
+
+ debianish.each do |os|
+ describe "for operating system #{os}" do
+
+ let(:params) {{}}
+ let(:facts) { { :operatingsystem => os } }
+
+ it { should contain_service('ntp').with_name('ntp') }
+ it 'should use the debian ntp servers by default' do
+ content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
+ expected_lines = ['server 0.debian.pool.ntp.org iburst',
+ 'server 1.debian.pool.ntp.org iburst',
+ 'server 2.debian.pool.ntp.org iburst',
+ 'server 3.debian.pool.ntp.org iburst']
+ (content.split("\n") & expected_lines).should == expected_lines
+ end
+ end
+ end
+
+ redhatish.each do |os|
+ describe "for operating system #{os}" do
+
+ let(:params) {{}}
+ let(:facts) { { :operatingsystem => os } }
+
+ it { should contain_service('ntp').with_name('ntpd') }
+ it 'should use the redhat ntp servers by default' do
+ content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
+ expected_lines = [
+ 'server 0.centos.pool.ntp.org',
+ 'server 1.centos.pool.ntp.org',
+ 'server 2.centos.pool.ntp.org']
+ (content.split("\n") & expected_lines).should == expected_lines
+ end
+ end
+ end
+
+ (redhatish + debianish).each do |os|
+ describe "for operating system #{os}" do
+
+ let(:facts) { { :operatingsystem => os } }
+
+ it { should contain_file('/etc/ntp.conf').with_owner('0') }
+ it { should contain_file('/etc/ntp.conf').with_group('0') }
+ it { should contain_file('/etc/ntp.conf').with_mode('0644') }
+ it { should contain_package('ntp').with_ensure('present') }
+ it { should contain_service('ntp').with_ensure('running') }
+ it { should contain_service('ntp').with_hasstatus(true) }
+ it { should contain_service('ntp').with_hasrestart(true) }
+ it 'should allow service ensure to be overridden' do
+ params[:ensure] = 'stopped'
+ subject.should contain_service('ntp').with_ensure('stopped')
+ end
+ it 'should allow package ensure to be overridden' do
+ params[:autoupdate] = true
+ subject.should contain_package('ntp').with_ensure('latest')
+ end
+ end
+ end
+ end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index a4aeeae..7fadc25 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,18 +1,7 @@
-require 'pathname'
-dir = Pathname.new(__FILE__).parent
-$LOAD_PATH.unshift(dir, dir + 'lib', dir + '../lib')
-
-require 'mocha'
require 'puppet'
-gem 'rspec', '=1.2.9'
-require 'spec/autorun'
-
-Spec::Runner.configure do |config|
- config.mock_with :mocha
-end
+require 'rspec-puppet'
-# We need this because the RAL uses 'should' as a method. This
-# allows us the same behaviour but with a different method name.
-class Object
- alias :must :should
+RSpec.configure do |c|
+ puts File.join(File.dirname(__FILE__), '../../')
+ c.module_path = File.join(File.dirname(__FILE__), '../../')
end