2016-09-22 12:43:57 +00:00
---
title: About the package Resource
2018-02-16 00:28:15 +00:00
platform: os
2016-09-22 12:43:57 +00:00
---
# package
2019-04-26 18:24:29 +00:00
Use the `package` Chef InSpec audit resource to test if the named package and/or package version is installed on the system.
2016-09-22 12:43:57 +00:00
2017-10-03 21:35:10 +00:00
<br>
2016-09-22 12:43:57 +00:00
2018-08-09 12:34:49 +00:00
## Availability
### Installation
2019-04-26 18:24:29 +00:00
This resource is distributed along with Chef InSpec itself. You can use it automatically.
2018-08-09 12:34:49 +00:00
### Version
This resource first became available in v1.0.0 of InSpec.
2016-09-27 19:03:23 +00:00
## Syntax
2016-09-22 12:43:57 +00:00
A `package` resource block declares a package and (optionally) a package version:
describe package('name') do
it { should be_installed }
end
where
* `('name')` must specify the name of a package, such as `'nginx'`
* `be_installed` is a valid matcher for this resource
2017-10-03 21:35:10 +00:00
<br>
2016-09-22 12:43:57 +00:00
2016-09-27 19:03:23 +00:00
## Examples
2016-09-22 12:43:57 +00:00
2019-04-26 18:24:29 +00:00
The following examples show how to use this Chef InSpec audit resource.
2016-09-22 12:43:57 +00:00
2017-09-06 12:19:04 +00:00
### Test if NGINX version 1.9.5 is installed
2016-09-22 12:43:57 +00:00
describe package('nginx') do
it { should be_installed }
2016-11-15 16:58:25 +00:00
its('version') { should eq '1.9.5' }
2016-09-22 12:43:57 +00:00
end
2016-09-27 19:03:23 +00:00
### Test that a package is not installed
2016-09-22 12:43:57 +00:00
describe package('some_package') do
it { should_not be_installed }
end
2019-02-06 08:45:38 +00:00
### Test that telnet is not installed
2016-09-22 12:43:57 +00:00
describe package('telnetd') do
it { should_not be_installed }
end
describe inetd_conf do
its('telnet') { should eq nil }
end
2016-09-27 19:03:23 +00:00
### Test if ClamAV (an antivirus engine) is installed and running
2016-09-22 12:43:57 +00:00
describe package('clamav') do
it { should be_installed }
its('version') { should eq '0.98.7' }
end
describe service('clamd') do
2019-02-06 08:45:38 +00:00
it { should be_enabled }
it { should be_installed }
it { should be_running }
2016-09-22 12:43:57 +00:00
end
2018-04-06 13:14:41 +00:00
### Verify if a package is installed according to my rpm database
2017-06-28 10:21:15 +00:00
2018-04-06 13:14:41 +00:00
describe package('some_package', rpm_dbpath: '/var/lib/my_rpmdb') do
it { should be_installed }
end
2017-06-28 10:21:15 +00:00
2016-09-27 19:03:23 +00:00
### Verify if Memcached is installed, enabled, and running
2016-09-22 12:43:57 +00:00
2019-04-26 18:24:29 +00:00
Memcached is an in-memory key-value store that helps improve the performance of database-driven websites and can be installed, maintained, and tested using the `memcached` cookbook (maintained by Chef). The following example is from the `memcached` cookbook and shows how to use a combination of the `package`, `service`, and `port` Chef InSpec audit resources to test if Memcached is installed, enabled, and running:
2016-09-22 12:43:57 +00:00
describe package('memcached') do
it { should be_installed }
end
describe service('memcached') do
it { should be_installed }
it { should be_enabled }
it { should be_running }
end
describe port(11_211) do
it { should be_listening }
end
2017-10-03 21:35:10 +00:00
<br>
## Matchers
2018-02-16 03:07:18 +00:00
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).
2017-10-03 21:35:10 +00:00
### be_held
The `be_held` matcher tests if the named package is "held". On dpkg platforms, a "held" package
will not be upgraded to a later version.
it { should be_held }
### be_installed
The `be_installed` matcher tests if the named package is installed on the system:
it { should be_installed }
### version
The `version` matcher tests if the named package version is on the system:
its('version') { should eq '1.2.3' }
2017-10-05 17:18:12 +00:00
2018-03-20 12:43:30 +00:00
You can also use the `cmp OPERATOR` matcher to perform comparisons using the version attribute:
2017-10-05 17:18:12 +00:00
its('version') { should cmp >= '7.35.0-1ubuntu3.10' }
2018-02-16 00:28:15 +00:00
`cmp` understands version numbers using Gem::Version, and can use the operators `==, <, <=, >=, and >`. It will compare versions by each segment, not as a string - so '7.4' is smaller than '7.30', for example.