config_spec.rb 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 accept_mutex on',
  126. :attr => 'accept_mutex',
  127. :value => 'on',
  128. :match => ' accept_mutex on;',
  129. },
  130. {
  131. :title => 'should set accept_mutex off',
  132. :attr => 'accept_mutex',
  133. :value => 'off',
  134. :match => ' accept_mutex off;',
  135. },
  136. {
  137. :title => 'should set accept_mutex_delay',
  138. :attr => 'accept_mutex_delay',
  139. :value => '500s',
  140. :match => ' accept_mutex_delay 500s;',
  141. },
  142. {
  143. :title => 'should set worker_connections',
  144. :attr => 'worker_connections',
  145. :value => '100',
  146. :match => ' worker_connections 100;',
  147. },
  148. {
  149. :title => 'should set log formats',
  150. :attr => 'log_format',
  151. :value => {
  152. 'format1' => 'FORMAT1',
  153. 'format2' => 'FORMAT2',
  154. },
  155. :match => [
  156. ' log_format format1 \'FORMAT1\';',
  157. ' log_format format2 \'FORMAT2\';',
  158. ],
  159. },
  160. {
  161. :title => 'should not set log formats',
  162. :attr => 'log_format',
  163. :value => {},
  164. :notmatch => /log_format/,
  165. },
  166. {
  167. :title => 'should set multi_accept',
  168. :attr => 'multi_accept',
  169. :value => 'on',
  170. :match => /\s*multi_accept\s+on;/,
  171. },
  172. {
  173. :title => 'should not set multi_accept',
  174. :attr => 'multi_accept',
  175. :value => 'off',
  176. :notmatch => /multi_accept/,
  177. },
  178. {
  179. :title => 'should set events_use',
  180. :attr => 'events_use',
  181. :value => 'eventport',
  182. :match => /\s*use\s+eventport;/,
  183. },
  184. {
  185. :title => 'should not set events_use',
  186. :attr => 'events_use',
  187. :value => false,
  188. :notmatch => /use /,
  189. },
  190. {
  191. :title => 'should set access_log',
  192. :attr => 'http_access_log',
  193. :value => '/path/to/access.log',
  194. :match => ' access_log /path/to/access.log;',
  195. },
  196. {
  197. :title => 'should set sendfile',
  198. :attr => 'sendfile',
  199. :value => 'on',
  200. :match => ' sendfile on;',
  201. },
  202. {
  203. :title => 'should not set sendfile',
  204. :attr => 'sendfile',
  205. :value => false,
  206. :notmatch => /sendfile/,
  207. },
  208. {
  209. :title => 'should set server_tokens',
  210. :attr => 'server_tokens',
  211. :value => 'on',
  212. :match => ' server_tokens on;',
  213. },
  214. {
  215. :title => 'should set types_hash_max_size',
  216. :attr => 'types_hash_max_size',
  217. :value => 10,
  218. :match => ' types_hash_max_size 10;',
  219. },
  220. {
  221. :title => 'should set types_hash_bucket_size',
  222. :attr => 'types_hash_bucket_size',
  223. :value => 10,
  224. :match => ' types_hash_bucket_size 10;',
  225. },
  226. {
  227. :title => 'should set server_names_hash_bucket_size',
  228. :attr => 'names_hash_bucket_size',
  229. :value => 10,
  230. :match => ' server_names_hash_bucket_size 10;',
  231. },
  232. {
  233. :title => 'should set server_names_hash_max_size',
  234. :attr => 'names_hash_max_size',
  235. :value => 10,
  236. :match => ' server_names_hash_max_size 10;',
  237. },
  238. {
  239. :title => 'should set keepalive_timeout',
  240. :attr => 'keepalive_timeout',
  241. :value => '123',
  242. :match => ' keepalive_timeout 123;',
  243. },
  244. {
  245. :title => 'should set tcp_nodelay',
  246. :attr => 'http_tcp_nodelay',
  247. :value => 'on',
  248. :match => ' tcp_nodelay on;',
  249. },
  250. {
  251. :title => 'should set tcp_nopush',
  252. :attr => 'http_tcp_nopush',
  253. :value => 'on',
  254. :match => ' tcp_nopush on;',
  255. },
  256. {
  257. :title => 'should set gzip',
  258. :attr => 'gzip',
  259. :value => 'on',
  260. :match => ' gzip on;',
  261. },
  262. {
  263. :title => 'should not set gzip',
  264. :attr => 'gzip',
  265. :value => 'off',
  266. :notmatch => /gzip/,
  267. },
  268. {
  269. :title => 'should set gzip_buffers',
  270. :attr => 'gzip_buffers',
  271. :value => '32 4k',
  272. :match => ' gzip_buffers 32 4k;',
  273. },
  274. {
  275. :title => 'should set gzip_comp_level',
  276. :attr => 'gzip_comp_level',
  277. :value => 5,
  278. :match => ' gzip_comp_level 5;',
  279. },
  280. {
  281. :title => 'should set gzip_disable',
  282. :attr => 'gzip_disable',
  283. :value => 'MSIE [1-6]\.(?!.*SV1)',
  284. :match => ' gzip_disable MSIE [1-6]\.(?!.*SV1);',
  285. },
  286. {
  287. :title => 'should set gzip_min_length',
  288. :attr => 'gzip_min_length',
  289. :value => '10',
  290. :match => ' gzip_min_length 10;',
  291. },
  292. {
  293. :title => 'should set gzip_http_version',
  294. :attr => 'gzip_http_version',
  295. :value => '1.0',
  296. :match => ' gzip_http_version 1.0;',
  297. },
  298. {
  299. :title => 'should set gzip_proxied',
  300. :attr => 'gzip_proxied',
  301. :value => 'any',
  302. :match => ' gzip_proxied any;',
  303. },
  304. {
  305. :title => 'should set gzip_types (array)',
  306. :attr => 'gzip_types',
  307. :value => ['text/plain','text/html'],
  308. :match => ' gzip_types text/plain text/html;',
  309. },
  310. {
  311. :title => 'should set gzip_types (string)',
  312. :attr => 'gzip_types',
  313. :value => ['text/plain'],
  314. :match => ' gzip_types text/plain;',
  315. },
  316. {
  317. :title => 'should set gzip_vary',
  318. :attr => 'gzip_vary',
  319. :value => 'on',
  320. :match => ' gzip_vary on;',
  321. },
  322. {
  323. :title => 'should set proxy_cache_path',
  324. :attr => 'proxy_cache_path',
  325. :value => '/path/to/proxy.cache',
  326. :match => %r'\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m;',
  327. },
  328. {
  329. :title => 'should not set proxy_cache_path',
  330. :attr => 'proxy_cache_path',
  331. :value => false,
  332. :notmatch => /proxy_cache_path/,
  333. },
  334. {
  335. :title => 'should set fastcgi_cache_path',
  336. :attr => 'fastcgi_cache_path',
  337. :value => '/path/to/proxy.cache',
  338. :match => %r'\s*fastcgi_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d3:100m max_size=500m inactive=20m;',
  339. },
  340. {
  341. :title => 'should not set fastcgi_cache_path',
  342. :attr => 'fastcgi_cache_path',
  343. :value => false,
  344. :notmatch => /fastcgi_cache_path/,
  345. },
  346. {
  347. :title => 'should set fastcgi_cache_use_stale',
  348. :attr => 'fastcgi_cache_use_stale',
  349. :value => 'invalid_header',
  350. :match => ' fastcgi_cache_use_stale invalid_header;',
  351. },
  352. {
  353. :title => 'should not set fastcgi_cache_use_stale',
  354. :attr => 'fastcgi_cache_use_stale',
  355. :value => false,
  356. :notmatch => /fastcgi_cache_use_stale/,
  357. },
  358. {
  359. :title => 'should contain ordered appended directives from hash',
  360. :attr => 'http_cfg_append',
  361. :value => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' },
  362. :match => [
  363. ' allow test value 3;',
  364. ' test1 test value 1;',
  365. ' test2 test value 2;',
  366. ],
  367. },
  368. {
  369. :title => 'should contain duplicate appended directives from list of hashes',
  370. :attr => 'http_cfg_append',
  371. :value => [[ 'allow', 'test value 1'], ['allow', 'test value 2' ]],
  372. :match => [
  373. ' allow test value 1;',
  374. ' allow test value 2;',
  375. ],
  376. },
  377. {
  378. :title => 'should contain duplicate appended directives from array values',
  379. :attr => 'http_cfg_append',
  380. :value => { 'test1' => ['test value 1', 'test value 2', 'test value 3'] },
  381. :match => [
  382. ' test1 test value 1;',
  383. ' test1 test value 2;',
  384. ],
  385. },
  386. {
  387. :title => 'should contain ordered appended directives from hash',
  388. :attr => 'nginx_cfg_prepend',
  389. :value => { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' },
  390. :match => [
  391. 'allow test value 3;',
  392. 'test1 test value 1;',
  393. 'test2 test value 2;',
  394. ],
  395. },
  396. {
  397. :title => 'should contain duplicate appended directives from list of hashes',
  398. :attr => 'nginx_cfg_prepend',
  399. :value => [[ 'allow', 'test value 1'], ['allow', 'test value 2' ]],
  400. :match => [
  401. 'allow test value 1;',
  402. 'allow test value 2;',
  403. ],
  404. },
  405. {
  406. :title => 'should contain duplicate appended directives from array values',
  407. :attr => 'nginx_cfg_prepend',
  408. :value => { 'test1' => ['test value 1', 'test value 2', 'test value 3'] },
  409. :match => [
  410. 'test1 test value 1;',
  411. 'test1 test value 2;',
  412. 'test1 test value 3;',
  413. ],
  414. },
  415. {
  416. :title => 'should set pid',
  417. :attr => 'pid',
  418. :value => '/path/to/pid',
  419. :match => 'pid /path/to/pid;',
  420. },
  421. {
  422. :title => 'should set tcp_nodelay',
  423. :attr => 'http_tcp_nodelay',
  424. :value => 'on',
  425. :match => ' tcp_nodelay on;',
  426. },
  427. {
  428. :title => 'should set tcp_nopush',
  429. :attr => 'http_tcp_nopush',
  430. :value => 'on',
  431. :match => ' tcp_nopush on;',
  432. },
  433. {
  434. :title => 'should set keepalive_timeout',
  435. :attr => 'keepalive_timeout',
  436. :value => '123',
  437. :match => ' keepalive_timeout 123;',
  438. },
  439. {
  440. :title => 'should set mail',
  441. :attr => 'mail',
  442. :value => true,
  443. :match => 'mail {',
  444. },
  445. {
  446. :title => 'should not set mail',
  447. :attr => 'mail',
  448. :value => false,
  449. :notmatch => /mail/,
  450. },
  451. {
  452. :title => 'should set proxy_buffers',
  453. :attr => 'proxy_buffers',
  454. :value => '50 5k',
  455. :match => ' proxy_buffers 50 5k;',
  456. },
  457. {
  458. :title => 'should set proxy_buffer_size',
  459. :attr => 'proxy_buffer_size',
  460. :value => '2k',
  461. :match => ' proxy_buffer_size 2k;',
  462. },
  463. {
  464. :title => 'should set proxy_http_version',
  465. :attr => 'proxy_http_version',
  466. :value => '1.1',
  467. :match => ' proxy_http_version 1.1;',
  468. },
  469. {
  470. :title => 'should not set proxy_http_version',
  471. :attr => 'proxy_http_version',
  472. :value => nil,
  473. :notmatch => 'proxy_http_version',
  474. },
  475. {
  476. :title => 'should contain ordered appended directives',
  477. :attr => 'proxy_set_header',
  478. :value => ['header1','header2'],
  479. :match => [
  480. ' proxy_set_header header1;',
  481. ' proxy_set_header header2;',
  482. ],
  483. },
  484. {
  485. :title => 'should set client_body_temp_path',
  486. :attr => 'client_body_temp_path',
  487. :value => '/path/to/body_temp',
  488. :match => ' client_body_temp_path /path/to/body_temp;',
  489. },
  490. {
  491. :title => 'should set proxy_temp_path',
  492. :attr => 'proxy_temp_path',
  493. :value => '/path/to/proxy_temp',
  494. :match => ' proxy_temp_path /path/to/proxy_temp;',
  495. },
  496. ].each do |param|
  497. context "when #{param[:attr]} is #{param[:value]}" do
  498. let :params do { param[:attr].to_sym => param[:value] } end
  499. it { is_expected.to contain_file("/etc/nginx/nginx.conf").with_mode('0644') }
  500. it param[:title] do
  501. matches = Array(param[:match])
  502. if matches.all? { |m| m.is_a? Regexp }
  503. matches.each { |item| is_expected.to contain_file('/etc/nginx/nginx.conf').with_content(item) }
  504. else
  505. lines = catalogue.resource('file', '/etc/nginx/nginx.conf').send(:parameters)[:content].split("\n")
  506. expect(lines & Array(param[:match])).to eq(Array(param[:match]))
  507. end
  508. Array(param[:notmatch]).each do |item|
  509. is_expected.to contain_file("/etc/nginx/nginx.conf").without_content(item)
  510. end
  511. end
  512. end
  513. end
  514. end
  515. context "when conf_dir is /path/to/nginx" do
  516. let(:params) {{:conf_dir => '/path/to/nginx'}}
  517. it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/mime\.types;}) }
  518. it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/conf\.d/\*\.conf;}) }
  519. it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/sites-enabled/\*;}) }
  520. end
  521. context "when confd_purge true" do
  522. let(:params) {{:confd_purge => true}}
  523. it { is_expected.to contain_file('/etc/nginx/conf.d').with(
  524. :purge => true,
  525. :recurse => true
  526. )}
  527. end
  528. context "when confd_purge false" do
  529. let(:params) {{:confd_purge => false}}
  530. it { is_expected.to contain_file('/etc/nginx/conf.d').without([
  531. 'ignore',
  532. 'purge',
  533. 'recurse'
  534. ])}
  535. end
  536. context "when vhost_purge true" do
  537. let(:params) {{:vhost_purge => true}}
  538. it { is_expected.to contain_file('/etc/nginx/sites-available').with(
  539. :purge => true,
  540. :recurse => true
  541. )}
  542. it { is_expected.to contain_file('/etc/nginx/sites-enabled').with(
  543. :purge => true,
  544. :recurse => true
  545. )}
  546. end
  547. context "when vhost_purge false" do
  548. let(:params) {{:vhost_purge => false}}
  549. it { is_expected.to contain_file('/etc/nginx/sites-available').without([
  550. 'ignore',
  551. 'purge',
  552. 'recurse'
  553. ])}
  554. it { is_expected.to contain_file('/etc/nginx/sites-enabled').without([
  555. 'ignore',
  556. 'purge',
  557. 'recurse'
  558. ])}
  559. it { is_expected.to contain_file('/var/log/nginx').without([
  560. 'ignore',
  561. 'purge',
  562. 'recurse'
  563. ])}
  564. end
  565. context "when daemon_user = www-data" do
  566. let :params do
  567. {
  568. :daemon_user => 'www-data',
  569. }
  570. end
  571. it { is_expected.to contain_file("/var/nginx/client_body_temp").with(:owner => 'www-data')}
  572. it { is_expected.to contain_file("/var/nginx/proxy_temp").with(:owner => 'www-data')}
  573. it { is_expected.to contain_file("/etc/nginx/nginx.conf").with_content %r{^user www-data;}}
  574. end
  575. context "when nginx_error_log_severity = invalid" do
  576. let(:params) {{:nginx_error_log_severity => 'invalid'}}
  577. 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/) }
  578. end
  579. end
  580. end