Latest package info test cases for resource

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2021-12-21 16:40:54 +05:30
parent ff2f7f4b7b
commit b0794c2262
9 changed files with 41 additions and 11 deletions

View file

@ -127,8 +127,7 @@ module Inspec::Resources
def fetch_latest_version(cmd_string)
cmd = inspec.command(cmd_string)
if cmd.exit_status != 0
Inspec::Log.error "Failed to fetch latest version. Error: #{cmd.stderr}"
nil
raise Inspec::Exceptions::ResourceFailed, "Failed to fetch latest version. Error: #{cmd.stderr}"
else
fetch_version_no(cmd.stdout)
end

1
test/fixtures/cmd/apk-info-cmd vendored Normal file
View file

@ -0,0 +1 @@
git-2.18.4-r0 description:\nDistributed version control system\n\ngit-2.18.4-r0 webpage:\nhttps://www.git-scm.com/\n\ngit-2.18.4-r0 installed size:\n13213696\n\n

3
test/fixtures/cmd/apt-list-curl vendored Normal file
View file

@ -0,0 +1,3 @@
Listing... Done
curl/focal-updates,focal-security 7.68.0-1ubuntu2.7 amd64 [upgradable from: 7.68.0-1ubuntu2.4]
curl/focal-updates,focal-security 7.68.0-1ubuntu2.7 i386

3
test/fixtures/cmd/get-pkg-versions vendored Normal file
View file

@ -0,0 +1,3 @@
Name Version Source ProviderName
---- ------- ------ ------------
Chef Client 17.8.25 Programs

1
test/fixtures/cmd/pacman-ss-grep-curl vendored Normal file
View file

@ -0,0 +1 @@
core/curl 7.80.0-1 [installed: 7.37.0-1]

View file

@ -0,0 +1 @@
"vim-console-8.1.1954 = up-to-date with remote\n"

8
test/fixtures/cmd/yum-list-curl vendored Normal file
View file

@ -0,0 +1,8 @@
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.piconets.webwerks.in
* epel: repo.extreme-ix.org
* extras: mirrors.piconets.webwerks.in
* updates: mirrors.piconets.webwerks.in
Installed Packages
curl.x86_64 7.29.0-59.el7_9.1 @updates

View file

@ -237,11 +237,15 @@ class MockLoader
"/sbin/auditctl -l" => cmd.call("auditctl"),
"/sbin/auditctl -s" => cmd.call("auditctl-s"),
"dpkg -s curl" => cmd.call("dpkg-s-curl"),
"apt list curl -a" => cmd.call("apt-list-curl"),
"dpkg -s held-package" => cmd.call("dpkg-s-held-package"),
"rpm -qi curl" => cmd.call("rpm-qi-curl"),
"yum list curl" => cmd.call("yum-list-curl"),
"Get-Package Chef Client -AllVersions" => cmd.call("get-pkg-versions"),
"rpm -qi --dbpath /var/lib/fake_rpmdb curl" => cmd.call("rpm-qi-curl"),
"rpm -qi --dbpath /var/lib/rpmdb_does_not_exist curl" => cmd_exit_1.call,
"pacman -Qi curl" => cmd.call("pacman-qi-curl"),
"pacman -Ss curl | grep curl | grep installed" => cmd.call("pacman-ss-grep-curl"),
"brew info --json=v1 curl" => cmd.call("brew-info--json-v1-curl"),
"brew info --json=v1 nginx" => cmd.call("brew-info--json-v1-nginx"),
"brew info --json=v1 nope" => cmd_exit_1.call,
@ -387,6 +391,7 @@ class MockLoader
"rpm -qa --queryformat '%{NAME} %{VERSION}-%{RELEASE} %{ARCH}\\n'" => cmd.call("rpm-qa-queryformat"),
# pkg query all packages
"pkg info vim-console" => cmd.call("pkg-info-vim-console"),
"pkg version -v | grep vim-console" => cmd.call("pkg-version-grep-vim-console"),
# port netstat on solaris 10 & 11
"netstat -an -f inet -f inet6" => cmd.call("s11-netstat-an-finet-finet6"),
# xinetd configuration
@ -574,6 +579,7 @@ class MockLoader
# alpine package commands
"apk info -vv --no-network | grep git" => cmd.call("apk-info-grep-git"),
"apk list --no-network --installed" => cmd.call("apk-info"),
"apk info git" => cmd.call("apk-info-cmd"),
# filesystem command
"2e7e0d4546342cee799748ec7e2b1c87ca00afbe590fa422a7c27371eefa88f0" => cmd.call("get-wmiobject-filesystem"),

View file

