2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
2019-09-13 04:10:59 +00:00
|
|
|
require "inspec/input"
|
2019-02-07 03:00:03 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Input and Events" do
|
|
|
|
let(:ipt) { Inspec::Input.new("input") }
|
2019-02-07 18:05:46 +00:00
|
|
|
|
|
|
|
#==============================================================#
|
|
|
|
# Create Event
|
|
|
|
#==============================================================#
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when creating an input" do
|
|
|
|
it "should have a creation event" do
|
2019-02-07 18:05:46 +00:00
|
|
|
creation_events = ipt.events.select { |e| e.action == :create }
|
2019-09-30 22:31:55 +00:00
|
|
|
_(creation_events).wont_be_empty
|
2019-02-07 03:00:03 +00:00
|
|
|
end
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "should only have a creation event if no value was provided" do
|
2019-02-07 18:05:46 +00:00
|
|
|
creation_events = ipt.events.select { |e| e.action == :create }
|
2019-09-30 22:31:55 +00:00
|
|
|
_(creation_events.count).must_equal 1
|
2019-02-07 03:00:03 +00:00
|
|
|
end
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "should have a create and a set event if a value was provided" do
|
|
|
|
ipt = Inspec::Input.new("input", value: 42)
|
2019-02-07 18:05:46 +00:00
|
|
|
creation_events = ipt.events.select { |e| e.action == :create }
|
2019-09-30 22:31:55 +00:00
|
|
|
_(creation_events.count).must_equal 1
|
2019-02-07 18:05:46 +00:00
|
|
|
set_events = ipt.set_events
|
2019-09-30 22:31:55 +00:00
|
|
|
_(set_events.count).must_equal 1
|
|
|
|
_(set_events.first.value).must_equal 42
|
2019-02-07 03:00:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-07 18:05:46 +00:00
|
|
|
#==============================================================#
|
|
|
|
# Set Events
|
|
|
|
#==============================================================#
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when setting an input using value=" do
|
|
|
|
it "should add a set event" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(ipt.set_events.count).must_equal 0
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.value = 42
|
2019-09-30 22:31:55 +00:00
|
|
|
_(ipt.set_events.count).must_equal 1
|
2019-02-07 03:00:03 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "should add one event for each value= operation" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(ipt.set_events.count).must_equal 0
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.value = 1
|
|
|
|
ipt.value = 2
|
|
|
|
ipt.value = 3
|
2019-09-30 22:31:55 +00:00
|
|
|
_(ipt.set_events.count).must_equal 3
|
2019-02-07 03:00:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-07 18:05:46 +00:00
|
|
|
#==============================================================#
|
|
|
|
# Picking a Winner
|
|
|
|
#==============================================================#
|
|
|
|
|
2019-02-07 03:00:03 +00:00
|
|
|
# For more real-world testing of metadata vs --attrs vs inline, see
|
|
|
|
# test/functional/inputs_test.rb
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "priority voting" do
|
|
|
|
it "value() should return the correct value when there is just one set operation" do
|
2019-02-07 04:19:09 +00:00
|
|
|
evt = Inspec::Input::Event.new(value: 42, priority: 25, action: :set)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(ipt.value).must_equal 42
|
2019-02-07 03:00:03 +00:00
|
|
|
end
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "should return the highest priority regardless of order" do
|
2019-02-07 04:19:09 +00:00
|
|
|
evt1 = Inspec::Input::Event.new(value: 1, priority: 25, action: :set)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt1)
|
2019-02-07 04:19:09 +00:00
|
|
|
evt2 = Inspec::Input::Event.new(value: 2, priority: 35, action: :set)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt2)
|
2019-02-07 04:19:09 +00:00
|
|
|
evt3 = Inspec::Input::Event.new(value: 3, priority: 15, action: :set)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt3)
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(ipt.value).must_equal 2
|
2019-02-07 03:00:03 +00:00
|
|
|
end
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "breaks ties using the last event of the highest priority" do
|
2019-02-07 04:19:09 +00:00
|
|
|
evt1 = Inspec::Input::Event.new(value: 1, priority: 15, action: :set)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt1)
|
2019-02-07 04:19:09 +00:00
|
|
|
evt2 = Inspec::Input::Event.new(value: 2, priority: 25, action: :set)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt2)
|
2019-02-07 04:19:09 +00:00
|
|
|
evt3 = Inspec::Input::Event.new(value: 3, priority: 25, action: :set)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt3)
|
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(ipt.value).must_equal 3
|
2019-02-07 18:05:46 +00:00
|
|
|
end
|
|
|
|
end
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-02-07 18:05:46 +00:00
|
|
|
#==============================================================#
|
|
|
|
# Stack Hueristics
|
|
|
|
#==============================================================#
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "when determining where the call came from" do
|
|
|
|
it "should get the line and file correct in the constructor" do
|
2019-02-07 18:05:46 +00:00
|
|
|
expected_file = __FILE__
|
2019-06-11 22:24:35 +00:00
|
|
|
expected_line = __LINE__; ipt = Inspec::Input.new("some_input") # Important to keep theses on one line
|
2019-02-07 18:05:46 +00:00
|
|
|
event = ipt.events.first
|
2019-09-30 22:31:55 +00:00
|
|
|
_(event.file).must_equal expected_file
|
|
|
|
_(event.line).must_equal expected_line
|
2019-02-07 04:19:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-07 18:05:46 +00:00
|
|
|
#==============================================================#
|
|
|
|
# Diagnostics
|
|
|
|
#==============================================================#
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "input diagnostics" do
|
|
|
|
it "should dump the events" do
|
|
|
|
evt1 = Inspec::Input::Event.new(value: { a: 1, b: 2 }, priority: 15, action: :set, provider: :unit_test)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt1)
|
2019-02-07 04:19:09 +00:00
|
|
|
evt2 = Inspec::Input::Event.new(action: :fetch, provider: :alcubierre, hit: false)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt2)
|
2019-06-11 22:24:35 +00:00
|
|
|
evt3 = Inspec::Input::Event.new(value: 12, action: :set, provider: :control_dsl, file: "/tmp/some/file.rb", line: 2)
|
2019-02-07 18:05:46 +00:00
|
|
|
ipt.update(event: evt3)
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-02-07 18:05:46 +00:00
|
|
|
text = ipt.diagnostic_string
|
2019-02-07 04:19:09 +00:00
|
|
|
lines = text.split("\n")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(lines.count).must_equal 5 # 3 events above + 1 create + 1 input name line
|
2019-02-07 04:19:09 +00:00
|
|
|
lines.shift # Not testing the inputs top line here
|
|
|
|
|
|
|
|
lines.each do |line|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(line).must_match(/^\s\s([a-z]+:\s\'.+\',\s)*?([a-z]+:\s\'.+\')$/) # key: 'value', key: 'value' ...
|
2019-02-07 04:19:09 +00:00
|
|
|
end
|
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(lines[0]).must_include "action: 'create',"
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(lines[1]).must_include "action: 'set',"
|
|
|
|
_(lines[1]).must_include "value: '{" # It should to_s the value
|
|
|
|
_(lines[1]).must_include "provider: 'unit_test'"
|
|
|
|
_(lines[1]).must_include "priority: '15'"
|
2019-02-07 04:19:09 +00:00
|
|
|
|
2019-02-07 03:00:03 +00:00
|
|
|
end
|
|
|
|
end
|
2019-06-11 22:24:35 +00:00
|
|
|
end
|