1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::PendingBuild, feature_category: :continuous_integration do
let_it_be_with_refind(:project) { create(:project) }
let_it_be_with_refind(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, :created, pipeline: pipeline) }
describe 'associations' do
it { is_expected.to belong_to :project }
it { is_expected.to belong_to :build }
it { is_expected.to belong_to :namespace }
end
describe 'scopes' do
describe '.with_instance_runners' do
subject(:pending_builds) { described_class.with_instance_runners }
let!(:pending_build_1) { create(:ci_pending_build, instance_runners_enabled: false) }
context 'when pending builds cannot be picked up by runner' do
it 'returns an empty collection of pending builds' do
expect(pending_builds).to be_empty
end
end
context 'when pending builds can be picked up by runner' do
let!(:pending_build_2) { create(:ci_pending_build) }
it 'returns matching pending builds' do
expect(pending_builds).to contain_exactly(pending_build_2)
end
end
end
describe '.for_tags' do
subject(:pending_builds) { described_class.for_tags(tag_ids) }
let_it_be(:pending_build_with_tags) { create(:ci_pending_build, tag_ids: [1, 2]) }
let_it_be(:pending_build_without_tags) { create(:ci_pending_build) }
context 'when tag_ids match pending builds' do
let(:tag_ids) { [1, 2] }
it 'returns matching pending builds' do
expect(pending_builds).to contain_exactly(pending_build_with_tags, pending_build_without_tags)
end
end
context 'when tag_ids does not match pending builds' do
let(:tag_ids) { [non_existing_record_id] }
it 'returns matching pending builds without tags' do
expect(pending_builds).to contain_exactly(pending_build_without_tags)
end
end
context 'when tag_ids is not provided' do
context 'with a nil value' do
let(:tag_ids) { nil }
it 'returns matching pending builds without tags' do
expect(pending_builds).to contain_exactly(pending_build_without_tags)
end
end
context 'with an empty array' do
let(:tag_ids) { [] }
it 'returns matching pending builds without tags' do
expect(pending_builds).to contain_exactly(pending_build_without_tags)
end
end
end
end
end
describe '.upsert_from_build!' do
context 'another pending entry does not exist' do
it 'creates a new pending entry' do
result = described_class.upsert_from_build!(build)
expect(result.rows.dig(0, 0)).to eq build.id
expect(build.reload.queuing_entry).to be_present
end
end
context 'when another queuing entry exists for given build' do
before do
create(:ci_pending_build, build: build, project: project)
end
it 'returns a build id as a result' do
result = described_class.upsert_from_build!(build)
expect(result.rows.dig(0, 0)).to eq build.id
end
end
context 'when project does not have shared runners enabled' do
before do
project.update!(shared_runners_enabled: false)
end
it 'sets instance_runners_enabled to false' do
described_class.upsert_from_build!(build)
expect(described_class.last.instance_runners_enabled).to be_falsey
end
end
context 'when project has shared runner' do
let_it_be(:runner) { create(:ci_runner, :instance) }
before do
project.update!(shared_runners_enabled: true)
end
it 'sets instance_runners_enabled to true' do
described_class.upsert_from_build!(build)
expect(described_class.last.instance_runners_enabled).to be_truthy
end
context 'when project is about to be deleted' do
before do
project.update!(pending_delete: true)
end
it 'sets instance_runners_enabled to false' do
described_class.upsert_from_build!(build)
expect(described_class.last.instance_runners_enabled).to be_falsey
end
end
context 'when builds are disabled' do
before do
project.project_feature.update!(builds_access_level: false)
end
it 'sets instance_runners_enabled to false' do
described_class.upsert_from_build!(build)
expect(described_class.last.instance_runners_enabled).to be_falsey
end
end
end
context 'when build has tags' do
let!(:build) { create(:ci_build, :tags) }
subject(:ci_pending_build) { described_class.last }
it 'sets tag_ids' do
described_class.upsert_from_build!(build)
expect(ci_pending_build.tag_ids).to eq(build.tags_ids)
end
end
context 'when a build project is nested in a subgroup' do
let_it_be(:group) { create(:group, :with_hierarchy, depth: 2, children: 1) }
let_it_be_with_refind(:project) { create(:project, namespace: group.descendants.first) }
let_it_be_with_refind(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, :created, pipeline: pipeline) }
subject(:latest_pending_build) { described_class.last }
context 'when build can be picked by a group runner' do
before do
project.update!(group_runners_enabled: true)
end
it 'denormalizes namespace traversal ids' do
described_class.upsert_from_build!(build)
expect(latest_pending_build.namespace_traversal_ids).not_to be_empty
expect(latest_pending_build.namespace_traversal_ids).to eq [group.id, project.namespace.id]
end
end
context 'when build can not be picked by a group runner' do
before do
project.update!(group_runners_enabled: false)
end
it 'creates an empty namespace traversal ids array' do
described_class.upsert_from_build!(build)
expect(latest_pending_build.namespace_traversal_ids).to be_empty
end
end
end
end
describe '.namespace_transfer_params' do
let(:group) { create(:group) }
let(:new_parent_group) { create(:group) }
let(:expected_transfer_params) do
{
namespace_traversal_ids: [new_parent_group.id, group.id],
namespace_id: group.id
}
end
it 'updates all pending builds with namespace_transfer_params', :aggregate_failures do
expect do
group.update!(parent: new_parent_group)
# reload is called to make sure traversal_ids are reloaded
end.to change { Ci::PendingBuild.namespace_transfer_params(group.reload) }.to(expected_transfer_params)
.and not_change { Ci::PendingBuild.namespace_transfer_params(new_parent_group.reload) }
end
end
describe 'partitioning' do
include Ci::PartitioningHelpers
before do
stub_current_partition_id(ci_testing_partition_id)
end
let(:new_pipeline ) { create(:ci_pipeline, project: pipeline.project) }
let(:new_build) { create(:ci_build, pipeline: new_pipeline) }
it 'assigns the same partition id as the one that build has', :aggregate_failures do
expect(new_build.partition_id).to eq ci_testing_partition_id
described_class.upsert_from_build!(build)
described_class.upsert_from_build!(new_build)
expect(build.reload.queuing_entry.partition_id).to eq pipeline.partition_id
expect(new_build.reload.queuing_entry.partition_id).to eq ci_testing_partition_id
end
end
it_behaves_like 'cleanup by a loose foreign key' do
let!(:parent) { create(:namespace) }
let!(:model) { create(:ci_pending_build, namespace: parent) }
end
it_behaves_like 'cleanup by a loose foreign key' do
let!(:parent) { create(:project) }
let!(:model) { create(:ci_pending_build, project: parent) }
end
end
|