config_spec.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. require 'spec_helper'
  2. describe 'nginx::config' do
  3. context 'with defaults' do
  4. it { is_expected.to contain_file("/etc/nginx").only_with(
  5. :path => "/etc/nginx",
  6. :ensure => 'directory',
  7. :owner => 'root',
  8. :group => 'root',
  9. :mode => '0644'
  10. )}
  11. it { is_expected.to contain_file("/etc/nginx/conf.d").only_with(
  12. :path => '/etc/nginx/conf.d',
  13. :ensure => 'directory',
  14. :owner => 'root',
  15. :group => 'root',
  16. :mode => '0644'
  17. )}
  18. it { is_expected.to contain_file("/etc/nginx/conf.mail.d").only_with(
  19. :path => '/etc/nginx/conf.mail.d',
  20. :ensure => 'directory',
  21. :owner => 'root',
  22. :group => 'root',
  23. :mode => '0644'
  24. )}
  25. it { is_expected.to contain_file("/etc/nginx/conf.d/vhost_autogen.conf").with_ensure('absent') }
  26. it { is_expected.to contain_file("/etc/nginx/conf.mail.d/vhost_autogen.conf").with_ensure('absent') }
  27. it { is_expected.to contain_file("/var/nginx").with(
  28. :ensure => 'directory',
  29. :owner => 'root',
  30. :group => 'root',
  31. :mode => '0644'
  32. )}
  33. it { is_expected.to contain_file("/var/nginx/client_body_temp").with(
  34. :ensure => 'directory',
  35. :group => 'root',
  36. :mode => '0644'
  37. )}
  38. it { is_expected.to contain_file("/var/nginx/proxy_temp").with(
  39. :ensure => 'directory',
  40. :group => 'root',
  41. :mode => '0644'
  42. )}
  43. it { is_expected.to contain_file('/etc/nginx/sites-enabled/default').with_ensure('absent') }
  44. it { is_expected.to contain_file("/etc/nginx/nginx.conf").with(
  45. :ensure => 'file',
  46. :owner => 'root',
  47. :group => 'root',
  48. :mode => '0644'
  49. )}
  50. it { is_expected.to contain_file("/tmp/nginx.d").with(
  51. :ensure => 'absent',
  52. :purge => true,
  53. :recurse => true
  54. )}
  55. it { is_expected.to contain_file("/tmp/nginx.mail.d").with(
  56. :ensure => 'absent',
  57. :purge => true,
  58. :recurse => true
  59. )}
  60. it { is_expected.to contain_file("/var/nginx/client_body_temp").with(:owner => 'nginx')}
  61. it { is_expected.to contain_file("/var/nginx/proxy_temp").with(:owner => 'nginx')}
  62. it { is_expected.to contain_file("/etc/nginx/nginx.conf").with_content %r{^user nginx;}}
  63. it { is_expected.to contain_file("/var/log/nginx").with(
  64. :ensure => 'directory',
  65. :group => 'root',
  66. :mode => '0644'
  67. )}
  68. describe "nginx.conf template content" do
  69. [
  70. {
  71. :title => 'should not set user',
  72. :attr => 'super_user',
  73. :value => false,
  74. :notmatch => /user/,
  75. },
  76. {
  77. :title => 'should set user',
  78. :attr => 'daemon_user',
  79. :value => 'test-user',
  80. :match => 'user test-user;',
  81. },
  82. {
  83. :title => 'should set worker_processes',
  84. :attr => 'worker_processes',
  85. :value => '4',
  86. :match => 'worker_processes 4;',
  87. },
  88. {
  89. :title => 'should set worker_processes',
  90. :attr => 'worker_processes',
  91. :value => 'auto',
  92. :match => 'worker_processes auto;',
  93. },
  94. {
  95. :title => 'should set worker_rlimit_nofile',
  96. :attr => 'worker_rlimit_nofile',
  97. :value => '10000',
  98. :match => 'worker_rlimit_nofile 10000;',
  99. },
  100. {
  101. :title => 'should set error_log',
  102. :attr => 'nginx_error_log',
  103. :value => '/path/to/error.log',
  104. :match => 'error_log /path/to/error.log error;',
  105. },
  106. {
  107. :title => 'should set error_log severity level',
  108. :attr => 'nginx_error_log_severity',
  109. :value => 'warn',
  110. :match => 'error_log /var/log/nginx/error.log warn;',
  111. },
  112. {
  113. :title => 'should set pid',
  114. :attr => 'pid',
  115. :value => '/path/to/pid',
  116. :match => 'pid /path/to/pid;',
  117. },
  118. {
  119. :title => 'should not set pid',
  120. :attr => 'pid',
  121. :value => false,
  122. :notmatch => /pid/,
  123. },
  124. {
  125. :title => 'should set worker_connections',
  126. :attr => 'worker_connections',
  127. :value => '100',
  128. :match => ' worker_connections 100;',
  129. },
  130. {
  131. :title => 'should set log formats',
  132. :attr => 'log_format',
  133. :value => {
  134. 'format1' => 'FORMAT1',
  135. 'format2' => 'FORMAT2',
  136. },
  137. :match => [
  138. ' log_format format1 \'FORMAT1\';',
  139. ' log_format format2 \'FORMAT2\';',
  140. ],
  141. },
  142. {
  143. :title => 'should not set log formats',
  144. :attr => 'log_format',
  145. :value => {},
  146. :notmatch => /log_format/,
  147. },
  148. {
  149. :title => 'should set multi_accept',
  150. :attr => 'multi_accept',
  151. :value => 'on',
  152. :match => /\s*multi_accept\s+on;/,
  153. },
  154. {
  155. :title => 'should not set multi_accept',
  156. :attr => 'multi_accept',
  157. :value => 'off',
  158. :notmatch => /multi_accept/,
  159. },
  160. {
  161. :title => 'should set events_use',
  162. :attr => 'events_use',
  163. :value => 'eventport',
  164. :match => /\s*use\s+eventport;/,
  165. },
  166. {
  167. :title => 'should not set events_use',
  168. :attr => 'events_use',
  169. :value => false,
  170. :notmatch => /use /,
  171. },
  172. {
  173. :title => 'should set access_log',
  174. :attr => 'http_access_log',
  175. :value => '/path/to/access.log',
  176. :match => ' access_log /path/to/access.log;',
  177. },
  178. {
  179. :title => 'should set sendfile',
  180. :attr => 'sendfile',
  181. :value => 'on',
  182. :match => ' sendfile on;',
  183. },
  184. {
  185. :title => 'should not set sendfile',
  186. :attr => 'sendfile',
  187. :value => false,
  188. :notmatch => /sendfile/,
  189. },
  190. {
  191. :title => 'should set server_tokens',
  192. :attr => 'server_tokens',
  193. :value => 'on',
  194. :match => ' server_tokens on;',
  195. },
  196. {
  197. :title => 'should set types_hash_max_size',
  198. :attr => 'types_hash_max_size',
  199. :value => 10,
  200. :match => ' types_hash_max_size 10;',
  201. },
  202. {
  203. :title => 'should set types_hash_bucket_size',
  204. :attr => 'types_hash_bucket_size',
  205. :value => 10,
  206. :match => ' types_hash_bucket_size 10;',
  207. },
  208. {
  209. :title => 'should set server_names_hash_bucket_size',
  210. :attr => 'names_hash_bucket_size',
  211. :value => 10,
  212. :match => ' server_names_hash_bucket_size 10;',
  213. },
  214. {
  215. :title => 'should set server_names_hash_max_size',
  216. :attr => 'names_hash_max_size',
  217. :value => 10,
  218. :match => ' server_names_hash_max_size 10;',
  219. },
  220. {
  221. :title => 'should set keepalive_timeout',
  222. :attr => 'keepalive_timeout',
  223. :value => '123',
  224. :match => ' keepalive_timeout 123;',
  225. },
  226. {
  227. :title => 'should set tcp_nodelay',
  228. :attr => 'http_tcp_nodelay',
  229. :value => 'on',
  230. :match => ' tcp_nodelay on;',
  231. },
  232. {
  233. :title => 'should set tcp_nopush',
  234. :attr => 'http_tcp_nopush',
  235. :value => 'on',
  236. :match => ' tcp_nopush on;',
  237. },
  238. {
  239. :title => 'should set gzip',
  240. :attr => 'gzip',
  241. :value => 'on',
  242. :match => ' gzip on;',
  243. },
  244. {
  245. :title => 'should not set gzip',
  246. :attr => 'gzip',
  247. :value => 'off',
  248. :notmatch => /gzip/,
  249. },
  250. {
  251. :title => 'should set gzip_buffers',
  252. :attr => 'gzip_buffers',
  253. :value => '32 4k',
  254. :match => ' gzip_buffers 32 4k;',
  255. },
  256. {
  257. :title => 'should set gzip_comp_level',
  258. :attr => 'gzip_comp_level',
  259. :value => 5,
  260. :match => ' gzip_comp_level 5;',
  261. },
  262. {
  263. :title => 'should set gzip_disable',
  264. :attr => 'gzip_disable',
  265. :value => 'MSIE [1-6]\.(?!.*SV1)',
  266. :match => ' gzip_disable MSIE [1-6]\.(?!.*SV1);',
  267. },
  268. {
  269. :title => 'should set gzip_min_length',
  270. :attr => 'gzip_min_length',
  271. :value => '10',
  272. :match => ' gzip_min_length 10;',
  273. },
  274. {
  275. :title => 'should set gzip_http_version',
  276. :attr => 'gzip_http_version',
  277. :value => '1.0',
  278. :match => ' gzip_http_version 1.0;',
  279. },
  280. {
  281. :title => 'should set gzip_proxied',
  282. :attr => 'gzip_proxied',
  283. :value => 'any',
  284. :match => ' gzip_proxied any;',
  285. },
  286. {
  287. :title => 'should set gzip_types (array)',
  288. :attr => 'gzip_types',
  289. :value => ['text/plain','text/html'],
  290. :match => ' gzip_types text/plain text/html;',
  291. },
  292. {
  293. :title => 'should set gzip_types (string)',
  294. :attr => 'gzip_types',
  295. :value => ['text/plain'],
  296. :match => ' gzip_types text/plain;',
  297. },
  298. {
  299. :title => 'should set gzip_vary',
  300. :attr => 'gzip_vary',
  301. :value => 'on',
  302. :match => ' gzip_vary on;',
  303. },
  304. {
  305. :title => 'should set proxy_cache_path',
  306. :attr => 'proxy_cache_path',
  307. :value => '/path/to/proxy.cache',
  308. :match => %r'\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m;',
  309. },
  310. {
  311. :title => 'should not set proxy_cache_path',
  312. :attr => 'proxy_cache_path',
  313. :value => false,
  314. :notmatch => /proxy_cache_path/,
  315. },
  316. {
  317. :title => 'should set fastcgi_cache_path',
  318. :attr => 'fastcgi_cache_path',
  319. :value => '/path/to/proxy.cache',
  320. :match => %r'\s*fastcgi_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d3:100m max_size=500m inactive=20m;',
  321. },
  322. {
  323. :title => 'should not set fastcgi_cache_path',
  324. :attr => 'fastcgi_cache_path',
  325. :value => false,
  326. :notmatch => /fastcgi_cache_path/,
  327. },
  328. {
  329. :title => 'should set fastcgi_cache_use_stale',
  330. :attr => 'fastcgi_cache_use_stale',
  331. :value => 'invalid_header',
  332. :match => ' fastcgi_cache_use_stale invalid_header;',
  333. },
  334. {
  335. :title => 'should not set fastcgi_cache_use_stale',
  336. :attr => 'fastcgi_cache_use_stale',
  337. :value => false,
  338. :notmatch => /fastcgi_cache_use_stale/,
  339. },
  340. {
  341. :title => 'should contain ordered appended directives from hash',
  342. :attr => 'http_cfg_append',
  343. :value => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' },
  344. :match => [
  345. ' allow test value 3;',
  346. ' test1 test value 1;',
  347. ' test2 test value 2;',
  348. ],
  349. },
  350. {
  351. :title => 'should contain duplicate appended directives from list of hashes',
  352. :attr => 'http_cfg_append',
  353. :value => [[ 'allow', 'test value 1'], ['allow', 'test value 2' ]],
  354. :match => [
  355. ' allow test value 1;',
  356. ' allow test value 2;',
  357. ],
  358. },
  359. {
  360. :title => 'should contain duplicate appended directives from array values',
  361. :attr => 'http_cfg_append',
  362. :value => { 'test1' => ['test value 1', 'test value 2', 'test value 3'] },
  363. :match => [
  364. ' test1 test value 1;',
  365. ' test1 test value 2;',
  366. ],
  367. },
  368. {
  369. :title => 'should contain ordered appended directives from hash',
  370. :attr => 'nginx_cfg_prepend',
  371. :value => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' },
  372. :match => [
  373. 'allow test value 3;',
  374. 'test1 test value 1;',
  375. 'test2 test value 2;',
  376. ],
  377. },
  378. {
  379. :title => 'should contain duplicate appended directives from list of hashes',
  380. :attr => 'nginx_cfg_prepend',
  381. :value => [[ 'allow', 'test value 1'], ['allow', 'test value 2' ]],
  382. :match => [
  383. 'allow test value 1;',
  384. 'allow test value 2;',
  385. ],
  386. },
  387. {
  388. :title => 'should contain duplicate appended directives from array values',
  389. :attr => 'nginx_cfg_prepend',
  390. :value => { 'test1' => ['test value 1', 'test value 2', 'test value 3'] },
  391. :match => [
  392. 'test1 test value 1;',
  393. 'test1 test value 2;',
  394. 'test1 test value 3;',
  395. ],
  396. },
  397. {
  398. :title => 'should set pid',
  399. :attr => 'pid',
  400. :value => '/path/to/pid',
  401. :match => 'pid /path/to/pid;',
  402. },
  403. {
  404. :title => 'should set tcp_nodelay',
  405. :attr => 'http_tcp_nodelay',
  406. :value => 'on',
  407. :match => ' tcp_nodelay on;',
  408. },
  409. {
  410. :title => 'should set tcp_nopush',
  411. :attr => 'http_tcp_nopush',
  412. :value => 'on',
  413. :match => ' tcp_nopush on;',
  414. },
  415. {
  416. :title => 'should set keepalive_timeout',
  417. :attr => 'keepalive_timeout',
  418. :value => '123',
  419. :match => ' keepalive_timeout 123;',
  420. },
  421. {
  422. :title => 'should set mail',
  423. :attr => 'mail',
  424. :value => true,
  425. :match => 'mail {',
  426. },
  427. {
  428. :title => 'should not set mail',
  429. :attr => 'mail',
  430. :value => false,
  431. :notmatch => /mail/,
  432. },
  433. {
  434. :title => 'should set proxy_buffers',
  435. :attr => 'proxy_buffers',
  436. :value => '50 5k',
  437. :match => ' proxy_buffers 50 5k;',
  438. },
  439. {
  440. :title => 'should set proxy_buffer_size',
  441. :attr => 'proxy_buffer_size',
  442. :value => '2k',
  443. :match => ' proxy_buffer_size 2k;',
  444. },
  445. {
  446. :title => 'should set proxy_http_version',
  447. :attr => 'proxy_http_version',
  448. :value => '1.1',
  449. :match => ' proxy_http_version 1.1;',
  450. },
  451. {
  452. :title => 'should not set proxy_http_version',
  453. :attr => 'proxy_http_version',
  454. :value => nil,
  455. :notmatch => 'proxy_http_version',
  456. },
  457. {
  458. :title => 'should contain ordered appended directives',
  459. :attr => 'proxy_set_header',
  460. :value => ['header1','header2'],
  461. :match => [
  462. ' proxy_set_header header1;',
  463. ' proxy_set_header header2;',
  464. ],
  465. },
  466. {
  467. :title => 'should set client_body_temp_path',
  468. :attr => 'client_body_temp_path',
  469. :value => '/path/to/body_temp',
  470. :match => ' client_body_temp_path /path/to/body_temp;',
  471. },
  472. {
  473. :title => 'should set proxy_temp_path',
  474. :attr => 'proxy_temp_path',
  475. :value => '/path/to/proxy_temp',
  476. :match => ' proxy_temp_path /path/to/proxy_temp;',
  477. },
  478. ].each do |param|
  479. context "when #{param[:attr]} is #{param[:value]}" do
  480. let :params do { param[:attr].to_sym => param[:value] } end
  481. it { is_expected.to contain_file("/etc/nginx/nginx.conf").with_mode('0644') }
  482. it param[:title] do
  483. matches = Array(param[:match])
  484. if matches.all? { |m| m.is_a? Regexp }
  485. matches.each { |item| is_expected.to contain_file('/etc/nginx/nginx.conf').with_content(item) }
  486. else
  487. lines = catalogue.resource('file', '/etc/nginx/nginx.conf').send(:parameters)[:content].split("\n")
  488. expect(lines & Array(param[:match])).to eq(Array(param[:match]))
  489. end
  490. Array(param[:notmatch]).each do |item|
  491. is_expected.to contain_file("/etc/nginx/nginx.conf").without_content(item)
  492. end
  493. end
  494. end
  495. end
  496. end
  497. context "when conf_dir is /path/to/nginx" do
  498. let(:params) {{:conf_dir => '/path/to/nginx'}}
  499. it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/mime\.types;}) }
  500. it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/conf\.d/\*\.conf;}) }
  501. it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/sites-enabled/\*;}) }
  502. end
  503. context "when confd_purge true" do
  504. let(:params) {{:confd_purge => true}}
  505. it { is_expected.to contain_file('/etc/nginx/conf.d').with(
  506. :purge => true,
  507. :recurse => true
  508. )}
  509. end
  510. context "when confd_purge false" do
  511. let(:params) {{:confd_purge => false}}
  512. it { is_expected.to contain_file('/etc/nginx/conf.d').without([
  513. 'ignore',
  514. 'purge',
  515. 'recurse'
  516. ])}
  517. end
  518. context "when vhost_purge true" do
  519. let(:params) {{:vhost_purge => true}}
  520. it { is_expected.to contain_file('/etc/nginx/sites-available').with(
  521. :purge => true,
  522. :recurse => true
  523. )}
  524. it { is_expected.to contain_file('/etc/nginx/sites-enabled').with(
  525. :purge => true,
  526. :recurse => true
  527. )}
  528. end
  529. context "when vhost_purge false" do
  530. let(:params) {{:vhost_purge => false}}
  531. it { is_expected.to contain_file('/etc/nginx/sites-available').without([
  532. 'ignore',
  533. 'purge',
  534. 'recurse'
  535. ])}
  536. it { is_expected.to contain_file('/etc/nginx/sites-enabled').without([
  537. 'ignore',
  538. 'purge',
  539. 'recurse'
  540. ])}
  541. it { is_expected.to contain_file('/var/log/nginx').without([
  542. 'ignore',
  543. 'purge',
  544. 'recurse'
  545. ])}
  546. end
  547. context "when daemon_user = www-data" do
  548. let :params do
  549. {
  550. :daemon_user => 'www-data',
  551. }
  552. end
  553. it { is_expected.to contain_file("/var/nginx/client_body_temp").with(:owner => 'www-data')}
  554. it { is_expected.to contain_file("/var/nginx/proxy_temp").with(:owner => 'www-data')}
  555. it { is_expected.to contain_file("/etc/nginx/nginx.conf").with_content %r{^user www-data;}}
  556. end
  557. context "when nginx_error_log_severity = invalid" do
  558. let(:params) {{:nginx_error_log_severity => 'invalid'}}
  559. it { expect { is_expected.to contain_class('nginx::config') }.to raise_error(Puppet::Error,/\$nginx_error_log_severity must be debug, info, notice, warn, error, crit, alert or emerg/) }
  560. end
  561. end
  562. end