2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-06 13:49:37 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Ci::LegacyStage do
|
2016-12-06 13:49:37 +00:00
|
|
|
let(:stage) { build(:ci_stage) }
|
|
|
|
let(:pipeline) { stage.pipeline }
|
|
|
|
let(:stage_name) { stage.name }
|
|
|
|
|
|
|
|
describe '#expectations' do
|
|
|
|
subject { stage }
|
|
|
|
|
|
|
|
it { is_expected.to include_module(StaticModel) }
|
|
|
|
|
|
|
|
it { is_expected.to respond_to(:pipeline) }
|
|
|
|
it { is_expected.to respond_to(:name) }
|
|
|
|
|
|
|
|
it { is_expected.to delegate_method(:project).to(:pipeline) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#statuses' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build) }
|
|
|
|
let!(:commit_status) { create_job(:commit_status) }
|
|
|
|
let!(:other_build) { create_job(:ci_build, stage: 'other stage') }
|
|
|
|
|
|
|
|
subject { stage.statuses }
|
|
|
|
|
|
|
|
it "returns only matching statuses" do
|
|
|
|
is_expected.to contain_exactly(stage_build, commit_status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-06 16:45:46 +00:00
|
|
|
describe '#groups' do
|
|
|
|
before do
|
|
|
|
create_job(:ci_build, name: 'rspec 0 2')
|
|
|
|
create_job(:ci_build, name: 'rspec 0 1')
|
|
|
|
create_job(:ci_build, name: 'spinach 0 1')
|
|
|
|
create_job(:commit_status, name: 'aaaaa')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an array of three groups' do
|
|
|
|
expect(stage.groups).to be_a Array
|
|
|
|
expect(stage.groups).to all(be_a Ci::Group)
|
|
|
|
expect(stage.groups.size).to eq 3
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns groups with correctly ordered statuses' do
|
|
|
|
expect(stage.groups.first.jobs.map(&:name))
|
|
|
|
.to eq ['aaaaa']
|
|
|
|
expect(stage.groups.second.jobs.map(&:name))
|
|
|
|
.to eq ['rspec 0 1', 'rspec 0 2']
|
|
|
|
expect(stage.groups.third.jobs.map(&:name))
|
|
|
|
.to eq ['spinach 0 1']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns groups with correct names' do
|
|
|
|
expect(stage.groups.map(&:name))
|
|
|
|
.to eq %w[aaaaa rspec spinach]
|
|
|
|
end
|
2017-06-13 07:22:22 +00:00
|
|
|
|
|
|
|
context 'when a name is nil on legacy pipelines' do
|
|
|
|
before do
|
|
|
|
pipeline.builds.first.update_attribute(:name, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an array of three groups' do
|
|
|
|
expect(stage.groups.map(&:name))
|
|
|
|
.to eq ['', 'aaaaa', 'rspec', 'spinach']
|
|
|
|
end
|
|
|
|
end
|
2017-05-06 16:45:46 +00:00
|
|
|
end
|
|
|
|
|
2016-12-20 10:00:56 +00:00
|
|
|
describe '#statuses_count' do
|
2016-12-20 10:26:24 +00:00
|
|
|
before do
|
2016-12-20 19:07:34 +00:00
|
|
|
create_job(:ci_build)
|
2016-12-20 10:26:24 +00:00
|
|
|
create_job(:ci_build, stage: 'other stage')
|
|
|
|
end
|
2016-12-20 10:00:56 +00:00
|
|
|
|
|
|
|
subject { stage.statuses_count }
|
|
|
|
|
2016-12-20 13:25:58 +00:00
|
|
|
it "counts statuses only from current stage" do
|
2016-12-20 10:00:56 +00:00
|
|
|
is_expected.to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-06 13:49:37 +00:00
|
|
|
describe '#builds' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build) }
|
|
|
|
let!(:commit_status) { create_job(:commit_status) }
|
|
|
|
|
|
|
|
subject { stage.builds }
|
|
|
|
|
|
|
|
it "returns only builds" do
|
|
|
|
is_expected.to contain_exactly(stage_build)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#status' do
|
|
|
|
subject { stage.status }
|
|
|
|
|
|
|
|
context 'if status is already defined' do
|
|
|
|
let(:stage) { build(:ci_stage, status: 'success') }
|
|
|
|
|
|
|
|
it "returns defined status" do
|
|
|
|
is_expected.to eq('success')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if status has to be calculated' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build, status: :failed) }
|
|
|
|
|
|
|
|
it "returns status of a build" do
|
|
|
|
is_expected.to eq('failed')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and builds are retried' do
|
|
|
|
let!(:new_build) { create_job(:ci_build, status: :success) }
|
|
|
|
|
2017-04-16 11:14:39 +00:00
|
|
|
before do
|
2020-09-07 15:09:04 +00:00
|
|
|
stage_build.update!(retried: true)
|
2017-04-16 11:14:39 +00:00
|
|
|
end
|
|
|
|
|
2016-12-06 13:49:37 +00:00
|
|
|
it "returns status of latest build" do
|
|
|
|
is_expected.to eq('success')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#detailed_status' do
|
2016-12-12 14:20:21 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { stage.detailed_status(user) }
|
2016-12-06 13:49:37 +00:00
|
|
|
|
|
|
|
context 'when build is created' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build, status: :created) }
|
|
|
|
|
|
|
|
it 'returns detailed status for created stage' do
|
2019-12-12 12:07:33 +00:00
|
|
|
expect(subject.text).to eq s_('CiStatusText|created')
|
2016-12-06 13:49:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is pending' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build, status: :pending) }
|
|
|
|
|
|
|
|
it 'returns detailed status for pending stage' do
|
2019-12-12 12:07:33 +00:00
|
|
|
expect(subject.text).to eq s_('CiStatusText|pending')
|
2016-12-06 13:49:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is running' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build, status: :running) }
|
|
|
|
|
|
|
|
it 'returns detailed status for running stage' do
|
2019-12-12 12:07:33 +00:00
|
|
|
expect(subject.text).to eq s_('CiStatus|running')
|
2016-12-06 13:49:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is successful' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build, status: :success) }
|
|
|
|
|
|
|
|
it 'returns detailed status for successful stage' do
|
2019-12-12 12:07:33 +00:00
|
|
|
expect(subject.text).to eq s_('CiStatusText|passed')
|
2016-12-06 13:49:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is failed' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build, status: :failed) }
|
|
|
|
|
|
|
|
it 'returns detailed status for failed stage' do
|
2019-12-12 12:07:33 +00:00
|
|
|
expect(subject.text).to eq s_('CiStatusText|failed')
|
2016-12-06 13:49:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is canceled' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build, status: :canceled) }
|
|
|
|
|
|
|
|
it 'returns detailed status for canceled stage' do
|
2019-12-12 12:07:33 +00:00
|
|
|
expect(subject.text).to eq s_('CiStatusText|canceled')
|
2016-12-06 13:49:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is skipped' do
|
|
|
|
let!(:stage_build) { create_job(:ci_build, status: :skipped) }
|
|
|
|
|
|
|
|
it 'returns detailed status for skipped stage' do
|
2019-12-12 12:07:33 +00:00
|
|
|
expect(subject.text).to eq s_('CiStatusText|skipped')
|
2016-12-06 13:49:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-09 12:07:52 +00:00
|
|
|
describe '#success?' do
|
|
|
|
context 'when stage is successful' do
|
|
|
|
before do
|
|
|
|
create_job(:ci_build, status: :success)
|
|
|
|
create_job(:generic_commit_status, status: :success)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is successful' do
|
|
|
|
expect(stage).to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when stage is not successful' do
|
|
|
|
before do
|
|
|
|
create_job(:ci_build, status: :failed)
|
|
|
|
create_job(:generic_commit_status, status: :success)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is not successful' do
|
|
|
|
expect(stage).not_to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#has_warnings?' do
|
|
|
|
context 'when stage has warnings' do
|
2017-01-18 10:07:12 +00:00
|
|
|
context 'when using memoized warnings flag' do
|
2017-01-19 17:49:14 +00:00
|
|
|
context 'when there are warnings' do
|
2019-10-01 12:05:59 +00:00
|
|
|
let(:stage) { build(:ci_stage, warnings: true) }
|
2017-01-18 10:07:12 +00:00
|
|
|
|
2017-03-08 09:58:41 +00:00
|
|
|
it 'returns true using memoized value' do
|
2017-01-19 17:49:14 +00:00
|
|
|
expect(stage).not_to receive(:statuses)
|
|
|
|
expect(stage).to have_warnings
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are no warnings' do
|
2019-10-01 12:05:59 +00:00
|
|
|
let(:stage) { build(:ci_stage, warnings: false) }
|
2017-01-19 17:49:14 +00:00
|
|
|
|
2017-03-08 09:58:41 +00:00
|
|
|
it 'returns false using memoized value' do
|
2017-01-19 17:49:14 +00:00
|
|
|
expect(stage).not_to receive(:statuses)
|
|
|
|
expect(stage).not_to have_warnings
|
2017-03-08 09:58:41 +00:00
|
|
|
end
|
|
|
|
end
|
2017-01-09 12:07:52 +00:00
|
|
|
end
|
|
|
|
|
2017-01-18 10:07:12 +00:00
|
|
|
context 'when calculating warnings from statuses' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :failed, :allowed_to_fail,
|
|
|
|
stage: stage_name, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
2017-01-19 17:49:14 +00:00
|
|
|
it 'has warnings calculated from statuses' do
|
|
|
|
expect(stage).to receive(:statuses).and_call_original
|
2017-01-18 10:07:12 +00:00
|
|
|
expect(stage).to have_warnings
|
|
|
|
end
|
2017-01-09 12:07:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when stage does not have warnings' do
|
|
|
|
before do
|
|
|
|
create(:ci_build, :success, stage: stage_name,
|
|
|
|
pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
2017-01-19 17:49:14 +00:00
|
|
|
it 'does not have warnings calculated from statuses' do
|
|
|
|
expect(stage).to receive(:statuses).and_call_original
|
2017-01-09 12:07:52 +00:00
|
|
|
expect(stage).not_to have_warnings
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-06 16:45:46 +00:00
|
|
|
def create_job(type, status: 'success', stage: stage_name, **opts)
|
|
|
|
create(type, pipeline: pipeline, stage: stage, status: status, **opts)
|
2016-12-06 13:49:37 +00:00
|
|
|
end
|
2019-05-02 18:27:35 +00:00
|
|
|
|
|
|
|
it_behaves_like 'manual playable stage', :ci_stage
|
2016-12-06 13:49:37 +00:00
|
|
|
end
|