diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-28 15:08:39 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-28 15:08:39 +0300 |
commit | aaff41e10e8c03e545af9ba157e79f67686972a0 (patch) | |
tree | dbfc8ba40f533bd62bd1ffae500ebb43bceb5878 /spec/services | |
parent | c00c766f13d6e0291831a25434eb54057708cc47 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services')
3 files changed, 0 insertions, 452 deletions
diff --git a/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb b/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb deleted file mode 100644 index 877a455ea44..00000000000 --- a/spec/services/metrics/dashboard/grafana_metric_embed_service_spec.rb +++ /dev/null @@ -1,279 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Metrics::Dashboard::GrafanaMetricEmbedService, feature_category: :metrics do - include MetricsDashboardHelpers - include ReactiveCachingHelpers - include GrafanaApiHelpers - - let_it_be(:project) { create(:project) } - let_it_be(:user) { create(:user) } - let_it_be(:grafana_integration) { create(:grafana_integration, project: project) } - - let(:grafana_url) do - valid_grafana_dashboard_link(grafana_integration.grafana_url) - end - - before_all do - project.add_maintainer(user) - end - - describe '.valid_params?' do - let(:valid_params) { { embedded: true, grafana_url: grafana_url } } - - 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 grafana_url' do - let(:params) { valid_params.except(:grafana_url) } - - it { is_expected.to be_falsey } - end - end - - describe '.from_cache' do - let(:params) { [project.id, user.id, grafana_url] } - - subject { described_class.from_cache(*params) } - - it 'initializes an instance of GrafanaMetricEmbedService' do - expect(subject).to be_an_instance_of(described_class) - expect(subject.project).to eq(project) - expect(subject.current_user).to eq(user) - expect(subject.params[:grafana_url]).to eq(grafana_url) - end - - context 'with unknown users' do - let(:params) { [project.id, current_user_id, grafana_url] } - - context 'when anonymous' do - where(:current_user_id) do - [nil, ''] - end - - with_them do - it 'sets current_user as nil' do - expect(subject.current_user).to be_nil - end - end - end - - context 'when invalid' do - let(:current_user_id) { non_existing_record_id } - - it 'raise record not found error' do - expect { subject } - .to raise_error(ActiveRecord::RecordNotFound, /Couldn't find User/) - end - end - end - end - - describe '#get_dashboard', :use_clean_rails_memory_store_caching do - let(:service_params) do - [ - project, - user, - { - embedded: true, - grafana_url: grafana_url - } - ] - end - - let(:service) { described_class.new(*service_params) } - let(:service_call) { service.get_dashboard } - - context 'without caching' do - before do - synchronous_reactive_cache(service) - end - - it_behaves_like 'raises error for users with insufficient permissions' - - context 'without a grafana integration' do - before do - allow(project).to receive(:grafana_integration).and_return(nil) - end - - it_behaves_like 'misconfigured dashboard service response', :bad_request - end - - context 'when grafana cannot be reached' do - before do - allow(grafana_integration.client).to receive(:get_dashboard).and_raise(::Grafana::Client::Error) - end - - it_behaves_like 'misconfigured dashboard service response', :service_unavailable - end - - context 'when panelId is missing' do - let(:grafana_url) do - grafana_integration.grafana_url + - '/d/XDaNK6amz/gitlab-omnibus-redis' \ - '?from=1570397739557&to=1570484139557' - end - - before do - stub_dashboard_request(grafana_integration.grafana_url) - end - - it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity - end - - context 'when uid is missing' do - let(:grafana_url) { grafana_integration.grafana_url + '/d/' } - - before do - stub_dashboard_request(grafana_integration.grafana_url) - end - - it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity - end - - context 'when the dashboard response contains misconfigured json' do - before do - stub_dashboard_request(grafana_integration.grafana_url, body: '') - end - - it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity - end - - context 'when the datasource response contains misconfigured json' do - before do - stub_dashboard_request(grafana_integration.grafana_url) - stub_datasource_request(grafana_integration.grafana_url, body: '') - end - - it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity - end - - context 'when the embed was created successfully' do - before do - stub_dashboard_request(grafana_integration.grafana_url) - stub_datasource_request(grafana_integration.grafana_url) - end - - context 'when project is private and user is member' do - it_behaves_like 'valid embedded dashboard service response' - end - - context 'when project is public and user is anonymous' do - let(:project) { create(:project, :public) } - let(:user) { nil } - let(:grafana_integration) { create(:grafana_integration, project: project) } - - it_behaves_like 'valid embedded dashboard service response' - end - end - end - - context 'with caching', :use_clean_rails_memory_store_caching do - let(:cache_params) { [project.id, user.id, grafana_url] } - - context 'when value not present in cache' do - it 'returns nil' do - expect(ExternalServiceReactiveCachingWorker) - .to receive(:perform_async) - .with(service.class, service.id, *cache_params) - - expect(service_call).to eq(nil) - end - end - - context 'when value present in cache' do - let(:return_value) { { 'http_status' => :ok, 'dashboard' => '{}' } } - - before do - stub_reactive_cache(service, return_value, cache_params) - end - - it 'returns cached value' do - expect(ReactiveCachingWorker) - .not_to receive(:perform_async) - .with(service.class, service.id, *cache_params) - - expect(service_call[:http_status]).to eq(return_value[:http_status]) - expect(service_call[:dashboard]).to eq(return_value[:dashboard]) - end - end - end - end -end - -RSpec.describe Metrics::Dashboard::GrafanaUidParser do - let_it_be(:grafana_integration) { create(:grafana_integration) } - let_it_be(:project) { grafana_integration.project } - - subject { described_class.new(grafana_url, project).parse } - - context 'with a Grafana-defined uid' do - let(:grafana_url) { grafana_integration.grafana_url + '/d/XDaNK6amz/?panelId=1' } - - it { is_expected.to eq 'XDaNK6amz' } - end - - context 'with a user-defined uid' do - let(:grafana_url) { grafana_integration.grafana_url + '/d/pgbouncer-main/pgbouncer-overview?panelId=1' } - - it { is_expected.to eq 'pgbouncer-main' } - end - - context 'when a uid is not present' do - let(:grafana_url) { grafana_integration.grafana_url } - - it { is_expected.to be nil } - end - - context 'when the url starts with unrelated content' do - let(:grafana_url) { 'js:' + grafana_integration.grafana_url } - - it { is_expected.to be nil } - end -end - -RSpec.describe Metrics::Dashboard::DatasourceNameParser do - include GrafanaApiHelpers - - let(:grafana_url) { valid_grafana_dashboard_link('https://gitlab.grafana.net') } - let(:grafana_dashboard) { Gitlab::Json.parse(fixture_file('grafana/dashboard_response.json'), symbolize_names: true) } - - subject { described_class.new(grafana_url, grafana_dashboard).parse } - - it { is_expected.to eq 'GitLab Omnibus' } - - context 'when the panelId is missing from the url' do - let(:grafana_url) { 'https:/gitlab.grafana.net/d/jbdbks/' } - - it { is_expected.to be nil } - end - - context 'when the panel is not present' do - # We're looking for panelId of 8, but only 6 is present - let(:grafana_dashboard) { { dashboard: { panels: [{ id: 6 }] } } } - - it { is_expected.to be nil } - end - - context 'when the dashboard panel does not have a datasource' do - let(:grafana_dashboard) { { dashboard: { panels: [{ id: 8 }] } } } - - it { is_expected.to be nil } - end -end diff --git a/spec/services/metrics/dashboard/pod_dashboard_service_spec.rb b/spec/services/metrics/dashboard/pod_dashboard_service_spec.rb deleted file mode 100644 index a6fcb6b4842..00000000000 --- a/spec/services/metrics/dashboard/pod_dashboard_service_spec.rb +++ /dev/null @@ -1,74 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Metrics::Dashboard::PodDashboardService, :use_clean_rails_memory_store_caching, - feature_category: :cell do - include MetricsDashboardHelpers - - let_it_be(:user) { create(:user) } - let_it_be(:project) { create(:project) } - let_it_be(:environment) { create(:environment, project: project) } - - let(:dashboard_path) { described_class::DASHBOARD_PATH } - let(:service_params) { [project, user, { environment: environment, dashboard_path: dashboard_path }] } - - before do - project.add_maintainer(user) - end - - subject { described_class.new(*service_params) } - - describe '#raw_dashboard' do - it_behaves_like '#raw_dashboard raises error if dashboard loading fails' - end - - describe '.valid_params?' do - let(:params) { { dashboard_path: described_class::DASHBOARD_PATH } } - - subject { described_class.valid_params?(params) } - - it { is_expected.to be_truthy } - - context 'missing dashboard_path' do - let(:params) { {} } - - it { is_expected.to be_falsey } - end - - context 'non-matching dashboard_path' do - let(:params) { { dashboard_path: 'path/to/bunk.yml' } } - - it { is_expected.to be_falsey } - end - end - - describe '#get_dashboard' do - let(:service_call) { subject.get_dashboard } - - it_behaves_like 'valid dashboard service response' - it_behaves_like 'caches the unprocessed dashboard for subsequent calls' - it_behaves_like 'refreshes cache when dashboard_version is changed' - it_behaves_like 'updates gitlab_metrics_dashboard_processing_time_ms metric' - - it_behaves_like 'dashboard_version contains SHA256 hash of dashboard file content' do - let(:dashboard_version) { subject.send(:dashboard_version) } - end - end - - describe '.all_dashboard_paths' do - it 'returns the dashboard attributes' do - all_dashboards = described_class.all_dashboard_paths(project) - - expect(all_dashboards).to eq( - [{ - path: described_class::DASHBOARD_PATH, - display_name: described_class::DASHBOARD_NAME, - default: false, - system_dashboard: false, - out_of_the_box_dashboard: true - }] - ) - end - end -end diff --git a/spec/services/metrics/dashboard/transient_embed_service_spec.rb b/spec/services/metrics/dashboard/transient_embed_service_spec.rb deleted file mode 100644 index 1e3ccde6ae3..00000000000 --- a/spec/services/metrics/dashboard/transient_embed_service_spec.rb +++ /dev/null @@ -1,99 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Metrics::Dashboard::TransientEmbedService, :use_clean_rails_memory_store_caching, - feature_category: :metrics do - 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 - - describe '.valid_params?' do - let(:params) { { embedded: 'true', embed_json: '{}' } } - - subject { described_class.valid_params?(params) } - - it { is_expected.to be_truthy } - - context 'missing embedded' do - let(:params) { { embed_json: '{}' } } - - it { is_expected.to be_falsey } - end - - context 'not embedded' do - let(:params) { { embedded: 'false', embed_json: '{}' } } - - it { is_expected.to be_falsey } - end - - context 'missing embed_json' do - let(:params) { { embedded: 'true' } } - - it { is_expected.to be_falsey } - end - end - - describe '#get_dashboard' do - let(:embed_json) { get_embed_json } - let(:service_params) { [project, user, { environment: environment, embedded: 'true', embed_json: embed_json }] } - let(:service_call) { described_class.new(*service_params).get_dashboard } - - 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_any_instance_of(described_class) - .to receive(:get_raw_dashboard) - .once - .and_call_original - - described_class.new(*service_params).get_dashboard - described_class.new(*service_params).get_dashboard - end - - it 'caches unique requests separately' do - alt_embed_json = get_embed_json('area-chart') - alt_service_params = [project, user, { environment: environment, embedded: 'true', embed_json: alt_embed_json }] - - embed = described_class.new(*service_params).get_dashboard - alt_embed = described_class.new(*alt_service_params).get_dashboard - - expect(embed).not_to eq(alt_embed) - expect(get_type_for_embed(embed)).to eq('line-graph') - expect(get_type_for_embed(alt_embed)).to eq('area-chart') - end - - context 'when embed_json cannot be parsed as json' do - let(:embed_json) { '' } - - it_behaves_like 'misconfigured dashboard service response', :unprocessable_entity - end - - private - - def get_embed_json(type = 'line-graph') - { - panel_groups: [{ - panels: [{ - type: type, - title: 'title', - y_label: 'y_label', - metrics: [{ - query_range: 'up', - label: 'y_label' - }] - }] - }] - }.to_json - end - - def get_type_for_embed(embed) - embed[:dashboard][:panel_groups][0][:panels][0][:type] - end - end -end |