mirror of
https://github.com/inspec/inspec
synced 2024-11-26 22:50:36 +00:00
parse_config - accept array notation to handle dots
This commit is contained in:
commit
f5466687cc
6 changed files with 80 additions and 33 deletions
|
@ -53,9 +53,20 @@ where each test
|
|||
- May run a command to `stdout`, and then run the test against that output
|
||||
- May use options to define how configuration data is to be parsed
|
||||
|
||||
## Matchers
|
||||
## Options
|
||||
|
||||
For a full list of available matchers, please visit our [matchers page](/inspec/matchers/).
|
||||
This resource supports multiple options to parse configuration data. Use the options in an `options` block stated outside of (and immediately before) the actual test. For example:
|
||||
|
||||
options = {
|
||||
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
|
||||
multiple_values: true
|
||||
}
|
||||
|
||||
output = command('some-command').stdout
|
||||
|
||||
describe parse_config(output, options) do
|
||||
its('setting') { should eq 1 }
|
||||
end
|
||||
|
||||
### assignment_regex
|
||||
|
||||
|
@ -114,3 +125,12 @@ Use `standalone_comments: false`, to parse the following:
|
|||
|
||||
'key = value # comment'
|
||||
params['key'] = 'value'
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
This resource is based on the `parse_config_file` resource. See the [`parse_config_file`](/inspec/resources/parse_config_file) resource for examples.
|
||||
|
||||
## Matchers
|
||||
|
||||
For a full list of available matchers, please visit our [matchers page](/inspec/matchers/).
|
||||
|
|
|
@ -50,9 +50,16 @@ where each test
|
|||
- May run a command to `stdout`, and then run the test against that output
|
||||
- May use options to define how configuration data is to be parsed
|
||||
|
||||
### Option Names Containing Periods
|
||||
|
||||
A possible behavior may occur when attempting to access option names containing periods with `its()`. There are two ways to work around it:
|
||||
|
||||
* Access the option by using the `params` attribute of the returned resource object
|
||||
* Since 4.24.5, `its` can be used by passing the option name in a single-element array. See the `parse config file` examples.
|
||||
|
||||
## Options
|
||||
|
||||
This resource supports the following options for parsing configuration data. Use them in an `options` block stated outside of (and immediately before) the actual test:
|
||||
This resource supports multiple options to parse configuration data. Use the options in an `options` block stated outside of (and immediately before) the actual test. For example:
|
||||
|
||||
options = {
|
||||
assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
|
||||
|
@ -62,32 +69,6 @@ This resource supports the following options for parsing configuration data. Use
|
|||
its('setting') { should eq 1 }
|
||||
end
|
||||
|
||||
## Examples
|
||||
|
||||
The following examples show how to use this Chef InSpec audit resource.
|
||||
|
||||
### Test a configuration setting
|
||||
|
||||
describe parse_config_file('/path/to/file.conf') do
|
||||
its('PARAM_X') { should eq 'Y' }
|
||||
end
|
||||
|
||||
### Use options, and then test a configuration setting
|
||||
|
||||
describe parse_config_file('/path/to/file.conf', { multiple_values: true }) do
|
||||
its('PARAM_X') { should include 'Y' }
|
||||
end
|
||||
|
||||
### Test a file with an ini-like structure (such as a yum.conf)
|
||||
|
||||
describe parse_config_file('/path/to/yum.conf') do
|
||||
its('main') { should include('gpgcheck' => '1') }
|
||||
end
|
||||
|
||||
## Matchers
|
||||
|
||||
For a full list of available matchers, please visit our [matchers page](/inspec/matchers/).
|
||||
|
||||
### assignment_regex
|
||||
|
||||
Use `assignment_regex` to test a key value using a regular expression:
|
||||
|
@ -145,3 +126,37 @@ Use `standalone_comments: false`, to parse the following:
|
|||
|
||||
'key = value # comment'
|
||||
params['key'] = 'value'
|
||||
|
||||
## Matchers
|
||||
|
||||
For a full list of available matchers, please visit our [matchers page](/inspec/matchers/).
|
||||
|
||||
## Examples
|
||||
|
||||
The following examples show how to use this Chef InSpec audit resource.
|
||||
|
||||
### Test A Configuration Setting
|
||||
|
||||
describe parse_config_file('/path/to/file.conf') do
|
||||
its('PARAM_X') { should eq 'Y' }
|
||||
end
|
||||
|
||||
### Use Options And Then Test A Configuration Setting
|
||||
|
||||
describe parse_config_file('/path/to/file.conf', { multiple_values: true }) do
|
||||
its('PARAM_X') { should include 'Y' }
|
||||
end
|
||||
|
||||
### Test A File With An INI File Structure
|
||||
|
||||
`yum.conf` is one example of an INI file structure type.
|
||||
|
||||
describe parse_config_file('/path/to/yum.conf') do
|
||||
its('main') { should include('gpgcheck' => '1') }
|
||||
end
|
||||
|
||||
### Test A Configuration Setting Containing Periods
|
||||
|
||||
describe parse_config_file('/etc/sysctl.conf') do
|
||||
its(['kernel.domainname']) { should eq 'example.com' }
|
||||
end
|
||||
|
|
|
@ -48,7 +48,7 @@ module Inspec::Resources
|
|||
# @return [Object] the value stored at this position
|
||||
def method_missing(*keys)
|
||||
# catch bahavior of rspec its implementation
|
||||
# @see https://github.com/rspec/rspec-its/blob/master/lib/rspec/its.rb#L110
|
||||
# @see https://github.com/rspec/rspec-its/blob/v1.2.0/lib/rspec/its.rb#L110
|
||||
keys.shift if keys.is_a?(Array) && keys[0] == :[]
|
||||
value(keys)
|
||||
end
|
||||
|
|
|
@ -55,8 +55,11 @@ module Inspec::Resources
|
|||
read_params unless @content.nil?
|
||||
end
|
||||
|
||||
def method_missing(name)
|
||||
read_params[name.to_s]
|
||||
def method_missing(*name)
|
||||
# catch bahavior of rspec its implementation
|
||||
# @see https://github.com/rspec/rspec-its/blob/v1.2.0/lib/rspec/its.rb#L110
|
||||
name.shift if name.is_a?(Array) && name[0] == :[]
|
||||
read_params[name[0].to_s]
|
||||
end
|
||||
|
||||
def params(*opts)
|
||||
|
|
|
@ -39,7 +39,7 @@ module Inspec::Resources
|
|||
# returns nil, if not existant or value
|
||||
def method_missing(*keys)
|
||||
# catch behavior of rspec its implementation
|
||||
# @see https://github.com/rspec/rspec-its/blob/master/lib/rspec/its.rb#L110
|
||||
# @see https://github.com/rspec/rspec-its/blob/v1.2.0/lib/rspec/its.rb#L110
|
||||
keys.shift if keys.is_a?(Array) && keys[0] == :[]
|
||||
|
||||
# map all symbols to strings
|
||||
|
|
|
@ -24,4 +24,13 @@ describe "Inspec::Resources::ParseConfig" do
|
|||
_(resource.params).must_equal result
|
||||
_(resource.send("kernel.domainname")).must_equal "example.com"
|
||||
end
|
||||
|
||||
it "parse_config resource accepts arrays due to rspec's its behavior" do
|
||||
options = {
|
||||
assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/,
|
||||
}
|
||||
params = [:[], "kernel.domainname"]
|
||||
resource = MockLoader.new(:centos6).load_resource("parse_config", "kernel.domainname = example.com", options)
|
||||
_(resource.send(*params)).must_equal "example.com"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue