From 43c77eb29d50a78cedda353a959c23f65d6a223c Mon Sep 17 00:00:00 2001 From: Alexander Olofsson Date: Wed, 15 Apr 2020 15:13:13 +0200 Subject: [PATCH] (#20) Add support for the Entropy package manager This adds extended support for the Entropy package manager, which is used by the Sabayon Linux distribution. Count, MD5, and Check updates are all handled. --- spec/util/package/packagehelpers_spec.rb | 165 +++++++++++++++++++++++ util/package/packagehelpers.rb | 62 +++++++++ 2 files changed, 227 insertions(+) diff --git a/spec/util/package/packagehelpers_spec.rb b/spec/util/package/packagehelpers_spec.rb index 51405a1..35ecfef 100644 --- a/spec/util/package/packagehelpers_spec.rb +++ b/spec/util/package/packagehelpers_spec.rb @@ -109,6 +109,14 @@ module Package File.expects(:exists?).with('/usr/bin/zypper').returns(true) PackageHelpers.packagemanager.should == :zypper end + + it 'should return entropy if equo is present on the system' do + File.expects(:exists?).with('/usr/bin/yum').returns(false) + File.expects(:exists?).with('/usr/bin/apt-get').returns(false) + File.expects(:exists?).with('/usr/bin/zypper').returns(false) + File.expects(:exists?).with('/usr/bin/equo').returns(true) + PackageHelpers.packagemanager.should == :entropy + end end describe "count" do @@ -124,6 +132,12 @@ module Package PackageHelpers.count end + it 'should call #entropy_count if equo is present on the system' do + PackageHelpers.expects(:packagemanager).returns(:entropy) + PackageHelpers.expects(:entropy_count) + PackageHelpers.count + end + it 'should fail if no compatible package manager is present on the system' do PackageHelpers.expects(:packagemanager).returns(nil) @@ -146,6 +160,12 @@ module Package PackageHelpers.md5 end + it 'should call #entropy_md5 if equo is present on the system' do + PackageHelpers.expects(:packagemanager).returns(:entropy) + PackageHelpers.expects(:entropy_md5) + PackageHelpers.md5 + end + it 'should fail if no compatible package manager is present on the system' do PackageHelpers.expects(:packagemanager).returns(nil) @@ -339,6 +359,96 @@ module Package end end + describe "entropy_count" do + it 'should raise if equo cannot be found on the system' do + File.expects(:exists?).with('/usr/bin/equo').returns(false) + + expect{ + PackageHelpers.entropy_count + }.to raise_error 'Cannot find equo at /usr/bin/equo' + end + + it 'should raise if the equo command failed' do + File.expects(:exists?).with('/usr/bin/equo').returns(true) + shell = mock + status = mock + shell.stubs(:runcommand) + shell.stubs(:status).returns(status) + status.stubs(:exitstatus).returns(-1) + Shell.expects(:new).with('/usr/bin/equo query list installed --quiet', :stdout => "").returns(shell) + + expect{ + PackageHelpers.entropy_count + }.to raise_error 'equo query list installed failed, exit code was -1' + end + + + it 'should return the count of packages' do + output = "acct-group/avahi +acct-group/avahi-autoipd +acct-group/ceph +acct-group/docker +acct-user/sshd" + + File.expects(:exists?).with('/usr/bin/equo').returns(true) + shell = mock + status = mock + Shell.stubs(:new).with('/usr/bin/equo query list installed --quiet', :stdout => output).returns(shell) + shell.stubs(:runcommand) + shell.stubs(:stdout).returns(output) + shell.expects(:status).returns(status) + status.stubs(:exitstatus).returns(0) + + result = PackageHelpers.entropy_count(output) + result.should == {:exitcode => 0, :output => "5"} + end + end + + describe "entropy_md5" do + it 'should raise if equo cannot be found on the system' do + File.expects(:exists?).with('/usr/bin/equo').returns(false) + + expect{ + PackageHelpers.entropy_md5 + }.to raise_error 'Cannot find equo at /usr/bin/equo' + end + + it 'should raise if the equo command failed' do + File.expects(:exists?).with('/usr/bin/equo').returns(true) + shell = mock + status = mock + shell.stubs(:runcommand) + shell.stubs(:status).returns(status) + status.stubs(:exitstatus).returns(-1) + Shell.expects(:new).with('/usr/bin/equo query list installed --quiet', :stdout => "").returns(shell) + + expect{ + PackageHelpers.entropy_md5 + }.to raise_error 'equo query list installed failed, exit code was -1' + end + + + it 'should return the md5 of packages' do + output = "acct-group/avahi +acct-group/avahi-autoipd +acct-group/ceph +acct-group/docker +acct-user/sshd" + + File.expects(:exists?).with('/usr/bin/equo').returns(true) + shell = mock + status = mock + Shell.stubs(:new).with('/usr/bin/equo query list installed --quiet', :stdout => output).returns(shell) + shell.stubs(:runcommand) + shell.stubs(:stdout).returns(output) + shell.expects(:status).returns(status) + status.stubs(:exitstatus).returns(0) + + result = PackageHelpers.entropy_md5(output) + result.should == {:exitcode => 0, :output => 'c2ca6bdfa2a2e219e131d9dbb6975a7a'} + end + end + describe "checkupdates" do it 'should call #yum_checkupdates if yum is present on the system' do PackageHelpers.expects(:packagemanager).returns(:yum) @@ -358,6 +468,12 @@ module Package PackageHelpers.checkupdates end + it 'should call #entropy_checkupdates if entropy is present on the system' do + PackageHelpers.expects(:packagemanager).returns(:entropy) + PackageHelpers.expects(:entropy_checkupdates) + PackageHelpers.checkupdates + end + it 'should fail if no compatible package manager is present on the system' do PackageHelpers.expects(:packagemanager).returns(nil) @@ -472,6 +588,55 @@ module Package {:package => 'package2', :version => '2.2.2', :repo => 'rspecrepo'}] end end + + describe "#entropy_checkupdates" do + it 'should raise if equo cannot be found on the system' do + File.expects(:exists?).with('/usr/bin/equo').returns(false) + + expect{ + PackageHelpers.entropy_checkupdates + }.to raise_error 'Cannot find equo at /usr/bin/equo' + end + + it 'should raise if the upgrade command failed' do + File.expects(:exists?).with('/usr/bin/equo').returns(true) + shell = mock + status = mock + Shell.stubs(:new).with('/usr/bin/equo upgrade --pretend', :stdout => '').returns(shell) + shell.stubs(:runcommand) + shell.expects(:status).returns(status) + status.stubs(:exitstatus).returns(-1) + + expect{ + PackageHelpers.entropy_checkupdates + }.to raise_error 'Equo upgrade --pretend failed, exit code was -1' + end + + + it 'should return the list of outdated packages' do + output = "╠ @@ Calculating dependencies... +╠ @@ These are the packages that would be installed: +╠ ## [U] [rspecrepo.org] app-spec/package1-2.5.3|0 [2.4.3-r1|0] +╠ ## [U] [rspecrepo.org] app-spec/package2-4.6-r1|0 [4.6|0] +╠ ## [N] [rspecrepo.org] app-spec/package-with-dash-0.2.2_beta4-r1|1" + + File.expects(:exists?).with('/usr/bin/equo').returns(true) + shell = mock + status = mock + Shell.stubs(:new).with('/usr/bin/equo upgrade --pretend', :stdout => output).returns(shell) + shell.stubs(:runcommand) + shell.expects(:status).returns(status) + status.stubs(:exitstatus).returns(0) + + result = PackageHelpers.entropy_checkupdates(output) + result[:exitcode].should == 0 + result[:output].should == output + result[:package_manager] == 'yum' + result[:outdated_packages].should == [ {:package => 'app-spec/package1', :version => '2.5.3', :repo => 'rspecrepo.org'}, + {:package => 'app-spec/package2', :version => '4.6-r1', :repo => 'rspecrepo.org'}, + {:package => 'app-spec/package-with-dash', :version => '0.2.2_beta4-r1', :repo => 'rspecrepo.org'}] + end + end end end end diff --git a/util/package/packagehelpers.rb b/util/package/packagehelpers.rb index 6075fac..c7c9ecf 100644 --- a/util/package/packagehelpers.rb +++ b/util/package/packagehelpers.rb @@ -11,6 +11,8 @@ def self.count return rpm_count elsif manager == :apt return dpkg_count + elsif manager == :entropy + return entropy_count else raise 'Cannot find a compatible package system to count packages' end @@ -42,12 +44,28 @@ def self.dpkg_count(output = "") return result end + def self.entropy_count(output = '') + raise 'Cannot find equo at /usr/bin/equo' unless File.exists?('/usr/bin/equo') + + result = {exitcode: nil, + output: ''} + cmd = Shell.new('/usr/bin/equo query list installed --quiet', stdout: output) + cmd.runcommand + result[:exitcode] = cmd.status.exitstatus + result[:output] = output.split("\n").size.to_s + + raise "equo query list installed failed, exit code was #{result[:exitcode]}" unless result[:exitcode].zero? + result + end + def self.md5 manager = packagemanager if manager == :yum return rpm_md5 elsif manager == :apt return dpkg_md5 + elsif manager == :entropy + return entropy_md5 else raise 'Cannot find a compatible package system to get a md5 of the package list' end @@ -80,6 +98,19 @@ def self.dpkg_md5(output = "") return result end + def self.entropy_md5(output = '') + raise 'Cannot find equo at /usr/bin/equo' unless File.exists?('/usr/bin/equo') + result = {exitcode: nil, + output: ''} + cmd = Shell.new('/usr/bin/equo query list installed --quiet', stdout: output) + cmd.runcommand + result[:exitcode] = cmd.status.exitstatus + result[:output] = Digest::MD5.new.hexdigest(output.split("\n").sort.join("\n")) + + raise "equo query list installed failed, exit code was #{result[:exitcode]}" unless result[:exitcode].zero? + result + end + def self.yum_clean(clean_mode) raise "Cannot find yum at /usr/bin/yum" unless File.exists?("/usr/bin/yum") result = {:exitcode => nil, @@ -120,6 +151,8 @@ def self.packagemanager return :apt elsif File.exists?('/usr/bin/zypper') return :zypper + elsif File.exists?('/usr/bin/equo') + return :entropy end end @@ -131,6 +164,8 @@ def self.checkupdates return apt_checkupdates elsif manager == :zypper return zypper_checkupdates + elsif manager == :entropy + return entropy_checkupdates else raise 'Cannot find a compatible package system to check updates' end @@ -215,6 +250,33 @@ def self.apt_checkupdates(output = "") result end + + def self.entropy_checkupdates(output = "") + raise 'Cannot find equo at /usr/bin/equo' unless File.exists?('/usr/bin/equo') + + result = {:exitcode => nil, + :output => output, + :outdated_packages => [], + :package_manager => 'entropy'} + + cmd = Shell.new('/usr/bin/equo upgrade --pretend', stdout: result[:output]) + cmd.runcommand + result[:exitcode] = cmd.status.exitstatus + + raise "Equo upgrade --pretend failed, exit code was #{result[:exitcode]}" unless result[:exitcode].zero? + + result[:output].each_line do |line| + next unless line =~ /## \[[A-Z]\]/ + + if line =~ /\[([^ ]+?)\] ([^ ]+?)-((?:[0-9]+|\.)+[a-z]?(?:_(?:alpha|beta|pre|rc|p)\d?)*(?:-r\d+)?)/ + result[:outdated_packages] << {package: $2.strip, + version: $3.strip, + repo: $1.strip} + end + end + + result + end end end end