Commit e22c6b03 authored by Nate Wolfe's avatar Nate Wolfe

(PE-1300) Don't ignore failed resource statuses

Prior to this commit, the PuppetDB report processor/terminus
ignored 'failed' resource statuses unless they have events.
This commit changes the processor so it no longer ignores them
and instead it fabricates an appropriate event to attach to
the 'failed' resource status.

Ideally, Puppet should be including resource events even for
'failed' statuses, but until then, and to support backwards
compatibility, we can just fabricate the events.
parent ed3de971
......@@ -40,22 +40,24 @@ Puppet::Reports.register_report(:puppetdb) do
"configuration-version" => configuration_version.to_s,
"start-time" => Puppet::Util::Puppetdb.to_wire_time(time),
"end-time" => Puppet::Util::Puppetdb.to_wire_time(time + run_duration),
"resource-events" =>
filter_events(resource_statuses.inject([]) do |events, status_entry|
resource, status = *status_entry
if ! (status.events.empty?)
events.concat(
status.events.map do |event|
event_to_hash(status, event)
end)
elsif status.skipped == true
events.concat([resource_status_to_skipped_event_hash(status)])
end
events
end)
"resource-events" => build_events_list
}
end
def build_events_list
filter_events(resource_statuses.inject([]) do |events, status_entry|
resource, status = *status_entry
if ! (status.events.empty?)
events.concat(status.events.map { |event| event_to_hash(status, event) })
elsif status.skipped == true
events.concat([fabricate_event(status, "skipped")])
elsif status.failed == true
events.concat([fabricate_event(status, "failure")])
end
events
end)
end
def run_duration
# TODO: this is wrong in puppet. I am consistently seeing reports where
# start-time + this value is less than the timestamp on the individual
......@@ -68,28 +70,27 @@ Puppet::Reports.register_report(:puppetdb) do
## Convert an instance of `Puppet::Transaction::Event` to a hash
## suitable for sending over the wire to PuppetDB
def event_to_hash(status, event)
def event_to_hash(resource_status, event)
{
"status" => event.status,
"timestamp" => Puppet::Util::Puppetdb.to_wire_time(event.time),
"resource-type" => status.resource_type,
"resource-title" => status.title,
"resource-type" => resource_status.resource_type,
"resource-title" => resource_status.title,
"property" => event.property,
"new-value" => event.desired_value,
"old-value" => event.previous_value,
"message" => event.message,
"file" => status.file,
"line" => status.line
"file" => resource_status.file,
"line" => resource_status.line
}
end
## Given an instance of `Puppet::Resource::Status` with
## a status of 'skipped', this method fabricates a PuppetDB
## event object representing the skipped resource.
def resource_status_to_skipped_event_hash(resource_status)
## Given an instance of `Puppet::Resource::Status` and a status string,
## this method fabricates a PuppetDB event object with the provided
## `"status"`.
def fabricate_event(resource_status, event_status)
{
"status" => "skipped",
"status" => event_status,
"timestamp" => Puppet::Util::Puppetdb.to_wire_time(resource_status.time),
"resource-type" => resource_status.resource_type,
"resource-title" => resource_status.title,
......
......@@ -124,17 +124,65 @@ describe processor do
end
end
context "skipped resource" do
context "skipped resource status" do
it "should include the resource" do
status.skipped = true
result = subject.send(:report_to_hash)
result["resource-events"].length.should == 1
event = result["resource-events"][0]
event["resource-type"].should == "Foo"
event["resource-title"].should == "foo"
event["status"].should == "skipped"
event["property"].should be_nil
event["new-val"].should be_nil
event["old-val"].should be_nil
event["message"].should be_nil
end
end
context "failed resource status" do
before :each do
status.stubs(:failed).returns(true)
end
context "with no events" do
it "should include a fabricated event" do
result = subject.send(:report_to_hash)
result["resource-events"].length.should == 1
event = result["resource-events"][0]
event["status"].should == "failure"
event["resource-type"].should == "Foo"
event["resource-title"].should == "foo"
event["property"].should be_nil
event["new-value"].should be_nil
event["old-value"].should be_nil
event["message"].should be_nil
event["file"].should == "foo"
event["line"].should == 1
end
end
context "with events" do
it "should include the actual event" do
event = Puppet::Transaction::Event.new
event.property = "barprop"
event.desired_value = "barval"
event.previous_value = "oldbarval"
event.message = "barmessage"
status.add_event(event)
result = subject.send(:report_to_hash)
result["resource-events"].length.should == 1
res_event = result["resource-events"][0]
res_event["resource-type"].should == "Foo"
res_event["resource-title"].should == "foo"
res_event["property"].should == "barprop"
res_event["new-value"].should == "barval"
res_event["old-value"].should == "oldbarval"
res_event["message"].should == "barmessage"
res_event["file"].should == "foo"
res_event["line"].should == 1
end
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment