2022-11-25 23:54:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'fast_spec_helper'
|
|
|
|
require_dependency 'gitlab/cluster/lifecycle_events'
|
|
|
|
|
|
|
|
RSpec.describe Gitlab::Memory::Watchdog::Configuration do
|
|
|
|
subject(:configuration) { described_class.new }
|
|
|
|
|
|
|
|
describe '#initialize' do
|
|
|
|
it 'initialize monitors' do
|
|
|
|
expect(configuration.monitors).to be_an_instance_of(described_class::MonitorStack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#handler' do
|
|
|
|
context 'when handler is not set' do
|
|
|
|
it 'defaults to NullHandler' do
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(configuration.handler).to be(Gitlab::Memory::Watchdog::Handlers::NullHandler.instance)
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
describe '#event_reporter' do
|
|
|
|
context 'when event reporter is not set' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Metrics).to receive(:counter)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to EventReporter' do
|
|
|
|
expect(configuration.event_reporter).to be_an_instance_of(::Gitlab::Memory::Watchdog::EventReporter)
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sleep_time_seconds' do
|
|
|
|
context 'when sleep_time_seconds is not set' do
|
|
|
|
it 'defaults to SLEEP_TIME_SECONDS' do
|
|
|
|
expect(configuration.sleep_time_seconds).to eq(described_class::DEFAULT_SLEEP_TIME_SECONDS)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#monitors' do
|
|
|
|
context 'when monitors are configured to be used' do
|
2023-03-04 22:38:38 +05:30
|
|
|
let(:monitor_name1) { :monitor1 }
|
|
|
|
let(:monitor_name2) { :monitor2 }
|
2022-11-25 23:54:43 +05:30
|
|
|
let(:payload1) do
|
|
|
|
{
|
|
|
|
message: 'monitor_1_text',
|
|
|
|
memwd_max_strikes: 5,
|
|
|
|
memwd_cur_strikes: 0
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:payload2) do
|
|
|
|
{
|
|
|
|
message: 'monitor_2_text',
|
|
|
|
memwd_max_strikes: 0,
|
|
|
|
memwd_cur_strikes: 1
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:monitor_class_1) do
|
|
|
|
Struct.new(:threshold_violated, :payload) do
|
|
|
|
def call
|
|
|
|
{ threshold_violated: !!threshold_violated, payload: payload || {} }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.name
|
|
|
|
'Monitor1'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:monitor_class_2) do
|
|
|
|
Struct.new(:threshold_violated, :payload) do
|
|
|
|
def call
|
|
|
|
{ threshold_violated: !!threshold_violated, payload: payload || {} }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.name
|
|
|
|
'Monitor2'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
context 'when two different monitor class are configured' do
|
|
|
|
shared_examples 'executes monitors and returns correct results' do
|
|
|
|
it 'calls each monitor and returns correct results', :aggregate_failures do
|
|
|
|
payloads = []
|
|
|
|
thresholds = []
|
|
|
|
strikes = []
|
|
|
|
monitor_names = []
|
|
|
|
|
|
|
|
configuration.monitors.call_each do |result|
|
|
|
|
payloads << result.payload
|
|
|
|
thresholds << result.threshold_violated?
|
|
|
|
strikes << result.strikes_exceeded?
|
|
|
|
monitor_names << result.monitor_name
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(payloads).to eq([payload1, payload2])
|
|
|
|
expect(thresholds).to eq([false, true])
|
|
|
|
expect(strikes).to eq([false, true])
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(monitor_names).to eq([monitor_name1, monitor_name2])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'monitors are not empty' do
|
|
|
|
expect(configuration.monitors).not_to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when monitors are not configured' do
|
|
|
|
it 'monitors are empty' do
|
|
|
|
expect(configuration.monitors).to be_empty
|
2023-01-13 00:05:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when monitors are configured inline' do
|
|
|
|
before do
|
|
|
|
configuration.monitors.push monitor_class_1, false, { message: 'monitor_1_text' }, max_strikes: 5
|
|
|
|
configuration.monitors.push monitor_class_2, true, { message: 'monitor_2_text' }, max_strikes: 0
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'executes monitors and returns correct results'
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
context 'when monitors are configured in a block' do
|
|
|
|
before do
|
|
|
|
configuration.monitors do |stack|
|
|
|
|
stack.push monitor_class_1, false, { message: 'monitor_1_text' }, max_strikes: 5
|
|
|
|
stack.push monitor_class_2, true, { message: 'monitor_2_text' }, max_strikes: 0
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
include_examples 'executes monitors and returns correct results'
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'when monitors are configured with monitor name' do
|
|
|
|
let(:monitor_name1) { :mon_one }
|
|
|
|
let(:monitor_name2) { :mon_two }
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
before do
|
|
|
|
configuration.monitors do |stack|
|
|
|
|
stack.push monitor_class_1, false, { message: 'monitor_1_text' }, max_strikes: 5, monitor_name: :mon_one
|
|
|
|
stack.push monitor_class_2, true, { message: 'monitor_2_text' }, max_strikes: 0, monitor_name: :mon_two
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'executes monitors and returns correct results'
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|