resource_location_spec.rb 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. require 'spec_helper'
  2. require 'digest/md5'
  3. describe 'nginx::resource::location' do
  4. let :title do
  5. 'rspec-test'
  6. end
  7. let :facts do
  8. {
  9. :osfamily => 'Debian',
  10. :operatingsystem => 'Debian',
  11. }
  12. end
  13. let :pre_condition do
  14. [
  15. 'include ::nginx::config',
  16. ]
  17. end
  18. describe 'os-independent items' do
  19. describe 'basic assumptions' do
  20. let :params do {
  21. :www_root => "/var/www/rspec",
  22. :vhost => 'vhost1',
  23. } end
  24. it { is_expected.to contain_class("nginx::config") }
  25. it { is_expected.to contain_concat__fragment("f25e14942fb58942ee13b1465a4e1719").with_content(/location rspec-test/) }
  26. it { is_expected.not_to contain_file('/etc/nginx/fastcgi_params') }
  27. it { is_expected.not_to contain_concat__fragment("vhost1-800-rspec-test-ssl") }
  28. it { is_expected.not_to contain_file("/etc/nginx/rspec-test_htpasswd") }
  29. end
  30. describe "vhost/location_header template content" do
  31. [
  32. {
  33. :title => 'should set the location',
  34. :attr => 'location',
  35. :value => 'my_location',
  36. :match => ' location my_location {',
  37. },
  38. {
  39. :title => 'should not set internal',
  40. :attr => 'internal',
  41. :value => false,
  42. :notmatch => /internal;/
  43. },
  44. {
  45. :title => 'should set internal',
  46. :attr => 'internal',
  47. :value => true,
  48. :match => ' internal;'
  49. },
  50. {
  51. :title => 'should not set mp4',
  52. :attr => 'mp4',
  53. :value => false,
  54. :notmatch => /mp4;/
  55. },
  56. {
  57. :title => 'should set mp4',
  58. :attr => 'mp4',
  59. :value => true,
  60. :match => ' mp4;'
  61. },
  62. {
  63. :title => 'should not set flv',
  64. :attr => 'flv',
  65. :value => false,
  66. :notmatch => /flv;/
  67. },
  68. {
  69. :title => 'should set flv',
  70. :attr => 'flv',
  71. :value => true,
  72. :match => ' flv;'
  73. },
  74. {
  75. :title => 'should set location_allow',
  76. :attr => 'location_allow',
  77. :value => %w( 127.0.0.1 10.0.0.1 ),
  78. :match => [
  79. ' allow 127.0.0.1;',
  80. ' allow 10.0.0.1;',
  81. ],
  82. },
  83. {
  84. :title => 'should set location_deny',
  85. :attr => 'location_deny',
  86. :value => %w( 127.0.0.1 10.0.0.1 ),
  87. :match => [
  88. ' deny 127.0.0.1;',
  89. ' deny 10.0.0.1;',
  90. ],
  91. },
  92. {
  93. :title => 'should contain ordered prepended directives',
  94. :attr => 'location_cfg_prepend',
  95. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'],
  96. 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'],
  97. 'subtest2' => '"sub test value2"' } },
  98. :match => [
  99. ' test1 test value 1;',
  100. ' test2 test value 2a;',
  101. ' test2 test value 2b;',
  102. ' test3 subtest1 "sub test value1a";',
  103. ' test3 subtest1 "sub test value1b";',
  104. ' test3 subtest2 "sub test value2";',
  105. ],
  106. },
  107. {
  108. :title => 'should contain custom prepended directives',
  109. :attr => 'location_custom_cfg_prepend',
  110. :value => { 'test1' => 'bar', 'test2' => ['foobar', 'barbaz'],
  111. 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'],
  112. 'subtest2' => '"sub test value2"' } },
  113. :match => [
  114. /^[ ]+test1\s+bar/,
  115. /^[ ]+test2\s+foobar/,
  116. /^[ ]+test2\s+barbaz/,
  117. /^[ ]+test3\s+subtest1 "sub test value1a"/,
  118. /^[ ]+test3\s+subtest1 "sub test value1b"/,
  119. /^[ ]+test3\s+subtest2 "sub test value2"/,
  120. ],
  121. },
  122. {
  123. :title => 'should contain raw_prepend directives',
  124. :attr => 'raw_prepend',
  125. :value => [
  126. 'if (a) {',
  127. ' b;',
  128. '}'
  129. ],
  130. :match => /^\s+if \(a\) {\n\s++b;\n\s+\}/,
  131. },
  132. ].each do |param|
  133. context "when #{param[:attr]} is #{param[:value]}" do
  134. let :default_params do { :location => 'location', :proxy => 'proxy_value', :vhost => 'vhost1' } end
  135. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  136. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
  137. it param[:title] do
  138. fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
  139. matches = Array(param[:match])
  140. if matches.all? { |m| m.is_a? Regexp }
  141. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  142. else
  143. lines = subject.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  144. expect(lines & matches).to eq(matches)
  145. end
  146. Array(param[:notmatch]).each do |item|
  147. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
  148. end
  149. end
  150. end
  151. end
  152. end
  153. describe "vhost/location_footer template content" do
  154. [
  155. {
  156. :title => 'should contain ordered appended directives',
  157. :attr => 'location_cfg_append',
  158. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'],
  159. 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'],
  160. 'subtest2' => '"sub test value2"' } },
  161. :match => [
  162. ' test1 test value 1;',
  163. ' test2 test value 2a;',
  164. ' test2 test value 2b;',
  165. ' test3 subtest1 "sub test value1a";',
  166. ' test3 subtest1 "sub test value1b";',
  167. ' test3 subtest2 "sub test value2";',
  168. ],
  169. },
  170. {
  171. :title => 'should contain include directives',
  172. :attr => 'include',
  173. :value => [ '/file1', '/file2' ],
  174. :match => [
  175. %r'^\s+include\s+/file1;',
  176. %r'^\s+include\s+/file2;',
  177. ],
  178. },
  179. {
  180. :title => 'should contain custom appended directives',
  181. :attr => 'location_custom_cfg_append',
  182. :value => { 'test1' => 'bar', 'test2' => ['foobar', 'barbaz'],
  183. 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'],
  184. 'subtest2' => '"sub test value2"' } },
  185. :match => [
  186. /^[ ]+test1\s+bar/,
  187. /^[ ]+test2\s+foobar/,
  188. /^[ ]+test2\s+barbaz/,
  189. /^[ ]+test3\s+subtest1 "sub test value1a"/,
  190. /^[ ]+test3\s+subtest1 "sub test value1b"/,
  191. /^[ ]+test3\s+subtest2 "sub test value2"/,
  192. ],
  193. },
  194. {
  195. :title => 'should contain raw_append directives',
  196. :attr => 'raw_append',
  197. :value => [
  198. 'if (a) {',
  199. ' b;',
  200. '}'
  201. ],
  202. :match => /^\s+if \(a\) {\n\s++b;\n\s+\}/,
  203. },
  204. ].each do |param|
  205. context "when #{param[:attr]} is #{param[:value]}" do
  206. let :default_params do { :location => 'location', :proxy => 'proxy_value', :vhost => 'vhost1' } end
  207. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  208. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
  209. it param[:title] do
  210. fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
  211. matches = Array(param[:match])
  212. if matches.all? { |m| m.is_a? Regexp }
  213. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  214. else
  215. lines = subject.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  216. expect(lines & matches).to eq(matches)
  217. end
  218. Array(param[:notmatch]).each do |item|
  219. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
  220. end
  221. end
  222. it "should end with a closing brace" do
  223. fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
  224. content = subject.resource('concat::fragment', fragment).send(:parameters)[:content]
  225. expect((content.split("\n").reject {|l| l =~ /^(\s*#|$)/ }.last).strip).to eq('}')
  226. end
  227. end
  228. end
  229. end
  230. describe "vhost_location_alias template content" do
  231. let :default_params do
  232. { :location => 'location', :vhost => 'vhost1', :location_alias => 'value' }
  233. end
  234. context "when location_alias is 'value'" do
  235. let :params do default_params end
  236. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
  237. it "should set alias" do
  238. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
  239. with_content(/^[ ]+alias\s+value;/)
  240. end
  241. end
  242. context "when autoindex is 'on'" do
  243. let :params do default_params.merge({ :autoindex => 'on' }) end
  244. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
  245. it "should set autoindex" do
  246. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
  247. with_content(/^[ ]+autoindex\s+on;/)
  248. end
  249. end
  250. context "when autoindex is not set" do
  251. let :params do default_params end
  252. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
  253. it "should not set autoindex" do
  254. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
  255. without_content(/^[ ]+autoindex[^;]+;/)
  256. end
  257. end
  258. end
  259. describe "vhost_location_directory template content" do
  260. let :default_params do
  261. {
  262. :location => 'location',
  263. :www_root => '/var/www/root',
  264. :vhost => 'vhost1',
  265. }
  266. end
  267. [
  268. {
  269. :title => 'should set www_root',
  270. :attr => 'www_root',
  271. :value => '/',
  272. :match => ' root /;'
  273. },
  274. {
  275. :title => 'should set try_file(s)',
  276. :attr => 'try_files',
  277. :value => ['name1','name2'],
  278. :match => ' try_files name1 name2;',
  279. },
  280. {
  281. :title => 'should set index_file(s)',
  282. :attr => 'index_files',
  283. :value => ['name1','name2'],
  284. :match => ' index name1 name2;',
  285. },
  286. {
  287. :title => 'should contain rewrite rules',
  288. :attr => 'rewrite_rules',
  289. :value => [
  290. '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last',
  291. '^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last',
  292. '^/users/(.*)$ /show?user=$1? last',
  293. ],
  294. :match => [
  295. ' rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;',
  296. ' rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;',
  297. ' rewrite ^/users/(.*)$ /show?user=$1? last;',
  298. ],
  299. },
  300. {
  301. :title => 'should not set rewrite_rules',
  302. :attr => 'rewrite_rules',
  303. :value => [],
  304. :notmatch => /rewrite/
  305. },
  306. {
  307. :title => 'should set auth_basic',
  308. :attr => 'auth_basic',
  309. :value => 'value',
  310. :match => ' auth_basic "value";',
  311. },
  312. {
  313. :title => 'should set auth_basic_user_file',
  314. :attr => 'auth_basic_user_file',
  315. :value => 'value',
  316. :match => ' auth_basic_user_file value;',
  317. },
  318. ].each do |param|
  319. context "when #{param[:attr]} is #{param[:value]}" do
  320. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  321. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
  322. it param[:title] do
  323. fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
  324. matches = Array(param[:match])
  325. if matches.all? { |m| m.is_a? Regexp }
  326. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  327. else
  328. lines = subject.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  329. expect(lines & matches).to eq(matches)
  330. end
  331. Array(param[:notmatch]).each do |item|
  332. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
  333. end
  334. end
  335. end
  336. end
  337. context "when autoindex is 'on'" do
  338. let :params do default_params.merge({ :autoindex => 'on' }) end
  339. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
  340. it "should set autoindex" do
  341. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
  342. with_content(/^[ ]+autoindex\s+on;/)
  343. end
  344. end
  345. context "when autoindex is not set" do
  346. let :params do default_params end
  347. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
  348. it "should not set autoindex" do
  349. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
  350. without_content(/^[ ]+autoindex[^;]+;/)
  351. end
  352. end
  353. end
  354. describe "vhost_location_empty template content" do
  355. [
  356. {
  357. :title => 'should contain ordered config directives',
  358. :attr => 'location_custom_cfg',
  359. :value => { 'test1' => ['test value 1a', 'test value 1b'], 'test2' => 'test value 2', 'allow' => 'test value 3',
  360. 'test4' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'],
  361. 'subtest2' => '"sub test value2"' } },
  362. :match => [
  363. ' allow test value 3;',
  364. ' test1 test value 1a;',
  365. ' test1 test value 1b;',
  366. ' test2 test value 2;',
  367. ' test4 subtest1 "sub test value1a";',
  368. ' test4 subtest1 "sub test value1b";',
  369. ' test4 subtest2 "sub test value2";',
  370. ],
  371. },
  372. ].each do |param|
  373. context "when #{param[:attr]} is #{param[:value]}" do
  374. let :default_params do { :location => 'location', :location_custom_cfg => {'test1'=>'value1'}, :vhost => 'vhost1' } end
  375. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  376. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
  377. it param[:title] do
  378. fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
  379. matches = Array(param[:match])
  380. if matches.all? { |m| m.is_a? Regexp }
  381. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  382. else
  383. lines = subject.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  384. expect(lines & matches).to eq(matches)
  385. end
  386. Array(param[:notmatch]).each do |item|
  387. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
  388. end
  389. end
  390. end
  391. end
  392. end
  393. describe "vhost_location_fastcgi template content" do
  394. let :default_params do
  395. {
  396. :location => 'location',
  397. :fastcgi => 'localhost:9000',
  398. :vhost => 'vhost1'
  399. }
  400. end
  401. [
  402. {
  403. :title => 'should set www_root',
  404. :attr => 'www_root',
  405. :value => '/',
  406. :match => %r'\s+root\s+/;'
  407. },
  408. {
  409. :title => 'should set fastcgi_split_path',
  410. :attr => 'fastcgi_split_path',
  411. :value => 'value',
  412. :match => %r'\s+fastcgi_split_path_info\s+value;'
  413. },
  414. {
  415. :title => 'should set try_file(s)',
  416. :attr => 'try_files',
  417. :value => ['name1','name2'],
  418. :match => %r'\s+try_files\s+name1 name2;',
  419. },
  420. {
  421. :title => 'should set fastcgi_params',
  422. :attr => 'fastcgi_params',
  423. :value => 'value',
  424. :match => %r'\s+include\s+value;'
  425. },
  426. {
  427. :title => 'should set fastcgi_pass',
  428. :attr => 'fastcgi',
  429. :value => 'value',
  430. :match => %r'\s+fastcgi_pass\s+value;'
  431. },
  432. ].each do |param|
  433. context "when #{param[:attr]} is #{param[:value]}" do
  434. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  435. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
  436. it param[:title] do
  437. fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
  438. matches = Array(param[:match])
  439. if matches.all? { |m| m.is_a? Regexp }
  440. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  441. else
  442. lines = subject.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  443. expect(lines & matches).to eq(matches)
  444. end
  445. Array(param[:notmatch]).each do |item|
  446. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
  447. end
  448. end
  449. end
  450. end
  451. context "when fastcgi_script is 'value'" do
  452. let :params do default_params.merge({ :fastcgi_script => 'value' }) end
  453. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
  454. it "should set fastcgi_script" do
  455. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
  456. with_content(%r|^[ ]+fastcgi_param\s+SCRIPT_FILENAME\s+value;|)
  457. end
  458. end
  459. context "when fastcgi_script is not set" do
  460. let :params do default_params end
  461. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
  462. it "should not set fastcgi_script" do
  463. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
  464. without_content(/^[ ]+fastcgi_param\s+SCRIPT_FILENAME\s+.+?;/)
  465. end
  466. end
  467. context "when fastcgi_param is {'CUSTOM_PARAM' => 'value'}" do
  468. let :params do default_params.merge({ :fastcgi_param => {'CUSTOM_PARAM' => 'value', 'CUSTOM_PARAM2' => 'value2'} }) end
  469. it "should set fastcgi_param" do
  470. should contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
  471. with_content(%r|fastcgi_param\s+CUSTOM_PARAM\s+value;|).
  472. with_content(%r|fastcgi_param\s+CUSTOM_PARAM2\s+value2;|)
  473. end
  474. end
  475. context "when fastcgi_param is not set" do
  476. let :params do default_params end
  477. it "should not set fastcgi_param" do
  478. should contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
  479. without_content(/fastcgi_param\s+CUSTOM_PARAM\s+.+?;/).
  480. without_content(/fastcgi_param\s+CUSTOM_PARAM2\s+.+?;/)
  481. end
  482. it "should not add comment # Enable custom fastcgi_params" do
  483. should contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
  484. without_content(/# Enable custom fastcgi_params\s+/)
  485. end
  486. end
  487. end
  488. describe "vhost_location_proxy template content" do
  489. [
  490. {
  491. :title => 'should set proxy_cache',
  492. :attr => 'proxy_cache',
  493. :value => 'value',
  494. :match => /^\s+proxy_cache\s+value;/,
  495. },
  496. {
  497. :title => 'should not set proxy_cache_valid',
  498. :attr => 'proxy_cache_valid',
  499. :value => false,
  500. :notmatch => /proxy_cache_valid\b/
  501. },
  502. {
  503. :title => 'should set proxy_cache_valid',
  504. :attr => 'proxy_cache_valid',
  505. :value => 'value',
  506. :match => /^\s+proxy_cache_valid\s+value;/,
  507. },
  508. {
  509. :title => 'should not set proxy_cache',
  510. :attr => 'proxy_cache',
  511. :value => false,
  512. :notmatch => /proxy_cache\b/
  513. },
  514. {
  515. :title => 'should set proxy_pass',
  516. :attr => 'proxy',
  517. :value => 'value',
  518. :match => /^\s+proxy_pass\s+value;/,
  519. },
  520. {
  521. :title => 'should set proxy_read_timeout',
  522. :attr => 'proxy_read_timeout',
  523. :value => 'value',
  524. :match => %r'\s+proxy_read_timeout\s+value;',
  525. },
  526. {
  527. :title => 'should set proxy_connect_timeout',
  528. :attr => 'proxy_connect_timeout',
  529. :value => 'value',
  530. :match => %r'\s+proxy_connect_timeout\s+value;',
  531. },
  532. {
  533. :title => 'should set proxy_read_timeout',
  534. :attr => 'proxy_read_timeout',
  535. :value => 'value',
  536. :match => %r'\s+proxy_read_timeout\s+value;',
  537. },
  538. {
  539. :title => 'should set proxy headers',
  540. :attr => 'proxy_set_header',
  541. :value => [ 'X-TestHeader1 value1', 'X-TestHeader2 value2' ],
  542. :match => [
  543. /^\s+proxy_set_header\s+X-TestHeader1 value1;/,
  544. /^\s+proxy_set_header\s+X-TestHeader2 value2;/,
  545. ]
  546. },
  547. {
  548. :title => 'should set proxy_method',
  549. :attr => 'proxy_method',
  550. :value => 'value',
  551. :match => %r'\s+proxy_method\s+value;',
  552. },
  553. {
  554. :title => 'should set proxy_set_body',
  555. :attr => 'proxy_set_body',
  556. :value => 'value',
  557. :match => %r'\s+proxy_set_body\s+value;',
  558. },
  559. {
  560. :title => 'should contain rewrite rules',
  561. :attr => 'rewrite_rules',
  562. :value => [
  563. '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last',
  564. '^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last',
  565. '^/users/(.*)$ /show?user=$1? last',
  566. ],
  567. :match => [
  568. ' rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;',
  569. ' rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;',
  570. ' rewrite ^/users/(.*)$ /show?user=$1? last;',
  571. ],
  572. },
  573. {
  574. :title => 'should not set rewrite_rules',
  575. :attr => 'rewrite_rules',
  576. :value => [],
  577. :notmatch => /rewrite/
  578. },
  579. ].each do |param|
  580. context "when #{param[:attr]} is #{param[:value]}" do
  581. let :default_params do { :location => 'location', :proxy => 'proxy_value', :vhost => 'vhost1' } end
  582. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  583. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
  584. it param[:title] do
  585. fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
  586. matches = Array(param[:match])
  587. if matches.all? { |m| m.is_a? Regexp }
  588. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  589. else
  590. lines = subject.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  591. expect(lines & matches).to eq(matches)
  592. end
  593. Array(param[:notmatch]).each do |item|
  594. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
  595. end
  596. end
  597. end
  598. end
  599. context "when proxy_cache_valid is 10m" do
  600. let :params do {
  601. :location => 'location',
  602. :proxy => 'proxy_value',
  603. :vhost => 'vhost1',
  604. :proxy_cache => 'true',
  605. :proxy_cache_valid => '10m',
  606. } end
  607. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).with_content(/proxy_cache_valid\s+10m;/) }
  608. end
  609. end
  610. describe "vhost_location_stub_status template content" do
  611. let :params do { :location => 'location', :stub_status => true, :vhost => 'vhost1' } end
  612. it do
  613. is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
  614. with_content(/stub_status\s+on/)
  615. end
  616. end
  617. context 'attribute resources' do
  618. context 'when fastcgi => "localhost:9000"' do
  619. let :params do { :fastcgi => 'localhost:9000', :vhost => 'vhost1' } end
  620. it { is_expected.to contain_file('/etc/nginx/fastcgi_params').with_mode('0770') }
  621. end
  622. context 'when ssl_only => true' do
  623. let :params do { :ssl_only => true, :vhost => 'vhost1', :www_root => '/', } end
  624. it { is_expected.not_to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-rspec-test")) }
  625. end
  626. context 'when ssl_only => false' do
  627. let :params do { :ssl_only => false, :vhost => 'vhost1', :www_root => '/', } end
  628. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-rspec-test")) }
  629. end
  630. context 'when ssl => true' do
  631. let :params do { :ssl => true, :vhost => 'vhost1', :www_root => '/', } end
  632. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-800-rspec-test-ssl")) }
  633. end
  634. context 'when ssl => false' do
  635. let :params do { :ssl => false, :vhost => 'vhost1', :www_root => '/', } end
  636. it { is_expected.not_to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-800-rspec-test-ssl")) }
  637. end
  638. context 'when auth_basic_user_file => true' do
  639. let :params do { :auth_basic_user_file => '/path/to/file', :vhost => 'vhost1', :www_root => '/', } end
  640. it { is_expected.to contain_file("/etc/nginx/rspec-test_htpasswd") }
  641. end
  642. context 'when ensure => absent' do
  643. let :params do {
  644. :www_root => '/',
  645. :vhost => 'vhost1',
  646. :ensure => 'absent',
  647. :ssl => true,
  648. :auth_basic_user_file => '/path/to/file',
  649. } end
  650. it { is_expected.to contain_file("/etc/nginx/rspec-test_htpasswd").with_ensure('absent') }
  651. end
  652. context "vhost missing" do
  653. let :params do {
  654. :www_root => '/',
  655. } end
  656. it { expect { is_expected.to contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, /Cannot create a location reference without attaching to a virtual host/) }
  657. end
  658. context "location type missing" do
  659. let :params do {
  660. :vhost => 'vhost1',
  661. } end
  662. it { expect { is_expected.to contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, /Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, stub_status, internal, or location_custom_cfg defined/) }
  663. end
  664. context "www_root and proxy are set" do
  665. let :params do {
  666. :vhost => 'vhost1',
  667. :www_root => '/',
  668. :proxy => 'http://localhost:8000/uri/',
  669. } end
  670. it { expect { is_expected.to contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, /Cannot define both directory and proxy in a virtual host/) }
  671. end
  672. context 'when vhost name is sanitized' do
  673. let :title do 'www.rspec-location.com' end
  674. let :params do {
  675. :vhost => 'www rspec-vhost com',
  676. :www_root => '/',
  677. :ssl => true,
  678. } end
  679. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("www_rspec-vhost_com-500-www.rspec-location.com")).with_target('/etc/nginx/sites-available/www_rspec-vhost_com.conf') }
  680. it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("www_rspec-vhost_com-800-www.rspec-location.com-ssl")).with_target('/etc/nginx/sites-available/www_rspec-vhost_com.conf') }
  681. end
  682. end
  683. end
  684. end