2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-20 21:58:43 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Ci::BuildMetadata do
|
2020-02-12 10:09:37 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:project) { create(:project, :repository, group: group, build_timeout: 2000) }
|
2018-03-20 21:58:43 -04:00
|
|
|
|
2020-02-12 10:09:37 -05:00
|
|
|
let_it_be(:pipeline) do
|
2018-03-20 21:58:43 -04:00
|
|
|
create(:ci_pipeline, project: project,
|
|
|
|
sha: project.commit.id,
|
|
|
|
ref: project.default_branch,
|
|
|
|
status: 'success')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline) }
|
2018-09-02 10:35:15 -04:00
|
|
|
let(:metadata) { build.metadata }
|
2018-03-20 21:58:43 -04:00
|
|
|
|
2018-12-04 05:24:21 -05:00
|
|
|
it_behaves_like 'having unique enum values'
|
|
|
|
|
2018-03-26 11:47:46 -04:00
|
|
|
describe '#update_timeout_state' do
|
2018-09-02 10:35:15 -04:00
|
|
|
subject { metadata }
|
2018-03-20 21:58:43 -04:00
|
|
|
|
2019-09-16 14:06:05 -04:00
|
|
|
shared_examples 'sets timeout' do |source, timeout|
|
|
|
|
it 'sets project_timeout_source' do
|
|
|
|
expect { subject.update_timeout_state }.to change { subject.reload.timeout_source }.to(source)
|
2018-03-20 21:58:43 -04:00
|
|
|
end
|
|
|
|
|
2019-09-16 14:06:05 -04:00
|
|
|
it 'sets project timeout' do
|
|
|
|
expect { subject.update_timeout_state }.to change { subject.reload.timeout }.to(timeout)
|
2018-03-20 21:58:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-16 14:06:05 -04:00
|
|
|
context 'when project timeout is set' do
|
|
|
|
context 'when runner is assigned to the job' do
|
|
|
|
before do
|
|
|
|
build.update!(runner: runner)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner timeout is not set' do
|
|
|
|
let(:runner) { create(:ci_runner, maximum_timeout: nil) }
|
|
|
|
|
|
|
|
it_behaves_like 'sets timeout', 'project_timeout_source', 2000
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner timeout is lower than project timeout' do
|
|
|
|
let(:runner) { create(:ci_runner, maximum_timeout: 1900) }
|
|
|
|
|
|
|
|
it_behaves_like 'sets timeout', 'runner_timeout_source', 1900
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when runner timeout is higher than project timeout' do
|
|
|
|
let(:runner) { create(:ci_runner, maximum_timeout: 2100) }
|
|
|
|
|
|
|
|
it_behaves_like 'sets timeout', 'project_timeout_source', 2000
|
|
|
|
end
|
2018-03-20 21:58:43 -04:00
|
|
|
end
|
|
|
|
|
2019-09-16 14:06:05 -04:00
|
|
|
context 'when job timeout is set' do
|
|
|
|
context 'when job timeout is higher than project timeout' do
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline, options: { job_timeout: 3000 }) }
|
2018-03-20 21:58:43 -04:00
|
|
|
|
2019-09-16 14:06:05 -04:00
|
|
|
it_behaves_like 'sets timeout', 'job_timeout_source', 3000
|
2018-03-20 21:58:43 -04:00
|
|
|
end
|
|
|
|
|
2019-09-16 14:06:05 -04:00
|
|
|
context 'when job timeout is lower than project timeout' do
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline, options: { job_timeout: 1000 }) }
|
|
|
|
|
|
|
|
it_behaves_like 'sets timeout', 'job_timeout_source', 1000
|
2018-03-20 21:58:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-16 14:06:05 -04:00
|
|
|
context 'when both runner and job timeouts are set' do
|
|
|
|
before do
|
2020-09-07 11:09:04 -04:00
|
|
|
build.update!(runner: runner)
|
2019-09-16 14:06:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when job timeout is higher than runner timeout' do
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline, options: { job_timeout: 3000 }) }
|
|
|
|
let(:runner) { create(:ci_runner, maximum_timeout: 2100) }
|
2018-03-20 21:58:43 -04:00
|
|
|
|
2019-09-16 14:06:05 -04:00
|
|
|
it_behaves_like 'sets timeout', 'runner_timeout_source', 2100
|
2018-03-20 21:58:43 -04:00
|
|
|
end
|
|
|
|
|
2019-09-16 14:06:05 -04:00
|
|
|
context 'when job timeout is lower than runner timeout' do
|
|
|
|
let(:build) { create(:ci_build, pipeline: pipeline, options: { job_timeout: 1900 }) }
|
|
|
|
let(:runner) { create(:ci_runner, maximum_timeout: 2100) }
|
|
|
|
|
|
|
|
it_behaves_like 'sets timeout', 'job_timeout_source', 1900
|
2018-03-20 21:58:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-22 08:08:47 -04:00
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
context 'when attributes are valid' do
|
|
|
|
it 'returns no errors' do
|
|
|
|
metadata.secrets = {
|
|
|
|
DATABASE_PASSWORD: {
|
|
|
|
vault: {
|
|
|
|
engine: { name: 'kv-v2', path: 'kv-v2' },
|
|
|
|
path: 'production/db',
|
|
|
|
field: 'password'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-11 11:11:19 -04:00
|
|
|
metadata.id_tokens = {
|
|
|
|
TEST_JWT_TOKEN: {
|
|
|
|
id_token: {
|
|
|
|
aud: 'https://gitlab.test'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 08:08:47 -04:00
|
|
|
|
|
|
|
expect(metadata).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when data is invalid' do
|
|
|
|
it 'returns errors' do
|
|
|
|
metadata.secrets = { DATABASE_PASSWORD: { vault: {} } }
|
2022-08-11 11:11:19 -04:00
|
|
|
metadata.id_tokens = { TEST_JWT_TOKEN: { id_token: { aud: nil } } }
|
2020-06-22 08:08:47 -04:00
|
|
|
|
|
|
|
aggregate_failures do
|
|
|
|
expect(metadata).to be_invalid
|
2022-08-11 11:11:19 -04:00
|
|
|
expect(metadata.errors.full_messages).to contain_exactly(
|
|
|
|
'Secrets must be a valid json schema',
|
|
|
|
'Id tokens must be a valid json schema'
|
|
|
|
)
|
2020-06-22 08:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-10-28 14:14:18 -04:00
|
|
|
|
|
|
|
describe 'set_cancel_gracefully' do
|
|
|
|
it 'sets cancel_gracefully' do
|
|
|
|
build.set_cancel_gracefully
|
|
|
|
|
|
|
|
expect(build.cancel_gracefully?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(build.cancel_gracefully?).to be false
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 07:18:17 -05:00
|
|
|
|
|
|
|
context 'loose foreign key on ci_builds_metadata.project_id' do
|
|
|
|
it_behaves_like 'cleanup by a loose foreign key' do
|
|
|
|
let!(:parent) { create(:project) }
|
|
|
|
let!(:model) { create(:ci_build_metadata, project: parent) }
|
|
|
|
end
|
|
|
|
end
|
2018-03-20 21:58:43 -04:00
|
|
|
end
|