Welcome to mirror list, hosted at ThFree Co, Russian Federation.

runner_manager_build_spec.rb « ci « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3a381313b766d64adc7234ca582b299d80243040 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::RunnerManagerBuild, model: true, feature_category: :runner_fleet do
  let_it_be(:runner) { create(:ci_runner) }
  let_it_be(:runner_manager) { create(:ci_runner_machine, runner: runner) }
  let_it_be(:build) { create(:ci_build, runner_manager: runner_manager) }

  it { is_expected.to belong_to(:build) }
  it { is_expected.to belong_to(:runner_manager) }

  describe 'partitioning' do
    context 'with build' do
      let(:build) { FactoryBot.build(:ci_build, partition_id: ci_testing_partition_id) }
      let(:runner_manager_build) { FactoryBot.build(:ci_runner_machine_build, build: build) }

      it 'sets partition_id to the current partition value' do
        expect { runner_manager_build.valid? }.to change { runner_manager_build.partition_id }
          .to(ci_testing_partition_id)
      end

      context 'when it is already set' do
        let(:runner_manager_build) { FactoryBot.build(:ci_runner_machine_build, partition_id: 125) }

        it 'does not change the partition_id value' do
          expect { runner_manager_build.valid? }.not_to change { runner_manager_build.partition_id }
        end
      end
    end

    context 'without build' do
      let(:runner_manager_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_manager_build.valid? }.not_to change { runner_manager_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_manager_builds.runner_manager_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_manager: 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_manager: runner_manager) }
      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_manager_id_and_build_id' do
    subject { scope.pluck_build_id_and_runner_manager_id }

    context 'with default scope' do
      let(:scope) { described_class }

      it { is_expected.to eq({ build.id => runner_manager.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