resource_vhost_spec.rb 29 KB

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