summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2019-07-18 13:10:16 -0700
committerGitHub <noreply@github.com>2019-07-18 13:10:16 -0700
commit14eab27130fdae53898faf84bc464fb91cbae822 (patch)
tree3a8bfdad60c22d9e9e9292e448d4b4eaf202bca3
parentb4d2c0919b48934aaa1694ec43a681a5917c07e0 (diff)
parent132e7c76926401e4e43849a4e2d86d51c79d7eb7 (diff)
downloadohai-14eab27130fdae53898faf84bc464fb91cbae822.tar.gz
Merge pull request #1369 from jjustice6/1368
Add other_versions subfield for RPM packages.
-rw-r--r--lib/ohai/plugins/packages.rb29
-rw-r--r--spec/data/plugins/rpmquery.output2
-rw-r--r--spec/unit/plugins/packages_spec.rb20
3 files changed, 50 insertions, 1 deletions
diff --git a/lib/ohai/plugins/packages.rb b/lib/ohai/plugins/packages.rb
index 04c19e96..f0b73fb0 100644
--- a/lib/ohai/plugins/packages.rb
+++ b/lib/ohai/plugins/packages.rb
@@ -48,7 +48,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 a031ef79..6fc583d2 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