resource_location_spec.rb 30 KB

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