2016-09-21 16:58:25 +00:00
---
2019-04-26 18:24:29 +00:00
title: Chef InSpec Universal Matchers Reference
2016-09-21 16:58:25 +00:00
---
2019-04-26 18:24:29 +00:00
# Chef InSpec Universal Matchers Reference
2016-09-21 16:58:25 +00:00
2019-04-26 18:24:29 +00:00
Chef InSpec uses matchers to help compare resource values to expectations.
2018-03-05 14:07:06 +00:00
The following matchers are available:
2016-09-21 16:58:25 +00:00
2018-03-01 14:20:20 +00:00
You may also use any matcher provided by [RSpec::Expectations ](https://relishapp.com/rspec/rspec-expectations/docs ), but those matchers are outside of InSpec's [scope of support ](https://www.inspec.io/docs/reference/inspec_and_friends/#rspec ).
The following InSpec-supported universal matchers are available:
* [`be` ](#be ) - make numeric comparisons
* [`be_in` ](#be_in ) - look for the property value in a list
* [`cmp` ](#cmp ) - general-use equality (try this first)
* [`eq` ](#eq ) - type-specific equality
* [`include` ](#include ) - look for an expected value in a list-valued property
* [`match` ](#match ) - look for patterns in text using regular expressions
2016-09-21 16:58:25 +00:00
2019-04-26 18:24:29 +00:00
See [Explore Chef InSpec resources ](https://learn.chef.io/modules/explore-inspec-resources#/ ) on Learn Chef Rally to learn more about InSpec's built-in matchers.
2017-10-03 21:35:10 +00:00
2016-09-21 16:58:25 +00:00
## be
This matcher can be followed by many different comparison operators.
Always make sure to use numbers, not strings, for these comparisons.
```ruby
describe file('/proc/cpuinfo') do
its('size') { should be >= 10 }
its('size') { should be < 1000 }
end
```
## cmp
2018-03-01 14:20:20 +00:00
Unlike `eq` , `cmp` is a matcher for less-restrictive comparisons. It will
2016-09-21 16:58:25 +00:00
try to fit the actual value to the type you are comparing it to. This is
meant to relieve the user from having to write type-casts and
resolutions.
```ruby
describe sshd_config do
its('Protocol') { should cmp 2 }
end
describe passwd.uid(0) do
its('users') { should cmp 'root' }
end
```
`cmp` behaves in the following way:
* Compare strings to numbers
2018-06-06 18:10:48 +00:00
```ruby
describe sshd_config do
2018-10-23 20:02:40 +00:00
# Only `'2'` works
2018-06-06 18:10:48 +00:00
its('Protocol') { should eq '2' }
2016-09-21 16:58:25 +00:00
2018-10-23 20:02:40 +00:00
# Both of these work
2018-06-06 18:10:48 +00:00
its('Protocol') { should cmp '2' }
its('Protocol') { should cmp 2 }
end
```
2016-09-21 16:58:25 +00:00
* String comparisons are not case-sensitive
2018-06-06 18:10:48 +00:00
```ruby
describe auditd_conf do
its('log_format') { should cmp 'raw' }
its('log_format') { should cmp 'RAW' }
end
```
2018-07-19 18:31:38 +00:00
2017-10-05 17:18:12 +00:00
* Recognize versions embedded in strings
2018-06-06 18:10:48 +00:00
```ruby
2018-10-23 20:02:40 +00:00
describe package('curl') do
2018-06-06 18:10:48 +00:00
its('version') { should cmp > '7.35.0-1ubuntu2.10' }
end
```
2016-09-21 16:58:25 +00:00
* Compare arrays with only one entry to a value
2018-06-06 18:10:48 +00:00
```ruby
describe passwd.uids(0) do
its('users') { should cmp 'root' }
its('users') { should cmp ['root'] }
end
```
2016-09-21 16:58:25 +00:00
* Single-value arrays of strings may also be compared to a regex
2018-06-06 18:10:48 +00:00
```ruby
describe auditd_conf do
its('log_format') { should cmp /raw/i }
end
```
2016-09-21 16:58:25 +00:00
* Improved printing of octal comparisons
2018-06-06 18:10:48 +00:00
```ruby
describe file('/proc/cpuinfo') do
its('mode') { should cmp '0345' }
end
2016-09-21 16:58:25 +00:00
2018-06-06 18:10:48 +00:00
expected: 0345
got: 0444
```
2016-09-21 16:58:25 +00:00
## eq
Test for exact equality of two values.
```ruby
describe sshd_config do
its('RSAAuthentication') { should_not eq 'no' }
its('Protocol') { should eq '2' }
end
```
2018-03-01 14:20:20 +00:00
`eq` fails if types don't match. Please keep this in mind, when comparing
2016-09-21 16:58:25 +00:00
configuration entries that are numbers:
```ruby
its('Port') { should eq '22' } # ok
its('Port') { should eq 22 }
# fails: '2' != 2 (string vs int)
```
For less restrictive comparisons, please use `cmp` .
## include
Verifies if a value is included in a list.
```ruby
describe passwd do
its('users') { should include 'my_user' }
end
```
2017-08-07 14:05:22 +00:00
## be_in
Verifies that an item is included in a list.
```ruby
describe resource do
its('item') { should be_in LIST }
end
```
2016-09-21 16:58:25 +00:00
## match
Check if a string matches a regular expression.
```ruby
describe sshd_config do
its('Ciphers') { should_not match /cbc/ }
end
```