From 205982806c243377b3709e392fd48d2b4d144042 Mon Sep 17 00:00:00 2001 From: Justin Kolberg Date: Wed, 20 May 2015 10:22:45 -0700 Subject: [PATCH] [api_results] avoid altering check "schema" --- lib/sensu/api/process.rb | 10 +++++++--- lib/sensu/server/process.rb | 1 - spec/api/process_spec.rb | 6 +++--- spec/helpers.rb | 4 ++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/sensu/api/process.rb b/lib/sensu/api/process.rb index 3b511c50..17e781d6 100644 --- a/lib/sensu/api/process.rb +++ b/lib/sensu/api/process.rb @@ -736,7 +736,8 @@ def resolve_event(event_json) checks.each_with_index do |check_name, check_index| result_key = "result:#{client_name}:#{check_name}" settings.redis.get(result_key) do |result_json| - response << MultiJson.load(result_json) + check_result = MultiJson.load(result_json) + response << {:client => client_name, :check => check_result} if client_index == clients.size - 1 && check_index == checks.size - 1 body MultiJson.dump(response) end @@ -760,7 +761,8 @@ def resolve_event(event_json) checks.each_with_index do |check_name, check_index| result_key = "result:#{client_name}:#{check_name}" settings.redis.get(result_key) do |result_json| - response << MultiJson.load(result_json) + check_result = MultiJson.load(result_json) + response << {:client => client_name, :check => check_result} if check_index == checks.size - 1 body MultiJson.dump(response) end @@ -776,7 +778,9 @@ def resolve_event(event_json) result_key = "result:#{client_name}:#{check_name}" settings.redis.get(result_key) do |result_json| unless result_json.nil? - body result_json + check_result = MultiJson.load(result_json) + response = {:client => client_name, :check => check_result} + body MultiJson.dump(response) else not_found! end diff --git a/lib/sensu/server/process.rb b/lib/sensu/server/process.rb index c12545f8..3c643b6e 100644 --- a/lib/sensu/server/process.rb +++ b/lib/sensu/server/process.rb @@ -217,7 +217,6 @@ def store_check_result(client, check, &callback) @redis.sadd("result:#{client[:name]}", check[:name]) result_key = "#{client[:name]}:#{check[:name]}" check_truncated = check.merge(:output => check[:output][0..256]) - check_truncated[:client] = client[:name] @redis.set("result:#{result_key}", MultiJson.dump(check_truncated)) do history_key = "history:#{result_key}" @redis.rpush(history_key, check[:status]) do diff --git a/spec/api/process_spec.rb b/spec/api/process_spec.rb index 3d41ee01..60090a53 100644 --- a/spec/api/process_spec.rb +++ b/spec/api/process_spec.rb @@ -840,7 +840,7 @@ expect(http.response_header.status).to eq(200) expect(body).to be_kind_of(Array) test_result = Proc.new do |result| - @check + result_template(@check) end expect(body).to contain(test_result) async_done @@ -854,7 +854,7 @@ expect(http.response_header.status).to eq(200) expect(body).to be_kind_of(Array) test_result = Proc.new do |result| - @check + result_template(@check) end expect(body).to contain(test_result) async_done @@ -867,7 +867,7 @@ api_request("/results/i-424242/test") do |http, body| expect(http.response_header.status).to eq(200) expect(body).to be_kind_of(Hash) - expect(body).to eq(@check) + expect(body).to eq(result_template(@check)) async_done end end diff --git a/spec/helpers.rb b/spec/helpers.rb index f43466c1..be6d5570 100644 --- a/spec/helpers.rb +++ b/spec/helpers.rb @@ -112,10 +112,10 @@ def check_template } end - def result_template + def result_template(check_result = nil) { :client => "i-424242", - :check => check_template + :check => check_result || check_template } end