/spec/unit/provider/package/pkg_spec.rb

https://github.com/jaredjennings/puppet-1 · Ruby · 255 lines · 241 code · 12 blank · 2 comment · 29 complexity · 9dec623bdcb33ca63ec0bcbb0e1de6bc MD5 · raw file

  1. #! /usr/bin/env ruby
  2. require 'spec_helper'
  3. describe Puppet::Type.type(:package).provider(:pkg) do
  4. let (:resource) { Puppet::Resource.new(:package, 'dummy', :parameters => {:name => 'dummy', :ensure => :latest}) }
  5. let (:provider) { described_class.new(resource) }
  6. before :each do
  7. described_class.stubs(:command).with(:pkg).returns('/bin/pkg')
  8. end
  9. def self.it_should_respond_to(*actions)
  10. actions.each do |action|
  11. it "should respond to :#{action}" do
  12. provider.should respond_to(action)
  13. end
  14. end
  15. end
  16. it_should_respond_to :install, :uninstall, :update, :query, :latest
  17. it "should be versionable" do
  18. described_class.should be_versionable
  19. end
  20. describe "#methods" do
  21. context ":pkg_state" do
  22. it "should raise error on unknown values" do
  23. expect {
  24. described_class.pkg_state('extra').should
  25. }.to raise_error(ArgumentError, /Unknown format/)
  26. end
  27. ['known', 'installed'].each do |k|
  28. it "should return known values" do
  29. described_class.pkg_state(k).should == {:status => k}
  30. end
  31. end
  32. end
  33. context ":ifo_flag" do
  34. it "should raise error on unknown values" do
  35. expect {
  36. described_class.ifo_flag('x--').should
  37. }.to raise_error(ArgumentError, /Unknown format/)
  38. end
  39. {'i--' => 'installed', '---'=> 'known'}.each do |k, v|
  40. it "should return known values" do
  41. described_class.ifo_flag(k).should == {:status => v}
  42. end
  43. end
  44. end
  45. context ":parse_line" do
  46. it "should raise error on unknown values" do
  47. expect {
  48. described_class.parse_line('pkg (mypkg) 1.2.3.4 i-- zzz').should
  49. }.to raise_error(ArgumentError, /Unknown line format/)
  50. end
  51. {
  52. 'spkg 0.0.7 i--' => {:name => 'spkg', :ensure => '0.0.7', :status => 'installed', :provider => :pkg},
  53. 'spkg (me) 0.0.7 i--' => {:name => 'spkg', :ensure => '0.0.7', :status => 'installed', :provider => :pkg, :publisher => 'me'},
  54. 'spkg (me) 0.0.7 if-' => {:name => 'spkg', :ensure => 'held', :status => 'installed', :provider => :pkg, :publisher => 'me'},
  55. 'spkg 0.0.7 installed -----' => {:name => 'spkg', :ensure => '0.0.7', :status => 'installed', :provider => :pkg},
  56. 'spkg (me) 0.0.7 installed -----' => {:name => 'spkg', :ensure => '0.0.7', :status => 'installed', :provider => :pkg, :publisher => 'me'},
  57. }.each do |k, v|
  58. it "[#{k}] should correctly parse" do
  59. described_class.parse_line(k).should == v
  60. end
  61. end
  62. end
  63. context ":latest" do
  64. it "should work correctly for ensure latest on solaris 11 (UFOXI) when there are no further packages to install" do
  65. described_class.expects(:pkg).with(:list,'-Hn','dummy').returns File.read(my_fixture('dummy_solaris11.installed'))
  66. provider.latest.should == "1.0.6-0.175.0.0.0.2.537"
  67. end
  68. it "should work correctly for ensure latest on solaris 11(known UFOXI)" do
  69. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'update', '-n', 'dummy'], {:failonfail => false, :combine => true}).returns ''
  70. $CHILD_STATUS.stubs(:exitstatus).returns 0
  71. described_class.expects(:pkg).with(:list,'-Hn','dummy').returns File.read(my_fixture('dummy_solaris11.known'))
  72. provider.latest.should == "1.0.6-0.175.0.0.0.2.537"
  73. end
  74. it "should work correctly for ensure latest on solaris 11 (IFO)" do
  75. described_class.expects(:pkg).with(:list,'-Hn','dummy').returns File.read(my_fixture('dummy_solaris11.ifo.installed'))
  76. provider.latest.should == "1.0.6-0.175.0.0.0.2.537"
  77. end
  78. it "should work correctly for ensure latest on solaris 11(known IFO)" do
  79. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'update', '-n', 'dummy'], {:failonfail => false, :combine => true}).returns ''
  80. $CHILD_STATUS.stubs(:exitstatus).returns 0
  81. described_class.expects(:pkg).with(:list,'-Hn','dummy').returns File.read(my_fixture('dummy_solaris11.ifo.known'))
  82. provider.latest.should == "1.0.6-0.175.0.0.0.2.537"
  83. end
  84. end
  85. context ":instances" do
  86. it "should correctly parse lines with preferred publisher" do
  87. described_class.expects(:pkg).with(:list,'-H').returns File.read(my_fixture('simple'))
  88. instances = described_class.instances.map { |p| {:name => p.get(:name), :ensure => p.get(:ensure)} }
  89. instances.size.should == 2
  90. instances[0].should == {:name => 'SUNWdummy', :ensure => "2.5.5-0.111"}
  91. instances[1].should == {:name => 'dummy2', :ensure =>"9.3.6.1-0.111"}
  92. end
  93. it "should correctly parse lines with non preferred publisher" do
  94. described_class.expects(:pkg).with(:list,'-H').returns File.read(my_fixture('publisher'))
  95. instances = described_class.instances.map { |p| {:name => p.get(:name), :ensure => p.get(:ensure)} }
  96. instances.size.should == 2
  97. instances[0].should == {:name => 'SUNWdummy', :ensure => "8.8-0.111"}
  98. instances[1].should == {:name => 'service/network/dummy', :ensure => "0.5.11-0.151.0.1"}
  99. end
  100. it "should correctly parse lines on solaris 11" do
  101. described_class.expects(:pkg).with(:list, '-H').returns File.read(my_fixture('solaris11'))
  102. described_class.expects(:warning).never
  103. instances = described_class.instances.map { |p| {:name => p.get(:name), :ensure => p.get(:ensure) }}
  104. instances.size.should == 2
  105. instances[0].should == {:name => 'dummy/dummy', :ensure => "3.0-0.175.0.0.0.2.537"}
  106. instances[1].should == {:name => 'dummy/dummy2', :ensure => "1.8.1.2-0.175.0.0.0.2.537"}
  107. end
  108. it "should fail on incorrect lines" do
  109. fake_output = File.read(my_fixture('incomplete'))
  110. described_class.expects(:pkg).with(:list,'-H').returns fake_output
  111. expect {
  112. described_class.instances
  113. }.to raise_error(ArgumentError, /Unknown line format pkg/)
  114. end
  115. it "should fail on unknown package status" do
  116. described_class.expects(:pkg).with(:list,'-H').returns File.read(my_fixture('unknown_status'))
  117. expect {
  118. described_class.instances
  119. }.to raise_error(ArgumentError, /Unknown format pkg/)
  120. end
  121. end
  122. context ":query" do
  123. context "on solaris 10" do
  124. it "should find the package" do
  125. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'list', '-H', 'dummy'], {:failonfail => false, :combine => true}).returns File.read(my_fixture('dummy_solaris10'))
  126. $CHILD_STATUS.stubs(:exitstatus).returns 0
  127. provider.query.should == {
  128. :name => 'dummy',
  129. :ensure => "2.5.5-0.111",
  130. :status => "installed",
  131. :provider => :pkg,
  132. }
  133. end
  134. it "should return :absent when the package is not found" do
  135. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'list', '-H', 'dummy'], {:failonfail => false, :combine => true}).returns ''
  136. $CHILD_STATUS.stubs(:exitstatus).returns 1
  137. provider.query.should == {:ensure => :absent, :name => "dummy"}
  138. end
  139. end
  140. context "on solaris 11" do
  141. it "should find the package" do
  142. $CHILD_STATUS.stubs(:exitstatus).returns 0
  143. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'list', '-H', 'dummy'], {:failonfail => false, :combine => true}).returns File.read(my_fixture('dummy_solaris11.installed'))
  144. provider.query.should == {
  145. :name => 'dummy',
  146. :status => 'installed',
  147. :ensure => "1.0.6-0.175.0.0.0.2.537",
  148. :provider => :pkg
  149. }
  150. end
  151. it "should return :absent when the package is not found" do
  152. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'list', '-H', 'dummy'], {:failonfail => false, :combine => true}).returns ''
  153. $CHILD_STATUS.stubs(:exitstatus).returns 1
  154. provider.query.should == {:ensure => :absent, :name => "dummy"}
  155. end
  156. end
  157. it "should return fail when the packageline cannot be parsed" do
  158. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'list', '-H', 'dummy'], {:failonfail => false, :combine => true}).returns(File.read(my_fixture('incomplete')))
  159. $CHILD_STATUS.stubs(:exitstatus).returns 0
  160. expect {
  161. provider.query
  162. }.to raise_error(ArgumentError, /Unknown line format/)
  163. end
  164. end
  165. context ":install" do
  166. it "should accept all licenses" do
  167. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'install', '--accept', 'dummy'], {:failonfail => false, :combine => true}).returns ''
  168. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'unfreeze', 'dummy'], {:failonfail => false, :combine => true}).returns ''
  169. $CHILD_STATUS.stubs(:exitstatus).returns 0
  170. provider.install
  171. end
  172. it "should install specific version(1)" do
  173. # Should install also check if the version installed is the same version we are asked to install? or should we rely on puppet for that?
  174. resource[:ensure] = '0.0.7'
  175. $CHILD_STATUS.stubs(:exitstatus).returns 0
  176. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'unfreeze', 'dummy'], {:failonfail => false, :combine => true})
  177. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'list', '-H', 'dummy'], {:failonfail => false, :combine => true}).returns 'dummy 0.0.6 installed -----'
  178. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'install', '--accept', 'dummy@0.0.7'], {:failonfail => false, :combine => true}).returns ''
  179. provider.install
  180. end
  181. it "should install specific version(2)" do
  182. resource[:ensure] = '0.0.8'
  183. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'unfreeze', 'dummy'], {:failonfail => false, :combine => true})
  184. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'list', '-H', 'dummy'], {:failonfail => false, :combine => true}).returns 'dummy 0.0.7 installed -----'
  185. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'install', '--accept', 'dummy@0.0.8'], {:failonfail => false, :combine => true}).returns ''
  186. $CHILD_STATUS.stubs(:exitstatus).returns 0
  187. provider.install
  188. end
  189. it "should install specific version(3)" do
  190. resource[:ensure] = '0.0.7'
  191. provider.expects(:query).with().returns({:ensure => '0.0.8'})
  192. provider.expects(:uninstall).with()
  193. $CHILD_STATUS.stubs(:exitstatus).returns 0
  194. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'unfreeze', 'dummy'], {:failonfail => false, :combine => true})
  195. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'install', '--accept', 'dummy@0.0.7'], {:failonfail => false, :combine => true}).returns ''
  196. provider.install
  197. end
  198. it "should install any if version is not specified" do
  199. resource[:ensure] = :present
  200. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'install', '--accept', 'dummy'], {:failonfail => false, :combine => true}).returns ''
  201. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'unfreeze', 'dummy'], {:failonfail => false, :combine => true})
  202. $CHILD_STATUS.stubs(:exitstatus).returns 0
  203. provider.install
  204. end
  205. it "should install if no version was previously installed, and a specific version was requested" do
  206. resource[:ensure] = '0.0.7'
  207. provider.expects(:query).with().returns({:ensure => :absent})
  208. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'unfreeze', 'dummy'], {:failonfail => false, :combine => true})
  209. Puppet::Util::Execution.expects(:execute).with(['/bin/pkg', 'install', '--accept', 'dummy@0.0.7'], {:failonfail => false, :combine => true}).returns ''
  210. $CHILD_STATUS.stubs(:exitstatus).returns 0
  211. provider.install
  212. end
  213. end
  214. context ":update" do
  215. it "should not raise error if not necessary" do
  216. provider.expects(:install).with(true).returns({:exit => 0})
  217. provider.update
  218. end
  219. it "should not raise error if not necessary (2)" do
  220. provider.expects(:install).with(true).returns({:exit => 4})
  221. provider.update
  222. end
  223. it "should raise error if necessary" do
  224. provider.expects(:install).with(true).returns({:exit => 1})
  225. expect {
  226. provider.update
  227. }.to raise_error(Puppet::Error, /Unable to update/)
  228. end
  229. end
  230. context ":uninstall" do
  231. it "should support current pkg version" do
  232. described_class.expects(:pkg).with(:version).returns('630e1ffc7a19')
  233. described_class.expects(:pkg).with([:uninstall, resource[:name]])
  234. provider.uninstall
  235. end
  236. it "should support original pkg commands" do
  237. described_class.expects(:pkg).with(:version).returns('052adf36c3f4')
  238. described_class.expects(:pkg).with([:uninstall, '-r', resource[:name]])
  239. provider.uninstall
  240. end
  241. end
  242. end
  243. end