diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/projects/prometheus/alerts_controller_spec.rb | 18 | ||||
-rw-r--r-- | spec/factories/ci/pipeline_artifacts.rb | 12 | ||||
-rw-r--r-- | spec/factories/prometheus_alert.rb | 4 | ||||
-rw-r--r-- | spec/lib/gitlab/import_export/all_models.yml | 2 | ||||
-rw-r--r-- | spec/models/ci/pipeline_artifact_spec.rb | 46 | ||||
-rw-r--r-- | spec/models/ci/pipeline_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 1 | ||||
-rw-r--r-- | spec/models/prometheus_alert_spec.rb | 35 | ||||
-rw-r--r-- | spec/serializers/prometheus_alert_entity_spec.rb | 2 |
9 files changed, 115 insertions, 6 deletions
diff --git a/spec/controllers/projects/prometheus/alerts_controller_spec.rb b/spec/controllers/projects/prometheus/alerts_controller_spec.rb index 6e3148231bd..cbd599506df 100644 --- a/spec/controllers/projects/prometheus/alerts_controller_spec.rb +++ b/spec/controllers/projects/prometheus/alerts_controller_spec.rb @@ -111,6 +111,7 @@ RSpec.describe Projects::Prometheus::AlertsController do describe 'GET #show' do let(:alert) do create(:prometheus_alert, + :with_runbook_url, project: project, environment: environment, prometheus_metric: metric) @@ -140,6 +141,7 @@ RSpec.describe Projects::Prometheus::AlertsController do 'query' => alert.query, 'operator' => alert.computed_operator, 'threshold' => alert.threshold, + 'runbook_url' => alert.runbook_url, 'alert_path' => alert_path(alert) } end @@ -225,7 +227,8 @@ RSpec.describe Projects::Prometheus::AlertsController do 'title' => metric.title, 'query' => metric.query, 'operator' => '>', - 'threshold' => 1.0 + 'threshold' => 1.0, + 'runbook_url' => 'https://sample.runbook.com' } end @@ -234,6 +237,7 @@ RSpec.describe Projects::Prometheus::AlertsController do opts, operator: '>', threshold: '1', + runbook_url: 'https://sample.runbook.com', environment_id: environment, prometheus_metric_id: metric ) @@ -250,14 +254,14 @@ RSpec.describe Projects::Prometheus::AlertsController do expect(json_response).to include(alert_params) end - it 'returns no_content for an invalid metric' do + it 'returns bad_request for an invalid metric' do make_request(prometheus_metric_id: 'invalid') - expect(response).to have_gitlab_http_status(:no_content) + expect(response).to have_gitlab_http_status(:bad_request) end it_behaves_like 'unprivileged' - it_behaves_like 'project non-specific environment', :no_content + it_behaves_like 'project non-specific environment', :bad_request end describe 'PUT #update' do @@ -304,6 +308,12 @@ RSpec.describe Projects::Prometheus::AlertsController do expect(json_response).to include(alert_params) end + it 'returns bad_request for an invalid alert data' do + make_request(runbook_url: 'bad-url') + + expect(response).to have_gitlab_http_status(:bad_request) + end + it_behaves_like 'unprivileged' it_behaves_like 'project non-specific environment', :not_found it_behaves_like 'project non-specific metric', :not_found diff --git a/spec/factories/ci/pipeline_artifacts.rb b/spec/factories/ci/pipeline_artifacts.rb new file mode 100644 index 00000000000..d3f3f9e92ac --- /dev/null +++ b/spec/factories/ci/pipeline_artifacts.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :ci_pipeline_artifact, class: 'Ci::PipelineArtifact' do + pipeline factory: :ci_pipeline + project { pipeline.project } + file_type { :code_coverage } + file_format { :raw } + file_store { Ci::PipelineArtifact::FILE_STORE_SUPPORTED.first } + size { 1.megabytes } + end +end diff --git a/spec/factories/prometheus_alert.rb b/spec/factories/prometheus_alert.rb index a9fede9efca..18cf1a20e0d 100644 --- a/spec/factories/prometheus_alert.rb +++ b/spec/factories/prometheus_alert.rb @@ -13,5 +13,9 @@ FactoryBot.define do prometheus_metric do |alert| build(:prometheus_metric, project: alert.project) end + + trait :with_runbook_url do + runbook_url { 'https://runbooks.gitlab.com/metric_gt_1'} + end end end diff --git a/spec/lib/gitlab/import_export/all_models.yml b/spec/lib/gitlab/import_export/all_models.yml index 58cae672cf7..bc51e8747d4 100644 --- a/spec/lib/gitlab/import_export/all_models.yml +++ b/spec/lib/gitlab/import_export/all_models.yml @@ -230,6 +230,7 @@ ci_pipelines: - daily_report_results - latest_builds_report_results - messages +- pipeline_artifacts ci_refs: - project - ci_pipelines @@ -519,6 +520,7 @@ project: - vulnerability_statistic - vulnerability_historical_statistics - product_analytics_events +- pipeline_artifacts award_emoji: - awardable - user diff --git a/spec/models/ci/pipeline_artifact_spec.rb b/spec/models/ci/pipeline_artifact_spec.rb new file mode 100644 index 00000000000..930102f087c --- /dev/null +++ b/spec/models/ci/pipeline_artifact_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::PipelineArtifact, type: :model do + let_it_be(:coverage_report) { create(:ci_pipeline_artifact) } + + describe 'associations' do + it { is_expected.to belong_to(:pipeline) } + it { is_expected.to belong_to(:project) } + end + + it_behaves_like 'having unique enum values' + + describe 'validations' do + it { is_expected.to validate_presence_of(:pipeline) } + it { is_expected.to validate_presence_of(:project) } + it { is_expected.to validate_presence_of(:file_type) } + it { is_expected.to validate_presence_of(:file_format) } + it { is_expected.to validate_presence_of(:size) } + it { is_expected.to validate_uniqueness_of(:file_type).scoped_to([:pipeline_id]).ignoring_case_sensitivity } + + context 'when attributes are valid' do + it 'returns no errors' do + expect(coverage_report).to be_valid + end + end + + context 'when file_store is invalid' do + it 'returns errors' do + coverage_report.file_store = 0 + + expect(coverage_report).to be_invalid + expect(coverage_report.errors.full_messages).to eq(["File store is not included in the list"]) + end + end + + context 'when size is over 10 megabytes' do + it 'returns errors' do + coverage_report.size = 11.megabytes + + expect(coverage_report).to be_invalid + end + end + end +end diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index c6d9dd8d555..bb3d02eb69f 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -46,6 +46,7 @@ RSpec.describe Ci::Pipeline, :mailer do it { is_expected.to respond_to :git_author_email } it { is_expected.to respond_to :short_sha } it { is_expected.to delegate_method(:full_path).to(:project).with_prefix } + it { is_expected.to have_many(:pipeline_artifacts) } describe 'associations' do it 'has a bidirectional relationship with projects' do diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 274e2021c0f..9dada39a7a0 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -122,6 +122,7 @@ RSpec.describe Project do it { is_expected.to have_many(:reviews).inverse_of(:project) } it { is_expected.to have_many(:packages).class_name('Packages::Package') } it { is_expected.to have_many(:package_files).class_name('Packages::PackageFile') } + it { is_expected.to have_many(:pipeline_artifacts) } it_behaves_like 'model with repository' do let_it_be(:container) { create(:project, :repository, path: 'somewhere') } diff --git a/spec/models/prometheus_alert_spec.rb b/spec/models/prometheus_alert_spec.rb index 7169a34d96f..ed9d981e2dd 100644 --- a/spec/models/prometheus_alert_spec.rb +++ b/spec/models/prometheus_alert_spec.rb @@ -74,6 +74,34 @@ RSpec.describe PrometheusAlert do end end + describe 'runbook validations' do + it 'disallow invalid urls' do + unsafe_url = %{https://replaceme.com/'><script>alert(document.cookie)</script>} + non_ascii_url = 'http://gitlab.com/user/project1/wiki/something€' + excessively_long_url = 'https://gitla' + 'b' * 1024 + '.com' + + is_expected.not_to allow_values( + unsafe_url, + non_ascii_url, + excessively_long_url + ).for(:runbook_url) + end + + it 'allow valid urls' do + external_url = 'http://runbook.gitlab.com/' + internal_url = 'http://192.168.1.1' + blank_url = '' + nil_url = nil + + is_expected.to allow_value( + external_url, + internal_url, + blank_url, + nil_url + ).for(:runbook_url) + end + end + describe '#full_query' do before do subject.operator = "gt" @@ -91,6 +119,7 @@ RSpec.describe PrometheusAlert do subject.operator = "gt" subject.threshold = 1 subject.prometheus_metric = metric + subject.runbook_url = 'runbook' end it 'returns the params of the prometheus alert' do @@ -102,7 +131,11 @@ RSpec.describe PrometheusAlert do "gitlab" => "hook", "gitlab_alert_id" => metric.id, "gitlab_prometheus_alert_id" => subject.id - }) + }, + "annotations" => { + "runbook" => "runbook" + } + ) end end end diff --git a/spec/serializers/prometheus_alert_entity_spec.rb b/spec/serializers/prometheus_alert_entity_spec.rb index aeee8de2a5b..ae8c97401f8 100644 --- a/spec/serializers/prometheus_alert_entity_spec.rb +++ b/spec/serializers/prometheus_alert_entity_spec.rb @@ -16,7 +16,7 @@ RSpec.describe PrometheusAlertEntity do end it 'exposes prometheus_alert attributes' do - expect(subject).to include(:id, :title, :query, :operator, :threshold) + expect(subject).to include(:id, :title, :query, :operator, :threshold, :runbook_url) end it 'exposes alert_path' do |