PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/spec/defines/resource_vhost_spec.rb

https://github.com/berngp-puppetmods/puppet-nginx
Ruby | 647 lines | 610 code | 37 blank | 0 comment | 4 complexity | 1356f407f3defe6ead6f0716a0cf7306 MD5 | raw file
Possible License(s): Apache-2.0
  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::params',
  22. 'include ::nginx::config',
  23. ]
  24. end
  25. describe 'os-independent items' do
  26. describe 'basic assumptions' do
  27. let :params do default_params end
  28. it { should contain_class("nginx::params") }
  29. it { should contain_class("nginx::config") }
  30. it { should contain_concat("/etc/nginx/sites-available/#{title}.conf").with({
  31. 'owner' => 'root',
  32. 'group' => 'root',
  33. 'mode' => '0644',
  34. })}
  35. it { should contain_concat__fragment("#{title}-header").with_content(%r{access_log[ ]+/var/log/nginx/www\.rspec\.example\.com\.access\.log}) }
  36. it { should contain_concat__fragment("#{title}-header").with_content(%r{error_log[ ]+/var/log/nginx/www\.rspec\.example\.com\.error\.log}) }
  37. it { should contain_concat__fragment("#{title}-footer") }
  38. it { should contain_nginx__resource__location("#{title}-default") }
  39. it { should_not contain_file("/etc/nginx/fastcgi_params") }
  40. it { should contain_file("#{title}.conf symlink").with({
  41. 'ensure' => 'link',
  42. 'path' => "/etc/nginx/sites-enabled/#{title}.conf",
  43. 'target' => "/etc/nginx/sites-available/#{title}.conf"
  44. })}
  45. end
  46. describe "vhost_header template content" do
  47. [
  48. {
  49. :title => 'should set the IPv4 listen IP',
  50. :attr => 'listen_ip',
  51. :value => '127.0.0.1',
  52. :match => ' listen 127.0.0.1:80;',
  53. },
  54. {
  55. :title => 'should set the IPv4 listen port',
  56. :attr => 'listen_port',
  57. :value => 45,
  58. :match => ' listen *:45;',
  59. },
  60. {
  61. :title => 'should set the IPv4 listen options',
  62. :attr => 'listen_options',
  63. :value => 'spdy default',
  64. :match => ' listen *:80 spdy default;',
  65. },
  66. {
  67. :title => 'should enable IPv6',
  68. :attr => 'ipv6_enable',
  69. :value => true,
  70. :match => ' listen [::]:80 default ipv6only=on;',
  71. },
  72. {
  73. :title => 'should not enable IPv6',
  74. :attr => 'ipv6_enable',
  75. :value => false,
  76. :notmatch => / listen \[::\]:80 default ipv6only=on;/,
  77. },
  78. {
  79. :title => 'should set the IPv6 listen IP',
  80. :attr => 'ipv6_listen_ip',
  81. :value => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  82. :match => ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80 default ipv6only=on;',
  83. },
  84. {
  85. :title => 'should set the IPv6 listen port',
  86. :attr => 'ipv6_listen_port',
  87. :value => 45,
  88. :match => ' listen [::]:45 default ipv6only=on;',
  89. },
  90. {
  91. :title => 'should set the IPv6 listen options',
  92. :attr => 'ipv6_listen_options',
  93. :value => 'spdy',
  94. :match => ' listen [::]:80 spdy;',
  95. },
  96. {
  97. :title => 'should set servername(s)',
  98. :attr => 'server_name',
  99. :value => ['name1','name2'],
  100. :match => ' server_name name1 name2;',
  101. },
  102. {
  103. :title => 'should rewrite www servername to non-www',
  104. :attr => 'rewrite_www_to_non_www',
  105. :value => true,
  106. :match => ' server_name rspec.example.com;',
  107. },
  108. {
  109. :title => 'should not rewrite www servername to non-www',
  110. :attr => 'rewrite_www_to_non_www',
  111. :value => false,
  112. :match => ' server_name www.rspec.example.com;',
  113. },
  114. {
  115. :title => 'should set auth_basic',
  116. :attr => 'auth_basic',
  117. :value => 'value',
  118. :match => ' auth_basic "value";',
  119. },
  120. {
  121. :title => 'should set auth_basic_user_file',
  122. :attr => 'auth_basic_user_file',
  123. :value => 'value',
  124. :match => ' auth_basic_user_file value;',
  125. },
  126. {
  127. :title => 'should contain ordered prepended directives',
  128. :attr => 'vhost_cfg_prepend',
  129. :value => { 'test1' => ['test value 1a', 'test value 1b'], 'test2' => 'test value 2', 'allow' => 'test value 3' },
  130. :match => [
  131. ' allow test value 3;',
  132. ' test1 test value 1a;',
  133. ' test1 test value 1b;',
  134. ' test2 test value 2;',
  135. ],
  136. },
  137. {
  138. :title => 'should set root',
  139. :attr => 'use_default_location',
  140. :value => false,
  141. :match => ' root /;',
  142. },
  143. {
  144. :title => 'should not set root',
  145. :attr => 'use_default_location',
  146. :value => true,
  147. :notmatch => / root \/;/,
  148. },
  149. {
  150. :title => 'should set proxy_set_header',
  151. :attr => 'proxy_set_header',
  152. :value => ['header1','header2'],
  153. :match => [
  154. ' proxy_set_header header1;',
  155. ' proxy_set_header header2;',
  156. ],
  157. },
  158. {
  159. :title => 'should rewrite to HTTPS',
  160. :attr => 'rewrite_to_https',
  161. :value => true,
  162. :match => [
  163. ' if ($ssl_protocol = "") {',
  164. ' return 301 https://$host$request_uri;',
  165. ],
  166. },
  167. {
  168. :title => 'should not rewrite to HTTPS',
  169. :attr => 'rewrite_to_https',
  170. :value => false,
  171. :notmatch => [
  172. /if \(\$ssl_protocol = ""\) \{/,
  173. / return 301 https:\/\/\$host\$request_uri;/,
  174. ],
  175. },
  176. {
  177. :title => 'should set access_log',
  178. :attr => 'access_log',
  179. :value => '/path/to/access.log',
  180. :match => ' access_log /path/to/access.log;',
  181. },
  182. {
  183. :title => 'should set error_log',
  184. :attr => 'error_log',
  185. :value => '/path/to/error.log',
  186. :match => ' error_log /path/to/error.log;',
  187. },
  188. ].each do |param|
  189. context "when #{param[:attr]} is #{param[:value]}" do
  190. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  191. it { should contain_concat__fragment("#{title}-header") }
  192. it param[:title] do
  193. lines = subject.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n")
  194. (lines & Array(param[:match])).should == Array(param[:match])
  195. Array(param[:notmatch]).each do |item|
  196. should contain_concat__fragment("#{title}-header").without_content(item)
  197. end
  198. end
  199. end
  200. end
  201. end
  202. describe "vhost_footer template content" do
  203. [
  204. {
  205. :title => 'should contain include directives',
  206. :attr => 'include_files',
  207. :value => [ '/file1', '/file2' ],
  208. :match => [
  209. 'include /file1;',
  210. 'include /file2;',
  211. ],
  212. },
  213. {
  214. :title => 'should contain ordered appended directives',
  215. :attr => 'vhost_cfg_append',
  216. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' },
  217. :match => [
  218. ' allow test value 3;',
  219. ' test1 test value 1;',
  220. ' test2 test value 2a;',
  221. ' test2 test value 2b;',
  222. ],
  223. },
  224. {
  225. :title => 'should contain www to non-www rewrite',
  226. :attr => 'rewrite_www_to_non_www',
  227. :value => true,
  228. :match => [
  229. ' listen *:80;',
  230. ' server_name www.rspec.example.com;',
  231. ' return 301 http://rspec.example.com$uri;',
  232. ],
  233. },
  234. {
  235. :title => 'should not contain www to non-www rewrite',
  236. :attr => 'rewrite_www_to_non_www',
  237. :value => false,
  238. :notmatch => [
  239. %r| listen \*:80;|,
  240. %r| server_name www\.rspec\.example\.com;|,
  241. %r| return 301 http://rspec\.example\.com\$uri;|
  242. ],
  243. },
  244. ].each do |param|
  245. context "when #{param[:attr]} is #{param[:value]}" do
  246. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  247. it { should contain_concat__fragment("#{title}-footer") }
  248. it param[:title] do
  249. lines = subject.resource('concat::fragment', "#{title}-footer").send(:parameters)[:content].split("\n")
  250. (lines & Array(param[:match])).should == Array(param[:match])
  251. Array(param[:notmatch]).each do |item|
  252. should contain_concat__fragment("#{title}-footer").without_content(item)
  253. end
  254. end
  255. end
  256. end
  257. end
  258. describe "vhost_ssl_header template content" do
  259. [
  260. {
  261. :title => 'should set the IPv4 listen IP',
  262. :attr => 'listen_ip',
  263. :value => '127.0.0.1',
  264. :match => ' listen 127.0.0.1:443 ssl;',
  265. },
  266. {
  267. :title => 'should set the IPv4 SSL listen port',
  268. :attr => 'ssl_port',
  269. :value => 45,
  270. :match => ' listen *:45 ssl;',
  271. },
  272. {
  273. :title => 'should set SPDY',
  274. :attr => 'spdy',
  275. :value => 'on',
  276. :match => ' listen *:443 ssl spdy;',
  277. },
  278. {
  279. :title => 'should not set SPDY',
  280. :attr => 'spdy',
  281. :value => 'off',
  282. :match => ' listen *:443 ssl;',
  283. },
  284. {
  285. :title => 'should set the IPv4 listen options',
  286. :attr => 'listen_options',
  287. :value => 'default',
  288. :match => ' listen *:443 ssl default;',
  289. },
  290. {
  291. :title => 'should enable IPv6',
  292. :attr => 'ipv6_enable',
  293. :value => true,
  294. :match => ' listen [::]:443 ssl default ipv6only=on;',
  295. },
  296. {
  297. :title => 'should disable IPv6',
  298. :attr => 'ipv6_enable',
  299. :value => false,
  300. :notmatch => / listen \[::\]:443 ssl default ipv6only=on;/,
  301. },
  302. {
  303. :title => 'should set the IPv6 listen IP',
  304. :attr => 'ipv6_listen_ip',
  305. :value => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
  306. :match => ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:443 ssl default ipv6only=on;',
  307. },
  308. {
  309. :title => 'should set the IPv6 listen port',
  310. :attr => 'ssl_port',
  311. :value => 45,
  312. :match => ' listen [::]:45 ssl default ipv6only=on;',
  313. },
  314. {
  315. :title => 'should set the IPv6 listen options',
  316. :attr => 'ipv6_listen_options',
  317. :value => 'spdy default',
  318. :match => ' listen [::]:443 ssl spdy default;',
  319. },
  320. {
  321. :title => 'should set servername(s)',
  322. :attr => 'server_name',
  323. :value => ['name1','name2'],
  324. :match => ' server_name name1 name2;',
  325. },
  326. {
  327. :title => 'should rewrite www servername to non-www',
  328. :attr => 'rewrite_www_to_non_www',
  329. :value => true,
  330. :match => ' server_name rspec.example.com;',
  331. },
  332. {
  333. :title => 'should not rewrite www servername to non-www',
  334. :attr => 'rewrite_www_to_non_www',
  335. :value => false,
  336. :match => ' server_name www.rspec.example.com;',
  337. },
  338. {
  339. :title => 'should set the SSL cache',
  340. :attr => 'ssl_cache',
  341. :value => 'shared:SSL:1m',
  342. :match => ' ssl_session_cache shared:SSL:1m;',
  343. },
  344. {
  345. :title => 'should set the SSL protocols',
  346. :attr => 'ssl_protocols',
  347. :value => 'SSLv3',
  348. :match => ' ssl_protocols SSLv3;',
  349. },
  350. {
  351. :title => 'should set the SSL ciphers',
  352. :attr => 'ssl_ciphers',
  353. :value => 'HIGH',
  354. :match => ' ssl_ciphers HIGH;',
  355. },
  356. {
  357. :title => 'should set auth_basic',
  358. :attr => 'auth_basic',
  359. :value => 'value',
  360. :match => ' auth_basic "value";',
  361. },
  362. {
  363. :title => 'should set auth_basic_user_file',
  364. :attr => 'auth_basic_user_file',
  365. :value => 'value',
  366. :match => ' auth_basic_user_file "value";',
  367. },
  368. {
  369. :title => 'should set access_log',
  370. :attr => 'access_log',
  371. :value => '/path/to/access.log',
  372. :match => ' access_log /path/to/access.log;',
  373. },
  374. {
  375. :title => 'should set error_log',
  376. :attr => 'error_log',
  377. :value => '/path/to/error.log',
  378. :match => ' error_log /path/to/error.log;',
  379. },
  380. {
  381. :title => 'should contain ordered prepend directives',
  382. :attr => 'vhost_cfg_prepend',
  383. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' },
  384. :match => [
  385. ' allow test value 3;',
  386. ' test1 test value 1;',
  387. ' test2 test value 2a;',
  388. ' test2 test value 2b;',
  389. ]
  390. },
  391. {
  392. :title => 'should contain ordered ssl prepend directives',
  393. :attr => 'vhost_cfg_ssl_prepend',
  394. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' },
  395. :match => [
  396. ' allow test value 3;',
  397. ' test1 test value 1;',
  398. ' test2 test value 2a;',
  399. ' test2 test value 2b;',
  400. ]
  401. },
  402. {
  403. :title => 'should set root',
  404. :attr => 'use_default_location',
  405. :value => false,
  406. :match => ' root /;',
  407. },
  408. {
  409. :title => 'should not set root',
  410. :attr => 'use_default_location',
  411. :value => true,
  412. :notmatch => / root \/;/,
  413. },
  414. ].each do |param|
  415. context "when #{param[:attr]} is #{param[:value]}" do
  416. let :params do default_params.merge({
  417. param[:attr].to_sym => param[:value],
  418. :ssl => true,
  419. :ssl_key => 'dummy.key',
  420. :ssl_cert => 'dummy.crt',
  421. }) end
  422. it { should contain_concat__fragment("#{title}-ssl-header") }
  423. it param[:title] do
  424. lines = subject.resource('concat::fragment', "#{title}-ssl-header").send(:parameters)[:content].split("\n")
  425. (lines & Array(param[:match])).should == Array(param[:match])
  426. Array(param[:notmatch]).each do |item|
  427. should contain_concat__fragment("#{title}-ssl-header").without_content(item)
  428. end
  429. end
  430. end
  431. end
  432. end
  433. describe "vhost_ssl_footer template content" do
  434. [
  435. {
  436. :title => 'should contain include directives',
  437. :attr => 'include_files',
  438. :value => [ '/file1', '/file2' ],
  439. :match => [
  440. 'include /file1;',
  441. 'include /file2;',
  442. ],
  443. },
  444. {
  445. :title => 'should contain ordered appended directives',
  446. :attr => 'vhost_cfg_append',
  447. :value => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' },
  448. :match => [
  449. ' allow test value 3;',
  450. ' test1 test value 1;',
  451. ' test2 test value 2;',
  452. ]
  453. },
  454. {
  455. :title => 'should contain ordered ssl appended directives',
  456. :attr => 'vhost_cfg_ssl_append',
  457. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' },
  458. :match => [
  459. ' allow test value 3;',
  460. ' test1 test value 1;',
  461. ' test2 test value 2a;',
  462. ' test2 test value 2b;',
  463. ]
  464. },
  465. {
  466. :title => 'should contain www to non-www rewrite',
  467. :attr => 'rewrite_www_to_non_www',
  468. :value => true,
  469. :match => [
  470. ' listen *:443 ssl;',
  471. ' server_name www.rspec.example.com;',
  472. ' return 301 https://rspec.example.com$uri;',
  473. ],
  474. },
  475. {
  476. :title => 'should not contain www to non-www rewrite',
  477. :attr => 'rewrite_www_to_non_www',
  478. :value => false,
  479. :notmatch => [
  480. %r| listen \*:443 ssl;|,
  481. %r| server_name www\.rspec\.example\.com;|,
  482. %r| return 301 https://rspec\.example\.com\$uri;|
  483. ],
  484. },
  485. ].each do |param|
  486. context "when #{param[:attr]} is #{param[:value]}" do
  487. let :params do default_params.merge({
  488. param[:attr].to_sym => param[:value],
  489. :ssl => true,
  490. :ssl_key => 'dummy.key',
  491. :ssl_cert => 'dummy.crt',
  492. }) end
  493. it { should contain_concat__fragment("#{title}-ssl-footer") }
  494. it param[:title] do
  495. lines = subject.resource('concat::fragment', "#{title}-ssl-footer").send(:parameters)[:content].split("\n")
  496. (lines & Array(param[:match])).should == Array(param[:match])
  497. Array(param[:notmatch]).each do |item|
  498. should contain_concat__fragment("#{title}-ssl-footer").without_content(item)
  499. end
  500. end
  501. end
  502. end
  503. end
  504. context 'attribute resources' do
  505. context "SSL cert missing" do
  506. let(:params) {{ :ssl => true, :ssl_key => 'key' }}
  507. it { expect { should contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error) }
  508. end
  509. context "SSL key missing" do
  510. let(:params) {{ :ssl => true, :ssl_cert => 'cert' }}
  511. it { expect { should contain_class('nginx::resource::vhost') }.to raise_error(Puppet::Error) }
  512. end
  513. context 'when use_default_location => true' do
  514. let :params do default_params.merge({
  515. :use_default_location => true,
  516. }) end
  517. it { should contain_nginx__resource__location("#{title}-default") }
  518. end
  519. context 'when use_default_location => false' do
  520. let :params do default_params.merge({
  521. :use_default_location => false,
  522. }) end
  523. it { should_not contain_nginx__resource__location("#{title}-default") }
  524. end
  525. context 'when location_cfg_prepend => { key => value }' do
  526. let :params do default_params.merge({
  527. :location_cfg_prepend => { 'key' => 'value' },
  528. }) end
  529. it { should contain_nginx__resource__location("#{title}-default").with_location_cfg_prepend({ 'key' => 'value' }) }
  530. end
  531. context 'when location_cfg_append => { key => value }' do
  532. let :params do default_params.merge({
  533. :location_cfg_append => { 'key' => 'value' },
  534. }) end
  535. it { should contain_nginx__resource__location("#{title}-default").with_location_cfg_append({ 'key' => 'value' }) }
  536. end
  537. context 'when fastcgi => "localhost:9000"' do
  538. let :params do default_params.merge({
  539. :fastcgi => 'localhost:9000',
  540. }) end
  541. it { should contain_file('/etc/nginx/fastcgi_params').with_mode('0770') }
  542. end
  543. context 'when listen_port == ssl_port' do
  544. let :params do default_params.merge({
  545. :listen_port => 80,
  546. :ssl_port => 80,
  547. }) end
  548. it { should_not contain_concat__fragment("#{title}-header") }
  549. it { should_not contain_concat__fragment("#{title}-footer") }
  550. end
  551. context 'when listen_port != ssl_port' do
  552. let :params do default_params.merge({
  553. :listen_port => 80,
  554. :ssl_port => 443,
  555. }) end
  556. it { should contain_concat__fragment("#{title}-header") }
  557. it { should contain_concat__fragment("#{title}-footer") }
  558. end
  559. context 'when ensure => absent' do
  560. let :params do default_params.merge({
  561. :ensure => 'absent',
  562. :ssl => true,
  563. :ssl_key => 'dummy.key',
  564. :ssl_cert => 'dummy.cert',
  565. }) end
  566. it { should contain_nginx__resource__location("#{title}-default").with_ensure('absent') }
  567. it { should contain_file("#{title}.conf symlink").with_ensure('absent') }
  568. end
  569. context 'when ssl => true and ssl_port == listen_port' do
  570. let :params do default_params.merge({
  571. :ssl => true,
  572. :listen_port => 80,
  573. :ssl_port => 80,
  574. :ssl_key => 'dummy.key',
  575. :ssl_cert => 'dummy.cert',
  576. }) end
  577. it { should contain_nginx__resource__location("#{title}-default").with_ssl_only(true) }
  578. it { should contain_concat__fragment("#{title}-ssl-header").with_content(%r{access_log[ ]+/var/log/nginx/ssl-www\.rspec\.example\.com\.access\.log}) }
  579. it { should contain_concat__fragment("#{title}-ssl-header").with_content(%r{error_log[ ]+/var/log/nginx/ssl-www\.rspec\.example\.com\.error\.log}) }
  580. it { should contain_concat__fragment("#{title}-ssl-footer") }
  581. it { should contain_file("/etc/nginx/#{title}.crt") }
  582. it { should contain_file("/etc/nginx/#{title}.key") }
  583. end
  584. context 'when passenger_cgi_param is set' do
  585. let :params do default_params.merge({
  586. :passenger_cgi_param => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }
  587. }) end
  588. it { should contain_concat__fragment("#{title}-header").with_content( /passenger_set_cgi_param test1 test value 1;/ ) }
  589. it { should contain_concat__fragment("#{title}-header").with_content( /passenger_set_cgi_param test2 test value 2;/ ) }
  590. it { should contain_concat__fragment("#{title}-header").with_content( /passenger_set_cgi_param test3 test value 3;/ ) }
  591. end
  592. context 'when passenger_cgi_param is set and ssl => true' do
  593. let :params do default_params.merge({
  594. :passenger_cgi_param => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' },
  595. :ssl => true,
  596. :ssl_key => 'dummy.key',
  597. :ssl_cert => 'dummy.cert',
  598. }) end
  599. it { should contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_set_cgi_param test1 test value 1;/ ) }
  600. it { should contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_set_cgi_param test2 test value 2;/ ) }
  601. it { should contain_concat__fragment("#{title}-ssl-header").with_content( /passenger_set_cgi_param test3 test value 3;/ ) }
  602. end
  603. context 'when vhost name is sanitized' do
  604. let :title do 'www rspec-vhost com' end
  605. let :params do default_params end
  606. it { should contain_concat('/etc/nginx/sites-available/www_rspec-vhost_com.conf') }
  607. end
  608. end
  609. end
  610. end