resource_location_spec.rb 28 KB

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