@ -6,25 +6,27 @@ describe "Inspec::Resources::Package" do
# arch linux
it "verify arch linux package parsing" do
resource = MockLoader.new(:arch).load_resource("package", "curl")
pkg = { name: "curl", installed: true, version: "7.37.0-1", type: "pacman" }
pkg = { name: "curl", installed: true, version: "7.37.0-1", type: "pacman", only_version_no: "7.37.0" }
_(resource.installed?).must_equal true
_(resource.version).must_equal "7.37.0-1"
_(resource.info).must_equal pkg
_(resource.latest?).must_equal false
end
# ubuntu
it "verify ubuntu package parsing" do
resource = MockLoader.new(:ubuntu).load_resource("package", "curl")
pkg = { name: "curl", installed: true, held: false, version: "7.35.0-1ubuntu2", type: "deb" }
pkg = { name: "curl", installed: true, held: false, version: "7.35.0-1ubuntu2", type: "deb", only_version_no: "7.35.0" }
_(resource.installed?).must_equal true
_(resource.held?).must_equal false
_(resource.version).must_equal "7.35.0-1ubuntu2"
_(resource.info).must_equal pkg
_(resource.latest?).must_equal false
end
it "verify ubuntu package which is held" do
resource = MockLoader.new(:ubuntu).load_resource("package", "held-package")
pkg = { name: "held-package", installed: true, held: true, version: "1.2.3-1", type: "deb" }
pkg = { name: "held-package", installed: true, held: true, version: "1.2.3-1", type: "deb", only_version_no: "1.2.3" }
_(resource.installed?).must_equal true
_(resource.held?).must_equal true
_(resource.version).must_equal "1.2.3-1"
@ -34,7 +36,7 @@ describe "Inspec::Resources::Package" do
# mint
it "verify mint package parsing" do
resource = MockLoader.new(:mint17).load_resource("package", "curl")
pkg = { name: "curl", installed: true, held: false, version: "7.35.0-1ubuntu2", type: "deb" }
pkg = { name: "curl", installed: true, held: false, version: "7.35.0-1ubuntu2", type: "deb", only_version_no: "7.35.0" }
_(resource.installed?).must_equal true
_(resource.version).must_equal "7.35.0-1ubuntu2"
_(resource.info).must_equal pkg
@ -48,6 +50,7 @@ describe "Inspec::Resources::Package" do
installed: true,
version: "7.29.0-19.el7",
type: "rpm",
only_version_no: "7.29.0"
}
end
@ -56,6 +59,7 @@ describe "Inspec::Resources::Package" do
_(resource.installed?).must_equal true
_(resource.version).must_equal "7.29.0-19.el7"
_(resource.info).must_equal pkg
_(resource.latest?).must_equal true
end
it "can build an `rpm` command containing `--dbpath`" do
@ -91,7 +95,7 @@ describe "Inspec::Resources::Package" do
# wrlinux
it "verify wrlinux package parsing" do
resource = MockLoader.new(:wrlinux).load_resource("package", "curl")
pkg = { name: "curl", installed: true, version: "7.29.0-19.el7", type: "rpm" }
pkg = { name: "curl", installed: true, version: "7.29.0-19.el7", type: "rpm", only_version_no: "7.29.0" }
_(resource.installed?).must_equal true
_(resource.version).must_equal "7.29.0-19.el7"
_(resource.info).must_equal pkg
@ -100,10 +104,11 @@ describe "Inspec::Resources::Package" do
# windows
it "verify windows package parsing" do
resource = MockLoader.new(:windows).load_resource("package", "Chef Client v12.12.15")
pkg = { name: "Chef Client v12.12.15 ", installed: true, version: "12.12.15.1", type: "windows" }
pkg = { name: "Chef Client v12.12.15 ", installed: true, version: "12.12.15.1", type: "windows", only_version_no: "12.12.15.1" }
_(resource.installed?).must_equal true
_(resource.version).must_equal "12.12.15.1"
_(resource.info).must_equal pkg
_(resource.latest?).must_equal false
end
# solaris 10
@ -127,10 +132,11 @@ describe "Inspec::Resources::Package" do
# darwin (brew)
it "can parse ouptut from 'brew' when package is installed" do
resource = MockLoader.new(:macos10_10).load_resource("package", "curl")
pkg = { name: "curl", installed: true, version: "7.52.1", type: "brew" }
pkg = { name: "curl", installed: true, version: "7.52.1", type: "brew", latest_version: "7.52.1", only_version_no: "7.52.1" }
_(resource.installed?).must_equal true
_(resource.version).must_equal "7.52.1"
_(resource.info).must_equal pkg
_(resource.latest?).must_equal true
end
it "can parse ouptut from 'brew' when package is not installed but exists" do
@ -152,19 +158,21 @@ describe "Inspec::Resources::Package" do
# alpine
it "can parse Alpine packages" do
resource = MockLoader.new(:alpine).load_resource("package", "git")
pkg = { name: "git", installed: true, version: "2.15.0-r1", type: "pkg" }
pkg = { name: "git", installed: true, version: "2.15.0-r1", type: "pkg", only_version_no: "2.15.0" }
_(resource.installed?).must_equal true
_(resource.version).must_equal "2.15.0-r1"
_(resource.info).must_equal pkg
_(resource.latest?).must_equal false
end
# freebsd
it "can parse FreeBSD packages" do
resource = MockLoader.new(:freebsd11).load_resource("package", "vim-console")
pkg = { name: "vim-console", installed: true, version: "8.1.1954", type: "pkg" }
pkg = { name: "vim-console", installed: true, version: "8.1.1954", type: "pkg", only_version_no: "8.1.1954" }
_(resource.installed?).must_equal true
_(resource.version).must_equal "8.1.1954"
_(resource.info).must_equal pkg
_(resource.latest?).must_equal true
end
# undefined