From 5372e109c0660e4670aa987568a51082beca1b3c Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 8 Apr 2020 15:09:29 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- .../create_default_alerts_worker_spec.rb | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 spec/workers/prometheus/create_default_alerts_worker_spec.rb (limited to 'spec/workers') diff --git a/spec/workers/prometheus/create_default_alerts_worker_spec.rb b/spec/workers/prometheus/create_default_alerts_worker_spec.rb new file mode 100644 index 00000000000..1b1867d5bb6 --- /dev/null +++ b/spec/workers/prometheus/create_default_alerts_worker_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Prometheus::CreateDefaultAlertsWorker do + let_it_be(:project) { create(:project) } + let(:worker) { described_class.new } + let(:logger) { worker.send(:logger) } + let(:service) { instance_double(Prometheus::CreateDefaultAlertsService) } + let(:service_result) { ServiceResponse.success } + + subject { described_class.new.perform(project.id) } + + before do + allow(Prometheus::CreateDefaultAlertsService) + .to receive(:new).with(project: project) + .and_return(service) + allow(service).to receive(:execute) + .and_return(service_result) + end + + it_behaves_like 'an idempotent worker' do + let(:job_args) { [project.id] } + + it 'calls the service' do + expect(service).to receive(:execute) + + subject + end + + context 'project is nil' do + let(:job_args) { [nil] } + + it 'does not call the service' do + expect(service).not_to receive(:execute) + + subject + end + end + + context 'when service returns an error' do + let(:error_message) { 'some message' } + let(:service_result) { ServiceResponse.error(message: error_message) } + + it 'succeeds and logs the error' do + expect(logger) + .to receive(:info) + .with(a_hash_including('message' => error_message)) + .exactly(worker_exec_times).times + + subject + end + end + end + + context 'when service raises an exception' do + let(:error_message) { 'some exception' } + let(:exception) { StandardError.new(error_message) } + + it 're-raises exception' do + allow(service).to receive(:execute).and_raise(exception) + + expect { subject }.to raise_error(exception) + end + end +end -- cgit v1.2.1