2016-09-22 12:43:57 +00:00
|
|
|
---
|
|
|
|
title: About the json Resource
|
|
|
|
---
|
|
|
|
|
|
|
|
# json
|
|
|
|
|
|
|
|
Use the `json` InSpec audit resource to test data in a JSON file.
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
## Syntax
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
A `json` resource block declares the data to be tested. Assume the following JSON file:
|
|
|
|
|
|
|
|
{
|
|
|
|
"name" : "hello",
|
|
|
|
"meta" : {
|
|
|
|
"creator" : "John Doe"
|
|
|
|
},
|
|
|
|
"array": [
|
|
|
|
"zero",
|
|
|
|
"one"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
This file can be queried using:
|
|
|
|
|
2017-03-03 19:03:54 +00:00
|
|
|
describe json('/path/to/name.json') do
|
2016-09-22 12:43:57 +00:00
|
|
|
its('name') { should eq 'hello' }
|
|
|
|
its(['meta','creator']) { should eq 'John Doe' }
|
|
|
|
its(['array', 1]) { should eq 'one' }
|
|
|
|
end
|
|
|
|
|
|
|
|
where
|
|
|
|
|
|
|
|
* `name` is a configuration setting in a JSON file
|
|
|
|
* `should eq 'foo'` tests a value of `name` as read from a JSON file versus the value declared in the test
|
|
|
|
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
## Matchers
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
This InSpec audit resource has the following matchers:
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### be
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
<%= partial "/shared/matcher_be" %>
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### cmp
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
<%= partial "/shared/matcher_cmp" %>
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### eq
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
<%= partial "/shared/matcher_eq" %>
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### include
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
<%= partial "/shared/matcher_include" %>
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### match
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
<%= partial "/shared/matcher_match" %>
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### name
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
The `name` matcher tests the value of `name` as read from a JSON file versus the value declared in the test:
|
|
|
|
|
|
|
|
its('name') { should eq 'foo' }
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
## Examples
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
The following examples show how to use this InSpec audit resource.
|
|
|
|
|
2016-09-27 19:03:23 +00:00
|
|
|
### Test a cookbook version in a policyfile.lock.json file
|
2016-09-22 12:43:57 +00:00
|
|
|
|
|
|
|
describe json('policyfile.lock.json') do
|
|
|
|
its(['cookbook_locks', 'omnibus', 'version']) { should eq('2.2.0') }
|
|
|
|
end
|