resource_vhost_spec.rb 32 KB

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