Chef InSpec uses matchers to help compare resource values to expectations. The following matchers are available:
You may also use any matcher provided by RSpec::Expectations, but those matchers are outside of InSpec’s scope of support.
The following InSpec-supported universal matchers are available:
be - make numeric comparisonsbe_in - look for the property value in a listcmp - general-use equality (try this first)eq - type-specific equalityinclude - look for an expected value in a list-valued propertymatch - look for patterns in text using regular expressionsSee Explore Chef InSpec resources on Learn Chef Rally to learn more about InSpec’s built-in matchers.
This matcher can be followed by many different comparison operators. Always make sure to use numbers, not strings, for these comparisons.
describe file('/proc/cpuinfo') do
its('size') { should be >= 10 }
its('size') { should be < 1000 }
end
Unlike eq, cmp is a matcher for less-restrictive comparisons. It will 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.
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:
describe sshd_config do
# Only `'2'` works
its('Protocol') { should eq '2' }
# Both of these work
its('Protocol') { should cmp '2' }
its('Protocol') { should cmp 2 }
end
describe auditd_conf do
its('log_format') { should cmp 'raw' }
its('log_format') { should cmp 'RAW' }
end
describe package('curl') do
its('version') { should cmp > '7.35.0-1ubuntu2.10' }
end
describe passwd.uids(0) do
its('users') { should cmp 'root' }
its('users') { should cmp ['root'] }
end
describe auditd_conf do
its('log_format') { should cmp /raw/i }
end
describe file('/proc/cpuinfo') do
its('mode') { should cmp '0345' }
end
expected: 0345
got: 0444
Test for exact equality of two values.
describe sshd_config do
its('RSAAuthentication') { should_not eq 'no' }
its('Protocol') { should eq '2' }
end
eq fails if types don’t match. Please keep this in mind, when comparing configuration entries that are numbers:
its('Port') { should eq '22' } # ok
its('Port') { should eq 22 }
# fails: '2' != 2 (string vs int)
For less restrictive comparisons, please use cmp.
Verifies if a value is included in a list.
describe passwd do
its('users') { should include 'my_user' }
end
Verifies that an item is included in a list.
describe resource do
its('item') { should be_in LIST }
end
Check if a string matches a regular expression.
describe sshd_config do
its('Ciphers') { should_not match /cbc/ }
end
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs.chef.io/inspec/matchers