replace_spec.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. require 'spec_helper_acceptance'
  2. describe 'replacement of' do
  3. basedir = default.tmpdir('concat')
  4. context 'file' do
  5. context 'should not succeed' do
  6. before(:all) do
  7. pp = <<-EOS
  8. file { '#{basedir}':
  9. ensure => directory,
  10. }
  11. file { '#{basedir}/file':
  12. content => "file exists\n"
  13. }
  14. EOS
  15. apply_manifest(pp)
  16. end
  17. pp = <<-EOS
  18. concat { '#{basedir}/file':
  19. replace => false,
  20. }
  21. concat::fragment { '1':
  22. target => '#{basedir}/file',
  23. content => '1',
  24. }
  25. concat::fragment { '2':
  26. target => '#{basedir}/file',
  27. content => '2',
  28. }
  29. EOS
  30. it 'applies the manifest twice with no stderr' do
  31. apply_manifest(pp, :catch_failures => true)
  32. apply_manifest(pp, :catch_changes => true)
  33. end
  34. describe file("#{basedir}/file") do
  35. it { should be_file }
  36. its(:content) {
  37. should match 'file exists'
  38. should_not match '1'
  39. should_not match '2'
  40. }
  41. end
  42. end
  43. context 'should succeed' do
  44. before(:all) do
  45. pp = <<-EOS
  46. file { '#{basedir}':
  47. ensure => directory,
  48. }
  49. file { '#{basedir}/file':
  50. content => "file exists\n"
  51. }
  52. EOS
  53. apply_manifest(pp)
  54. end
  55. pp = <<-EOS
  56. concat { '#{basedir}/file':
  57. replace => true,
  58. }
  59. concat::fragment { '1':
  60. target => '#{basedir}/file',
  61. content => '1',
  62. }
  63. concat::fragment { '2':
  64. target => '#{basedir}/file',
  65. content => '2',
  66. }
  67. EOS
  68. it 'applies the manifest twice with no stderr' do
  69. apply_manifest(pp, :catch_failures => true)
  70. apply_manifest(pp, :catch_changes => true)
  71. end
  72. describe file("#{basedir}/file") do
  73. it { should be_file }
  74. its(:content) {
  75. should_not match 'file exists'
  76. should match '1'
  77. should match '2'
  78. }
  79. end
  80. end
  81. end # file
  82. context 'symlink', :unless => (fact("osfamily") == "windows") do
  83. context 'should not succeed' do
  84. # XXX the core puppet file type will replace a symlink with a plain file
  85. # when using ensure => present and source => ... but it will not when using
  86. # ensure => present and content => ...; this is somewhat confusing behavior
  87. before(:all) do
  88. pp = <<-EOS
  89. file { '#{basedir}':
  90. ensure => directory,
  91. }
  92. file { '#{basedir}/file':
  93. ensure => link,
  94. target => '#{basedir}/dangling',
  95. }
  96. EOS
  97. apply_manifest(pp)
  98. end
  99. pp = <<-EOS
  100. concat { '#{basedir}/file':
  101. replace => false,
  102. }
  103. concat::fragment { '1':
  104. target => '#{basedir}/file',
  105. content => '1',
  106. }
  107. concat::fragment { '2':
  108. target => '#{basedir}/file',
  109. content => '2',
  110. }
  111. EOS
  112. it 'applies the manifest twice with no stderr' do
  113. apply_manifest(pp, :catch_failures => true)
  114. apply_manifest(pp, :catch_changes => true)
  115. end
  116. # XXX specinfra doesn't support be_linked_to on AIX
  117. describe file("#{basedir}/file"), :unless => (fact("osfamily") == "AIX" or fact("osfamily") == "windows") do
  118. it { should be_linked_to "#{basedir}/dangling" }
  119. end
  120. describe file("#{basedir}/dangling") do
  121. # XXX serverspec does not have a matcher for 'exists'
  122. it { should_not be_file }
  123. it { should_not be_directory }
  124. end
  125. end
  126. context 'should succeed' do
  127. # XXX the core puppet file type will replace a symlink with a plain file
  128. # when using ensure => present and source => ... but it will not when using
  129. # ensure => present and content => ...; this is somewhat confusing behavior
  130. before(:all) do
  131. pp = <<-EOS
  132. file { '#{basedir}':
  133. ensure => directory,
  134. }
  135. file { '#{basedir}/file':
  136. ensure => link,
  137. target => '#{basedir}/dangling',
  138. }
  139. EOS
  140. apply_manifest(pp)
  141. end
  142. pp = <<-EOS
  143. concat { '#{basedir}/file':
  144. replace => true,
  145. }
  146. concat::fragment { '1':
  147. target => '#{basedir}/file',
  148. content => '1',
  149. }
  150. concat::fragment { '2':
  151. target => '#{basedir}/file',
  152. content => '2',
  153. }
  154. EOS
  155. it 'applies the manifest twice with no stderr' do
  156. apply_manifest(pp, :catch_failures => true)
  157. apply_manifest(pp, :catch_changes => true)
  158. end
  159. describe file("#{basedir}/file") do
  160. it { should be_file }
  161. its(:content) {
  162. should match '1'
  163. should match '2'
  164. }
  165. end
  166. end
  167. end # symlink
  168. context 'directory' do
  169. context 'should not succeed' do
  170. before(:all) do
  171. pp = <<-EOS
  172. file { '#{basedir}':
  173. ensure => directory,
  174. }
  175. file { '#{basedir}/file':
  176. ensure => directory,
  177. }
  178. EOS
  179. apply_manifest(pp)
  180. end
  181. pp = <<-EOS
  182. concat { '#{basedir}/file': }
  183. concat::fragment { '1':
  184. target => '#{basedir}/file',
  185. content => '1',
  186. }
  187. concat::fragment { '2':
  188. target => '#{basedir}/file',
  189. content => '2',
  190. }
  191. EOS
  192. it 'applies the manifest twice with stderr for changing to file' do
  193. expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/change from directory to file failed/)
  194. expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/change from directory to file failed/)
  195. end
  196. describe file("#{basedir}/file") do
  197. it { should be_directory }
  198. end
  199. end
  200. # XXX concat's force param currently enables the creation of empty files
  201. # when there are no fragments, and the replace param will only replace
  202. # files and symlinks, not directories. The semantics either need to be
  203. # changed, extended, or a new param introduced to control directory
  204. # replacement.
  205. context 'should succeed', :pending => 'not yet implemented' do
  206. pp = <<-EOS
  207. concat { '#{basedir}/file':
  208. force => true,
  209. }
  210. concat::fragment { '1':
  211. target => '#{basedir}/file',
  212. content => '1',
  213. }
  214. concat::fragment { '2':
  215. target => '#{basedir}/file',
  216. content => '2',
  217. }
  218. EOS
  219. it 'applies the manifest twice with no stderr' do
  220. apply_manifest(pp, :catch_failures => true)
  221. apply_manifest(pp, :catch_changes => true)
  222. end
  223. describe file("#{basedir}/file") do
  224. it { should be_file }
  225. its(:content) { should match '1' }
  226. end
  227. end
  228. end # directory
  229. end