diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-08 18:09:29 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-08 18:09:29 +0300 |
commit | 5372e109c0660e4670aa987568a51082beca1b3c (patch) | |
tree | 76f8f1178d5f304f0aea8c0c610729f695c9e18e /spec | |
parent | 403678e00406edc8094f087ec70e00aa29e49bef (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
5 files changed, 215 insertions, 0 deletions
diff --git a/spec/models/concerns/prometheus_adapter_spec.rb b/spec/models/concerns/prometheus_adapter_spec.rb index bc58df7e7c2..fdc98ba74b8 100644 --- a/spec/models/concerns/prometheus_adapter_spec.rb +++ b/spec/models/concerns/prometheus_adapter_spec.rb @@ -18,6 +18,29 @@ describe PrometheusAdapter, :use_clean_rails_memory_store_caching do let(:environment_query) { Gitlab::Prometheus::Queries::EnvironmentQuery } describe '#query' do + describe 'validate_query' do + let(:environment) { build_stubbed(:environment, slug: 'env-slug') } + let(:validation_query) { Gitlab::Prometheus::Queries::ValidateQuery.name } + let(:query) { 'avg(response)' } + let(:validation_respone) { { data: { valid: true } } } + + around do |example| + Timecop.freeze { example.run } + end + + context 'with valid data' do + subject { service.query(:validate, query) } + + before do + stub_reactive_cache(service, validation_respone, validation_query, query) + end + + it 'returns query data' do + is_expected.to eq(query: { valid: true }) + end + end + end + describe 'environment' do let(:environment) { build_stubbed(:environment, slug: 'env-slug') } diff --git a/spec/models/project_services/chat_notification_service_spec.rb b/spec/models/project_services/chat_notification_service_spec.rb index 64c7a9b230d..1caec5c6eb7 100644 --- a/spec/models/project_services/chat_notification_service_spec.rb +++ b/spec/models/project_services/chat_notification_service_spec.rb @@ -75,6 +75,30 @@ describe ChatNotificationService do end end + context 'with "channel" property' do + before do + allow(chat_service).to receive(:channel).and_return(channel) + end + + context 'empty string' do + let(:channel) { '' } + + it 'does not include the channel' do + expect(chat_service).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true) + expect(chat_service.execute(data)).to be(true) + end + end + + context 'empty spaces' do + let(:channel) { ' ' } + + it 'does not include the channel' do + expect(chat_service).to receive(:notify).with(any_args, hash_excluding(:channel)).and_return(true) + expect(chat_service.execute(data)).to be(true) + end + end + end + shared_examples 'with channel specified' do |channel, expected_channels| before do allow(chat_service).to receive(:push_channel).and_return(channel) diff --git a/spec/models/project_services/prometheus_service_spec.rb b/spec/models/project_services/prometheus_service_spec.rb index f663f0ab7cb..415d634d405 100644 --- a/spec/models/project_services/prometheus_service_spec.rb +++ b/spec/models/project_services/prometheus_service_spec.rb @@ -123,6 +123,34 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do end end + describe 'callbacks' do + context 'after_create' do + let(:project) { create(:project) } + let(:service) { build(:prometheus_service, project: project) } + + subject(:create_service) { service.save! } + + it 'creates default alerts' do + expect(Prometheus::CreateDefaultAlertsWorker) + .to receive(:perform_async) + .with(project_id: project.id) + + create_service + end + + context 'no project exists' do + let(:service) { build(:prometheus_service, :instance) } + + it 'does not create default alerts' do + expect(Prometheus::CreateDefaultAlertsWorker) + .not_to receive(:perform_async) + + create_service + end + end + end + end + describe '#test' do before do service.manual_configuration = true diff --git a/spec/services/prometheus/create_default_alerts_service_spec.rb b/spec/services/prometheus/create_default_alerts_service_spec.rb new file mode 100644 index 00000000000..3382844c99a --- /dev/null +++ b/spec/services/prometheus/create_default_alerts_service_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Prometheus::CreateDefaultAlertsService do + let_it_be(:project) { create(:project) } + let(:instance) { described_class.new(project: project) } + let(:expected_alerts) { described_class::DEFAULT_ALERTS } + + describe '#execute' do + subject(:execute) { instance.execute } + + shared_examples 'no alerts created' do + it 'does not create alerts' do + expect { execute }.not_to change { project.reload.prometheus_alerts.count } + end + end + + context 'no environment' do + it_behaves_like 'no alerts created' + end + + context 'environment exists' do + let_it_be(:environment) { create(:environment, project: project) } + + context 'no found metric' do + it_behaves_like 'no alerts created' + end + + context 'metric exists' do + before do + create_expected_metrics! + end + + context 'alert exists already' do + before do + create_pre_existing_alerts!(environment) + end + + it_behaves_like 'no alerts created' + end + + it 'creates alerts' do + expect { execute }.to change { project.reload.prometheus_alerts.count } + .by(expected_alerts.size) + end + + context 'multiple environments' do + let!(:production) { create(:environment, project: project, name: 'production') } + + it 'uses the production environment' do + expect { execute }.to change { production.reload.prometheus_alerts.count } + .by(expected_alerts.size) + end + end + end + end + end + + private + + def create_expected_metrics! + expected_alerts.each do |alert_hash| + create(:prometheus_metric, :common, identifier: alert_hash.fetch(:identifier)) + end + end + + def create_pre_existing_alerts!(environment) + expected_alerts.each do |alert_hash| + metric = PrometheusMetric.for_identifier(alert_hash[:identifier]).first! + create(:prometheus_alert, prometheus_metric: metric, project: project, environment: environment) + end + end +end 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 |