resource_geo_spec.rb 3.3 KB

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