diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-31 15:07:02 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-31 15:07:02 +0300 |
commit | 74ecf758e30be848144df1672b5080a29fafbc0a (patch) | |
tree | 0bc719293526a2384b9db6638d42d49f1f5c9c9c /spec/services | |
parent | 26d2324ac136c7a28235789ff9a0b2974b5f7c7b (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services')
5 files changed, 9 insertions, 446 deletions
diff --git a/spec/services/alert_management/process_prometheus_alert_service_spec.rb b/spec/services/alert_management/process_prometheus_alert_service_spec.rb index eb5f3808021..9fe77bf2b17 100644 --- a/spec/services/alert_management/process_prometheus_alert_service_spec.rb +++ b/spec/services/alert_management/process_prometheus_alert_service_spec.rb @@ -66,22 +66,6 @@ RSpec.describe AlertManagement::ProcessPrometheusAlertService, feature_category: expect(alert.environment).to eq(environment) end end - - context 'prometheus alert given' do - let(:prometheus_alert) { create(:prometheus_alert, project: project) } - let(:alert) { project.alert_management_alerts.last } - - before do - payload['labels']['gitlab_alert_id'] = prometheus_alert.prometheus_metric_id - end - - it 'sets the prometheus alert and environment' do - execute - - expect(alert.prometheus_alert).to eq(prometheus_alert) - expect(alert.environment).to eq(prometheus_alert.environment) - end - end end context 'when alert payload is invalid' do diff --git a/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb b/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb deleted file mode 100644 index 0818bdd8b9c..00000000000 --- a/spec/services/metrics/dashboard/clone_dashboard_service_spec.rb +++ /dev/null @@ -1,190 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Metrics::Dashboard::CloneDashboardService, :use_clean_rails_memory_store_caching, feature_category: :metrics do - include MetricsDashboardHelpers - - let_it_be(:user) { create(:user) } - let_it_be(:project) { create(:project, :repository) } - let_it_be(:environment) { create(:environment, project: project) } - - describe '#execute' do - subject(:service_call) { described_class.new(project, user, params).execute } - - let(:commit_message) { 'test' } - let(:branch) { "dashboard_new_branch" } - let(:dashboard) { 'config/prometheus/common_metrics.yml' } - let(:file_name) { 'custom_dashboard.yml' } - let(:file_content_hash) { YAML.safe_load(File.read(dashboard)) } - let(:params) do - { - dashboard: dashboard, - file_name: file_name, - commit_message: commit_message, - branch: branch - } - end - - context 'user does not have push right to repository' do - it_behaves_like 'misconfigured dashboard service response with stepable', :forbidden, 'You are not allowed to push into this branch. Create another branch or open a merge request.' - end - - context 'with rights to push to the repository' do - before do - project.add_maintainer(user) - end - - context 'wrong target file extension' do - let(:file_name) { 'custom_dashboard.txt' } - - it_behaves_like 'misconfigured dashboard service response with stepable', :bad_request, 'The file name should have a .yml extension' - end - - context 'wrong source dashboard file' do - let(:dashboard) { 'config/prometheus/common_metrics_123.yml' } - - it_behaves_like 'misconfigured dashboard service response with stepable', :not_found, 'Not found.' - end - - context 'path traversal attack attempt' do - let(:dashboard) { 'config/prometheus/../database.yml' } - - it_behaves_like 'misconfigured dashboard service response with stepable', :not_found, 'Not found.' - end - - context 'path traversal attack attempt on target file' do - let(:file_name) { '../../custom_dashboard.yml' } - let(:dashboard_attrs) do - { - commit_message: commit_message, - branch_name: branch, - start_branch: project.default_branch, - encoding: 'text', - file_path: ".gitlab/dashboards/custom_dashboard.yml", - file_content: file_content_hash.to_yaml - } - end - - it 'strips target file name to safe value', :aggregate_failures do - allow(::Gitlab::Metrics::Dashboard::Processor).to receive(:new).and_return(double(process: file_content_hash)) - service_instance = instance_double(::Files::CreateService) - expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(service_instance) - expect(service_instance).to receive(:execute).and_return(status: :success) - - service_call - end - end - - context 'valid parameters' do - before do - allow(::Gitlab::Metrics::Dashboard::Processor).to receive(:new).and_return(double(process: file_content_hash)) - end - - it_behaves_like 'valid dashboard cloning process', ::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH, - [ - ::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter, - ::Gitlab::Metrics::Dashboard::Stages::CustomMetricsInserter - ] - - context 'selected branch already exists' do - let(:branch) { 'existing_branch' } - - before do - project.repository.add_branch(user, branch, 'master') - end - - it_behaves_like 'misconfigured dashboard service response with stepable', :bad_request, 'There was an error creating the dashboard, branch named: existing_branch already exists.' - - # temporary not available function for first iteration - # follow up issue https://gitlab.com/gitlab-org/gitlab/issues/196237 which - # require this feature - # it 'pass correct params to Files::CreateService', :aggregate_failures do - # project.repository.add_branch(user, branch, 'master') - # - # service_instance = instance_double(::Files::CreateService) - # expect(::Files::CreateService).to receive(:new).with(project, user, dashboard_attrs).and_return(service_instance) - # expect(service_instance).to receive(:execute).and_return(status: :success) - # - # service_call - # end - end - - context 'blank branch name' do - let(:branch) { '' } - - it_behaves_like 'misconfigured dashboard service response with stepable', :bad_request, 'There was an error creating the dashboard, branch name is invalid.' - end - - context 'dashboard file already exists' do - let(:branch) { 'custom_dashboard' } - - before do - Files::CreateService.new( - project, - user, - commit_message: 'Create custom dashboard custom_dashboard.yml', - branch_name: 'master', - start_branch: 'master', - file_path: ".gitlab/dashboards/custom_dashboard.yml", - file_content: File.read('config/prometheus/common_metrics.yml') - ).execute - end - - it_behaves_like 'misconfigured dashboard service response with stepable', :bad_request, "A file with 'custom_dashboard.yml' already exists in custom_dashboard branch" - end - - it 'extends dashboard template path to absolute url' do - allow(::Files::CreateService).to receive(:new).and_return(double(execute: { status: :success })) - - expect_file_read(Rails.root.join('config/prometheus/common_metrics.yml'), content: '') - - service_call - end - - context 'Files::CreateService success' do - before do - allow(::Files::CreateService).to receive(:new).and_return(double(execute: { status: :success })) - end - - it 'clears dashboards cache' do - expect(project.repository).to receive(:refresh_method_caches).with([:metrics_dashboard]) - - service_call - end - - it 'returns success', :aggregate_failures do - result = service_call - dashboard_details = { - path: '.gitlab/dashboards/custom_dashboard.yml', - display_name: 'custom_dashboard.yml', - default: false, - system_dashboard: false - } - - expect(result[:status]).to be :success - expect(result[:http_status]).to be :created - expect(result[:dashboard]).to match dashboard_details - end - end - - context 'Files::CreateService fails' do - before do - allow(::Files::CreateService).to receive(:new).and_return(double(execute: { status: :error })) - end - - it 'does NOT clear dashboards cache' do - expect(project.repository).not_to receive(:refresh_method_caches) - - service_call - end - - it 'returns error' do - result = service_call - expect(result[:status]).to be :error - end - end - end - end - end -end diff --git a/spec/services/metrics/dashboard/dynamic_embed_service_spec.rb b/spec/services/metrics/dashboard/dynamic_embed_service_spec.rb deleted file mode 100644 index 1643f552a70..00000000000 --- a/spec/services/metrics/dashboard/dynamic_embed_service_spec.rb +++ /dev/null @@ -1,158 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Metrics::Dashboard::DynamicEmbedService, :use_clean_rails_memory_store_caching, - feature_category: :metrics do - include MetricsDashboardHelpers - - let_it_be(:project) { build(:project) } - let_it_be(:user) { create(:user) } - let_it_be(:environment) { create(:environment, project: project) } - - before do - project.add_maintainer(user) if user - end - - let(:dashboard_path) { '.gitlab/dashboards/test.yml' } - let(:group) { 'Group A' } - let(:title) { 'Super Chart A1' } - let(:y_label) { 'y_label' } - - describe '.valid_params?' do - let(:valid_params) do - { - embedded: true, - dashboard_path: dashboard_path, - group: group, - title: title, - y_label: y_label - } - end - - subject { described_class.valid_params?(params) } - - let(:params) { valid_params } - - it { is_expected.to be_truthy } - - context 'missing embedded' do - let(:params) { valid_params.except(:embedded) } - - it { is_expected.to be_falsey } - end - - context 'not embedded' do - let(:params) { valid_params.merge(embedded: 'false') } - - it { is_expected.to be_falsey } - end - - context 'undefined dashboard' do - let(:params) { valid_params.except(:dashboard_path) } - - it { is_expected.to be_truthy } - end - - context 'missing dashboard' do - let(:dashboard) { '' } - - it { is_expected.to be_truthy } - end - - context 'missing group' do - let(:group) { '' } - - it { is_expected.to be_falsey } - end - - context 'missing title' do - let(:title) { '' } - - it { is_expected.to be_falsey } - end - - context 'undefined y-axis label' do - let(:params) { valid_params.except(:y_label) } - - it { is_expected.to be_falsey } - end - end - - describe '#get_dashboard' do - let(:service_params) do - [ - project, - user, - { - environment: environment, - dashboard_path: dashboard_path, - group: group, - title: title, - y_label: y_label - } - ] - end - - let(:service_call) { described_class.new(*service_params).get_dashboard } - - context 'when the dashboard does not exist' do - it_behaves_like 'misconfigured dashboard service response', :not_found - end - - context 'when the dashboard is exists' do - let(:project) { project_with_dashboard(dashboard_path) } - - it_behaves_like 'valid embedded dashboard service response' - it_behaves_like 'raises error for users with insufficient permissions' - - it 'caches the unprocessed dashboard for subsequent calls' do - expect(YAML).to receive(:safe_load).once.and_call_original - - described_class.new(*service_params).get_dashboard - described_class.new(*service_params).get_dashboard - end - - context 'when the specified group is not present on the dashboard' do - let(:group) { 'Group Not Found' } - - it_behaves_like 'misconfigured dashboard service response', :not_found - end - - context 'when the specified title is not present on the dashboard' do - let(:title) { 'Title Not Found' } - - it_behaves_like 'misconfigured dashboard service response', :not_found - end - - context 'when the specified y-axis label is not present on the dashboard' do - let(:y_label) { 'Y-Axis Not Found' } - - it_behaves_like 'misconfigured dashboard service response', :not_found - end - end - - shared_examples 'uses system dashboard' do - it 'uses the overview dashboard' do - expect(Gitlab::Metrics::Dashboard::Finder) - .to receive(:find_raw) - .with(project, dashboard_path: system_dashboard_path) - .once - - service_call - end - end - - context 'when the dashboard is nil' do - let(:dashboard_path) { nil } - - it_behaves_like 'uses system dashboard' - end - - context 'when the dashboard is not present' do - let(:dashboard_path) { '' } - - it_behaves_like 'uses system dashboard' - end - end -end diff --git a/spec/services/projects/prometheus/alerts/notify_service_spec.rb b/spec/services/projects/prometheus/alerts/notify_service_spec.rb index cc1f83ddc2b..73932887cd9 100644 --- a/spec/services/projects/prometheus/alerts/notify_service_spec.rb +++ b/spec/services/projects/prometheus/alerts/notify_service_spec.rb @@ -17,91 +17,12 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService, feature_category: :i subject { service.execute(token_input) } context 'with valid payload' do - let_it_be(:alert_firing) { create(:prometheus_alert, project: project) } - let_it_be(:alert_resolved) { create(:prometheus_alert, project: project) } - let_it_be(:cluster, reload: true) { create(:cluster, :provided_by_user, projects: [project]) } - - let(:payload_raw) { prometheus_alert_payload(firing: [alert_firing], resolved: [alert_resolved]) } + let(:payload_raw) { prometheus_alert_payload(firing: ['Alert A'], resolved: ['Alert B']) } let(:payload) { ActionController::Parameters.new(payload_raw).permit! } let(:payload_alert_firing) { payload_raw['alerts'].first } let(:token) { 'token' } let(:source) { 'Prometheus' } - context 'with environment specific clusters' do - let(:prd_cluster) do - cluster - end - - let(:stg_cluster) do - create(:cluster, :provided_by_user, projects: [project], enabled: true, environment_scope: 'stg/*') - end - - let(:stg_environment) do - create(:environment, project: project, name: 'stg/1') - end - - let(:alert_firing) do - create(:prometheus_alert, project: project, environment: stg_environment) - end - - before do - create(:clusters_integrations_prometheus, cluster: prd_cluster, alert_manager_token: token) - create(:clusters_integrations_prometheus, cluster: stg_cluster, alert_manager_token: nil) - end - - context 'without token' do - let(:token_input) { nil } - - include_examples 'processes one firing and one resolved prometheus alerts' - end - - context 'with token' do - it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized - end - end - - context 'with project specific cluster using prometheus integration' do - where(:cluster_enabled, :integration_enabled, :configured_token, :token_input, :result) do - true | true | token | token | :success - true | true | nil | nil | :success - true | true | token | 'x' | :failure - true | true | token | nil | :failure - true | false | token | token | :failure - false | true | token | token | :failure - false | nil | nil | token | :failure - end - - with_them do - before do - cluster.update!(enabled: cluster_enabled) - - unless integration_enabled.nil? - create( - :clusters_integrations_prometheus, - cluster: cluster, - enabled: integration_enabled, - alert_manager_token: configured_token - ) - end - end - - case result = params[:result] - when :success - include_examples 'processes one firing and one resolved prometheus alerts' - when :failure - it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized - else - raise "invalid result: #{result.inspect}" - end - end - end - - context 'without project specific cluster' do - let_it_be(:cluster) { create(:cluster, enabled: true) } - - it_behaves_like 'alerts service responds with an error and takes no actions', :unauthorized - end - context 'with manual prometheus installation' do where(:alerting_setting, :configured_token, :token_input, :result) do true | token | token | :success @@ -230,7 +151,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService, feature_category: :i context 'with multiple firing alerts and resolving alerts' do let(:payload_raw) do - prometheus_alert_payload(firing: [alert_firing, alert_firing], resolved: [alert_resolved]) + prometheus_alert_payload(firing: ['Alert A', 'Alert A'], resolved: ['Alert B']) end it 'processes Prometheus alerts' do @@ -248,7 +169,7 @@ RSpec.describe Projects::Prometheus::Alerts::NotifyService, feature_category: :i context 'when payload exceeds max amount of processable alerts' do # We are defining 2 alerts in payload_raw above let(:max_alerts) { 1 } - let(:fingerprint) { prometheus_alert_payload_fingerprint(alert_resolved) } + let(:fingerprint) { prometheus_alert_payload_fingerprint('Alert A') } before do stub_const("#{described_class}::PROCESS_MAX_ALERTS", max_alerts) diff --git a/spec/services/search_service_spec.rb b/spec/services/search_service_spec.rb index c937a93c6ef..4ba704532e6 100644 --- a/spec/services/search_service_spec.rb +++ b/spec/services/search_service_spec.rb @@ -89,6 +89,12 @@ RSpec.describe SearchService, feature_category: :global_search do end end + describe '#search_type' do + subject { described_class.new(user, search: valid_search).search_type } + + it { is_expected.to eq('basic') } + end + describe '#show_snippets?' do context 'when :snippets is \'true\'' do it 'returns true' do |