debian-mirror-gitlab/spec/serializers/ci/dag_pipeline_entity_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

164 lines
5.7 KiB
Ruby
Raw Permalink Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Ci::DagPipelineEntity do
2020-05-24 23:13:21 +05:30
let_it_be(:request) { double(:request) }
2022-10-11 01:57:18 +05:30
let_it_be(:pipeline) { create(:ci_pipeline) }
2020-05-24 23:13:21 +05:30
let(:entity) { described_class.new(pipeline, request: request) }
describe '#as_json' do
subject { entity.as_json }
2020-07-28 23:09:34 +05:30
RSpec.shared_examples "matches schema" do
it 'matches schema' do
expect(subject.to_json).to match_schema('entities/dag_pipeline')
end
end
2020-05-24 23:13:21 +05:30
context 'when pipeline is empty' do
it 'contains stages' do
expect(subject).to include(:stages)
expect(subject[:stages]).to be_empty
end
2020-07-28 23:09:34 +05:30
it_behaves_like "matches schema"
2020-05-24 23:13:21 +05:30
end
context 'when pipeline has jobs' do
2022-10-11 01:57:18 +05:30
let_it_be(:build_stage) { create(:ci_stage, name: 'build', pipeline: pipeline) }
let_it_be(:test_stage) { create(:ci_stage, name: 'test', pipeline: pipeline) }
let_it_be(:deploy_stage) { create(:ci_stage, name: 'deploy', pipeline: pipeline) }
let!(:build_job) { create(:ci_build, ci_stage: build_stage, pipeline: pipeline) }
let!(:test_job) { create(:ci_build, ci_stage: test_stage, pipeline: pipeline) }
let!(:deploy_job) { create(:ci_build, ci_stage: deploy_stage, pipeline: pipeline) }
2020-05-24 23:13:21 +05:30
it 'contains 3 stages' do
stages = subject[:stages]
expect(stages.size).to eq 3
expect(stages.map { |s| s[:name] }).to contain_exactly('build', 'test', 'deploy')
end
2020-07-28 23:09:34 +05:30
it_behaves_like "matches schema"
2020-05-24 23:13:21 +05:30
end
2020-06-23 00:09:42 +05:30
context 'when pipeline has parallel jobs, DAG needs and GenericCommitStatus' do
2022-08-13 15:12:31 +05:30
let!(:stage_build) { create(:ci_stage, name: 'build', position: 1, pipeline: pipeline) }
let!(:stage_test) { create(:ci_stage, name: 'test', position: 2, pipeline: pipeline) }
let!(:stage_deploy) { create(:ci_stage, name: 'deploy', position: 3, pipeline: pipeline) }
2020-05-24 23:13:21 +05:30
2022-10-11 01:57:18 +05:30
let!(:job_build_1) { create(:ci_build, name: 'build 1', ci_stage: stage_build, pipeline: pipeline) }
let!(:job_build_2) { create(:ci_build, name: 'build 2', ci_stage: stage_build, pipeline: pipeline) }
let!(:commit_status) { create(:generic_commit_status, ci_stage: stage_build, pipeline: pipeline) }
2020-05-24 23:13:21 +05:30
2022-10-11 01:57:18 +05:30
let!(:job_rspec_1) { create(:ci_build, name: 'rspec 1/2', ci_stage: stage_test, pipeline: pipeline) }
let!(:job_rspec_2) { create(:ci_build, name: 'rspec 2/2', ci_stage: stage_test, pipeline: pipeline) }
2020-05-24 23:13:21 +05:30
let!(:job_jest) do
2022-10-11 01:57:18 +05:30
create(:ci_build, name: 'jest', ci_stage: stage_test, scheduling_type: 'dag', pipeline: pipeline)
.tap do |job|
2020-05-24 23:13:21 +05:30
create(:ci_build_need, name: 'build 1', build: job)
end
end
let!(:job_deploy_ruby) do
2022-10-11 01:57:18 +05:30
create(:ci_build, name: 'deploy_ruby', ci_stage: stage_deploy, scheduling_type: 'dag', pipeline: pipeline)
.tap do |job|
2020-05-24 23:13:21 +05:30
create(:ci_build_need, name: 'rspec 1/2', build: job)
create(:ci_build_need, name: 'rspec 2/2', build: job)
end
end
let!(:job_deploy_js) do
2022-10-11 01:57:18 +05:30
create(:ci_build, name: 'deploy_js', ci_stage: stage_deploy, scheduling_type: 'dag', pipeline: pipeline)
.tap do |job|
2020-05-24 23:13:21 +05:30
create(:ci_build_need, name: 'jest', build: job)
end
end
2021-03-11 19:13:27 +05:30
it 'performs the smallest number of queries', :request_store do
2020-05-24 23:13:21 +05:30
log = ActiveRecord::QueryRecorder.new { subject }
2021-04-29 21:17:54 +05:30
# stages, project, builds, build_needs
expect(log.count).to eq 4
2020-05-24 23:13:21 +05:30
end
it 'contains all the data' do
expected_result = {
stages: [
{
name: 'build',
groups: [
2020-06-23 00:09:42 +05:30
{
name: 'build 1', size: 1, jobs: [
{ name: 'build 1', scheduling_type: 'stage' }
]
},
{
name: 'build 2', size: 1, jobs: [
{ name: 'build 2', scheduling_type: 'stage' }
]
},
{
name: 'generic', size: 1, jobs: [
{ name: 'generic', scheduling_type: nil }
]
}
2020-05-24 23:13:21 +05:30
]
},
{
name: 'test',
groups: [
2020-06-23 00:09:42 +05:30
{
name: 'jest', size: 1, jobs: [
{ name: 'jest', scheduling_type: 'dag', needs: ['build 1'] }
]
},
{
name: 'rspec', size: 2, jobs: [
{ name: 'rspec 1/2', scheduling_type: 'stage' },
{ name: 'rspec 2/2', scheduling_type: 'stage' }
]
}
2020-05-24 23:13:21 +05:30
]
},
{
name: 'deploy',
groups: [
2020-06-23 00:09:42 +05:30
{
name: 'deploy_js', size: 1, jobs: [
{ name: 'deploy_js', scheduling_type: 'dag', needs: ['jest'] }
]
},
{
name: 'deploy_ruby', size: 1, jobs: [
{ name: 'deploy_ruby', scheduling_type: 'dag', needs: ['rspec 1/2', 'rspec 2/2'] }
]
}
2020-05-24 23:13:21 +05:30
]
}
]
}
expect(subject.fetch(:stages)).not_to be_empty
expect(subject.fetch(:stages)[0].fetch(:name)).to eq 'build'
expect(subject.fetch(:stages)[0]).to eq expected_result.fetch(:stages)[0]
expect(subject.fetch(:stages)[1].fetch(:name)).to eq 'test'
expect(subject.fetch(:stages)[1]).to eq expected_result.fetch(:stages)[1]
expect(subject.fetch(:stages)[2].fetch(:name)).to eq 'deploy'
expect(subject.fetch(:stages)[2]).to eq expected_result.fetch(:stages)[2]
end
2020-07-28 23:09:34 +05:30
it_behaves_like "matches schema"
2020-05-24 23:13:21 +05:30
end
end
end