diff options
Diffstat (limited to 'spec/lib/gitlab/memory/reporter_spec.rb')
-rw-r--r-- | spec/lib/gitlab/memory/reporter_spec.rb | 85 |
1 files changed, 36 insertions, 49 deletions
diff --git a/spec/lib/gitlab/memory/reporter_spec.rb b/spec/lib/gitlab/memory/reporter_spec.rb index 924397ceb4f..64ae740a5d7 100644 --- a/spec/lib/gitlab/memory/reporter_spec.rb +++ b/spec/lib/gitlab/memory/reporter_spec.rb @@ -26,15 +26,15 @@ RSpec.describe Gitlab::Memory::Reporter, :aggregate_failures, feature_category: FileUtils.rm_rf(reports_path) end - describe '#run_report', time_travel_to: '2020-02-02 10:30:45 0000' do + describe '#run_report', time_travel_to: '2020-02-02 10:30:45 +0000' do let(:report_duration_counter) { instance_double(::Prometheus::Client::Counter) } let(:file_size) { 1_000_000 } let(:report_file) { "#{reports_path}/fake_report.2020-02-02.10:30:45:000.worker_1.abc123.gz" } - - let(:input) { StringIO.new } - let(:output) { StringIO.new } + let(:output) { File.read(report_file) } before do + stub_const('Gitlab::Memory::Reporter::COMPRESS_CMD', %w[cat]) + allow(SecureRandom).to receive(:uuid).and_return('abc123') allow(Gitlab::Metrics).to receive(:counter).and_return(report_duration_counter) @@ -44,22 +44,13 @@ RSpec.describe Gitlab::Memory::Reporter, :aggregate_failures, feature_category: allow(File).to receive(:size).with(report_file).and_return(file_size) allow(logger).to receive(:info) - - stub_gzip end shared_examples 'runs and stores reports' do it 'runs the given report and returns true' do expect(reporter.run_report(report)).to be(true) - expect(output.string).to eq('I ran') - end - - it 'closes read and write streams' do - expect(input).to receive(:close).ordered.at_least(:once) - expect(output).to receive(:close).ordered.at_least(:once) - - reporter.run_report(report) + expect(output).to eq('I ran') end it 'logs start and finish event' do @@ -111,39 +102,47 @@ RSpec.describe Gitlab::Memory::Reporter, :aggregate_failures, feature_category: end context 'when an error occurs' do - before do - allow(report).to receive(:run).and_raise(RuntimeError.new('report failed')) - end + shared_examples 'handles errors gracefully' do + it 'logs the error and returns false' do + expect(logger).to receive(:info).ordered.with(hash_including(message: 'started')) + expect(logger).to receive(:error).ordered.with( + hash_including( + message: 'failed', error: match(error_message) + )) + + expect(reporter.run_report(report)).to be(false) + end + + context 'when compression process is still running' do + it 'terminates the process' do + allow(logger).to receive(:info) + allow(logger).to receive(:error) - it 'logs the error and returns false' do - expect(logger).to receive(:info).ordered.with(hash_including(message: 'started')) - expect(logger).to receive(:error).ordered.with( - hash_including( - message: 'failed', error: '#<RuntimeError: report failed>' - )) + expect(Gitlab::ProcessManagement).to receive(:signal).with(an_instance_of(Integer), :KILL) - expect(reporter.run_report(report)).to be(false) + reporter.run_report(report) + end + end end - it 'closes read and write streams' do - allow(logger).to receive(:info) - allow(logger).to receive(:error) + context 'when cause was an error being raised' do + let(:error_message) { 'report failed' } - expect(input).to receive(:close).ordered.at_least(:once) - expect(output).to receive(:close).ordered.at_least(:once) + before do + allow(report).to receive(:run).and_raise(RuntimeError.new('report failed')) + end - reporter.run_report(report) + it_behaves_like 'handles errors gracefully' end - context 'when compression process is still running' do - it 'terminates the process' do - allow(logger).to receive(:info) - allow(logger).to receive(:error) + context 'when cause was compression command failing' do + let(:error_message) { "StandardError: exit 1: cat:" } - expect(Gitlab::ProcessManagement).to receive(:signal).with(an_instance_of(Integer), :KILL) - - reporter.run_report(report) + before do + stub_const('Gitlab::Memory::Reporter::COMPRESS_CMD', %w[cat --bad-flag]) end + + it_behaves_like 'handles errors gracefully' end end @@ -191,16 +190,4 @@ RSpec.describe Gitlab::Memory::Reporter, :aggregate_failures, feature_category: it_behaves_like 'runs and stores reports' end end - - # We need to stub out the call into gzip. We do this by intercepting the write - # end of the pipe and replacing it with a StringIO instead, which we can - # easily inspect for contents. - def stub_gzip - pid = 42 - allow(IO).to receive(:pipe).and_return([input, output]) - allow(Process).to receive(:spawn).with( - "gzip", "--fast", in: input, out: an_instance_of(File), err: an_instance_of(IO) - ).and_return(pid) - allow(Process).to receive(:waitpid).with(pid) - end end |