Added new pattern option, fix for input options on dsl and functional test cases for input options

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2021-04-16 01:05:35 +05:30
parent 85ca124809
commit e77d5331c5
8 changed files with 125 additions and 10 deletions

View file

@ -50,7 +50,6 @@ module Inspec
:file, # File containing the input-changing action, if known
:line, # Line in file containing the input-changing action, if known
:hit, # if action is :fetch, true if the remote source had the input
:pattern, # Regex Pattern to validate input value
].freeze
# Value has a special handler
@ -230,6 +229,8 @@ module Inspec
end
end
events << options[:event] if options.key? :event
enforce_required_validation!
enforce_type_restriction!
enforce_pattern_restriction!
end
@ -260,7 +261,6 @@ module Inspec
end
end
event.value = options[:value] if options.key?(:value)
event.pattern = options[:pattern] if options.key?(:pattern)
options[:event] = event
end
@ -283,8 +283,7 @@ module Inspec
action: :create,
provider: options[:provider],
file: loc.path,
line: loc.lineno,
pattern: options[:pattern]
line: loc.lineno
)
end
@ -310,7 +309,7 @@ module Inspec
def value=(new_value, priority = DEFAULT_PRIORITY_FOR_VALUE_SET)
# Inject a new Event with the new value.
location = Event.probe_stack
event = Event.new(
events << Event.new(
action: :set,
provider: :value_setter,
priority: priority,
@ -318,14 +317,13 @@ module Inspec
file: location.path,
line: location.lineno
)
event.pattern = pattern if pattern
events << event
enforce_required_validation!
enforce_type_restriction!
enforce_pattern_restriction!
end
def value
enforce_required_validation!
current_value
end
@ -445,7 +443,6 @@ module Inspec
@pattern = pattern
end
def valid_numeric?(value)
Float(value)
true

View file

@ -180,7 +180,15 @@ module Inspec
options[:priority] ||= 20
options[:provider] = :inline_control_code
evt = Inspec::Input.infer_event(options)
Inspec::InputRegistry.find_or_register_input(input_name, __profile_id, event: evt).value
Inspec::InputRegistry.find_or_register_input(
input_name,
__profile_id,
type: options[:type],
required: options[:required],
description: options[:description],
pattern: options[:pattern],
event: evt
).value
end
end

View file

@ -0,0 +1,38 @@
# copyright: 2021, Chef Software, Inc.
title "Testing all option flags on input through DSL"
control "pattern_flag_success_check" do
describe input("input_value_01", value: 5, pattern: "^\d*[13579]$") do
it { should eq 5 }
end
end
control "pattern_flag_failure_check" do
describe input("input_value_02", value: 2, pattern: "^\d*[13579]$") do
it { should eq 2 }
end
end
control "required_flag_success_check" do
describe input("input_value_03", value: 5, required: true) do
it { should eq 5 }
end
end
control "required_flag_failure_check" do
describe input("input_value_04", required: true) do
it { should eq 5 }
end
end
control "type_flag_success_check" do
describe input("input_value_05", value: 5, type: "Numeric") do
it { should eq 5 }
end
end
control "type_flag_failure_check" do
describe input("input_value_06", value: 5, type: "String") do
it { should eq 5 }
end
end

View file

@ -0,0 +1,9 @@
name: dsl
title: InSpec Profile to test all option flags on input through dsl
maintainer: Chef Software, Inc.
copyright: Chef Software, Inc.
license: Apache-2.0
summary: A profile that tests all option flags on input through dsl
version: 0.1.0
supports:
platform: os

View file

@ -0,0 +1,8 @@
# copyright: 2021, Chef Software, Inc.
title "Testing :pattern flag"
control "pattern_flag_checking_odd_num" do
describe input("input_value_01") do
it { should eq 5 }
end
end

View file

@ -0,0 +1,14 @@
name: metadata-pattern
title: InSpec Profile to test :pattern flag on inputs using metadata
maintainer: Chef Software, Inc.
copyright: Chef Software, Inc.
license: Apache-2.0
summary: A profile that tests the :pattern flag on inputs
version: 0.1.0
supports:
platform: os
inputs:
- name: input_value_01
value: 5
pattern: ^\d*[13579]$
required: true

View file

@ -462,4 +462,42 @@ describe "inputs" do
assert_json_controls_passing(result)
end
end
describe "when a profile is used with input options" do
it "should be a success for valid values when pattern flag is passed through metadata file" do
result = run_inspec_process("exec #{inputs_profiles_path}/metadata-pattern", json: true)
_(result.stderr).must_be_empty
assert_json_controls_passing(result)
end
it "should be a success for valid values when required, type and pattern flag is passed through dsl" do
result = run_inspec_process("exec #{inputs_profiles_path}/dsl --controls pattern_flag_success_check required_flag_success_check type_flag_success_check", json: true)
_(result.stderr).must_be_empty
assert_json_controls_passing(result)
end
it "should be a failure for invalid value when required flag is passed through dsl" do
result = run_inspec_process("exec #{inputs_profiles_path}/dsl --controls required_flag_failure_check", json: true)
_(result.stderr).must_be_empty
output = JSON.parse(result[0])
assert_equal "failed", output["profiles"][0]["controls"][0]["results"][0]["status"]
assert_exit_code(100, result)
end
it "should be a failure for invalid value when type flag is passed through dsl" do
result = run_inspec_process("exec #{inputs_profiles_path}/dsl --controls type_flag_failure_check", json: true)
_(result.stderr).must_be_empty
output = JSON.parse(result[0])
assert_equal "failed", output["profiles"][0]["controls"][0]["results"][0]["status"]
assert_exit_code(100, result)
end
it "should be a failure for invalid value when pattern flag is passed through dsl" do
result = run_inspec_process("exec #{inputs_profiles_path}/dsl --controls pattern_flag_failure_check", json: true)
_(result.stderr).must_be_empty
output = JSON.parse(result[0])
assert_equal "failed", output["profiles"][0]["controls"][0]["results"][0]["status"]
assert_exit_code(100, result)
end
end
end

View file

@ -15,6 +15,7 @@ describe Inspec::Input do
required: true,
title: "how is this different than description",
type: "Numeric",
pattern: "^[0-9][0-9]$",
}.each do |field, value|
it "should be able to recall the #{field} field" do
opts[field] = value
@ -32,6 +33,7 @@ describe Inspec::Input do
title: "Best input ever",
description: "important",
type: "Numeric",
pattern: "^[0-9][0-9]$",
required: true)
_(input.to_hash).must_equal({
@ -41,6 +43,7 @@ describe Inspec::Input do
title: "Best input ever",
description: "important",
type: "Numeric",
pattern: "^[0-9][0-9]$",
required: true,
},
})