concat_fragment_spec.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. require 'spec_helper'
  2. describe 'concat::fragment', :type => :define do
  3. shared_examples 'fragment' do |title, params|
  4. params = {} if params.nil?
  5. p = {
  6. :content => nil,
  7. :source => nil,
  8. :order => 10,
  9. :ensure => 'present',
  10. }.merge(params)
  11. safe_name = title.gsub(/[\/\n]/, '_')
  12. safe_target_name = p[:target].gsub(/[\/\n]/, '_')
  13. concatdir = '/var/lib/puppet/concat'
  14. fragdir = "#{concatdir}/#{safe_target_name}"
  15. id = 'root'
  16. if p[:ensure] == 'absent'
  17. safe_ensure = p[:ensure]
  18. else
  19. safe_ensure = 'file'
  20. end
  21. let(:title) { title }
  22. let(:facts) do
  23. {
  24. :concat_basedir => concatdir,
  25. :id => id,
  26. :osfamily => 'Debian',
  27. :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  28. }
  29. end
  30. let(:params) { params }
  31. let(:pre_condition) do
  32. "concat{ '#{p[:target]}': }"
  33. end
  34. it do
  35. should contain_class('concat::setup')
  36. should contain_concat(p[:target])
  37. should contain_file("#{fragdir}/fragments/#{p[:order]}_#{safe_name}").with({
  38. :ensure => safe_ensure,
  39. :owner => id,
  40. :mode => '0640',
  41. :source => p[:source],
  42. :content => p[:content],
  43. :alias => "concat_fragment_#{title}",
  44. :backup => false,
  45. })
  46. end
  47. end
  48. context 'title' do
  49. ['0', '1', 'a', 'z'].each do |title|
  50. it_behaves_like 'fragment', title, {
  51. :target => '/etc/motd',
  52. }
  53. end
  54. end # title
  55. context 'target =>' do
  56. ['./etc/motd', 'etc/motd', 'motd_header'].each do |target|
  57. context target do
  58. it_behaves_like 'fragment', target, {
  59. :target => '/etc/motd',
  60. }
  61. end
  62. end
  63. context 'false' do
  64. let(:title) { 'motd_header' }
  65. let(:facts) {{ :concat_basedir => '/tmp' }}
  66. let(:params) {{ :target => false }}
  67. it 'should fail' do
  68. expect { should }.to raise_error(Puppet::Error, /is not a string/)
  69. end
  70. end
  71. end # target =>
  72. context 'ensure =>' do
  73. ['present', 'absent'].each do |ens|
  74. context ens do
  75. it_behaves_like 'fragment', 'motd_header', {
  76. :ensure => ens,
  77. :target => '/etc/motd',
  78. }
  79. end
  80. end
  81. context 'any value other than \'present\' or \'absent\'' do
  82. let(:title) { 'motd_header' }
  83. let(:facts) {{ :concat_basedir => '/tmp' }}
  84. let(:params) {{ :ensure => 'invalid', :target => '/etc/motd' }}
  85. it 'should create a warning' do
  86. pending('rspec-puppet support for testing warning()')
  87. end
  88. end
  89. end # ensure =>
  90. context 'content =>' do
  91. ['', 'ashp is our hero'].each do |content|
  92. context content do
  93. it_behaves_like 'fragment', 'motd_header', {
  94. :content => content,
  95. :target => '/etc/motd',
  96. }
  97. end
  98. end
  99. context 'false' do
  100. let(:title) { 'motd_header' }
  101. let(:facts) {{ :concat_basedir => '/tmp' }}
  102. let(:params) {{ :content => false, :target => '/etc/motd' }}
  103. it 'should fail' do
  104. expect { should }.to raise_error(Puppet::Error, /is not a string/)
  105. end
  106. end
  107. end # content =>
  108. context 'source =>' do
  109. ['', '/foo/bar', ['/foo/bar', '/foo/baz']].each do |source|
  110. context source do
  111. it_behaves_like 'fragment', 'motd_header', {
  112. :source => source,
  113. :target => '/etc/motd',
  114. }
  115. end
  116. end
  117. context 'false' do
  118. let(:title) { 'motd_header' }
  119. let(:facts) {{ :concat_basedir => '/tmp' }}
  120. let(:params) {{ :source => false, :target => '/etc/motd' }}
  121. it 'should fail' do
  122. expect { should }.to raise_error(Puppet::Error, /is not a string or an Array/)
  123. end
  124. end
  125. end # source =>
  126. context 'order =>' do
  127. ['', '42', 'a', 'z'].each do |order|
  128. context '\'\'' do
  129. it_behaves_like 'fragment', 'motd_header', {
  130. :order => order,
  131. :target => '/etc/motd',
  132. }
  133. end
  134. end
  135. context 'false' do
  136. let(:title) { 'motd_header' }
  137. let(:facts) {{ :concat_basedir => '/tmp' }}
  138. let(:params) {{ :order => false, :target => '/etc/motd' }}
  139. it 'should fail' do
  140. expect { should }.to raise_error(Puppet::Error, /is not a string or integer/)
  141. end
  142. end
  143. end # order =>
  144. context 'more than one content source' do
  145. error_msg = 'You cannot specify more than one of $content, $source, $ensure => /target'
  146. context 'ensure => target and source' do
  147. let(:title) { 'motd_header' }
  148. let(:facts) do
  149. {
  150. :concat_basedir => '/tmp',
  151. :osfamily => 'Debian',
  152. :id => 'root',
  153. }
  154. end
  155. let(:params) do
  156. {
  157. :target => '/etc/motd',
  158. :ensure => '/foo',
  159. :source => '/bar',
  160. }
  161. end
  162. it 'should fail' do
  163. expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape(error_msg)}/m)
  164. end
  165. end
  166. context 'ensure => target and content' do
  167. let(:title) { 'motd_header' }
  168. let(:facts) do
  169. {
  170. :concat_basedir => '/tmp',
  171. :osfamily => 'Debian',
  172. :id => 'root',
  173. }
  174. end
  175. let(:params) do
  176. {
  177. :target => '/etc/motd',
  178. :ensure => '/foo',
  179. :content => 'bar',
  180. }
  181. end
  182. it 'should fail' do
  183. expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape(error_msg)}/m)
  184. end
  185. end
  186. context 'source and content' do
  187. let(:title) { 'motd_header' }
  188. let(:facts) do
  189. {
  190. :concat_basedir => '/tmp',
  191. :osfamily => 'Debian',
  192. :id => 'root',
  193. }
  194. end
  195. let(:params) do
  196. {
  197. :target => '/etc/motd',
  198. :source => '/foo',
  199. :content => 'bar',
  200. }
  201. end
  202. it 'should fail' do
  203. expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape(error_msg)}/m)
  204. end
  205. end
  206. end # more than one content source
  207. describe 'deprecated parameter' do
  208. context 'mode =>' do
  209. context '1755' do
  210. it_behaves_like 'fragment', 'motd_header', {
  211. :mode => '1755',
  212. :target => '/etc/motd',
  213. }
  214. it 'should create a warning' do
  215. pending('rspec-puppet support for testing warning()')
  216. end
  217. end
  218. end # mode =>
  219. context 'owner =>' do
  220. context 'apenny' do
  221. it_behaves_like 'fragment', 'motd_header', {
  222. :owner => 'apenny',
  223. :target => '/etc/motd',
  224. }
  225. it 'should create a warning' do
  226. pending('rspec-puppet support for testing warning()')
  227. end
  228. end
  229. end # owner =>
  230. context 'group =>' do
  231. context 'apenny' do
  232. it_behaves_like 'fragment', 'motd_header', {
  233. :group => 'apenny',
  234. :target => '/etc/motd',
  235. }
  236. it 'should create a warning' do
  237. pending('rspec-puppet support for testing warning()')
  238. end
  239. end
  240. end # group =>
  241. context 'backup =>' do
  242. context 'foo' do
  243. it_behaves_like 'fragment', 'motd_header', {
  244. :backup => 'foo',
  245. :target => '/etc/motd',
  246. }
  247. it 'should create a warning' do
  248. pending('rspec-puppet support for testing warning()')
  249. end
  250. end
  251. end # backup =>
  252. end # deprecated params
  253. end