debian-mirror-gitlab/spec/serializers/pipeline_serializer_spec.rb

273 lines
9.4 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe PipelineSerializer do
2020-04-08 14:13:33 +05:30
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
2017-08-17 22:00:37 +05:30
let(:serializer) do
2019-07-07 11:18:12 +05:30
described_class.new(current_user: user, project: project)
2017-08-17 22:00:37 +05:30
end
subject { serializer.represent(resource) }
describe '#represent' do
context 'when used without pagination' do
it 'created a not paginated serializer' do
expect(serializer).not_to be_paginated
end
context 'when a single object is being serialized' do
2022-11-25 23:54:43 +05:30
let(:resource) { build_stubbed(:ci_empty_pipeline, project: project) }
2017-08-17 22:00:37 +05:30
it 'serializers the pipeline object' do
expect(subject[:id]).to eq resource.id
end
end
context 'when multiple objects are being serialized' do
2022-11-25 23:54:43 +05:30
let(:resource) { Array.new(2) { build_stubbed(:ci_pipeline, project: project) } }
2017-08-17 22:00:37 +05:30
it 'serializers the array of pipelines' do
expect(subject).not_to be_empty
2022-11-25 23:54:43 +05:30
expect(subject.size).to eq(2)
2017-08-17 22:00:37 +05:30
end
end
end
context 'when used with pagination' do
2019-07-07 11:18:12 +05:30
let(:request) { double(url: "#{Gitlab.config.gitlab.url}:8080/api/v4/projects?#{query.to_query}", query_parameters: query) }
2017-08-17 22:00:37 +05:30
let(:response) { spy('response') }
2019-07-07 11:18:12 +05:30
let(:query) { {} }
2017-08-17 22:00:37 +05:30
let(:serializer) do
2022-11-25 23:54:43 +05:30
described_class.new(current_user: user, project: project).with_pagination(request, response)
2017-08-17 22:00:37 +05:30
end
it 'created a paginated serializer' do
expect(serializer).to be_paginated
end
context 'when resource is not paginatable' do
context 'when a single pipeline object is being serialized' do
let(:resource) { create(:ci_empty_pipeline) }
2019-07-07 11:18:12 +05:30
let(:query) { { page: 1, per_page: 1 } }
2017-08-17 22:00:37 +05:30
it 'raises error' do
expect { subject }.to raise_error(
Gitlab::Serializer::Pagination::InvalidResourceError)
end
end
end
context 'when resource is paginatable relation' do
let(:resource) { Ci::Pipeline.all }
2019-07-07 11:18:12 +05:30
let(:query) { { page: 1, per_page: 2 } }
2017-08-17 22:00:37 +05:30
context 'when a single pipeline object is present in relation' do
2017-09-10 17:25:29 +05:30
before do
2022-11-25 23:54:43 +05:30
create(:ci_empty_pipeline, project: project)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it 'serializes pipeline relation' do
expect(subject.first).to have_key :id
end
end
context 'when a multiple pipeline objects are being serialized' do
2017-09-10 17:25:29 +05:30
before do
2022-11-25 23:54:43 +05:30
create_list(:ci_empty_pipeline, 3, project: project)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it 'serializes appropriate number of objects' do
expect(subject.count).to be 2
end
it 'appends relevant headers' do
expect(response).to receive(:[]=).with('X-Total', '3')
expect(response).to receive(:[]=).with('X-Total-Pages', '2')
expect(response).to receive(:[]=).with('X-Per-Page', '2')
subject
end
end
end
end
2019-07-07 11:18:12 +05:30
context 'when there are pipelines for merge requests' do
let(:resource) { Ci::Pipeline.all }
let!(:merge_request_1) do
create(:merge_request,
2022-11-25 23:54:43 +05:30
:with_detached_merge_request_pipeline,
target_project: project,
target_branch: 'master',
source_project: project,
source_branch: 'feature')
2019-07-07 11:18:12 +05:30
end
let!(:merge_request_2) do
create(:merge_request,
2022-11-25 23:54:43 +05:30
:with_detached_merge_request_pipeline,
target_project: project,
target_branch: 'master',
source_project: project,
source_branch: '2-mb-file')
2019-07-07 11:18:12 +05:30
end
2022-11-25 23:54:43 +05:30
before_all do
2019-07-07 11:18:12 +05:30
project.add_developer(user)
end
it 'includes merge requests information' do
2022-11-25 23:54:43 +05:30
expect(subject).to be_all { |entry| entry[:merge_request].present? }
2019-07-07 11:18:12 +05:30
end
2019-10-12 21:52:04 +05:30
it 'preloads related merge requests' do
2019-07-07 11:18:12 +05:30
recorded = ActiveRecord::QueryRecorder.new { subject }
2020-01-01 13:55:28 +05:30
expected_query = "SELECT \"merge_requests\".* FROM \"merge_requests\" " \
"WHERE \"merge_requests\".\"id\" IN (#{merge_request_1.id}, #{merge_request_2.id})"
2019-07-07 11:18:12 +05:30
2020-01-01 13:55:28 +05:30
expect(recorded.log).to include(a_string_starting_with(expected_query))
2019-07-07 11:18:12 +05:30
end
end
2018-11-08 19:23:39 +05:30
describe 'number of queries when preloaded' do
subject { serializer.represent(resource, preload: true) }
2019-12-21 20:55:43 +05:30
2017-08-17 22:00:37 +05:30
let(:resource) { Ci::Pipeline.all }
2022-11-25 23:54:43 +05:30
# Create pipelines only once and change their attributes if needed.
before_all do
2018-03-17 18:26:18 +05:30
# Since RequestStore.active? is true we have to allow the
# gitaly calls in this block
2019-12-04 20:38:33 +05:30
# Issue: https://gitlab.com/gitlab-org/gitlab-foss/issues/37772
2018-03-17 18:26:18 +05:30
Gitlab::GitalyClient.allow_n_plus_1_calls do
2018-11-08 19:23:39 +05:30
Ci::Pipeline::COMPLETED_STATUSES.each do |status|
2018-03-17 18:26:18 +05:30
create_pipeline(status)
end
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
Gitlab::GitalyClient.reset_counts
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
context 'with the same ref' do
2017-09-10 17:25:29 +05:30
it 'verifies number of queries', :request_store do
recorded = ActiveRecord::QueryRecorder.new { subject }
2021-06-08 01:23:25 +05:30
expected_queries = Gitlab.ee? ? 33 : 30
2018-05-09 12:01:36 +05:30
2019-07-31 22:56:46 +05:30
expect(recorded.count).to be_within(2).of(expected_queries)
2017-09-10 17:25:29 +05:30
expect(recorded.cached_count).to eq(0)
end
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
context 'with different refs' do
2022-11-25 23:54:43 +05:30
before do
Ci::Pipeline.update_all(%(ref = 'feature-' || id))
Ci::Build.update_all(%(ref = 'feature-' || stage_id))
2017-09-10 17:25:29 +05:30
end
2018-11-08 19:23:39 +05:30
it 'verifies number of queries', :request_store do
recorded = ActiveRecord::QueryRecorder.new { subject }
# For each ref there is a permission check if maintainer can update
# pipeline. With the same ref this check is cached but if refs are
# different then there is an extra query per ref
2019-12-04 20:38:33 +05:30
# https://gitlab.com/gitlab-org/gitlab-foss/issues/46368
2021-06-08 01:23:25 +05:30
expected_queries = Gitlab.ee? ? 36 : 33
2020-07-28 23:09:34 +05:30
expect(recorded.count).to be_within(2).of(expected_queries)
expect(recorded.cached_count).to eq(0)
end
end
context 'with triggered pipelines' do
before do
pipeline_1 = create(:ci_pipeline)
build_1 = create(:ci_build, pipeline: pipeline_1)
create(:ci_sources_pipeline, source_job: build_1)
pipeline_2 = create(:ci_pipeline)
build_2 = create(:ci_build, pipeline: pipeline_2)
create(:ci_sources_pipeline, source_job: build_2)
end
it 'verifies number of queries', :request_store do
recorded = ActiveRecord::QueryRecorder.new { subject }
# Existing numbers are high and require performance optimization
2021-01-03 14:25:43 +05:30
# Ongoing issue:
2020-07-28 23:09:34 +05:30
# https://gitlab.com/gitlab-org/gitlab/-/issues/225156
2022-03-02 08:16:31 +05:30
expected_queries = Gitlab.ee? ? 78 : 74
2019-12-21 20:55:43 +05:30
2019-07-31 22:56:46 +05:30
expect(recorded.count).to be_within(2).of(expected_queries)
2018-11-08 19:23:39 +05:30
expect(recorded.cached_count).to eq(0)
end
2017-08-17 22:00:37 +05:30
end
2021-04-29 21:17:54 +05:30
context 'with build environments' do
2021-09-04 01:27:46 +05:30
let_it_be(:production) { create(:environment, :production, project: project) }
let_it_be(:staging) { create(:environment, :staging, project: project) }
2021-04-29 21:17:54 +05:30
2021-09-04 01:27:46 +05:30
it 'executes one query to fetch all related environments', :request_store do
pipeline = create(:ci_pipeline, project: project)
create(:ci_build, :manual, pipeline: pipeline, environment: production.name)
create(:ci_build, :manual, pipeline: pipeline, environment: staging.name)
create(:ci_build, :scheduled, pipeline: pipeline, environment: production.name)
create(:ci_build, :scheduled, pipeline: pipeline, environment: staging.name)
2021-04-29 21:17:54 +05:30
2022-11-25 23:54:43 +05:30
expect { subject }.not_to exceed_query_limit(1).for_query(/SELECT "environments".*/)
2021-04-29 21:17:54 +05:30
end
end
2021-04-17 20:07:23 +05:30
context 'with scheduled and manual builds' do
before do
create(:ci_build, :scheduled, pipeline: resource.first)
create(:ci_build, :scheduled, pipeline: resource.second)
create(:ci_build, :manual, pipeline: resource.first)
create(:ci_build, :manual, pipeline: resource.second)
end
it 'sends at most one metadata query for each type of build', :request_store do
# 1 for the existing failed builds and 2 for the added scheduled and manual builds
2022-11-25 23:54:43 +05:30
expect { subject }.not_to exceed_query_limit(1 + 2).for_query(/SELECT "ci_builds_metadata".*/)
2021-04-17 20:07:23 +05:30
end
end
2017-08-17 22:00:37 +05:30
def create_pipeline(status)
2017-09-10 17:25:29 +05:30
create(:ci_empty_pipeline,
project: project,
status: status,
2023-01-13 00:05:48 +05:30
name: 'Build pipeline',
2022-11-25 23:54:43 +05:30
ref: 'feature').tap do |pipeline|
Ci::Build::AVAILABLE_STATUSES.each do |build_status|
create_build(pipeline, status, build_status)
2017-08-17 22:00:37 +05:30
end
end
end
def create_build(pipeline, stage, status)
create(:ci_build, :tags, :triggered, :artifacts,
2022-11-25 23:54:43 +05:30
pipeline: pipeline, stage: stage,
name: stage, status: status, ref: pipeline.ref)
2017-08-17 22:00:37 +05:30
end
end
end
describe '#represent_status' do
context 'when represents only status' do
2022-11-25 23:54:43 +05:30
let(:resource) { build_stubbed(:ci_pipeline) }
let(:status) { resource.detailed_status(instance_double('User')) }
2017-08-17 22:00:37 +05:30
subject { serializer.represent_status(resource) }
it 'serializes only status' do
expect(subject[:text]).to eq(status.text)
expect(subject[:label]).to eq(status.label)
expect(subject[:icon]).to eq(status.icon)
2018-11-08 19:23:39 +05:30
expect(subject[:favicon]).to match_asset_path("/assets/ci_favicons/#{status.favicon}.png")
2017-08-17 22:00:37 +05:30
end
end
end
end