Skip to content

Commit 162ed65

Browse files
author
Dan Bode
committed
Add unit tests for memcached manifest
Also adds the standard module Rakefile, a spec_helper for rspec-puppet, and a README to help setup the tests.
1 parent 4a1970a commit 162ed65

File tree

4 files changed

+131
-13
lines changed

4 files changed

+131
-13
lines changed

README-DEVELOPER

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
In order to run tests:
2+
- puppet and facter must be installed and available in Ruby's LOADPATH
3+
- the latest revision of rspec-puppet must be installed
4+
- rake, and rspec2 must be install
5+
6+
- the name of the module directory needs to be memcached
7+
8+
to run all tests:
9+
rake spec

Rakefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'rake'
2+
3+
task :default => [:spec]
4+
5+
desc "Run all module spec tests (Requires rspec-puppet gem)"
6+
task :spec do
7+
system("rspec spec/**/*_spec.rb")
8+
end
9+
10+
desc "Build package"
11+
task :build do
12+
system("puppet-module build")
13+
end
14+

spec/classes/memcached_spec.rb

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
require 'spec_helper'
2+
describe 'memcached' do
3+
4+
let :default_params do
5+
{
6+
:package_ensure => 'present',
7+
:logfile => '/var/log/memcached.log',
8+
:max_memory => false,
9+
:listen_ip => '0.0.0.0',
10+
:tcp_port => '11211',
11+
:udp_port => '11211',
12+
:user => 'nobody',
13+
:max_connections => '8192'
14+
}
15+
end
16+
17+
[ {},
18+
{
19+
:package_ensure => 'latest',
20+
:logfile => '/var/log/memcached.log',
21+
:max_memory => '2',
22+
:listen_ip => '127.0.0.1',
23+
:tcp_port => '11212',
24+
:udp_port => '11213',
25+
:user => 'somebdy',
26+
:max_connections => '8193'
27+
}
28+
].each do |param_set|
29+
describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
30+
31+
let :param_hash do
32+
default_params.merge(param_set)
33+
end
34+
35+
let :params do
36+
param_set
37+
end
38+
39+
['Debian', 'Ubuntu'].each do |os|
40+
41+
let :facts do
42+
{
43+
:operatingsystem => os,
44+
:memorysize => '1',
45+
:processorcount => '1',
46+
}
47+
end
48+
49+
describe "on supported operatingsystem: #{os}" do
50+
51+
it { should contain_class('memcached::params') }
52+
53+
it { should contain_package('memcached').with_ensure(param_hash[:package_ensure]) }
54+
55+
it { should contain_file('/etc/memcached.conf').with(
56+
'owner' => 'root',
57+
'group' => 'root'
58+
)}
59+
60+
it { should contain_service('memcached').with(
61+
'ensure' => 'running',
62+
'enable' => true,
63+
'hasrestart' => true,
64+
'hasstatus' => false
65+
)}
66+
67+
it 'should compile the template based on the class parameters' do
68+
content = param_value(
69+
subject,
70+
'file',
71+
'/etc/memcached.conf',
72+
'content'
73+
)
74+
expected_lines = [
75+
"logfile #{param_hash[:logfile]}",
76+
"-l #{param_hash[:listen_ip]}",
77+
"-p #{param_hash[:tcp_port]}",
78+
"-U #{param_hash[:udp_port]}",
79+
"-u #{param_hash[:user]}",
80+
"-c #{param_hash[:max_connections]}",
81+
"-t #{facts[:processorcount]}"
82+
]
83+
if(param_hash[:max_memory])
84+
expected_lines.push("-m #{param_hash[:max_memory]}")
85+
else
86+
expected_lines.push("-m #{((facts[:memorysize].to_f*1024)*0.95).floor}")
87+
end
88+
(content.split("\n") & expected_lines).should =~ expected_lines
89+
end
90+
end
91+
end
92+
['Redhat'].each do |os|
93+
describe 'on supported platform' do
94+
it 'should fail' do
95+
96+
end
97+
end
98+
end
99+
end
100+
end
101+
end

spec/spec_helper.rb

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
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'
2+
require 'rubygems'
3+
require 'rspec-puppet'
94

10-
Spec::Runner.configure do |config|
11-
config.mock_with :mocha
5+
# get the value of a certain parameter
6+
def param_value(subject, type, title, param)
7+
subject.resource(type, title).send(:parameters)[param.to_sym]
128
end
139

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
10+
RSpec.configure do |c|
11+
c.module_path = File.join(File.dirname(__FILE__), '../../')
1812
end

0 commit comments

Comments
 (0)