replace_spec.rb 6.8 KB

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