debian-mirror-gitlab/spec/models/ci/runner_machine_build_spec.rb
2023-05-27 22:25:52 +05:30

100 lines
3.5 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::RunnerMachineBuild, model: true, feature_category: :runner_fleet do
let_it_be(:runner) { create(:ci_runner) }
let_it_be(:runner_machine) { create(:ci_runner_machine, runner: runner) }
let_it_be(:build) { create(:ci_build, runner_machine: runner_machine) }
it { is_expected.to belong_to(:build) }
it { is_expected.to belong_to(:runner_machine) }
describe 'partitioning' do
context 'with build' do
let(:build) { FactoryBot.build(:ci_build, partition_id: ci_testing_partition_id) }
let(:runner_machine_build) { FactoryBot.build(:ci_runner_machine_build, build: build) }
it 'sets partition_id to the current partition value' do
expect { runner_machine_build.valid? }.to change { runner_machine_build.partition_id }
.to(ci_testing_partition_id)
end
context 'when it is already set' do
let(:runner_machine_build) { FactoryBot.build(:ci_runner_machine_build, partition_id: 125) }
it 'does not change the partition_id value' do
expect { runner_machine_build.valid? }.not_to change { runner_machine_build.partition_id }
end
end
end
context 'without build' do
let(:runner_machine_build) { FactoryBot.build(:ci_runner_machine_build, build: nil) }
it { is_expected.to validate_presence_of(:partition_id) }
it 'does not change the partition_id value' do
expect { runner_machine_build.valid? }.not_to change { runner_machine_build.partition_id }
end
end
end
describe 'ci_sliding_list partitioning' do
let(:connection) { described_class.connection }
let(:partition_manager) { Gitlab::Database::Partitioning::PartitionManager.new(described_class) }
let(:partitioning_strategy) { described_class.partitioning_strategy }
it { expect(partitioning_strategy.missing_partitions).to be_empty }
it { expect(partitioning_strategy.extra_partitions).to be_empty }
it { expect(partitioning_strategy.current_partitions).to include partitioning_strategy.initial_partition }
it { expect(partitioning_strategy.active_partition).to be_present }
end
context 'loose foreign key on p_ci_runner_machine_builds.runner_machine_id' do # rubocop:disable RSpec/ContextWording
it_behaves_like 'cleanup by a loose foreign key' do
let!(:parent) { create(:ci_runner_machine) }
let!(:model) { create(:ci_runner_machine_build, runner_machine: parent) }
end
end
describe '.for_build' do
subject(:for_build) { described_class.for_build(build_id) }
context 'with valid build_id' do
let(:build_id) { build.id }
it { is_expected.to contain_exactly(described_class.find_by_build_id(build_id)) }
end
context 'with valid build_ids' do
let(:build2) { create(:ci_build, runner_machine: runner_machine) }
let(:build_id) { [build, build2] }
it { is_expected.to eq(described_class.where(build_id: build_id)) }
end
context 'with non-existeng build_id' do
let(:build_id) { non_existing_record_id }
it { is_expected.to be_empty }
end
end
describe '.pluck_runner_machine_id_and_build_id' do
subject { scope.pluck_build_id_and_runner_machine_id }
context 'with default scope' do
let(:scope) { described_class }
it { is_expected.to eq({ build.id => runner_machine.id }) }
end
context 'with scope excluding build' do
let(:scope) { described_class.where(build_id: non_existing_record_id) }
it { is_expected.to be_empty }
end
end
end