spec/lib/flapjack/data/event_spec.rb in flapjack-0.9.5 vs spec/lib/flapjack/data/event_spec.rb in flapjack-0.9.6
- old
+ new
@@ -20,15 +20,22 @@
'acknowledgement_id' => '1234',
'duration' => (60 * 60),
'tags' => ['dev'] }
}
+ before(:each) do
+ Flapjack::Data::Event.instance_variable_set('@previous_base_time_str', nil)
+ end
+
context 'class' do
it "returns the next event (blocking, archiving)" do
expect(mock_redis).to receive(:brpoplpush).
with('events', /^events_archive:/, 0).and_return(event_data.to_json)
+ expect(Flapjack::Data::Migration).to receive(:purge_expired_archive_index).with(:redis => mock_redis)
+ expect(mock_redis).to receive(:sadd).
+ with('known_events_archive_keys', /^events_archive:/)
expect(mock_redis).to receive(:expire)
result = Flapjack::Data::Event.next('events', :block => true,
:archive_events => true, :redis => mock_redis)
expect(result).to be_an_instance_of(Flapjack::Data::Event)
@@ -44,10 +51,13 @@
end
it "returns the next event (non-blocking, archiving)" do
expect(mock_redis).to receive(:rpoplpush).
with('events', /^events_archive:/).and_return(event_data.to_json)
+ expect(Flapjack::Data::Migration).to receive(:purge_expired_archive_index).with(:redis => mock_redis)
+ expect(mock_redis).to receive(:sadd).
+ with('known_events_archive_keys', /^events_archive:/)
expect(mock_redis).to receive(:expire)
result = Flapjack::Data::Event.next('events', :block => false,
:archive_events => true, :redis => mock_redis)
expect(result).to be_an_instance_of(Flapjack::Data::Event)
@@ -64,10 +74,13 @@
it "rejects invalid event JSON (archiving)" do
bad_event_json = '{{{'
expect(mock_redis).to receive(:brpoplpush).
with('events', /^events_archive:/, 0).and_return(bad_event_json)
+ expect(Flapjack::Data::Migration).to receive(:purge_expired_archive_index).with(:redis => mock_redis)
+ expect(mock_redis).to receive(:sadd).
+ with('known_events_archive_keys', /^events_archive:/)
expect(mock_redis).to receive(:multi)
expect(mock_redis).to receive(:lrem).with(/^events_archive:/, 1, bad_event_json)
expect(mock_redis).to receive(:lpush).with(/^events_rejected:/, bad_event_json)
expect(mock_redis).to receive(:exec)
expect(mock_redis).to receive(:expire)
@@ -92,10 +105,13 @@
it "rejects an event with missing '#{required_key}' key (archiving)" do
bad_event_data = event_data.clone
bad_event_data.delete(required_key)
bad_event_json = bad_event_data.to_json
+ expect(Flapjack::Data::Migration).to receive(:purge_expired_archive_index).with(:redis => mock_redis)
+ expect(mock_redis).to receive(:sadd).
+ with('known_events_archive_keys', /^events_archive:/)
expect(mock_redis).to receive(:brpoplpush).
with('events', /^events_archive:/, 0).and_return(bad_event_json)
expect(mock_redis).to receive(:multi)
expect(mock_redis).to receive(:lrem).with(/^events_archive:/, 1, bad_event_json)
expect(mock_redis).to receive(:lpush).with(/^events_rejected:/, bad_event_json)
@@ -124,10 +140,13 @@
bad_event_data = event_data.clone
bad_event_data[required_key] = {'hello' => 'there'}
bad_event_json = bad_event_data.to_json
expect(mock_redis).to receive(:brpoplpush).
with('events', /^events_archive:/, 0).and_return(bad_event_json)
+ expect(Flapjack::Data::Migration).to receive(:purge_expired_archive_index).with(:redis => mock_redis)
+ expect(mock_redis).to receive(:sadd).
+ with('known_events_archive_keys', /^events_archive:/)
expect(mock_redis).to receive(:multi)
expect(mock_redis).to receive(:lrem).with(/^events_archive:/, 1, bad_event_json)
expect(mock_redis).to receive(:lpush).with(/^events_rejected:/, bad_event_json)
expect(mock_redis).to receive(:exec)
expect(mock_redis).to receive(:expire)
@@ -154,10 +173,13 @@
['time', 'details', 'perfdata', 'acknowledgement_id', 'duration', 'tags'].each do |optional_key|
it "rejects an event with invalid '#{optional_key}' key (archiving)" do
bad_event_data = event_data.clone
bad_event_data[optional_key] = {'hello' => 'there'}
bad_event_json = bad_event_data.to_json
+ expect(Flapjack::Data::Migration).to receive(:purge_expired_archive_index).with(:redis => mock_redis)
+ expect(mock_redis).to receive(:sadd).
+ with('known_events_archive_keys', /^events_archive:/)
expect(mock_redis).to receive(:brpoplpush).
with('events', /^events_archive:/, 0).and_return(bad_event_json)
expect(mock_redis).to receive(:multi)
expect(mock_redis).to receive(:lrem).with(/^events_archive:/, 1, bad_event_json)
expect(mock_redis).to receive(:lpush).with(/^events_rejected:/, bad_event_json)
@@ -186,10 +208,13 @@
['type', 'state'].each do |key|
it "it matches case-insensitively for #{key} (archiving)" do
case_event_data = event_data.clone
case_event_data[key] = event_data[key].upcase
+ expect(Flapjack::Data::Migration).to receive(:purge_expired_archive_index).with(:redis => mock_redis)
+ expect(mock_redis).to receive(:sadd).
+ with('known_events_archive_keys', /^events_archive:/)
expect(mock_redis).to receive(:brpoplpush).
with('events', /^events_archive:/, 0).and_return(case_event_data.to_json)
expect(mock_redis).to receive(:expire)
result = Flapjack::Data::Event.next('events', :block => true,
@@ -212,10 +237,13 @@
['time', 'duration'].each do |key|
it "it accepts an event with a numeric #{key} key (archiving)" do
num_event_data = event_data.clone
num_event_data[key] = event_data[key].to_i.to_s
+ expect(Flapjack::Data::Migration).to receive(:purge_expired_archive_index).with(:redis => mock_redis)
+ expect(mock_redis).to receive(:sadd).
+ with('known_events_archive_keys', /^events_archive:/)
expect(mock_redis).to receive(:brpoplpush).
with('events', /^events_archive:/, 0).and_return(num_event_data.to_json)
expect(mock_redis).to receive(:expire)
result = Flapjack::Data::Event.next('events', :block => true,
@@ -238,9 +266,12 @@
bad_event_data = event_data.clone
bad_event_data[key] = 'NaN'
bad_event_json = bad_event_data.to_json
expect(mock_redis).to receive(:brpoplpush).
with('events', /^events_archive:/, 0).and_return(bad_event_json)
+ expect(Flapjack::Data::Migration).to receive(:purge_expired_archive_index).with(:redis => mock_redis)
+ expect(mock_redis).to receive(:sadd).
+ with('known_events_archive_keys', /^events_archive:/)
expect(mock_redis).to receive(:multi)
expect(mock_redis).to receive(:lrem).with(/^events_archive:/, 1, bad_event_json)
expect(mock_redis).to receive(:lpush).with(/^events_rejected:/, bad_event_json)
expect(mock_redis).to receive(:exec)
expect(mock_redis).to receive(:expire)