concat_fragment_spec.rb 7.5 KB

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