Adding toml resource (#1924)

* Adding toml resource

This adds a `toml` resource that inherits from the json resource and
behaves the same way as the JSON and YAML resources.

Signed-off-by: Nolan Davidson <ndavidson@chef.io>
This commit is contained in:
Nolan Davidson 2017-06-15 16:54:12 -04:00 committed by Adam Leff
parent 3d4f1f8d39
commit 52cc27dd06
8 changed files with 87 additions and 1 deletions

View file

@ -137,6 +137,7 @@ require 'resources/shadow'
require 'resources/ssl'
require 'resources/ssh_conf'
require 'resources/sys_info'
require 'resources/toml'
require 'resources/users'
require 'resources/vbscript'
require 'resources/virtualization'

26
lib/resources/toml.rb Normal file
View file

@ -0,0 +1,26 @@
# encoding: utf-8
# author: Nolan Davidson
require 'toml'
module Inspec::Resources
class TomlConfig < JsonConfig
name 'toml'
desc 'Use the toml InSpec resource to test configuration data in a TOML file'
example "
describe toml('default.toml') do
its('key') { should eq('value') }
its (['arr', 1]) { should eq 2 }
its (['mytable', 'key1']) { should eq 'value1' }
end
"
def parse(content)
TOML::Parser.new(content).parsed
end
def to_s
"TOML #{@path}"
end
end
end

View file

@ -0,0 +1,6 @@
key = "value"
arr = [1, 2, 3]
[mytable]
key1 = "value1"
key2 = "value2"

View file

@ -15,7 +15,7 @@ gid = case node['platform_family']
'root'
end
['yml', 'json', 'csv', 'ini'].each { |filetype|
['yml', 'json', 'csv', 'ini', 'toml'].each { |filetype|
if node['platform_family'] != 'windows'
cookbook_file "/tmp/example.#{filetype}" do

View file

@ -148,6 +148,7 @@ class MockLoader
'test_ca_public.key.pem' => mockfile.call('test_ca_public.key.pem'),
# Test DH parameters, 2048 bit long safe prime, generator 2 for dh_params in PEM format
'dh_params.dh_pem' => mockfile.call('dh_params.dh_pem'),
'default.toml' => mockfile.call('default.toml'),
}
# create all mock commands

View file

@ -0,0 +1,13 @@
# encoding: utf-8
if os.unix?
filename = '/tmp/example.toml'
else
filename = 'c:/windows/temp/example.toml'
end
describe toml(filename) do
its ('key') { should eq('value') }
its (['arr', 1]) { should eq 2 }
its (['mytable', 'key1']) { should eq 'value1' }
end

View file

@ -0,0 +1,6 @@
key = "value"
arr = [1, 2, 3]
[mytable]
key1 = "value1"
key2 = "value2"

View file

@ -0,0 +1,33 @@
# encoding: utf-8
require 'helper'
require 'inspec/resource'
describe 'Inspec::Resources::TOML' do
describe 'when loading valid TOML' do
let (:resource) { load_resource('toml', 'default.toml') }
it 'gets params as a hash' do
_(resource.params).must_be_kind_of Hash
end
it 'retrieves nil if a param is missing' do
_(resource.params['missing']).must_be_nil
end
it 'retrieves params by name' do
_(resource.params['key']).must_equal 'value'
end
it 'retrieves array by name' do
_(resource.params['arr']).must_be_kind_of Array
_(resource.params['arr']).must_equal [1, 2, 3]
end
it 'retrieves table by name as hash' do
h = {"key1" => "value1", "key2" => "value2"}
_(resource.params['mytable']).must_be_kind_of Hash
_(resource.params['mytable']).must_equal h
end
end
end