Added input and input file option for shell, along with functional test cases

Signed-off-by: Nikita Mathur <nikita.mathur@chef.io>
This commit is contained in:
Nikita Mathur 2021-04-08 12:58:36 +05:30
parent 499bc99c71
commit 5865cdd1c4
2 changed files with 35 additions and 0 deletions

View file

@ -325,6 +325,10 @@ class Inspec::InspecCLI < Inspec::BaseCLI
desc: "Maximum seconds to allow a command to run. Default 3600.",
long_desc: "Maximum seconds to allow commands to run. Default 3600. A timed out command is considered an error."
option :inspect, type: :boolean, default: false, desc: "Use verbose/debugging output for resources."
option :input_file, type: :array,
desc: "Load one or more input files, a YAML file with values for the shell to use"
option :input, type: :array, banner: "name1=value1 name2=value2",
desc: "Specify one or more inputs directly on the command line, as --input NAME=VALUE. Accepts single-quoted YAML and JSON structures."
def shell_func
o = config
diagnose(o)

View file

@ -2,6 +2,7 @@ require "functional/helper"
describe "inspec shell tests" do
include FunctionalHelper
let(:input_file_from_basic_input_profile) { File.join(profile_path, "inputs", "basic", "files", "flat.yaml") }
parallelize_me!
@ -22,6 +23,22 @@ describe "inspec shell tests" do
out
end
def assert_shell_c_with_inputs(code, input_cmd, input, exit_status, json = false, stderr= "")
json_suffix = " --reporter 'json'" if json
command = "shell -c '#{code.tr("'", '\\\'')}'#{input_cmd} #{input}#{json_suffix}"
# On darwin this value is:
# shell -c 'describe file(\"/Users/nickschwaderer/Documents/inspec/inspec/test/functional/inspec_shell_test.rb\") do it { should exist } end' --reporter 'json'"
# appears to break in windows.
out = inspec(command)
actual = out.stderr.gsub(/\e\[(\d+)(;\d+)*m/, "") # strip ANSI color codes
_(actual).must_equal stderr
assert_exit_code exit_status, out
out
end
it "loads a dependency" do
res = inspec("shell -c 'example_config' --depends #{example_profile}")
@ -178,6 +195,20 @@ describe "inspec shell tests" do
_(out.stdout).must_include "0 successful"
_(out.stdout).must_include "1 failure"
end
it "loads input from external input file" do
skip_windows! # Breakage confirmed
out = assert_shell_c_with_inputs("describe input(\"a_quoted_string\") do it { should cmp \"Should not have quotes\" } end", " --input-file", input_file_from_basic_input_profile, 0)
_(out.stdout).must_include "1 successful"
_(out.stdout).must_include "0 failures"
end
it "loads input from input cli" do
skip_windows! # Breakage confirmed
out = assert_shell_c_with_inputs("describe input(\"test_input_01\") do it { should cmp \"value_from_cli_01\" } end", " --input", "test_input_01='value_from_cli_01'", 0)
_(out.stdout).must_include "1 successful"
_(out.stdout).must_include "0 failures"
end
end
# Pry does not support STDIN from windows currently. Skipping these for now.