2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/objects/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-02-07 04:19:09 +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-02-07 04:19:09 +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-02-07 04:19:09 +00:00
|
|
|
creation_events.count.must_equal 1
|
2019-02-07 18:05:46 +00:00
|
|
|
set_events = ipt.set_events
|
2019-02-07 04:19:09 +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-02-07 18:05:46 +00:00
|
|
|
ipt.set_events.count.must_equal 0
|
|
|
|
ipt.value = 42
|
|
|
|
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-02-07 18:05:46 +00:00
|
|
|
ipt.set_events.count.must_equal 0
|
|
|
|
ipt.value = 1
|
|
|
|
ipt.value = 2
|
|
|
|
ipt.value = 3
|
|
|
|
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)
|
|
|
|
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-02-07 18:05:46 +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)
|
|
|
|
|
|
|
|
ipt.value.must_equal 3
|
|
|
|
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
|
|
|
|
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")
|
|
|
|
lines.count.must_equal 5 # 3 events above + 1 create + 1 input name line
|
|
|
|
lines.shift # Not testing the inputs top line here
|
|
|
|
|
|
|
|
lines.each do |line|
|
2019-05-31 21:59:06 +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
|
|
|
|
|
|
|
|
lines[0].must_include "action: 'create',"
|
|
|
|
|
|
|
|
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 03:00:03 +00:00
|
|
|
end
|
|
|
|
end
|
2019-06-11 22:24:35 +00:00
|
|
|
end
|