resource_geo_spec.rb 3.4 KB

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