2016-09-22 12:43:57 +00:00
|
|
|
---
|
|
|
|
title: About the json Resource
|
2018-02-16 00:28:15 +00:00
|
|
|
platform: os
|
2016-09-22 12:43:57 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# json
|
|
|
|
|
2019-04-26 18:24:29 +00:00
|
|
|
Use the `json` Chef InSpec audit resource to test data in a JSON file.
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
|
|
|
|
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 `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
|
|
|
|
|
2019-01-23 13:59:10 +00:00
|
|
|
The `json` resource can also be used with JSON formatted output from a command.
|
|
|
|
Using the same JSON as the previous example, it can be queried using:
|
2019-01-20 20:12:28 +00:00
|
|
|
|
|
|
|
describe json({ command: 'retrieve_data.py --json'}) do
|
|
|
|
its('name') { should eq 'hello' }
|
|
|
|
its(['meta','creator']) { should eq 'John Doe' }
|
|
|
|
its(['array', 1]) { should eq 'one' }
|
|
|
|
end
|
|
|
|
|
2019-01-23 13:59:10 +00:00
|
|
|
Finally, content can be passed directly to the resource:
|
2019-01-20 20:12:28 +00:00
|
|
|
|
|
|
|
describe json({ content: '{\"item1\": { \"status\": \"available\" } }' }) do
|
|
|
|
its(['item1', 'status']) { should cmp 'available' }
|
|
|
|
end
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2017-10-03 21:35:10 +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
|
|
|
|
2018-02-26 22:48:35 +00:00
|
|
|
### name
|
|
|
|
|
|
|
|
The `name` matcher tests the value of the filename as read from a JSON file versus the value declared in the test:
|
|
|
|
|
|
|
|
its('name') { should eq '/tmp/example.json' }
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
### Test a cookbook version in a policyfile.lock.json file
|
2016-09-22 12:43:57 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
describe json('policyfile.lock.json') do
|
|
|
|
its(['cookbook_locks', 'omnibus', 'version']) { should eq('2.2.0') }
|
|
|
|
end
|
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
|
|
|
|
2019-02-21 17:23:52 +00:00
|
|
|
### Test JSON output from an HTTP API
|
|
|
|
|
|
|
|
Our example API has a `/health` endpoint, which looks like this:
|
|
|
|
|
|
|
|
{
|
|
|
|
"service": {
|
|
|
|
"port": 3000,
|
|
|
|
"status": "ok"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Example test:
|
|
|
|
|
|
|
|
describe json(content: http('http://localhost:3000/health').body) do
|
|
|
|
its(['service', 'port']) { should eq 3000 }
|
|
|
|
its(['service', 'status']) { should eq 'ok' }
|
|
|
|
end
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
## Matchers
|
2016-09-22 12:43:57 +00:00
|
|
|
|
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/).
|