resource_location_spec.rb 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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("vhost1-500-33c6aa94600c830ad2d316bb4db36724").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_satisfy',
  70. :attr => 'location_satisfy',
  71. :value => 'any',
  72. :match => ' satisfy any;'
  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. {
  133. :title => 'should contain rewrite rules',
  134. :attr => 'rewrite_rules',
  135. :value => [
  136. '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last',
  137. '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.ra last',
  138. '^/users/(.*)$ /show?user=$1? last',
  139. ],
  140. :match => [
  141. /rewrite \^\(\/download\/\.\*\)\/media\/\(\.\*\)\\\.\.\*\$ \$1\/mp3\/\$2\.mp3 last/,
  142. /rewrite \^\(\/download\/\.\*\)\/media\/\(\.\*\)\\\.\.\*\$ \$1\/mp3\/\$2\.ra last/,
  143. /rewrite \^\/users\/\(\.\*\)\$ \/show\?user=\$1\? last/,
  144. ],
  145. },
  146. {
  147. :title => 'should not set rewrite_rules',
  148. :attr => 'rewrite_rules',
  149. :value => [],
  150. :notmatch => /rewrite/
  151. },
  152. {
  153. :title => 'should set auth_basic',
  154. :attr => 'auth_basic',
  155. :value => 'value',
  156. :match => ' auth_basic "value";',
  157. },
  158. {
  159. :title => 'should set auth_basic_user_file',
  160. :attr => 'auth_basic_user_file',
  161. :value => 'value',
  162. :match => ' auth_basic_user_file value;',
  163. },
  164. ].each do |param|
  165. context "when #{param[:attr]} is #{param[:value]}" do
  166. let :default_params do { :location => 'location', :proxy => 'proxy_value', :vhost => 'vhost1' } end
  167. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  168. it { is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")) }
  169. it param[:title] do
  170. fragment = "vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")
  171. matches = Array(param[:match])
  172. if matches.all? { |m| m.is_a? Regexp }
  173. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  174. else
  175. lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  176. expect(lines & matches).to eq(matches)
  177. end
  178. Array(param[:notmatch]).each do |item|
  179. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")).without_content(item)
  180. end
  181. end
  182. end
  183. end
  184. end
  185. describe "vhost/location_footer template content" do
  186. [
  187. {
  188. :title => 'should contain ordered appended directives',
  189. :attr => 'location_cfg_append',
  190. :value => { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'],
  191. 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'],
  192. 'subtest2' => '"sub test value2"' } },
  193. :match => [
  194. ' test1 test value 1;',
  195. ' test2 test value 2a;',
  196. ' test2 test value 2b;',
  197. ' test3 subtest1 "sub test value1a";',
  198. ' test3 subtest1 "sub test value1b";',
  199. ' test3 subtest2 "sub test value2";',
  200. ],
  201. },
  202. {
  203. :title => 'should contain include directives',
  204. :attr => 'include',
  205. :value => [ '/file1', '/file2' ],
  206. :match => [
  207. %r'^\s+include\s+/file1;',
  208. %r'^\s+include\s+/file2;',
  209. ],
  210. },
  211. {
  212. :title => 'should contain custom appended directives',
  213. :attr => 'location_custom_cfg_append',
  214. :value => { 'test1' => 'bar', 'test2' => ['foobar', 'barbaz'],
  215. 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'],
  216. 'subtest2' => '"sub test value2"' } },
  217. :match => [
  218. /^[ ]+test1\s+bar/,
  219. /^[ ]+test2\s+foobar/,
  220. /^[ ]+test2\s+barbaz/,
  221. /^[ ]+test3\s+subtest1 "sub test value1a"/,
  222. /^[ ]+test3\s+subtest1 "sub test value1b"/,
  223. /^[ ]+test3\s+subtest2 "sub test value2"/,
  224. ],
  225. },
  226. {
  227. :title => 'should contain raw_append directives',
  228. :attr => 'raw_append',
  229. :value => [
  230. 'if (a) {',
  231. ' b;',
  232. '}'
  233. ],
  234. :match => /^\s+if \(a\) {\n\s++b;\n\s+\}/,
  235. },
  236. ].each do |param|
  237. context "when #{param[:attr]} is #{param[:value]}" do
  238. let :default_params do { :location => 'location', :proxy => 'proxy_value', :vhost => 'vhost1' } end
  239. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  240. it { is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")) }
  241. it param[:title] do
  242. fragment = "vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")
  243. matches = Array(param[:match])
  244. if matches.all? { |m| m.is_a? Regexp }
  245. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  246. else
  247. lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  248. expect(lines & matches).to eq(matches)
  249. end
  250. Array(param[:notmatch]).each do |item|
  251. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")).without_content(item)
  252. end
  253. end
  254. it "should end with a closing brace" do
  255. fragment = "vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")
  256. content = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content]
  257. expect((content.split("\n").reject {|l| l =~ /^(\s*#|$)/ }.last).strip).to eq('}')
  258. end
  259. end
  260. end
  261. end
  262. describe "vhost_location_alias template content" do
  263. let :default_params do
  264. { :location => 'location', :vhost => 'vhost1', :location_alias => 'value' }
  265. end
  266. context "when location_alias is 'value'" do
  267. let :params do default_params end
  268. it { is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("location")) }
  269. it "should set alias" do
  270. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("location")).
  271. with_content(/^[ ]+alias\s+value;/)
  272. end
  273. end
  274. context "when autoindex is 'on'" do
  275. let :params do default_params.merge({ :autoindex => 'on' }) end
  276. it { is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("location")) }
  277. it "should set autoindex" do
  278. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("location")).
  279. with_content(/^[ ]+autoindex\s+on;/)
  280. end
  281. end
  282. context "when autoindex is not set" do
  283. let :params do default_params end
  284. it { is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("location")) }
  285. it "should not set autoindex" do
  286. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("location")).
  287. without_content(/^[ ]+autoindex[^;]+;/)
  288. end
  289. end
  290. end
  291. describe "vhost_location_directory template content" do
  292. let :default_params do
  293. {
  294. :location => 'location',
  295. :www_root => '/var/www/root',
  296. :vhost => 'vhost1',
  297. }
  298. end
  299. [
  300. {
  301. :title => 'should set www_root',
  302. :attr => 'www_root',
  303. :value => '/',
  304. :match => ' root /;'
  305. },
  306. {
  307. :title => 'should set try_file(s)',
  308. :attr => 'try_files',
  309. :value => ['name1','name2'],
  310. :match => ' try_files name1 name2;',
  311. },
  312. {
  313. :title => 'should set index_file(s)',
  314. :attr => 'index_files',
  315. :value => ['name1','name2'],
  316. :match => ' index name1 name2;',
  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("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")) }
  322. it param[:title] do
  323. fragment = "vhost1-500-" + Digest::MD5.hexdigest("#{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 = catalogue.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("vhost1-500-" + Digest::MD5.hexdigest("#{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("vhost1-500-" + Digest::MD5.hexdigest("location")) }
  340. it "should set autoindex" do
  341. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("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("vhost1-500-" + Digest::MD5.hexdigest("location")) }
  348. it "should not set autoindex" do
  349. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("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("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")) }
  377. it param[:title] do
  378. fragment = "vhost1-500-" + Digest::MD5.hexdigest("#{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 = catalogue.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("vhost1-500-" + Digest::MD5.hexdigest("#{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("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")) }
  436. it param[:title] do
  437. fragment = "vhost1-500-" + Digest::MD5.hexdigest("#{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 = catalogue.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("vhost1-500-" + Digest::MD5.hexdigest("#{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("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")) }
  454. it "should set fastcgi_script" do
  455. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{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("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")) }
  462. it "should not set fastcgi_script" do
  463. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{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("vhost1-500-" + Digest::MD5.hexdigest("#{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 {'HTTP_PROXY' => '""'}" do
  476. let :params do default_params.merge({ :fastcgi_param => {'HTTP_PROXY' => '""'} }) end
  477. it "should set fastcgi_param" do
  478. should contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")).
  479. with_content(%r|fastcgi_param\s+HTTP_PROXY\s+"";|)
  480. end
  481. end
  482. context "when fastcgi_param is not set" do
  483. let :params do default_params end
  484. it "should not set fastcgi_param" do
  485. should contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")).
  486. without_content(/fastcgi_param\s+CUSTOM_PARAM\s+.+?;/).
  487. without_content(/fastcgi_param\s+CUSTOM_PARAM2\s+.+?;/)
  488. end
  489. it "should not add comment # Enable custom fastcgi_params" do
  490. should contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")).
  491. without_content(/# Enable custom fastcgi_params\s+/)
  492. end
  493. end
  494. end
  495. describe "vhost_location_uwsgi template content" do
  496. let :default_params do
  497. {
  498. :location => 'location',
  499. :uwsgi => 'unix:/home/project/uwsgi.socket',
  500. :vhost => 'vhost1'
  501. }
  502. end
  503. [
  504. {
  505. :title => 'should set www_root',
  506. :attr => 'www_root',
  507. :value => '/',
  508. :match => %r'\s+root\s+/;'
  509. },
  510. {
  511. :title => 'should set try_file(s)',
  512. :attr => 'try_files',
  513. :value => ['name1','name2'],
  514. :match => %r'\s+try_files\s+name1 name2;',
  515. },
  516. {
  517. :title => 'should set uwsgi_params',
  518. :attr => 'uwsgi_params',
  519. :value => 'value',
  520. :match => %r'\s+include\s+value;'
  521. },
  522. {
  523. :title => 'should set uwsgi_pass',
  524. :attr => 'uwsgi',
  525. :value => 'value',
  526. :match => %r'\s+uwsgi_pass\s+value;'
  527. },
  528. ].each do |param|
  529. context "when #{param[:attr]} is #{param[:value]}" do
  530. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  531. it { is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")) }
  532. it param[:title] do
  533. fragment = "vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")
  534. matches = Array(param[:match])
  535. if matches.all? { |m| m.is_a? Regexp }
  536. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  537. else
  538. lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  539. expect(lines & matches).to eq(matches)
  540. end
  541. Array(param[:notmatch]).each do |item|
  542. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")).without_content(item)
  543. end
  544. end
  545. end
  546. end
  547. end
  548. describe "vhost_location_proxy template content" do
  549. [
  550. {
  551. :title => 'should set proxy_cache',
  552. :attr => 'proxy_cache',
  553. :value => 'value',
  554. :match => /^\s+proxy_cache\s+value;/,
  555. },
  556. {
  557. :title => 'should not set proxy_cache_valid',
  558. :attr => 'proxy_cache_valid',
  559. :value => false,
  560. :notmatch => /proxy_cache_valid\b/
  561. },
  562. {
  563. :title => 'should set proxy_cache_valid when string',
  564. :attr => 'proxy_cache_valid',
  565. :value => 'value',
  566. :match => /^\s+proxy_cache_valid\s+value;/,
  567. },
  568. {
  569. :title => 'should set proxy_cache_valid when array of strings',
  570. :attr => 'proxy_cache_valid',
  571. :value => ['value1','value2'],
  572. :match => [
  573. /^\s+proxy_cache_valid\s+value1;/,
  574. /^\s+proxy_cache_valid\s+value2;/,
  575. ]
  576. },
  577. {
  578. :title => 'should not set proxy_cache',
  579. :attr => 'proxy_cache',
  580. :value => false,
  581. :notmatch => /proxy_cache\b/
  582. },
  583. {
  584. :title => 'should set proxy_cache_key',
  585. :attr => 'proxy_cache_key',
  586. :value => 'value',
  587. :match => /^\s+proxy_cache_key\s+value;/,
  588. },
  589. {
  590. :title => 'should set proxy_cache_use_stale',
  591. :attr => 'proxy_cache_use_stale',
  592. :value => 'value',
  593. :match => /^\s+proxy_cache_use_stale\s+value;/
  594. },
  595. {
  596. :title => 'should set proxy_pass',
  597. :attr => 'proxy',
  598. :value => 'value',
  599. :match => /^\s+proxy_pass\s+value;/,
  600. },
  601. {
  602. :title => 'should set proxy_read_timeout',
  603. :attr => 'proxy_read_timeout',
  604. :value => 'value',
  605. :match => %r'\s+proxy_read_timeout\s+value;',
  606. },
  607. {
  608. :title => 'should set proxy_connect_timeout',
  609. :attr => 'proxy_connect_timeout',
  610. :value => 'value',
  611. :match => %r'\s+proxy_connect_timeout\s+value;',
  612. },
  613. {
  614. :title => 'should set proxy_read_timeout',
  615. :attr => 'proxy_read_timeout',
  616. :value => 'value',
  617. :match => %r'\s+proxy_read_timeout\s+value;',
  618. },
  619. {
  620. :title => 'should set proxy headers',
  621. :attr => 'proxy_set_header',
  622. :value => [ 'X-TestHeader1 value1', 'X-TestHeader2 value2' ],
  623. :match => [
  624. /^\s+proxy_set_header\s+X-TestHeader1 value1;/,
  625. /^\s+proxy_set_header\s+X-TestHeader2 value2;/,
  626. ]
  627. },
  628. {
  629. :title => 'should hide proxy headers',
  630. :attr => 'proxy_hide_header',
  631. :value => [ 'X-TestHeader1 value1', 'X-TestHeader2 value2' ],
  632. :match => [
  633. /^\s+proxy_hide_header\s+X-TestHeader1 value1;/,
  634. /^\s+proxy_hide_header\s+X-TestHeader2 value2;/,
  635. ]
  636. },
  637. {
  638. :title => 'should set proxy_method',
  639. :attr => 'proxy_method',
  640. :value => 'value',
  641. :match => %r'\s+proxy_method\s+value;',
  642. },
  643. {
  644. :title => 'should set proxy_set_body',
  645. :attr => 'proxy_set_body',
  646. :value => 'value',
  647. :match => %r'\s+proxy_set_body\s+value;',
  648. },
  649. ].each do |param|
  650. context "when #{param[:attr]} is #{param[:value]}" do
  651. let :default_params do { :location => 'location', :proxy => 'proxy_value', :vhost => 'vhost1' } end
  652. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  653. it { is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")) }
  654. it param[:title] do
  655. fragment = "vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")
  656. matches = Array(param[:match])
  657. if matches.all? { |m| m.is_a? Regexp }
  658. matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) }
  659. else
  660. lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n")
  661. expect(lines & matches).to eq(matches)
  662. end
  663. Array(param[:notmatch]).each do |item|
  664. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")).without_content(item)
  665. end
  666. end
  667. end
  668. end
  669. context "when proxy_cache_valid is 10m" do
  670. let :params do {
  671. :location => 'location',
  672. :proxy => 'proxy_value',
  673. :vhost => 'vhost1',
  674. :proxy_cache => 'true',
  675. :proxy_cache_valid => '10m',
  676. } end
  677. it { is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("location")).with_content(/proxy_cache_valid\s+10m;/) }
  678. end
  679. end
  680. describe "vhost_location_stub_status template content" do
  681. let :params do { :location => 'location', :stub_status => true, :vhost => 'vhost1' } end
  682. it do
  683. is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("#{params[:location]}")).
  684. with_content(/stub_status\s+on/)
  685. end
  686. end
  687. context 'attribute resources' do
  688. context 'when fastcgi => "localhost:9000"' do
  689. let :params do { :fastcgi => 'localhost:9000', :vhost => 'vhost1' } end
  690. it { is_expected.to contain_file('/etc/nginx/fastcgi_params').with_mode('0770') }
  691. end
  692. context 'when uwsgi => "unix:/home/project/uwsgi.socket"' do
  693. let :params do { :uwsgi => 'uwsgi_upstream', :vhost => 'vhost1' } end
  694. it { should contain_file('/etc/nginx/uwsgi_params') }
  695. end
  696. context 'when ssl_only => true' do
  697. let :params do { :ssl_only => true, :vhost => 'vhost1', :www_root => '/', } end
  698. it { is_expected.not_to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("rspec-test")) }
  699. end
  700. context 'when ssl_only => false' do
  701. let :params do { :ssl_only => false, :vhost => 'vhost1', :www_root => '/', } end
  702. it { is_expected.to contain_concat__fragment("vhost1-500-" + Digest::MD5.hexdigest("rspec-test")) }
  703. end
  704. context 'when ssl => true' do
  705. let :params do { :ssl => true, :vhost => 'vhost1', :www_root => '/', } end
  706. it { is_expected.to contain_concat__fragment("vhost1-800-" + Digest::MD5.hexdigest("rspec-test") + "-ssl") }
  707. end
  708. context 'when ssl => false' do
  709. let :params do { :ssl => false, :vhost => 'vhost1', :www_root => '/', } end
  710. it { is_expected.not_to contain_concat__fragment("vhost1-800-" + Digest::MD5.hexdigest("rspec-test") + "-ssl") }
  711. end
  712. context "vhost missing" do
  713. let :params do {
  714. :www_root => '/',
  715. } end
  716. 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/) }
  717. end
  718. context "location type missing" do
  719. let :params do {
  720. :vhost => 'vhost1',
  721. } end
  722. 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, uwsgi, stub_status, internal, or location_custom_cfg defined/) }
  723. end
  724. context "www_root and proxy are set" do
  725. let :params do {
  726. :vhost => 'vhost1',
  727. :www_root => '/',
  728. :proxy => 'http://localhost:8000/uri/',
  729. } end
  730. 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/) }
  731. end
  732. context 'when vhost name is sanitized' do
  733. let :title do 'www.rspec-location.com' end
  734. let :params do {
  735. :vhost => 'www rspec-vhost com',
  736. :www_root => '/',
  737. :ssl => true,
  738. } end
  739. it { is_expected.to contain_concat__fragment("www_rspec-vhost_com-500-" + Digest::MD5.hexdigest("www.rspec-location.com")).with_target('/etc/nginx/sites-available/www_rspec-vhost_com.conf') }
  740. it { is_expected.to contain_concat__fragment("www_rspec-vhost_com-800-" + Digest::MD5.hexdigest("www.rspec-location.com") + "-ssl").with_target('/etc/nginx/sites-available/www_rspec-vhost_com.conf') }
  741. end
  742. end
  743. end
  744. end