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

282 lines
9.1 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
2018-03-17 18:26:18 +05:30
let(:resource) { create(: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
2018-03-17 18:26:18 +05:30
let(:resource) { create_list(:ci_pipeline, 2, project: project) }
2017-08-17 22:00:37 +05:30
it 'serializers the array of pipelines' do
expect(subject).not_to be_empty
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
described_class.new(current_user: user)
.with_pagination(request, response)
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
create(:ci_empty_pipeline)
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
create_list(:ci_empty_pipeline, 3)
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,
:with_detached_merge_request_pipeline,
target_project: project,
target_branch: 'master',
source_project: project,
source_branch: 'feature')
end
let!(:merge_request_2) do
create(:merge_request,
:with_detached_merge_request_pipeline,
target_project: project,
target_branch: 'master',
source_project: project,
source_branch: '2-mb-file')
end
before do
project.add_developer(user)
end
it 'includes merge requests information' do
expect(subject.all? { |entry| entry[:merge_request].present? }).to be_truthy
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 }
before 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
let(:ref) { 'feature' }
2017-09-10 17:25:29 +05:30
it 'verifies number of queries', :request_store do
recorded = ActiveRecord::QueryRecorder.new { subject }
2021-01-03 14:25:43 +05:30
expected_queries = Gitlab.ee? ? 39 : 36
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
def ref
@sequence ||= 0
@sequence += 1
"feature-#{@sequence}"
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-01-03 14:25:43 +05:30
expected_queries = Gitlab.ee? ? 42 : 39
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
let(:ref) { 'feature' }
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
2021-04-29 21:17:54 +05:30
expected_queries = Gitlab.ee? ? 82 : 76
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
let(:ref) { 'feature' }
it 'verifies number of queries', :request_store do
stub_licensed_features(protected_environments: true)
env = create(:environment, project: project)
create(:ci_build, :scheduled, project: project, environment: env.name)
create(:ci_build, :scheduled, project: project, environment: env.name)
create(:ci_build, :scheduled, project: project, environment: env.name)
recorded = ActiveRecord::QueryRecorder.new { subject }
expected_queries = Gitlab.ee? ? 61 : 57
expect(recorded.count).to be_within(1).of(expected_queries)
expect(recorded.cached_count).to eq(0)
end
end
2021-04-17 20:07:23 +05:30
context 'with scheduled and manual builds' do
let(:ref) { 'feature' }
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
expect { subject }.not_to exceed_query_limit(1 + 2).for_query /SELECT "ci_builds_metadata".*/
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,
ref: ref).tap do |pipeline|
2017-08-17 22:00:37 +05:30
Ci::Build::AVAILABLE_STATUSES.each do |status|
create_build(pipeline, status, status)
end
end
end
def create_build(pipeline, stage, status)
create(:ci_build, :tags, :triggered, :artifacts,
pipeline: pipeline, stage: stage,
2017-09-10 17:25:29 +05:30
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
let(:resource) { create(:ci_pipeline) }
let(:status) { resource.detailed_status(double('user')) }
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