d8f2981c8d13acfb3c8e025f79cc79f7d79e6302
[mirror/dsa-puppet.git] / 3rdparty / modules / memcached / spec / classes / memcached_spec.rb
1 require 'spec_helper'
2 describe 'memcached' do
3
4   describe 'with manage_firewall parameter' do
5     ['Debian','RedHat'].each do |osfam|
6       context "on osfamily #{osfam}" do
7         let(:facts) do
8           { :osfamily       => osfam,
9             :memorysize     => '1000 MB',
10             :processorcount => '1',
11           }
12         end
13
14         ['true',true].each do |value|
15           context "set to #{value}" do
16             let(:params) { { :manage_firewall => value } }
17
18             it { should contain_class('memcached') }
19
20             it { should contain_firewall('100_tcp_11211_for_memcached') }
21             it { should contain_firewall('100_udp_11211_for_memcached') }
22           end
23         end
24
25         ['false',false].each do |value|
26           context "set to #{value}" do
27             let(:params) { { :manage_firewall => value } }
28
29             it { should contain_class('memcached') }
30
31             it { should_not contain_firewall('100_tcp_11211_for_memcached') }
32             it { should_not contain_firewall('100_udp_11211_for_memcached') }
33           end
34         end
35
36         context 'set to an invalid type (array)' do
37           let(:params) { { :manage_firewall => ['invalid','type'] } }
38
39           it do
40             expect {
41               should contain_class('memcached')
42             }.to raise_error(Puppet::Error)
43           end
44         end
45       end
46     end
47   end
48
49   let :default_params do
50     {
51       :package_ensure  => 'present',
52       :logfile         => '/var/log/memcached.log',
53       :max_memory      => false,
54       :item_size       => false,
55       :lock_memory     => false,
56       :listen_ip       => '0.0.0.0',
57       :tcp_port        => '11211',
58       :udp_port        => '11211',
59       :user            => 'nobody',
60       :max_connections => '8192',
61       :install_dev     => false,
62       :processorcount  => 1,
63       :use_sasl        => false,
64       :large_mem_pages => false,
65     }
66   end
67
68   [ {},
69     {
70       :package_ensure  => 'latest',
71       :logfile         => '/var/log/memcached.log',
72       :max_memory      => '2',
73       :item_size       => false,
74       :lock_memory     => true,
75       :listen_ip       => '127.0.0.1',
76       :tcp_port        => '11212',
77       :udp_port        => '11213',
78       :user            => 'somebdy',
79       :max_connections => '8193',
80       :verbosity       => 'vvv',
81       :processorcount  => 3,
82       :use_sasl        => true,
83       :large_mem_pages => true,
84     },
85     {
86       :package_ensure  => 'present',
87       :logfile         => '/var/log/memcached.log',
88       :max_memory      => '20%',
89       :lock_memory     => false,
90       :listen_ip       => '127.0.0.1',
91       :tcp_port        => '11212',
92       :udp_port        => '11213',
93       :user            => 'somebdy',
94       :max_connections => '8193',
95       :verbosity       => 'vvv',
96       :install_dev     => true,
97       :processorcount  => 1
98     },
99     {
100       :listen_ip       => '',
101     },
102     {
103       :pidfile         => false,
104     },
105     {
106       :pidfile         => '/var/log/memcached.pid',
107     },
108     {
109       :package_ensure  => 'absent',
110       :install_dev     => true
111     },
112     {
113       :service_manage => false
114     }
115   ].each do |param_set|
116     describe "when #{param_set == {} ? "using default" : "specifying"} class parameters" do
117
118       let :param_hash do
119         default_params.merge(param_set)
120       end
121
122       let :params do
123         param_set
124       end
125
126       ['Debian'].each do |osfamily|
127
128         let :facts do
129           {
130             :osfamily => osfamily,
131             :memorysize => '1000 MB',
132             :processorcount => '1',
133           }
134         end
135
136         describe "on supported osfamily: #{osfamily}" do
137
138           it { should contain_class("memcached::params") }
139
140           it { should contain_package("memcached").with_ensure(param_hash[:package_ensure]) }
141
142           it { should_not contain_firewall('100_tcp_11211_for_memcached') }
143           it { should_not contain_firewall('100_udp_11211_for_memcached') }
144
145           it {
146             if param_hash[:install_dev]
147             should contain_package("libmemcached-dev").with_ensure(param_hash[:package_ensure])
148             end
149           }
150
151           it { should contain_file("/etc/memcached.conf").with(
152             'owner'   => 'root',
153             'group'   => 'root'
154           )}
155
156           it { 
157             if param_hash[:service_manage] == false
158               should_not contain_service('memcached')
159             elsif param_hash[:package_ensure] == 'absent'
160               should contain_service("memcached").with(
161                 'ensure'     => 'stopped',
162                 'enable'     => false
163               )
164             else
165               should contain_service("memcached").with(
166                 'ensure'     => 'running',
167                 'enable'     => true,
168                 'hasrestart' => true,
169                 'hasstatus'  => false
170               )
171             end
172           }
173
174           it 'should compile the template based on the class parameters' do
175             content = param_value(
176               subject,
177               'file',
178               '/etc/memcached.conf',
179               'content'
180             )
181             expected_lines = [
182               "logfile #{param_hash[:logfile]}",
183               "-p #{param_hash[:tcp_port]}",
184               "-U #{param_hash[:udp_port]}",
185               "-u #{param_hash[:user]}",
186               "-c #{param_hash[:max_connections]}",
187               "-t #{param_hash[:processorcount]}"
188             ]
189             if(param_hash[:max_memory])
190               if(param_hash[:max_memory].end_with?('%'))
191                 expected_lines.push("-m 200")
192               else
193                 expected_lines.push("-m #{param_hash[:max_memory]}")
194               end
195             else
196               expected_lines.push("-m 950")
197             end
198             if(param_hash[:listen_ip] != '')
199               expected_lines.push("-l #{param_hash[:listen_ip]}")
200             end
201             if(param_hash[:lock_memory])
202               expected_lines.push("-k")
203             end
204             if(param_hash[:pidfile])
205               expected_lines.push("-P #{param_hash[:pidfile]}")
206             end
207             if(param_hash[:verbosity])
208               expected_lines.push("-vvv")
209             end
210             if(param_hash[:use_sasl])
211               expected_lines.push("-S")
212             end
213             if(param_hash[:large_mem_pages])
214               expected_lines.push("-L")
215             end
216             (content.split("\n") & expected_lines).should =~ expected_lines
217           end
218         end
219       end
220       ['Redhat'].each do |osfamily|
221         describe 'on supported platform' do
222           it 'should fail' do
223
224           end
225         end
226       end
227     end
228   end
229 end
230
231 # vim: expandtab shiftwidth=2 softtabstop=2