resource_vhost_spec.rb 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. require 'spec_helper'
  2. describe 'nginx::resource::vhost' do
  3. let :title do
  4. 'www.rspec.example.com'
  5. end
  6. let :default_params do
  7. {
  8. :www_root => '/',
  9. :ipv6_enable => true,
  10. :listen_unix_socket_enable => true,
  11. }
  12. end
  13. let :facts do
  14. {
  15. :ipaddress6 => '::',
  16. }
  17. end
  18. let :pre_condition do
  19. [
  20. 'include ::nginx::config',
  21. ]
  22. end
  23. describe 'os-independent items' do
  24. describe 'basic assumptions' do
  25. let :params do default_params end
  26. it { is_expected.to contain_class("nginx::config") }
  27. it { is_expected.to contain_concat("/etc/nginx/sites-available/#{title}.conf").with({
  28. 'owner' => 'root',
  29. 'group' => 'root',
  30. 'mode' => '0644',
  31. })}
  32. it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{access_log\s+/var/log/nginx/www\.rspec\.example\.com\.access\.log combined;}) }
  33. it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{error_log\s+/var/log/nginx/www\.rspec\.example\.com\.error\.log}) }
  34. it { is_expected.to contain_concat__fragment("#{title}-footer") }
  35. it { is_expected.to contain_nginx__resource__location("#{title}-default") }
  36. it { is_expected.not_to contain_file("/etc/nginx/fastcgi_params") }
  37. it { is_expected.to contain_file("#{title}.conf symlink").with({
  38. 'ensure' => 'link',
  39. 'path' => "/etc/nginx/sites-enabled/#{title}.conf",
  40. 'target' => "/etc/nginx/sites-available/#{title}.conf"
  41. })}
  42. end
  43. describe "vhost_header template content" do
  44. [
  45. {
  46. :title => 'should not contain www to non-www rewrite',
  47. :attr => 'rewrite_www_to_non_www',
  48. :value => false,
  49. :notmatch => %r|
  50. ^
  51. \s+server_name\s+www\.rspec\.example\.com;\n
  52. \s+return\s+301\s+http://rspec\.example\.com\$request_uri;
  53. |x,
  54. },
  55. {
  56. :title => 'should contain www to non-www rewrite',
  57. :attr => 'rewrite_www_to_non_www',
  58. :value => true,
  59. :match => %r|
  60. ^
  61. \s+server_name\s+www\.rspec\.example\.com;\n
  62. \s+return\s+301\s+http://rspec\.example\.com\$request_uri;
  63. |x,
  64. },
  65. {
  66. :title => 'should set the IPv4 listen IP',
  67. :attr => 'listen_ip',
  68. :value => '127.0.0.1',
  69. :match => %r'\s+listen\s+127.0.0.1:80;',
  70. },
  71. {
  72. :title => 'should set the IPv4 listen port',
  73. :attr => 'listen_port',
  74. :value => 45,
  75. :match => %r'\s+listen\s+\*:45;',
  76. },
  77. {
  78. :title => 'should set the IPv4 listen options',
  79. :attr => 'listen_options',
  80. :value => 'spdy default',
  81. :match => %r'\s+listen\s+\*:80 spdy default;',
  82. },
  83. {
  84. :title => 'should enable IPv6',
  85. :attr => 'ipv6_enable',
  86. :value => true,
  87. :match => %r'\s+listen\s+\[::\]:80 default ipv6only=on;',
  88. },
  89. {
  90. :title => 'should not enable IPv6',
  91. :attr => 'ipv6_enable',
  92. :value => false,
  93. :notmatch => %r'\slisten \[::\]:80 default ipv6only=on;',
  94. },
  95. {
  96. :title => 'should set the IPv6 listen IP',
  97. :attr => 'ipv6_listen_ip',
  98. :value => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  99. :match => %r'\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:80 default ipv6only=on;',
  100. },
  101. {
  102. :title => 'should set the IPv6 listen port',
  103. :attr => 'ipv6_listen_port',
  104. :value => 45,
  105. :match => %r'\s+listen\s+\[::\]:45 default ipv6only=on;',
  106. },
  107. {
  108. :title => 'should set the IPv6 listen options',
  109. :attr => 'ipv6_listen_options',
  110. :value => 'spdy',
  111. :match => %r'\s+listen\s+\[::\]:80 spdy;',
  112. },
  113. {
  114. :title => 'should enable listening on unix socket',
  115. :attr => 'listen_unix_socket_enable',
  116. :value => true,
  117. :match => %r'\s+listen\s+unix:/var/run/nginx\.sock;',
  118. },
  119. {
  120. :title => 'should not enable listening on unix socket',
  121. :attr => 'listen_unix_socket_enable',
  122. :value => false,
  123. :notmatch => %r'\s+listen\s+unix:/var/run/nginx\.sock;',
  124. },
  125. {
  126. :title => 'should set the listen unix socket',
  127. :attr => 'listen_unix_socket',
  128. :value => '/var/run/puppet_nginx.sock',
  129. :match => %r'\s+listen\s+unix:/var/run/puppet_nginx\.sock;',
  130. },
  131. {
  132. :title => 'should set the listen unix socket options',
  133. :attr => 'listen_unix_socket_options',
  134. :value => 'spdy',
  135. :match => %r'\s+listen\s+unix:/var/run/nginx\.sock spdy;',
  136. },
  137. {
  138. :title => 'should set servername(s)',
  139. :attr => 'server_name',
  140. :value => ['www.foo.com','foo.com'],
  141. :match => %r'\s+server_name\s+www.foo.com foo.com;',
  142. },
  143. {
  144. :title => 'should rewrite www servername to non-www',
  145. :attr => 'rewrite_www_to_non_www',
  146. :value => true,
  147. :match => %r'\s+server_name\s+rspec.example.com;',
  148. },
  149. {
  150. :title => 'should not rewrite www servername to non-www',
  151. :attr => 'rewrite_www_to_non_www',
  152. :value => false,
  153. :match => %r'\s+server_name\s+www.rspec.example.com;',
  154. },
  155. {
  156. :title => 'should set auth_basic',
  157. :attr => 'auth_basic',
  158. :value => 'value',
  159. :match => %r'\s+auth_basic\s+"value";',
  160. },
  161. {
  162. :title => 'should set auth_basic_user_file',
  163. :attr => 'auth_basic_user_file',
  164. :value => 'value',
  165. :match => %r'\s+auth_basic_user_file\s+value;',
  166. },
  167. {
  168. :title => 'should set the client_body_timeout',
  169. :attr => 'client_body_timeout',
  170. :value => 'value',
  171. :match => /^\s+client_body_timeout\s+value;/
  172. },
  173. {
  174. :title => 'should set the client_header_timeout',
  175. :attr => 'client_header_timeout',
  176. :value => 'value',
  177. :match => /^\s+client_header_timeout\s+value;/
  178. },
  179. {
  180. :title => 'should set the gzip_types',
  181. :attr => 'gzip_types',
  182. :value => 'value',
  183. :match => /^\s+gzip_types\s+value;/
  184. },
  185. {
  186. :title => 'should contain raw_prepend directives',
  187. :attr => 'raw_prepend',
  188. :value => [
  189. 'if (a) {',
  190. ' b;',
  191. '}'
  192. ],
  193. :match => /^\s+if \(a\) {\n\s++b;\n\s+\}/,
  194. },
  195. {
  196. :title => 'should contain ordered prepended directives',
  197. :attr => 'vhost_cfg_prepend',
  198. :value => { 'test1' => ['test value 1a', 'test value 1b'], 'test2' => 'test value 2', 'allow' => 'test value 3' },
  199. :match => [
  200. ' allow test value 3;',
  201. ' test1 test value 1a;',
  202. ' test1 test value 1b;',
  203. ' test2 test value 2;',
  204. ],
  205. },
  206. {
  207. :title => 'should set root',
  208. :attr => 'use_default_location',
  209. :value => false,
  210. :match => ' root /;',
  211. },
  212. {
  213. :title => 'should not set root',
  214. :attr => 'use_default_location',
  215. :value => true,
  216. :notmatch => / root \/;/,
  217. },
  218. {
  219. :title => 'should rewrite to HTTPS',
  220. :attr => 'rewrite_to_https',
  221. :value => true,
  222. :match => [
  223. ' if ($ssl_protocol = "") {',
  224. ' return 301 https://$host$request_uri;',
  225. ],
  226. },
  227. {
  228. :title => 'should not rewrite to HTTPS',
  229. :attr => 'rewrite_to_https',
  230. :value => false,
  231. :notmatch => [
  232. %r'if \(\$ssl_protocol = ""\) \{',
  233. %r'\s+return 301 https://\$host\$request_uri;',
  234. ],
  235. },
  236. {
  237. :title => 'should set access_log',
  238. :attr => 'access_log',
  239. :value => '/path/to/access.log',
  240. :match => ' access_log /path/to/access.log combined;',
  241. },
  242. {
  243. :title => 'should set access_log off',
  244. :attr => 'access_log',
  245. :value => 'off',
  246. :match => ' access_log off;',
  247. },
  248. {
  249. :title => 'should set access_log to syslog',
  250. :attr => 'access_log',
  251. :value => 'syslog:server=localhost',
  252. :match => ' access_log syslog:server=localhost combined;',
  253. },
  254. {
  255. :title => 'should set format_log custom_format',
  256. :attr => 'format_log',
  257. :value => 'custom',
  258. :match => ' access_log /var/log/nginx/www.rspec.example.com.access.log custom;',
  259. },
  260. {
  261. :title => 'should set error_log',
  262. :attr => 'error_log',
  263. :value => '/path/to/error.log',
  264. :match => ' error_log /path/to/error.log;',
  265. },
  266. ].each do |param|
  267. context "when #{param[:attr]} is #{param[:value]}" do
  268. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  269. it { is_expected.to contain_concat__fragment("#{title}-header") }
  270. it param[:title] do
  271. matches = Array(param[:match])
  272. if matches.all? { |m| m.is_a? Regexp }
  273. matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) }
  274. else
  275. lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n")
  276. expect(lines & Array(param[:match])).to eq(Array(param[:match]))
  277. end
  278. Array(param[:notmatch]).each do |item|
  279. is_expected.to contain_concat__fragment("#{title}-header").without_content(item)
  280. end
  281. end
  282. end
  283. end
  284. end
  285. describe "vhost_footer template content" do
  286. [
  287. {
  288. :title => 'should not contain www to non-www rewrite',
  289. :attr => 'rewrite_www_to_non_www',
  290. :value => false,
  291. :notmatch => %r|
  292. ^
  293. \s+server_name\s+www\.rspec\.example\.com;\n
  294. \s+return\s+301\s+https://rspec\.example\.com\$request_uri;
  295. |x,
  296. },
  297. {
  298. :title => 'should contain include directives',
  299. :attr => 'include_files',
  300. :value => [ '/file1', '/file2' ],
  301. :match => [
  302. %r'^\s+include\s+/file1;',
  303. %r'^\s+include\s+/file2;',
  304. ],
  305. },
  306. {
  307. :title => 'should contain ordered appended directives',
  308. :attr => 'vhost_cfg_append',
  309. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' },
  310. :match => [
  311. ' allow test value 3;',
  312. ' test1 test value 1;',
  313. ' test2 test value 2a;',
  314. ' test2 test value 2b;',
  315. ],
  316. },
  317. {
  318. :title => 'should contain raw_append directives',
  319. :attr => 'raw_append',
  320. :value => [
  321. 'if (a) {',
  322. ' b;',
  323. '}'
  324. ],
  325. :match => /^\s+if \(a\) {\n\s++b;\n\s+\}/,
  326. },
  327. ].each do |param|
  328. context "when #{param[:attr]} is #{param[:value]}" do
  329. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  330. it { is_expected.to contain_concat__fragment("#{title}-footer") }
  331. it param[:title] do
  332. matches = Array(param[:match])
  333. if matches.all? { |m| m.is_a? Regexp }
  334. matches.each { |item| is_expected.to contain_concat__fragment("#{title}-footer").with_content(item) }
  335. else
  336. lines = catalogue.resource('concat::fragment', "#{title}-footer").send(:parameters)[:content].split("\n")
  337. expect(lines & Array(param[:match])).to eq(Array(param[:match]))
  338. end
  339. Array(param[:notmatch]).each do |item|
  340. is_expected.to contain_concat__fragment("#{title}-footer").without_content(item)
  341. end
  342. end
  343. end
  344. end
  345. end
  346. describe "vhost_ssl_header template content" do
  347. [
  348. {
  349. :title => 'should not contain www to non-www rewrite',
  350. :attr => 'rewrite_www_to_non_www',
  351. :value => false,
  352. :notmatch => %r|
  353. ^
  354. \s+server_name\s+www\.rspec\.example\.com;\n
  355. \s+return\s+301\s+https://rspec\.example\.com\$request_uri;
  356. |x,
  357. },
  358. {
  359. :title => 'should contain www to non-www rewrite',
  360. :attr => 'rewrite_www_to_non_www',
  361. :value => true,
  362. :match => %r|
  363. ^
  364. \s+server_name\s+www\.rspec\.example\.com;\n
  365. \s+return\s+301\s+https://rspec\.example\.com\$request_uri;
  366. |x,
  367. },
  368. {
  369. :title => 'should set the IPv4 listen IP',
  370. :attr => 'listen_ip',
  371. :value => '127.0.0.1',
  372. :match => %r'\s+listen\s+127.0.0.1:443 ssl;',
  373. },
  374. {
  375. :title => 'should set the IPv4 SSL listen port',
  376. :attr => 'ssl_port',
  377. :value => 45,
  378. :match => %r'\s+listen\s+\*:45 ssl;',
  379. },
  380. {
  381. :title => 'should set SPDY',
  382. :attr => 'spdy',
  383. :value => 'on',
  384. :match => %r'\s+listen\s+\*:443 ssl spdy;',
  385. },
  386. {
  387. :title => 'should not set SPDY',
  388. :attr => 'spdy',
  389. :value => 'off',
  390. :match => %r'\s+listen\s+\*:443 ssl;',
  391. },
  392. {
  393. :title => 'should set HTTP2',
  394. :attr => 'http2',
  395. :value => 'on',
  396. :match => %r'\s+listen\s+\*:443 ssl http2;',
  397. },
  398. {
  399. :title => 'should not set HTTP2',
  400. :attr => 'http2',
  401. :value => 'off',
  402. :match => %r'\s+listen\s+\*:443 ssl;',
  403. },
  404. {
  405. :title => 'should set the IPv4 listen options',
  406. :attr => 'listen_options',
  407. :value => 'default',
  408. :match => %r'\s+listen\s+\*:443 ssl default;',
  409. },
  410. {
  411. :title => 'should enable IPv6',
  412. :attr => 'ipv6_enable',
  413. :value => true,
  414. :match => %r'\s+listen\s+\[::\]:443 ssl default ipv6only=on;',
  415. },
  416. {
  417. :title => 'should disable IPv6',
  418. :attr => 'ipv6_enable',
  419. :value => false,
  420. :notmatch => / listen \[::\]:443 ssl default ipv6only=on;/,
  421. },
  422. {
  423. :title => 'should set the IPv6 listen IP',
  424. :attr => 'ipv6_listen_ip',
  425. :value => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  426. :match => %r'\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:443 ssl default ipv6only=on;',
  427. },
  428. {
  429. :title => 'should set the IPv6 listen port',
  430. :attr => 'ssl_port',
  431. :value => 45,
  432. :match => %r'\s+listen\s+\[::\]:45 ssl default ipv6only=on;',
  433. },
  434. {
  435. :title => 'should set the IPv6 listen options',
  436. :attr => 'ipv6_listen_options',
  437. :value => 'spdy default',
  438. :match => %r'\s+listen\s+\[::\]:443 ssl spdy default;',
  439. },
  440. {
  441. :title => 'should set servername(s)',
  442. :attr => 'server_name',
  443. :value => ['www.foo.com','foo.com'],
  444. :match => %r'\s+server_name\s+www.foo.com foo.com;',
  445. },
  446. {
  447. :title => 'should rewrite www servername to non-www',
  448. :attr => 'rewrite_www_to_non_www',
  449. :value => true,
  450. :match => %r'\s+server_name\s+rspec.example.com;',
  451. },
  452. {
  453. :title => 'should not rewrite www servername to non-www',
  454. :attr => 'rewrite_www_to_non_www',
  455. :value => false,
  456. :match => %r'\s+server_name\s+www.rspec.example.com;',
  457. },
  458. {
  459. :title => 'should set the SSL buffer size',
  460. :attr => 'ssl_buffer_size',
  461. :value => '4k',
  462. :match => ' ssl_buffer_size 4k;',
  463. },
  464. {
  465. :title => 'should set the SSL client certificate file',
  466. :attr => 'ssl_client_cert',
  467. :value => '/tmp/client_certificate',
  468. :match => %r'\s+ssl_client_certificate\s+/tmp/client_certificate;',
  469. },
  470. {
  471. :title => 'should set the SSL CRL file',
  472. :attr => 'ssl_crl',
  473. :value => '/tmp/crl',
  474. :match => %r'\s+ssl_crl\s+/tmp/crl;',
  475. },
  476. {
  477. :title => 'should set the SSL DH parameters file',
  478. :attr => 'ssl_dhparam',
  479. :value => '/tmp/dhparam',
  480. :match => %r'\s+ssl_dhparam\s+/tmp/dhparam;',
  481. },
  482. {
  483. :title => 'should set the SSL stapling file',
  484. :attr => 'ssl_stapling_file',
  485. :value => '/tmp/stapling_file',
  486. :match => %r'\s+ssl_stapling_file\s+/tmp/stapling_file;',
  487. },
  488. {
  489. :title => 'should set the SSL trusted certificate file',
  490. :attr => 'ssl_trusted_cert',
  491. :value => '/tmp/trusted_certificate',
  492. :match => %r'\s+ssl_trusted_certificate\s+/tmp/trusted_certificate;',
  493. },
  494. {
  495. :title => 'should set the SSL cache',
  496. :attr => 'ssl_cache',
  497. :value => 'shared:SSL:1m',
  498. :match => %r'\s+ssl_session_cache\s+shared:SSL:1m;',
  499. },
  500. {
  501. :title => 'should set the SSL timeout',
  502. :attr => 'ssl_session_timeout',
  503. :value => '30m',
  504. :match => ' ssl_session_timeout 30m;',
  505. },
  506. {
  507. :title => 'should set the SSL protocols',
  508. :attr => 'ssl_protocols',
  509. :value => 'TLSv1',
  510. :match => %r'\s+ssl_protocols\s+TLSv1;',
  511. },
  512. {
  513. :title => 'should set the SSL ciphers',
  514. :attr => 'ssl_ciphers',
  515. :value => 'HIGH',
  516. :match => %r'\s+ssl_ciphers\s+HIGH;',
  517. },
  518. {
  519. :title => 'should set auth_basic',
  520. :attr => 'auth_basic',
  521. :value => 'value',
  522. :match => %r'\s+auth_basic\s+"value";',
  523. },
  524. {
  525. :title => 'should set auth_basic_user_file',
  526. :attr => 'auth_basic_user_file',
  527. :value => 'value',
  528. :match => %r'\s+auth_basic_user_file\s+"value";',
  529. },
  530. {
  531. :title => 'should set the client_body_timeout',
  532. :attr => 'client_body_timeout',
  533. :value => 'value',
  534. :match => /^\s+client_body_timeout\s+value;/
  535. },
  536. {
  537. :title => 'should set the client_header_timeout',
  538. :attr => 'client_header_timeout',
  539. :value => 'value',
  540. :match => /^\s+client_header_timeout\s+value;/
  541. },
  542. {
  543. :title => 'should set the gzip_types',
  544. :attr => 'gzip_types',
  545. :value => 'value',
  546. :match => /^\s+gzip_types\s+value;/
  547. },
  548. {
  549. :title => 'should set access_log',
  550. :attr => 'access_log',
  551. :value => '/path/to/access.log',
  552. :match => ' access_log /path/to/access.log combined;',
  553. },
  554. {
  555. :title => 'should set access_log off',
  556. :attr => 'access_log',
  557. :value => 'off',
  558. :match => ' access_log off;',
  559. },
  560. {
  561. :title => 'should set access_log to syslog',
  562. :attr => 'access_log',
  563. :value => 'syslog:server=localhost',
  564. :match => ' access_log syslog:server=localhost combined;',
  565. },
  566. {
  567. :title => 'should set format_log custom_format',
  568. :attr => 'format_log',
  569. :value => 'custom',
  570. :match => ' access_log /var/log/nginx/ssl-www.rspec.example.com.access.log custom;',
  571. },
  572. {
  573. :title => 'should set error_log',
  574. :attr => 'error_log',
  575. :value => '/path/to/error.log',
  576. :match => ' error_log /path/to/error.log;',
  577. },
  578. {
  579. :title => 'should contain raw_prepend directives',
  580. :attr => 'raw_prepend',
  581. :value => [
  582. 'if (a) {',
  583. ' b;',
  584. '}'
  585. ],
  586. :match => /^\s+if \(a\) {\n\s++b;\n\s+\}/,
  587. },
  588. {
  589. :title => 'should contain ordered prepend directives',
  590. :attr => 'vhost_cfg_prepend',
  591. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' },
  592. :match => [
  593. ' allow test value 3;',
  594. ' test1 test value 1;',
  595. ' test2 test value 2a;',
  596. ' test2 test value 2b;',
  597. ]
  598. },
  599. {
  600. :title => 'should contain ordered ssl prepend directives',
  601. :attr => 'vhost_cfg_ssl_prepend',
  602. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' },
  603. :match => [
  604. ' allow test value 3;',
  605. ' test1 test value 1;',
  606. ' test2 test value 2a;',
  607. ' test2 test value 2b;',
  608. ]
  609. },
  610. {
  611. :title => 'should set root',
  612. :attr => 'use_default_location',
  613. :value => false,
  614. :match => ' root /;',
  615. },
  616. {
  617. :title => 'should not set root',
  618. :attr => 'use_default_location',
  619. :value => true,
  620. :notmatch => / root \/;/,
  621. },
  622. ].each do |param|
  623. context "when #{param[:attr]} is #{param[:value]}" do
  624. let :params do default_params.merge({
  625. param[:attr].to_sym => param[:value],
  626. :ssl => true,
  627. :ssl_key => 'dummy.key',
  628. :ssl_cert => 'dummy.crt',
  629. }) end
  630. it { is_expected.to contain_concat__fragment("#{title}-ssl-header") }
  631. it param[:title] do
  632. matches = Array(param[:match])
  633. if matches.all? { |m| m.is_a? Regexp }
  634. matches.each { |item| is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(item) }
  635. else
  636. lines = catalogue.resource('concat::fragment', "#{title}-ssl-header").send(:parameters)[:content].split("\n")
  637. expect(lines & Array(param[:match])).to eq(Array(param[:match]))
  638. end
  639. Array(param[:notmatch]).each do |item|
  640. is_expected.to contain_concat__fragment("#{title}-ssl-header").without_content(item)
  641. end
  642. end
  643. end
  644. end
  645. end
  646. describe "vhost_ssl_footer template content" do
  647. [
  648. {
  649. :title => 'should not contain www to non-www rewrite',
  650. :attr => 'rewrite_www_to_non_www',
  651. :value => false,
  652. :notmatch => %r|
  653. ^
  654. \s+server_name\s+www\.rspec\.example\.com;\n
  655. \s+return\s+301\s+https://rspec\.example\.com\$request_uri;
  656. |x,
  657. },
  658. {
  659. :title => 'should contain include directives',
  660. :attr => 'include_files',
  661. :value => [ '/file1', '/file2' ],
  662. :match => [
  663. %r'^\s+include\s+/file1;',
  664. %r'^\s+include\s+/file2;',
  665. ],
  666. },
  667. {
  668. :title => 'should contain ordered appended directives',
  669. :attr => 'vhost_cfg_append',
  670. :value => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' },
  671. :match => [
  672. ' allow test value 3;',
  673. ' test1 test value 1;',
  674. ' test2 test value 2;',
  675. ]
  676. },
  677. {
  678. :title => 'should contain raw_append directives',
  679. :attr => 'raw_append',
  680. :value => [
  681. 'if (a) {',
  682. ' b;',
  683. '}'
  684. ],
  685. :match => /^\s+if \(a\) {\n\s++b;\n\s+\}/,
  686. },
  687. {
  688. :title => 'should contain ordered ssl appended directives',
  689. :attr => 'vhost_cfg_ssl_append',
  690. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' },
  691. :match => [
  692. ' allow test value 3;',
  693. ' test1 test value 1;',
  694. ' test2 test value 2a;',
  695. ' test2 test value 2b;',
  696. ]
  697. },
  698. ].each do |param|
  699. context "when #{param[:attr]} is #{param[:value]}" do
  700. let :params do default_params.merge({
  701. param[:attr].to_sym => param[:value],
  702. :ssl => true,
  703. :ssl_key => 'dummy.key',
  704. :ssl_cert => 'dummy.crt',
  705. }) end
  706. it { is_expected.to contain_concat__fragment("#{title}-ssl-footer") }
  707. it param[:title] do
  708. matches = Array(param[:match])
  709. if matches.all? { |m| m.is_a? Regexp }
  710. matches.each { |item| is_expected.to contain_concat__fragment("#{title}-ssl-footer").with_content(item) }
  711. else
  712. lines = catalogue.resource('concat::fragment', "#{title}-ssl-footer").send(:parameters)[:content].split("\n")
  713. expect(lines & Array(param[:match])).to eq(Array(param[:match]))
  714. end
  715. Array(param[:notmatch]).each do |item|
  716. is_expected.to contain_concat__fragment("#{title}-ssl-footer").without_content(item)
  717. end
  718. end
  719. end
  720. end
  721. end
  722. context 'attribute resources' do
  723. context "with SSL enabled, www rewrite to naked domain with multiple server_names" do
  724. let :title do 'foo.com' end
  725. let(:params) do
  726. {
  727. :ssl => true,
  728. :ssl_cert => 'cert',
  729. :ssl_key => 'key',
  730. :server_name => %w(www.foo.com bar.foo.com foo.com),
  731. :use_default_location => false,
  732. :rewrite_www_to_non_www => true,
  733. }
  734. end
  735. it "should set the server_name of the rewrite server stanza to every server_name with 'www.' stripped" do
  736. is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(/^\s+server_name\s+foo.com\s+bar.foo.com\s+foo.com;/)
  737. end
  738. end
  739. context "with SSL disabled, www rewrite to naked domain with multiple server_names" do
  740. let :title do 'foo.com' end
  741. let(:params) do
  742. {
  743. :server_name => %w(www.foo.com bar.foo.com foo.com),
  744. :use_default_location => false,
  745. :rewrite_www_to_non_www => true,
  746. }
  747. end
  748. it "should set the server_name of the rewrite server stanza to every server_name with 'www.' stripped" do
  749. is_expected.to contain_concat__fragment("#{title}-header").with_content(/^\s+server_name\s+foo.com\s+bar.foo.com\s+foo.com;/)
  750. end
  751. end
  752. context "SSL cert missing" do
  753. let(:params) {{ :ssl => true, :ssl_key => 'key' }}
  754. it { expect { is_expected.to contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error) }
  755. end
  756. context "SSL key missing" do
  757. let(:params) {{ :ssl => true, :ssl_cert => 'cert' }}
  758. it { expect { is_expected.to contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error) }
  759. end
  760. context 'when use_default_location => true' do
  761. let :params do default_params.merge({
  762. :use_default_location => true,
  763. }) end
  764. it { is_expected.to contain_nginx__resource__location("#{title}-default") }
  765. end
  766. context 'when use_default_location => false' do
  767. let :params do default_params.merge({
  768. :use_default_location => false,
  769. }) end
  770. it { is_expected.not_to contain_nginx__resource__location("#{title}-default") }
  771. end
  772. context 'when location_cfg_prepend => { key => value }' do
  773. let :params do default_params.merge({
  774. :location_cfg_prepend => { 'key' => 'value' },
  775. }) end
  776. it { is_expected.to contain_nginx__resource__location("#{title}-default").with_location_cfg_prepend({ 'key' => 'value' }) }
  777. end
  778. context "when location_raw_prepend => [ 'foo;' ]" do
  779. let :params do default_params.merge({
  780. :location_raw_prepend => [ 'foo;' ],
  781. }) end
  782. it { is_expected.to contain_nginx__resource__location("#{title}-default").with_raw_prepend([ 'foo;' ]) }
  783. end
  784. context "when location_raw_append => [ 'foo;' ]" do
  785. let :params do default_params.merge({
  786. :location_raw_append => [ 'foo;' ],
  787. }) end
  788. it { is_expected.to contain_nginx__resource__location("#{title}-default").with_raw_append([ 'foo;' ]) }
  789. end
  790. context 'when location_cfg_append => { key => value }' do
  791. let :params do default_params.merge({
  792. :location_cfg_append => { 'key' => 'value' },
  793. }) end
  794. it { is_expected.to contain_nginx__resource__location("#{title}-default").with_location_cfg_append({ 'key' => 'value' }) }
  795. end
  796. context 'when fastcgi => "localhost:9000"' do
  797. let :params do default_params.merge({
  798. :fastcgi => 'localhost:9000',
  799. }) end
  800. it { is_expected.to contain_file('/etc/nginx/fastcgi_params').with_mode('0770') }
  801. end
  802. context 'when uwsgi => "uwsgi_upstream"' do
  803. let :params do default_params.merge({
  804. :uwsgi => 'uwsgi_upstream',
  805. }) end
  806. it { should contain_file('/etc/nginx/uwsgi_params').with_mode('0770') }
  807. end
  808. context 'when listen_port == ssl_port' do
  809. let :params do default_params.merge({
  810. :listen_port => 80,
  811. :ssl_port => 80,
  812. }) end
  813. it { is_expected.not_to contain_concat__fragment("#{title}-header") }
  814. it { is_expected.not_to contain_concat__fragment("#{title}-footer") }
  815. end
  816. context 'when listen_port == "ssl_port"' do
  817. let :params do default_params.merge({
  818. :listen_port => 80,
  819. :ssl_port => '80',
  820. }) end
  821. it { is_expected.not_to contain_concat__fragment("#{title}-header") }
  822. it { is_expected.not_to contain_concat__fragment("#{title}-footer") }
  823. end
  824. context 'when listen_port != ssl_port' do
  825. let :params do default_params.merge({
  826. :listen_port => 80,
  827. :ssl_port => 443,
  828. }) end
  829. it { is_expected.to contain_concat__fragment("#{title}-header") }
  830. it { is_expected.to contain_concat__fragment("#{title}-footer") }
  831. end
  832. context 'when listen_port != "ssl_port"' do
  833. let :params do default_params.merge({
  834. :listen_port => 80,
  835. :ssl_port => '443',
  836. }) end
  837. it { is_expected.to contain_concat__fragment("#{title}-header") }
  838. it { is_expected.to contain_concat__fragment("#{title}-footer") }
  839. end
  840. context 'when ensure => absent' do
  841. let :params do default_params.merge({
  842. :ensure => 'absent',
  843. :ssl => true,
  844. :ssl_key => 'dummy.key',
  845. :ssl_cert => 'dummy.cert',
  846. }) end
  847. it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ensure('absent') }
  848. it { is_expected.to contain_file("#{title}.conf symlink").with_ensure('absent') }
  849. end
  850. context 'when ssl => true and ssl_port == listen_port' do
  851. let :params do default_params.merge({
  852. :ssl => true,
  853. :listen_port => 80,
  854. :ssl_port => 80,
  855. :ssl_key => 'dummy.key',
  856. :ssl_cert => 'dummy.cert',
  857. }) end
  858. it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ssl_only(true) }
  859. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{access_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.access\.log combined;}) }
  860. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{error_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.error\.log}) }
  861. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate\s+dummy.cert;}) }
  862. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate_key\s+dummy.key;}) }
  863. it { is_expected.to contain_concat__fragment("#{title}-ssl-footer") }
  864. end
  865. context 'when ssl_client_cert is set' do
  866. let :params do default_params.merge({
  867. :ssl => true,
  868. :listen_port => 80,
  869. :ssl_port => 80,
  870. :ssl_key => 'dummy.key',
  871. :ssl_cert => 'dummy.cert',
  872. :ssl_client_cert => 'client.cert',
  873. :ssl_verify_client => 'optional',
  874. }) end
  875. it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ssl_only(true) }
  876. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{access_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.access\.log combined;}) }
  877. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{error_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.error\.log}) }
  878. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_verify_client\s+optional;}) }
  879. end
  880. context 'when passenger_cgi_param is set' do
  881. let :params do default_params.merge({
  882. :passenger_cgi_param => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }
  883. }) end
  884. it { is_expected.to contain_concat__fragment("#{title}-header").with_content( /passenger_set_cgi_param test1 test value 1;/ ) }
  885. it { is_expected.to contain_concat__fragment("#{title}-header").with_content( /passenger_set_cgi_param test2 test value 2;/ ) }
  886. it { is_expected.to contain_concat__fragment("#{title}-header").with_content( /passenger_set_cgi_param test3 test value 3;/ ) }
  887. end
  888. context 'when passenger_cgi_param is set and ssl => true' do
  889. let :params do default_params.merge({
  890. :passenger_cgi_param => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' },
  891. :ssl => true,
  892. :ssl_key => 'dummy.key',
  893. :ssl_cert => 'dummy.cert',
  894. }) end
  895. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_set_cgi_param test1 test value 1;/ ) }
  896. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_set_cgi_param test2 test value 2;/ ) }
  897. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_set_cgi_param test3 test value 3;/ ) }
  898. end
  899. context 'when passenger_set_header is set' do
  900. let :params do default_params.merge({
  901. :passenger_set_header => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }
  902. }) end
  903. it { is_expected.to contain_concat__fragment("#{title}-header").with_content( /passenger_set_header test1 test value 1;/ ) }
  904. it { is_expected.to contain_concat__fragment("#{title}-header").with_content( /passenger_set_header test2 test value 2;/ ) }
  905. it { is_expected.to contain_concat__fragment("#{title}-header").with_content( /passenger_set_header test3 test value 3;/ ) }
  906. end
  907. context 'when passenger_set_header is set and ssl => true' do
  908. let :params do default_params.merge({
  909. :passenger_set_header => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' },
  910. :ssl => true,
  911. :ssl_key => 'dummy.key',
  912. :ssl_cert => 'dummy.cert',
  913. }) end
  914. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_set_header test1 test value 1;/ ) }
  915. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_set_header test2 test value 2;/ ) }
  916. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_set_header test3 test value 3;/ ) }
  917. end
  918. context 'when passenger_env_var is set' do
  919. let :params do default_params.merge({
  920. :passenger_env_var => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }
  921. }) end
  922. it { is_expected.to contain_concat__fragment("#{title}-header").with_content( /passenger_env_var test1 test value 1;/ ) }
  923. it { is_expected.to contain_concat__fragment("#{title}-header").with_content( /passenger_env_var test2 test value 2;/ ) }
  924. it { is_expected.to contain_concat__fragment("#{title}-header").with_content( /passenger_env_var test3 test value 3;/ ) }
  925. end
  926. context 'when passenger_env_var is set and ssl => true' do
  927. let :params do default_params.merge({
  928. :passenger_env_var => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' },
  929. :ssl => true,
  930. :ssl_key => 'dummy.key',
  931. :ssl_cert => 'dummy.cert',
  932. }) end
  933. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_env_var test1 test value 1;/ ) }
  934. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_env_var test2 test value 2;/ ) }
  935. it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_env_var test3 test value 3;/ ) }
  936. end
  937. context 'when vhost name is sanitized' do
  938. let :title do 'www rspec-vhost com' end
  939. let :params do default_params end
  940. it { is_expected.to contain_concat('/etc/nginx/sites-available/www_rspec-vhost_com.conf') }
  941. end
  942. context 'when add_header is set' do
  943. let :params do default_params.merge({
  944. :add_header => { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' }
  945. }) end
  946. it 'should have correctly ordered entries in the config' do
  947. is_expected.to contain_concat__fragment("#{title}-header").with_content(/
  948. %r|
  949. \s+add_header\s+header1 test value 1;\n
  950. \s+add_header\s+header2 test value 2;\n
  951. \s+add_header\s+header3 test value 3;\n
  952. |/)
  953. end
  954. end
  955. context 'when add_header is set and ssl => true' do
  956. let :params do default_params.merge({
  957. :add_header => { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' },
  958. :ssl => true,
  959. :ssl_key => 'dummy.key',
  960. :ssl_cert => 'dummy.cert',
  961. }) end
  962. it 'should have correctly ordered entries in the config' do
  963. is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(/
  964. %r|
  965. \s+add_header\s+header1 test value 1;\n
  966. \s+add_header\s+header2 test value 2;\n
  967. \s+add_header\s+header3 test value 3;\n
  968. |/)
  969. end
  970. end
  971. end
  972. end
  973. end