Skip to content

Commit 05c580b

Browse files
committed
Merge pull request #6 from bodepd/10846_add_unit_tests
(#10846) add spec tests for ntp class
2 parents baae368 + f52e93c commit 05c580b

File tree

3 files changed

+101
-15
lines changed

3 files changed

+101
-15
lines changed

Rakefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'rake'
2+
require 'rspec/core/rake_task'
3+
4+
task :default do
5+
sh %{rake -T}
6+
end
7+
8+
# Aliases for spec. The (s) versions are used by rvm specs/tests.
9+
task :test => [:spec]
10+
task :tests => [:spec]
11+
task :specs => [:spec]
12+
13+
desc 'Run all RSpec tests'
14+
RSpec::Core::RakeTask.new(:spec) do |t|
15+
t.rspec_opts = ['--color']
16+
end
17+
18+
desc 'Generate code coverage'
19+
RSpec::Core::RakeTask.new(:coverage) do |t|
20+
t.rcov = true
21+
t.rcov_opts = ['--exclude', 'spec']
22+
end

spec/classes/ntp_spec.rb

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env rspec
2+
require 'spec_helper'
3+
4+
describe 'ntp' do
5+
6+
def param_value(subject, type, title, param)
7+
catalogue.resource(type, title).send(:parameters)[param.to_sym]
8+
end
9+
10+
let(:params) { {:servers => 'fake.pool.ntp.org'} }
11+
12+
describe 'test platform specific resources' do
13+
14+
debianish = ['debian', 'ubuntu']
15+
redhatish = ['centos', 'redhat', 'oel', 'linux']
16+
17+
debianish.each do |os|
18+
describe "for operating system #{os}" do
19+
20+
let(:params) {{}}
21+
let(:facts) { { :operatingsystem => os } }
22+
23+
it { should contain_service('ntp').with_name('ntp') }
24+
it 'should use the debian ntp servers by default' do
25+
content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
26+
expected_lines = ['server 0.debian.pool.ntp.org iburst',
27+
'server 1.debian.pool.ntp.org iburst',
28+
'server 2.debian.pool.ntp.org iburst',
29+
'server 3.debian.pool.ntp.org iburst']
30+
(content.split("\n") & expected_lines).should == expected_lines
31+
end
32+
end
33+
end
34+
35+
redhatish.each do |os|
36+
describe "for operating system #{os}" do
37+
38+
let(:params) {{}}
39+
let(:facts) { { :operatingsystem => os } }
40+
41+
it { should contain_service('ntp').with_name('ntpd') }
42+
it 'should use the redhat ntp servers by default' do
43+
content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
44+
expected_lines = [
45+
'server 0.centos.pool.ntp.org',
46+
'server 1.centos.pool.ntp.org',
47+
'server 2.centos.pool.ntp.org']
48+
(content.split("\n") & expected_lines).should == expected_lines
49+
end
50+
end
51+
end
52+
53+
(redhatish + debianish).each do |os|
54+
describe "for operating system #{os}" do
55+
56+
let(:facts) { { :operatingsystem => os } }
57+
58+
it { should contain_file('/etc/ntp.conf').with_owner('0') }
59+
it { should contain_file('/etc/ntp.conf').with_group('0') }
60+
it { should contain_file('/etc/ntp.conf').with_mode('0644') }
61+
it { should contain_package('ntp').with_ensure('present') }
62+
it { should contain_service('ntp').with_ensure('running') }
63+
it { should contain_service('ntp').with_hasstatus(true) }
64+
it { should contain_service('ntp').with_hasrestart(true) }
65+
it 'should allow service ensure to be overridden' do
66+
params[:ensure] = 'stopped'
67+
subject.should contain_service('ntp').with_ensure('stopped')
68+
end
69+
it 'should allow package ensure to be overridden' do
70+
params[:autoupdate] = true
71+
subject.should contain_package('ntp').with_ensure('latest')
72+
end
73+
end
74+
end
75+
end
76+
end

spec/spec_helper.rb

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
require 'pathname'
2-
dir = Pathname.new(__FILE__).parent
3-
$LOAD_PATH.unshift(dir, dir + 'lib', dir + '../lib')
4-
5-
require 'mocha'
61
require 'puppet'
7-
gem 'rspec', '=1.2.9'
8-
require 'spec/autorun'
9-
10-
Spec::Runner.configure do |config|
11-
config.mock_with :mocha
12-
end
2+
require 'rspec-puppet'
133

14-
# We need this because the RAL uses 'should' as a method. This
15-
# allows us the same behaviour but with a different method name.
16-
class Object
17-
alias :must :should
4+
RSpec.configure do |c|
5+
c.module_path = File.join(File.dirname(__FILE__), '../../')
186
end

0 commit comments

Comments
 (0)