concat_spec.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. require 'spec_helper'
  2. describe 'concat', :type => :define do
  3. shared_examples 'concat' do |title, params, id|
  4. params = {} if params.nil?
  5. id = 'root' if id.nil?
  6. # default param values
  7. p = {
  8. :ensure => 'present',
  9. :path => title,
  10. :owner => nil,
  11. :group => nil,
  12. :mode => '0644',
  13. :warn => false,
  14. :force => false,
  15. :backup => 'puppet',
  16. :replace => true,
  17. :order => 'alpha',
  18. :ensure_newline => false,
  19. }.merge(params)
  20. safe_name = title.gsub('/', '_')
  21. concatdir = '/var/lib/puppet/concat'
  22. fragdir = "#{concatdir}/#{safe_name}"
  23. concat_name = 'fragments.concat.out'
  24. default_warn_message = '# This file is managed by Puppet. DO NOT EDIT.'
  25. file_defaults = {
  26. :backup => false,
  27. }
  28. let(:title) { title }
  29. let(:params) { params }
  30. let(:facts) do
  31. {
  32. :concat_basedir => concatdir,
  33. :id => id,
  34. :osfamily => 'Debian',
  35. :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  36. :kernel => 'Linux',
  37. }
  38. end
  39. if p[:ensure] == 'present'
  40. it do
  41. should contain_file(fragdir).with(file_defaults.merge({
  42. :ensure => 'directory',
  43. :mode => '0750',
  44. }))
  45. end
  46. it do
  47. should contain_file("#{fragdir}/fragments").with(file_defaults.merge({
  48. :ensure => 'directory',
  49. :mode => '0750',
  50. :force => true,
  51. :ignore => ['.svn', '.git', '.gitignore'],
  52. :purge => true,
  53. :recurse => true,
  54. }))
  55. end
  56. [
  57. "#{fragdir}/fragments.concat",
  58. "#{fragdir}/#{concat_name}",
  59. ].each do |file|
  60. it do
  61. should contain_file(file).with(file_defaults.merge({
  62. :ensure => 'present',
  63. :mode => '0640',
  64. }))
  65. end
  66. end
  67. it do
  68. should contain_file(title).with(file_defaults.merge({
  69. :ensure => 'present',
  70. :owner => p[:owner],
  71. :group => p[:group],
  72. :mode => p[:mode],
  73. :replace => p[:replace],
  74. :path => p[:path],
  75. :alias => "concat_#{title}",
  76. :source => "#{fragdir}/#{concat_name}",
  77. :backup => p[:backup],
  78. }))
  79. end
  80. cmd = "#{concatdir}/bin/concatfragments.sh " +
  81. "-o \"#{concatdir}/#{safe_name}/fragments.concat.out\" " +
  82. "-d \"#{concatdir}/#{safe_name}\""
  83. # flag order: fragdir, warnflag, forceflag, orderflag, newlineflag
  84. if p.has_key?(:warn)
  85. case p[:warn]
  86. when TrueClass
  87. message = default_warn_message
  88. when 'true', 'yes', 'on'
  89. # should generate a stringified boolean warning
  90. message = default_warn_message
  91. when FalseClass
  92. message = nil
  93. when 'false', 'no', 'off'
  94. # should generate a stringified boolean warning
  95. message = nil
  96. else
  97. message = p[:warn]
  98. end
  99. unless message.nil?
  100. cmd += " -w \'#{message}\'"
  101. end
  102. end
  103. cmd += " -f" if p[:force]
  104. cmd += " -n" if p[:order] == 'numeric'
  105. cmd += " -l" if p[:ensure_newline] == true
  106. it do
  107. should contain_exec("concat_#{title}").with({
  108. :alias => "concat_#{fragdir}",
  109. :command => cmd,
  110. :unless => "#{cmd} -t",
  111. })
  112. end
  113. else
  114. [
  115. fragdir,
  116. "#{fragdir}/fragments",
  117. "#{fragdir}/fragments.concat",
  118. "#{fragdir}/#{concat_name}",
  119. ].each do |file|
  120. it do
  121. should contain_file(file).with(file_defaults.merge({
  122. :ensure => 'absent',
  123. :backup => false,
  124. :force => true,
  125. }))
  126. end
  127. end
  128. it do
  129. should contain_file(title).with(file_defaults.merge({
  130. :ensure => 'absent',
  131. :backup => p[:backup],
  132. }))
  133. end
  134. it do
  135. should contain_exec("concat_#{title}").with({
  136. :alias => "concat_#{fragdir}",
  137. :command => 'true',
  138. :unless => 'true',
  139. :path => '/bin:/usr/bin',
  140. })
  141. end
  142. end
  143. end
  144. context 'title' do
  145. context 'without path param' do
  146. # title/name is the default value for the path param. therefore, the
  147. # title must be an absolute path unless path is specified
  148. ['/foo', '/foo/bar', '/foo/bar/baz'].each do |title|
  149. context title do
  150. it_behaves_like 'concat', '/etc/foo.bar'
  151. end
  152. end
  153. ['./foo', 'foo', 'foo/bar'].each do |title|
  154. context title do
  155. let(:title) { title }
  156. it 'should fail' do
  157. expect { should }.to raise_error(Puppet::Error, /is not an absolute path/)
  158. end
  159. end
  160. end
  161. end
  162. context 'with path param' do
  163. ['./foo', 'foo', 'foo/bar'].each do |title|
  164. context title do
  165. it_behaves_like 'concat', title, { :path => '/etc/foo.bar' }
  166. end
  167. end
  168. end
  169. end # title =>
  170. context 'as non-root user' do
  171. it_behaves_like 'concat', '/etc/foo.bar', {}, 'bob'
  172. end
  173. context 'ensure =>' do
  174. ['present', 'absent'].each do |ens|
  175. context ens do
  176. it_behaves_like 'concat', '/etc/foo.bar', { :ensure => ens }
  177. end
  178. end
  179. context 'invalid' do
  180. let(:title) { '/etc/foo.bar' }
  181. let(:params) {{ :ensure => 'invalid' }}
  182. it 'should fail' do
  183. expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape('does not match "^present$|^absent$"')}/)
  184. end
  185. end
  186. end # ensure =>
  187. context 'path =>' do
  188. context '/foo' do
  189. it_behaves_like 'concat', '/etc/foo.bar', { :path => '/foo' }
  190. end
  191. ['./foo', 'foo', 'foo/bar', false].each do |path|
  192. context path do
  193. let(:title) { '/etc/foo.bar' }
  194. let(:params) {{ :path => path }}
  195. it 'should fail' do
  196. expect { should }.to raise_error(Puppet::Error, /is not an absolute path/)
  197. end
  198. end
  199. end
  200. end # path =>
  201. context 'owner =>' do
  202. context 'apenney' do
  203. it_behaves_like 'concat', '/etc/foo.bar', { :owner => 'apenny' }
  204. end
  205. context 'false' do
  206. let(:title) { '/etc/foo.bar' }
  207. let(:params) {{ :owner => false }}
  208. it 'should fail' do
  209. expect { should }.to raise_error(Puppet::Error, /is not a string/)
  210. end
  211. end
  212. end # owner =>
  213. context 'group =>' do
  214. context 'apenney' do
  215. it_behaves_like 'concat', '/etc/foo.bar', { :group => 'apenny' }
  216. end
  217. context 'false' do
  218. let(:title) { '/etc/foo.bar' }
  219. let(:params) {{ :group => false }}
  220. it 'should fail' do
  221. expect { should }.to raise_error(Puppet::Error, /is not a string/)
  222. end
  223. end
  224. end # group =>
  225. context 'mode =>' do
  226. context '1755' do
  227. it_behaves_like 'concat', '/etc/foo.bar', { :mode => '1755' }
  228. end
  229. context 'false' do
  230. let(:title) { '/etc/foo.bar' }
  231. let(:params) {{ :mode => false }}
  232. it 'should fail' do
  233. expect { should }.to raise_error(Puppet::Error, /is not a string/)
  234. end
  235. end
  236. end # mode =>
  237. context 'warn =>' do
  238. [true, false, '# foo'].each do |warn|
  239. context warn do
  240. it_behaves_like 'concat', '/etc/foo.bar', { :warn => warn }
  241. end
  242. end
  243. context '(stringified boolean)' do
  244. ['true', 'yes', 'on', 'false', 'no', 'off'].each do |warn|
  245. context warn do
  246. it_behaves_like 'concat', '/etc/foo.bar', { :warn => warn }
  247. it 'should create a warning' do
  248. pending('rspec-puppet support for testing warning()')
  249. end
  250. end
  251. end
  252. end
  253. context '123' do
  254. let(:title) { '/etc/foo.bar' }
  255. let(:params) {{ :warn => 123 }}
  256. it 'should fail' do
  257. expect { should }.to raise_error(Puppet::Error, /is not a string or boolean/)
  258. end
  259. end
  260. end # warn =>
  261. context 'force =>' do
  262. [true, false].each do |force|
  263. context force do
  264. it_behaves_like 'concat', '/etc/foo.bar', { :force => force }
  265. end
  266. end
  267. context '123' do
  268. let(:title) { '/etc/foo.bar' }
  269. let(:params) {{ :force => 123 }}
  270. it 'should fail' do
  271. expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
  272. end
  273. end
  274. end # force =>
  275. context 'backup =>' do
  276. context 'reverse' do
  277. it_behaves_like 'concat', '/etc/foo.bar', { :backup => 'reverse' }
  278. end
  279. context 'false' do
  280. it_behaves_like 'concat', '/etc/foo.bar', { :backup => false }
  281. end
  282. context 'true' do
  283. it_behaves_like 'concat', '/etc/foo.bar', { :backup => true }
  284. end
  285. context 'true' do
  286. let(:title) { '/etc/foo.bar' }
  287. let(:params) {{ :backup => [] }}
  288. it 'should fail' do
  289. expect { should }.to raise_error(Puppet::Error, /backup must be string or bool/)
  290. end
  291. end
  292. end # backup =>
  293. context 'replace =>' do
  294. [true, false].each do |replace|
  295. context replace do
  296. it_behaves_like 'concat', '/etc/foo.bar', { :replace => replace }
  297. end
  298. end
  299. context '123' do
  300. let(:title) { '/etc/foo.bar' }
  301. let(:params) {{ :replace => 123 }}
  302. it 'should fail' do
  303. expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
  304. end
  305. end
  306. end # replace =>
  307. context 'order =>' do
  308. ['alpha', 'numeric'].each do |order|
  309. context order do
  310. it_behaves_like 'concat', '/etc/foo.bar', { :order => order }
  311. end
  312. end
  313. context 'invalid' do
  314. let(:title) { '/etc/foo.bar' }
  315. let(:params) {{ :order => 'invalid' }}
  316. it 'should fail' do
  317. expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape('does not match "^alpha$|^numeric$"')}/)
  318. end
  319. end
  320. end # order =>
  321. context 'ensure_newline =>' do
  322. [true, false].each do |ensure_newline|
  323. context 'true' do
  324. it_behaves_like 'concat', '/etc/foo.bar', { :ensure_newline => ensure_newline}
  325. end
  326. end
  327. context '123' do
  328. let(:title) { '/etc/foo.bar' }
  329. let(:params) {{ :ensure_newline => 123 }}
  330. it 'should fail' do
  331. expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
  332. end
  333. end
  334. end # ensure_newline =>
  335. describe 'deprecated parameter' do
  336. context 'gnu =>' do
  337. context 'foo' do
  338. it_behaves_like 'concat', '/etc/foo.bar', { :gnu => 'foo'}
  339. it 'should create a warning' do
  340. pending('rspec-puppet support for testing warning()')
  341. end
  342. end
  343. end
  344. end
  345. end
  346. # vim:sw=2:ts=2:expandtab:textwidth=79