summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/seeders/ci/runner/runner_fleet_seeder_spec.rb
blob: fe52b586d4993cbd57f6c9b793df90beda448d3b (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
# frozen_string_literal: true

require 'spec_helper'

NULL_LOGGER = Gitlab::JsonLogger.new('/dev/null')

RSpec.describe ::Gitlab::Seeders::Ci::Runner::RunnerFleetSeeder, feature_category: :runner_fleet do
  let_it_be(:user) { create(:user, :admin, username: 'test-admin') }

  subject(:seeder) do
    described_class.new(NULL_LOGGER,
                        username: user.username,
                        registration_prefix: registration_prefix,
                        runner_count: runner_count)
  end

  describe '#seed', :enable_admin_mode do
    subject(:seed) { seeder.seed }

    let(:runner_count) { 20 }
    let(:registration_prefix) { 'prefix-' }
    let(:runner_releases_url) do
      ::Gitlab::CurrentSettings.current_application_settings.public_runner_releases_url
    end

    before do
      WebMock.stub_request(:get, runner_releases_url).to_return(
        body: '[]',
        status: 200,
        headers: { 'Content-Type' => 'application/json' }
      )
    end

    it 'creates expected hierarchy', :aggregate_failures do
      expect { seed }.to change { Ci::Runner.count }.by(runner_count)
        .and change { Ci::Runner.instance_type.count }.by(1)
        .and change { Project.count }.by(3)
        .and change { Group.count }.by(6)

      expect(Group.search(registration_prefix)).to contain_exactly(
        an_object_having_attributes(name: "#{registration_prefix}top-level group 1"),
        an_object_having_attributes(name: "#{registration_prefix}top-level group 2"),
        an_object_having_attributes(name: "#{registration_prefix}group 1.1"),
        an_object_having_attributes(name: "#{registration_prefix}group 1.1.1"),
        an_object_having_attributes(name: "#{registration_prefix}group 1.1.2"),
        an_object_having_attributes(name: "#{registration_prefix}group 2.1")
      )

      expect(Project.search(registration_prefix)).to contain_exactly(
        an_object_having_attributes(name: "#{registration_prefix}project 1.1.1.1"),
        an_object_having_attributes(name: "#{registration_prefix}project 1.1.2.1"),
        an_object_having_attributes(name: "#{registration_prefix}project 2.1.1")
      )

      project_1_1_1_1 = Project.find_by_name("#{registration_prefix}project 1.1.1.1")
      project_1_1_2_1 = Project.find_by_name("#{registration_prefix}project 1.1.2.1")
      project_2_1_1 = Project.find_by_name("#{registration_prefix}project 2.1.1")
      expect(seed).to contain_exactly(
        { project_id: project_1_1_1_1.id, runner_ids: an_instance_of(Array) },
        { project_id: project_1_1_2_1.id, runner_ids: an_instance_of(Array) },
        { project_id: project_2_1_1.id, runner_ids: an_instance_of(Array) }
      )
      seed.each do |project|
        expect(project[:runner_ids].length).to be_between(0, 5)
        expect(Project.find(project[:project_id]).all_available_runners.ids).to include(*project[:runner_ids])
        expect(::Ci::Pipeline.for_project(project[:runner_ids])).to be_empty
        expect(::Ci::Build.where(runner_id: project[:runner_ids])).to be_empty
      end
    end
  end
end