diff options
author | Joshua Justice <jjustice6@bloomberg.net> | 2019-07-18 17:43:34 -0400 |
---|---|---|
committer | Joshua Justice <jjustice6@bloomberg.net> | 2019-07-18 17:43:40 -0400 |
commit | 356048913ca302a9b61fc50fad610240404847fe (patch) | |
tree | a500b4f6e5cbe0a5554cdbbc2de744cf93862f53 | |
parent | 7af17943dc0668d91c230320cfeeb13e83827a07 (diff) | |
download | ohai-356048913ca302a9b61fc50fad610240404847fe.tar.gz |
Backport the fix for multiple versions of a package being installed.
Signed-off-by: Joshua Justice <jjustice6@bloomberg.net>
-rw-r--r-- | lib/ohai/plugins/packages.rb | 29 | ||||
-rw-r--r-- | spec/data/plugins/rpmquery.output | 2 | ||||
-rw-r--r-- | spec/unit/plugins/packages_spec.rb | 20 |
3 files changed, 50 insertions, 1 deletions
diff --git a/lib/ohai/plugins/packages.rb b/lib/ohai/plugins/packages.rb index f7054a15..84ccc43e 100644 --- a/lib/ohai/plugins/packages.rb +++ b/lib/ohai/plugins/packages.rb @@ -50,7 +50,34 @@ Ohai.plugin(:Packages) do pkgs.each do |pkg| name, epoch, version, release, installdate, arch = pkg.split - packages[name] = { "epoch" => epoch, "version" => version, "release" => release, "installdate" => installdate, "arch" => arch } + if packages[name] + # We have more than one package with this exact name! + # Create an "versions" array for tracking all versions of packages with this name. + # The top-level package information will be the first one returned by rpm -qa, + # all versions go in this list, with the same information they'd normally have. + if packages[name]["versions"].nil? + # add the data of the first package to the list, so that all versions are in the list. + packages[name]["versions"] = [] + packages[name]["versions"] << Mash.new({ "epoch" => packages[name]["epoch"], + "version" => packages[name]["version"], + "release" => packages[name]["release"], + "installdate" => packages[name]["installdate"], + "arch" => packages[name]["arch"] }) + end + packages[name]["versions"] << Mash.new({ "epoch" => epoch, "version" => version, "release" => release, "installdate" => installdate, "arch" => arch }) # Add this package version to the list + # When this was originally written, it didn't account for multiple versions of the same package + # so it just kept clobbering the package data if it encountered multiple versions + # of the same package. As a result, the last duplicate returned by rpm -qa was what was present; + # here we clobber that data for compatibility. Note that we can't overwrite the entire hash + # without losing the versions array. + packages[name]["epoch"] = epoch + packages[name]["version"] = version + packages[name]["release"] = release + packages[name]["installdate"] = installdate + packages[name]["arch"] = arch + else + packages[name] = { "epoch" => epoch, "version" => version, "release" => release, "installdate" => installdate, "arch" => arch } + end end when "arch" diff --git a/spec/data/plugins/rpmquery.output b/spec/data/plugins/rpmquery.output index 02de1a61..ada89595 100644 --- a/spec/data/plugins/rpmquery.output +++ b/spec/data/plugins/rpmquery.output @@ -3,3 +3,5 @@ tzdata 0 2016d 1.el7 1463486618 noarch nss-softokn-freebl 0 3.16.2.3 14.2.el7_2 1463486619 x86_64 glibc 0 2.17 106.el7_2.6 1463486666 x86_64 libstdc++ 0 4.8.5 4.el7 1463486669 x86_64 +kernel 0 3.10.0 862.2.3.el7 1526310781 x86_64 +kernel 0 3.10.0 862.el7 1521745632 x86_64 diff --git a/spec/unit/plugins/packages_spec.rb b/spec/unit/plugins/packages_spec.rb index 5e5d922f..d61e81ca 100644 --- a/spec/unit/plugins/packages_spec.rb +++ b/spec/unit/plugins/packages_spec.rb @@ -100,6 +100,26 @@ describe Ohai::System, "plugin packages" do expect(plugin[:packages]["tzdata"][:installdate]).to eq("1463486618") expect(plugin[:packages]["tzdata"][:arch]).to eq("noarch") end + + it "handles multiple packages with the same name" do + expect(plugin[:packages]["kernel"][:version]).to eq("3.10.0") + expect(plugin[:packages]["kernel"][:release]).to eq("862.el7") + expect(plugin[:packages]["kernel"][:epoch]).to eq("0") + expect(plugin[:packages]["kernel"][:installdate]).to eq("1521745632") + expect(plugin[:packages]["kernel"][:arch]).to eq("x86_64") + # and now the version list: + expect(plugin[:packages]["kernel"]["versions"].first[:version]).to eq("3.10.0") + expect(plugin[:packages]["kernel"]["versions"].first[:release]).to eq("862.2.3.el7") + expect(plugin[:packages]["kernel"]["versions"].first[:epoch]).to eq("0") + expect(plugin[:packages]["kernel"]["versions"].first[:installdate]).to eq("1526310781") + expect(plugin[:packages]["kernel"]["versions"].first[:arch]).to eq("x86_64") + expect(plugin[:packages]["kernel"]["versions"].last[:version]).to eq("3.10.0") + expect(plugin[:packages]["kernel"]["versions"].last[:release]).to eq("862.el7") + expect(plugin[:packages]["kernel"]["versions"].last[:epoch]).to eq("0") + expect(plugin[:packages]["kernel"]["versions"].last[:installdate]).to eq("1521745632") + expect(plugin[:packages]["kernel"]["versions"].last[:arch]).to eq("x86_64") + + end end context "on arch" do |