2019-10-25 05:06:06 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-09 15:45:49 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Ci::BuildPresenter do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2017-01-09 15:45:49 -05:00
|
|
|
let(:pipeline) { create(:ci_pipeline, project: project) }
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline) }
|
|
|
|
|
2017-01-10 17:41:04 -05:00
|
|
|
subject(:presenter) do
|
2017-01-09 15:45:49 -05:00
|
|
|
described_class.new(build)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'inherits from Gitlab::View::Presenter::Delegated' do
|
2018-05-16 16:58:20 -04:00
|
|
|
expect(described_class.ancestors).to include(Gitlab::View::Presenter::Delegated)
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#initialize' do
|
|
|
|
it 'takes a build and optional params' do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect { presenter }.not_to raise_error
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes build' do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter.build).to eq(build)
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'forwards missing methods to build' do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter.ref).to eq('master')
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#erased_by_user?' do
|
|
|
|
it 'takes a build and optional params' do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter).not_to be_erased_by_user
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#erased_by_name' do
|
|
|
|
context 'when build is not erased' do
|
|
|
|
before do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter).to receive(:erased_by_user?).and_return(false)
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil' do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter.erased_by_name).to be_nil
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
2017-01-10 17:41:04 -05:00
|
|
|
|
2017-01-09 15:45:49 -05:00
|
|
|
context 'when build is erased' do
|
|
|
|
before do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter).to receive(:erased_by_user?).and_return(true)
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(build).to receive(:erased_by)
|
|
|
|
.and_return(double(:user, name: 'John Doe'))
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the name of the eraser' do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter.erased_by_name).to eq('John Doe')
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-05 15:10:52 -04:00
|
|
|
describe '#status_title' do
|
2017-04-06 09:32:56 -04:00
|
|
|
context 'when build is auto-canceled' do
|
2017-04-05 15:10:52 -04:00
|
|
|
before do
|
2017-04-06 09:32:56 -04:00
|
|
|
expect(build).to receive(:auto_canceled?).and_return(true)
|
|
|
|
expect(build).to receive(:auto_canceled_by_id).and_return(1)
|
2017-04-05 15:10:52 -04:00
|
|
|
end
|
|
|
|
|
2017-04-06 09:53:02 -04:00
|
|
|
it 'shows that the build is auto-canceled' do
|
2017-04-06 09:32:56 -04:00
|
|
|
status_title = presenter.status_title
|
2017-04-05 15:10:52 -04:00
|
|
|
|
2017-04-06 09:32:56 -04:00
|
|
|
expect(status_title).to include('auto-canceled')
|
|
|
|
expect(status_title).to include('Pipeline #1')
|
2017-04-05 15:10:52 -04:00
|
|
|
end
|
2017-04-06 09:32:56 -04:00
|
|
|
end
|
2017-04-05 15:10:52 -04:00
|
|
|
|
2018-04-05 17:04:42 -04:00
|
|
|
context 'when build failed' do
|
|
|
|
let(:build) { create(:ci_build, :failed, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'returns the reason of failure' do
|
|
|
|
status_title = presenter.status_title
|
|
|
|
|
2018-08-09 07:05:13 -04:00
|
|
|
expect(status_title).to eq('Failed - (unknown failure)')
|
2017-04-06 09:32:56 -04:00
|
|
|
end
|
2018-04-05 17:04:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build has failed && retried' do
|
|
|
|
let(:build) { create(:ci_build, :failed, :retried, pipeline: pipeline) }
|
2017-04-05 15:10:52 -04:00
|
|
|
|
2018-04-05 17:04:42 -04:00
|
|
|
it 'does not include retried title' do
|
|
|
|
status_title = presenter.status_title
|
|
|
|
|
|
|
|
expect(status_title).not_to include('(retried)')
|
2018-08-09 07:05:13 -04:00
|
|
|
expect(status_title).to eq('Failed - (unknown failure)')
|
2018-04-05 17:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build has failed and is allowed to' do
|
|
|
|
let(:build) { create(:ci_build, :failed, :allowed_to_fail, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'returns the reason of failure' do
|
|
|
|
status_title = presenter.status_title
|
|
|
|
|
2018-08-09 07:05:13 -04:00
|
|
|
expect(status_title).to eq('Failed - (unknown failure)')
|
2018-04-05 17:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'For any other build' do
|
|
|
|
let(:build) { create(:ci_build, :success, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'returns the status' do
|
|
|
|
tooltip_description = presenter.status_title
|
|
|
|
|
|
|
|
expect(tooltip_description).to eq('Success')
|
2017-04-05 15:10:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-09 15:45:49 -05:00
|
|
|
describe 'quack like a Ci::Build permission-wise' do
|
|
|
|
context 'user is not allowed' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, public_builds: false) }
|
2017-01-09 15:45:49 -05:00
|
|
|
|
|
|
|
it 'returns false' do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter.can?(nil, :read_build)).to be_falsy
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user is allowed' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2017-01-09 15:45:49 -05:00
|
|
|
|
|
|
|
it 'returns true' do
|
2017-01-10 17:41:04 -05:00
|
|
|
expect(presenter.can?(nil, :read_build)).to be_truthy
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-04 09:21:47 -04:00
|
|
|
|
|
|
|
describe '#trigger_variables' do
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline, trigger_request: trigger_request) }
|
|
|
|
let(:trigger) { create(:ci_trigger, project: project) }
|
|
|
|
let(:trigger_request) { create(:ci_trigger_request, pipeline: pipeline, trigger: trigger) }
|
|
|
|
|
|
|
|
context 'when variable is stored in ci_pipeline_variables' do
|
|
|
|
let!(:pipeline_variable) { create(:ci_pipeline_variable, pipeline: pipeline) }
|
|
|
|
|
|
|
|
context 'when pipeline is triggered by trigger API' do
|
|
|
|
it 'returns variables' do
|
|
|
|
expect(presenter.trigger_variables).to eq([pipeline_variable.to_runner_variable])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pipeline is not triggered by trigger API' do
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'does not return variables' do
|
|
|
|
expect(presenter.trigger_variables).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when variable is stored in ci_trigger_requests.variables' do
|
|
|
|
before do
|
|
|
|
trigger_request.update_attribute(:variables, { 'TRIGGER_KEY_1' => 'TRIGGER_VALUE_1' } )
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns variables' do
|
|
|
|
expect(presenter.trigger_variables).to eq(trigger_request.user_variables)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-05 17:04:42 -04:00
|
|
|
|
|
|
|
describe '#tooltip_message' do
|
|
|
|
context 'When build has failed' do
|
|
|
|
let(:build) { create(:ci_build, :script_failure, pipeline: pipeline) }
|
|
|
|
|
|
|
|
it 'returns the reason of failure' do
|
|
|
|
tooltip = subject.tooltip_message
|
|
|
|
|
2018-08-09 07:05:13 -04:00
|
|
|
expect(tooltip).to eq("#{build.name} - failed - (script failure)")
|
2018-04-05 17:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'When build has failed and retried' do
|
|
|
|
let(:build) { create(:ci_build, :script_failure, :retried, pipeline: pipeline) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'includes the reason of failure and the retried title' do
|
2018-04-05 17:04:42 -04:00
|
|
|
tooltip = subject.tooltip_message
|
|
|
|
|
2018-08-09 07:05:13 -04:00
|
|
|
expect(tooltip).to eq("#{build.name} - failed - (script failure) (retried)")
|
2018-04-05 17:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'When build has failed and is allowed to' do
|
|
|
|
let(:build) { create(:ci_build, :script_failure, :allowed_to_fail, pipeline: pipeline) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'includes the reason of failure' do
|
2018-04-05 17:04:42 -04:00
|
|
|
tooltip = subject.tooltip_message
|
|
|
|
|
2018-08-09 07:05:13 -04:00
|
|
|
expect(tooltip).to eq("#{build.name} - failed - (script failure) (allowed to fail)")
|
2018-04-05 17:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'For any other build (no retried)' do
|
|
|
|
let(:build) { create(:ci_build, :success, pipeline: pipeline) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'includes build name and status' do
|
2018-04-05 17:04:42 -04:00
|
|
|
tooltip = subject.tooltip_message
|
|
|
|
|
|
|
|
expect(tooltip).to eq("#{build.name} - passed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'For any other build (retried)' do
|
|
|
|
let(:build) { create(:ci_build, :success, :retried, pipeline: pipeline) }
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'includes build name and status' do
|
2018-04-05 17:04:42 -04:00
|
|
|
tooltip = subject.tooltip_message
|
|
|
|
|
|
|
|
expect(tooltip).to eq("#{build.name} - passed (retried)")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-19 03:20:53 -04:00
|
|
|
|
2018-10-03 02:05:50 -04:00
|
|
|
describe '#execute_in' do
|
|
|
|
subject { presenter.execute_in }
|
|
|
|
|
|
|
|
context 'when build is scheduled' do
|
|
|
|
context 'when schedule is not expired' do
|
|
|
|
let(:build) { create(:ci_build, :scheduled) }
|
|
|
|
|
|
|
|
it 'returns execution time' do
|
2020-08-31 11:10:41 -04:00
|
|
|
freeze_time do
|
2018-10-06 07:21:47 -04:00
|
|
|
is_expected.to be_like_time(60.0)
|
2018-10-03 02:05:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when schedule is expired' do
|
|
|
|
let(:build) { create(:ci_build, :expired_scheduled) }
|
|
|
|
|
|
|
|
it 'returns execution time' do
|
2020-08-31 11:10:41 -04:00
|
|
|
freeze_time do
|
2018-10-03 02:05:50 -04:00
|
|
|
is_expected.to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when build is not delayed' do
|
|
|
|
let(:build) { create(:ci_build) }
|
|
|
|
|
|
|
|
it 'does not return execution time' do
|
2020-08-31 11:10:41 -04:00
|
|
|
freeze_time do
|
2018-10-03 02:05:50 -04:00
|
|
|
is_expected.to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-19 03:20:53 -04:00
|
|
|
describe '#callout_failure_message' do
|
2018-05-24 16:06:58 -04:00
|
|
|
let(:build) { create(:ci_build, :failed, :api_failure) }
|
2018-04-19 03:20:53 -04:00
|
|
|
|
|
|
|
it 'returns a verbose failure reason' do
|
|
|
|
description = subject.callout_failure_message
|
2018-05-24 16:06:58 -04:00
|
|
|
expect(description).to eq('There has been an API failure, please try again')
|
2018-04-19 03:20:53 -04:00
|
|
|
end
|
|
|
|
end
|
2017-01-09 15:45:49 -05:00
|
|
|
end
|