resource_geo_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. require 'spec_helper'
  2. describe 'nginx::resource::geo' do
  3. let :title do
  4. 'client_network'
  5. end
  6. let :default_params do
  7. {
  8. :default => 'extra',
  9. :networks => {
  10. '172.16.0.0/12' => 'intra',
  11. '192.168.0.0/16' => 'intra',
  12. '10.0.0.0/8' => 'intra',
  13. },
  14. :proxies => [ '1.2.3.4', '4.3.2.1' ]
  15. }
  16. end
  17. let :pre_condition do
  18. [
  19. 'include ::nginx::config',
  20. ]
  21. end
  22. describe 'os-independent items' do
  23. describe 'basic assumptions' do
  24. let :params do default_params end
  25. it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with(
  26. {
  27. 'owner' => 'root',
  28. 'group' => 'root',
  29. 'mode' => '0644',
  30. 'ensure' => 'file',
  31. 'content' => /geo \$#{title}/,
  32. }
  33. )}
  34. end
  35. describe "geo.conf template content" do
  36. [
  37. {
  38. :title => 'should set address',
  39. :attr => 'address',
  40. :value => '$remote_addr',
  41. :match => 'geo $remote_addr $client_network {'
  42. },
  43. {
  44. :title => 'should set ranges',
  45. :attr => 'ranges',
  46. :value => true,
  47. :match => ' ranges;'
  48. },
  49. {
  50. :title => 'should set default',
  51. :attr => 'default',
  52. :value => 'extra',
  53. :match => [ ' default extra;' ],
  54. },
  55. {
  56. :title => 'should contain ordered network directives',
  57. :attr => 'networks',
  58. :value => {
  59. '192.168.0.0/16' => 'intra',
  60. '172.16.0.0/12' => 'intra',
  61. '10.0.0.0/8' => 'intra',
  62. },
  63. :match => [
  64. ' 10.0.0.0/8 intra;',
  65. ' 172.16.0.0/12 intra;',
  66. ' 192.168.0.0/16 intra;',
  67. ],
  68. },
  69. {
  70. :title => 'should set multiple proxies',
  71. :attr => 'proxies',
  72. :value => [ '1.2.3.4', '4.3.2.1' ],
  73. :match => [
  74. ' proxy 1.2.3.4;',
  75. ' proxy 4.3.2.1;'
  76. ]
  77. },
  78. {
  79. :title => 'should set proxy_recursive',
  80. :attr => 'proxy_recursive',
  81. :value => true,
  82. :match => ' proxy_recursive;'
  83. },
  84. {
  85. :title => 'should set delete',
  86. :attr => 'delete',
  87. :value => '192.168.0.0/16',
  88. :match => ' delete 192.168.0.0/16;'
  89. },
  90. ].each do |param|
  91. context "when #{param[:attr]} is #{param[:value]}" do
  92. let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
  93. it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_mode('0644') }
  94. it param[:title] do
  95. verify_contents(subject, "/etc/nginx/conf.d/#{title}-geo.conf", Array(param[:match]))
  96. Array(param[:notmatch]).each do |item|
  97. is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").without_content(item)
  98. end
  99. end
  100. end
  101. end
  102. context 'when ensure => absent' do
  103. let :params do default_params.merge(
  104. {
  105. :ensure => 'absent'
  106. }
  107. ) end
  108. it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_ensure('absent') }
  109. end
  110. end
  111. end
  112. end