mirror of
https://github.com/inspec/inspec
synced 2024-11-26 14:40:26 +00:00
add json resource
This commit is contained in:
parent
2eeba59db3
commit
0e43d4ca6a
5 changed files with 89 additions and 1 deletions
56
lib/resources/json.rb
Normal file
56
lib/resources/json.rb
Normal file
|
@ -0,0 +1,56 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require 'json'
|
||||
|
||||
# Parses a json document
|
||||
# Usage:
|
||||
# describe yaml('policyfile.lock.json') do
|
||||
# its('cookbook_locks.omnibus.version') { should eq('2.2.0') }
|
||||
# end
|
||||
class JsonConfig < Vulcano.resource(1)
|
||||
name 'json'
|
||||
|
||||
# make params readable
|
||||
attr_reader :params
|
||||
|
||||
def initialize(path)
|
||||
@path = path
|
||||
@file_content = vulcano.file(@path).content
|
||||
@params = parse(@file_content)
|
||||
end
|
||||
|
||||
def parse(content)
|
||||
JSON.parse(content, object_class: OpenStruct)
|
||||
end
|
||||
|
||||
def extract_value(keys, value)
|
||||
key = keys.shift
|
||||
return nil if key.nil?
|
||||
|
||||
# check if key is a num, try to extract from array
|
||||
if key.to_i.to_s == key
|
||||
value = value[key.to_i]
|
||||
# if value is an array, iterate over each child
|
||||
elsif value.is_a?(Array)
|
||||
value = value.map { |i|
|
||||
extract_value([key], i)
|
||||
}
|
||||
# normal value extraction
|
||||
else
|
||||
value = value[key].nil? ? nil : value[key]
|
||||
end
|
||||
|
||||
# check if further keys exist
|
||||
if !keys.first.nil?
|
||||
return extract_value(keys.clone, value)
|
||||
else
|
||||
return value
|
||||
end
|
||||
end
|
||||
|
||||
def method_missing(name)
|
||||
# split dotted path
|
||||
keys = name.to_s.split('.')
|
||||
extract_value(keys, @params.clone)
|
||||
end
|
||||
end
|
|
@ -30,6 +30,7 @@ require 'resources/file'
|
|||
require 'resources/gem'
|
||||
require 'resources/group_policy'
|
||||
require 'resources/inetd_conf'
|
||||
require 'resources/json'
|
||||
require 'resources/kernel_module'
|
||||
require 'resources/kernel_parameter'
|
||||
require 'resources/limits_conf'
|
||||
|
|
|
@ -43,7 +43,8 @@ def loadResource (resource, *args)
|
|||
'/etc/group' => mockfile.('group'),
|
||||
'/etc/audit/auditd.conf' => mockfile.('auditd.conf'),
|
||||
'/etc/mysql/my.cnf' => mockfile.('mysql.conf'),
|
||||
'/etc/mysql/mysql2.conf' => mockfile.('mysql2.conf')
|
||||
'/etc/mysql/mysql2.conf' => mockfile.('mysql2.conf'),
|
||||
'policyfile.lock.json' => mockfile.('policyfile.lock.json'),
|
||||
}
|
||||
|
||||
# create all mock commands
|
||||
|
|
12
test/unit/mock/files/policyfile.lock.json
Normal file
12
test/unit/mock/files/policyfile.lock.json
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"name": "demo",
|
||||
"run_list": [
|
||||
"apache2",
|
||||
"omnibus"
|
||||
],
|
||||
"cookbook_locks": {
|
||||
"omnibus": {
|
||||
"version": "2.2.0"
|
||||
}
|
||||
}
|
||||
}
|
18
test/unit/resource_json_test.rb
Normal file
18
test/unit/resource_json_test.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require 'helper'
|
||||
require 'vulcano/resource'
|
||||
|
||||
describe 'Vulcano:Resources::JSON' do
|
||||
describe 'json' do
|
||||
|
||||
let(:resource) { loadResource('json', 'policyfile.lock.json') }
|
||||
|
||||
it 'verify yaml parsing' do
|
||||
_(resource.params).wont_equal nil
|
||||
_(resource.send('name')).must_equal 'demo'
|
||||
_(resource.send('run_list')).must_equal %w{apache2 omnibus}
|
||||
_(resource.send('cookbook_locks.omnibus.version')).must_equal '2.2.0'
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue