resource_mailhost_spec.rb 14 KB

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