PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/puphpet/puppet/modules/nginx/spec/defines/resource_mailhost_spec.rb

https://github.com/innesm4/Environment-setup
Ruby | 401 lines | 372 code | 29 blank | 0 comment | 3 complexity | 9674a9925688f0c5b02a9aa0daf31940 MD5 | raw file
Possible License(s): Apache-2.0
  1. require 'spec_helper'
  2. describe 'nginx::resource::mailhost' do
  3. let :title do
  4. 'www.rspec.example.com'
  5. end
  6. let :facts do
  7. {
  8. :osfamily => 'debian',
  9. :operatingsystem => 'debian',
  10. :ipaddress6 => '::',
  11. }
  12. end
  13. let :default_params do
  14. {
  15. :listen_port => 25,
  16. :ipv6_enable => true,
  17. }
  18. end
  19. let :pre_condition do
  20. [
  21. 'include ::nginx::config',
  22. ]
  23. end
  24. describe 'os-independent items' do
  25. describe 'basic assumptions' do
  26. let :params do default_params end
  27. it { should contain_class("nginx::config") }
  28. it { should contain_concat("/etc/nginx/conf.mail.d/#{title}.conf").with({
  29. 'owner' => 'root',
  30. 'group' => 'root',
  31. 'mode' => '0644',
  32. })}
  33. it { should contain_concat__fragment("#{title}-header") }
  34. it { should_not contain_concat__fragment("#{title}-ssl") }
  35. end
  36. describe "mailhost template content" do
  37. [
  38. {
  39. :title => 'should set the IPv4 listen IP',
  40. :attr => 'listen_ip',
  41. :value => '127.0.0.1',
  42. :match => ' listen 127.0.0.1:25;',
  43. },
  44. {
  45. :title => 'should set the IPv4 listen port',
  46. :attr => 'listen_port',
  47. :value => 45,
  48. :match => ' listen *:45;',
  49. },
  50. {
  51. :title => 'should set the IPv4 listen options',
  52. :attr => 'listen_options',
  53. :value => 'spdy default',
  54. :match => ' listen *:25 spdy default;',
  55. },
  56. {
  57. :title => 'should enable IPv6',
  58. :attr => 'ipv6_enable',
  59. :value => true,
  60. :match => ' listen [::]:80 default ipv6only=on;',
  61. },
  62. {
  63. :title => 'should not enable IPv6',
  64. :attr => 'ipv6_enable',
  65. :value => false,
  66. :notmatch => / listen \[::\]:80 default ipv6only=on;/,
  67. },
  68. {
  69. :title => 'should set the IPv6 listen IP',
  70. :attr => 'ipv6_listen_ip',
  71. :value => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  72. :match => ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80 default ipv6only=on;',
  73. },
  74. {
  75. :title => 'should set the IPv6 listen port',
  76. :attr => 'ipv6_listen_port',
  77. :value => 45,
  78. :match => ' listen [::]:45 default ipv6only=on;',
  79. },
  80. {
  81. :title => 'should set the IPv6 listen options',
  82. :attr => 'ipv6_listen_options',
  83. :value => 'spdy',
  84. :match => ' listen [::]:80 spdy;',
  85. },
  86. {
  87. :title => 'should set servername(s)',
  88. :attr => 'server_name',
  89. :value => ['name1','name2'],
  90. :match => ' server_name name1 name2;',
  91. },
  92. {
  93. :title => 'should set protocol',
  94. :attr => 'protocol',
  95. :value => 'test-protocol',
  96. :match => ' protocol test-protocol;',
  97. },
  98. {
  99. :title => 'should set xclient',
  100. :attr => 'xclient',
  101. :value => 'test-xclient',
  102. :match => ' xclient test-xclient;',
  103. },
  104. {
  105. :title => 'should set auth_http',
  106. :attr => 'auth_http',
  107. :value => 'test-auth_http',
  108. :match => ' auth_http test-auth_http;',
  109. },
  110. {
  111. :title => 'should set starttls',
  112. :attr => 'starttls',
  113. :value => 'on',
  114. :match => ' starttls on;',
  115. },
  116. {
  117. :title => 'should set starttls',
  118. :attr => 'starttls',
  119. :value => 'only',
  120. :match => ' starttls only;',
  121. },
  122. {
  123. :title => 'should not enable SSL',
  124. :attr => 'starttls',
  125. :value => 'off',
  126. :notmatch => / ssl_session_timeout 5m;/,
  127. },
  128. ].each do |param|
  129. context "when #{param[:attr]} is #{param[:value]}" do
  130. let :default_params do {
  131. :listen_port => 25,
  132. :ipv6_enable => true,
  133. :ssl_cert => 'dummy.crt',
  134. :ssl_key => 'dummy.key',
  135. } end
  136. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  137. it { should contain_concat__fragment("#{title}-header") }
  138. it param[:title] do
  139. lines = subject.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n")
  140. (lines & Array(param[:match])).should == Array(param[:match])
  141. Array(param[:notmatch]).each do |item|
  142. should contain_concat__fragment("#{title}-header").without_content(item)
  143. end
  144. end
  145. end
  146. end
  147. end
  148. describe "mailhost template content (SSL enabled)" do
  149. [
  150. {
  151. :title => 'should enable SSL',
  152. :attr => 'starttls',
  153. :value => 'on',
  154. :match => ' ssl_session_timeout 5m;',
  155. },
  156. {
  157. :title => 'should enable SSL',
  158. :attr => 'starttls',
  159. :value => 'only',
  160. :match => ' ssl_session_timeout 5m;',
  161. },
  162. {
  163. :title => 'should not enable SSL',
  164. :attr => 'starttls',
  165. :value => 'off',
  166. :notmatch => / ssl_session_timeout 5m;/,
  167. },
  168. {
  169. :title => 'should set ssl_certificate',
  170. :attr => 'ssl_cert',
  171. :value => 'test-ssl-cert',
  172. :match => ' ssl_certificate test-ssl-cert;',
  173. },
  174. {
  175. :title => 'should set ssl_certificate_key',
  176. :attr => 'ssl_key',
  177. :value => 'test-ssl-cert-key',
  178. :match => ' ssl_certificate_key test-ssl-cert-key;',
  179. },
  180. ].each do |param|
  181. context "when #{param[:attr]} is #{param[:value]}" do
  182. let :default_params do {
  183. :listen_port => 25,
  184. :starttls => 'on',
  185. :ssl_cert => 'dummy.crt',
  186. :ssl_key => 'dummy.key',
  187. } end
  188. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  189. it { should contain_concat__fragment("#{title}-header") }
  190. it param[:title] do
  191. lines = subject.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n")
  192. (lines & Array(param[:match])).should == Array(param[:match])
  193. Array(param[:notmatch]).each do |item|
  194. should contain_concat__fragment("#{title}-header").without_content(item)
  195. end
  196. end
  197. end
  198. end
  199. end
  200. describe "mailhost_ssl template content" do
  201. [
  202. {
  203. :title => 'should set the IPv4 SSL listen port',
  204. :attr => 'ssl_port',
  205. :value => '45',
  206. :match => ' listen 45;',
  207. },
  208. {
  209. :title => 'should enable IPv6',
  210. :attr => 'ipv6_enable',
  211. :value => true,
  212. :match => ' listen [::]:80 default ipv6only=on;',
  213. },
  214. {
  215. :title => 'should not enable IPv6',
  216. :attr => 'ipv6_enable',
  217. :value => false,
  218. :notmatch => / listen \[::\]:80 default ipv6only=on;/,
  219. },
  220. {
  221. :title => 'should set the IPv6 listen IP',
  222. :attr => 'ipv6_listen_ip',
  223. :value => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  224. :match => ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80 default ipv6only=on;',
  225. },
  226. {
  227. :title => 'should set the IPv6 listen port',
  228. :attr => 'ipv6_listen_port',
  229. :value => 45,
  230. :match => ' listen [::]:45 default ipv6only=on;',
  231. },
  232. {
  233. :title => 'should set the IPv6 listen options',
  234. :attr => 'ipv6_listen_options',
  235. :value => 'spdy',
  236. :match => ' listen [::]:80 spdy;',
  237. },
  238. {
  239. :title => 'should set servername(s)',
  240. :attr => 'server_name',
  241. :value => ['name1','name2'],
  242. :match => ' server_name name1 name2;',
  243. },
  244. {
  245. :title => 'should set protocol',
  246. :attr => 'protocol',
  247. :value => 'test-protocol',
  248. :match => ' protocol test-protocol;',
  249. },
  250. {
  251. :title => 'should set xclient',
  252. :attr => 'xclient',
  253. :value => 'test-xclient',
  254. :match => ' xclient test-xclient;',
  255. },
  256. {
  257. :title => 'should set auth_http',
  258. :attr => 'auth_http',
  259. :value => 'test-auth_http',
  260. :match => ' auth_http test-auth_http;',
  261. },
  262. {
  263. :title => 'should set ssl_certificate',
  264. :attr => 'ssl_cert',
  265. :value => 'test-ssl-cert',
  266. :match => ' ssl_certificate test-ssl-cert;',
  267. },
  268. {
  269. :title => 'should set ssl_certificate_key',
  270. :attr => 'ssl_key',
  271. :value => 'test-ssl-cert-key',
  272. :match => ' ssl_certificate_key test-ssl-cert-key;',
  273. },
  274. ].each do |param|
  275. context "when #{param[:attr]} is #{param[:value]}" do
  276. let :default_params do {
  277. :listen_port => 25,
  278. :ipv6_enable => true,
  279. :ssl => true,
  280. :ssl_cert => 'dummy.crt',
  281. :ssl_key => 'dummy.key',
  282. } end
  283. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  284. it { should contain_concat__fragment("#{title}-ssl") }
  285. it param[:title] do
  286. lines = subject.resource('concat::fragment', "#{title}-ssl").send(:parameters)[:content].split("\n")
  287. (lines & Array(param[:match])).should == Array(param[:match])
  288. Array(param[:notmatch]).each do |item|
  289. should contain_concat__fragment("#{title}-ssl").without_content(item)
  290. end
  291. end
  292. end
  293. end
  294. end
  295. context 'attribute resources' do
  296. context "SSL cert missing and ssl => true" do
  297. let :params do default_params.merge({
  298. :ssl => true,
  299. :ssl_key => 'key',
  300. }) end
  301. it { expect { should contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) }
  302. end
  303. context "SSL key missing and ssl => true" do
  304. let :params do default_params.merge({
  305. :ssl => true,
  306. :ssl_cert => 'cert',
  307. }) end
  308. it { expect { should contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) }
  309. end
  310. context "SSL cert missing and starttls => 'on'" do
  311. let :params do default_params.merge({
  312. :starttls => 'on',
  313. :ssl_key => 'key',
  314. }) end
  315. it { expect { should contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) }
  316. end
  317. context "SSL key missing and starttls => 'on'" do
  318. let :params do default_params.merge({
  319. :starttls => 'on',
  320. :ssl_cert => 'cert',
  321. }) end
  322. it { expect { should contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) }
  323. end
  324. context "SSL cert missing and starttls => 'only'" do
  325. let :params do default_params.merge({
  326. :starttls => 'only',
  327. :ssl_key => 'key',
  328. }) end
  329. it { expect { should contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) }
  330. end
  331. context "SSL key missing and starttls => 'only'" do
  332. let :params do default_params.merge({
  333. :starttls => 'only',
  334. :ssl_cert => 'cert',
  335. }) end
  336. it { expect { should contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) }
  337. end
  338. context 'when listen_port != ssl_port' do
  339. let :params do default_params.merge({
  340. :listen_port => 80,
  341. :ssl_port => 443,
  342. }) end
  343. it { should contain_concat__fragment("#{title}-header") }
  344. end
  345. context 'when listen_port == ssl_port' do
  346. let :params do default_params.merge({
  347. :listen_port => 80,
  348. :ssl_port => 80,
  349. }) end
  350. it { should_not contain_concat__fragment("#{title}-header") }
  351. end
  352. context 'when ssl => true' do
  353. let :params do default_params.merge({
  354. :ensure => 'absent',
  355. :ssl => true,
  356. :ssl_key => 'dummy.key',
  357. :ssl_cert => 'dummy.cert',
  358. }) end
  359. it { should contain_concat__fragment("#{title}-header") }
  360. it { should contain_concat__fragment("#{title}-ssl") }
  361. end
  362. context 'when ssl => false' do
  363. let :params do default_params.merge({
  364. :ensure => 'absent',
  365. :ssl => false,
  366. }) end
  367. it { should contain_concat__fragment("#{title}-header") }
  368. it { should_not contain_concat__fragment("#{title}-ssl") }
  369. end
  370. end
  371. end
  372. end