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

reschedule_delete_orphaned_deployments_spec.rb « migrations « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eb91602388cbae7779d3541c8b575eee56e452d1 (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
# frozen_string_literal: true

require 'spec_helper'

require_migration!

RSpec.describe RescheduleDeleteOrphanedDeployments, :sidekiq, schema: 20210617161348 do
  let!(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') }
  let!(:project) { table(:projects).create!(namespace_id: namespace.id) }
  let!(:environment) { table(:environments).create!(name: 'production', slug: 'production', project_id: project.id) }
  let(:background_migration_jobs) { table(:background_migration_jobs) }

  before do
    create_deployment!(environment.id, project.id)
    create_deployment!(environment.id, project.id)
    create_deployment!(environment.id, project.id)
    create_deployment!(non_existing_record_id, project.id)
    create_deployment!(non_existing_record_id, project.id)
    create_deployment!(non_existing_record_id, project.id)
    create_deployment!(non_existing_record_id, project.id)

    stub_const("#{described_class}::BATCH_SIZE", 1)
  end

  it 'steal existing background migration jobs' do
    expect(Gitlab::BackgroundMigration).to receive(:steal).with('DeleteOrphanedDeployments')

    migrate!
  end

  it 'cleans up background migration jobs tracking records' do
    old_successful_job = background_migration_jobs.create!(
      class_name: 'DeleteOrphanedDeployments',
      status: Gitlab::Database::BackgroundMigrationJob.statuses[:succeeded],
      arguments: [table(:deployments).minimum(:id), table(:deployments).minimum(:id)]
    )

    old_pending_job = background_migration_jobs.create!(
      class_name: 'DeleteOrphanedDeployments',
      status: Gitlab::Database::BackgroundMigrationJob.statuses[:pending],
      arguments: [table(:deployments).maximum(:id), table(:deployments).maximum(:id)]
    )

    migrate!

    expect { old_successful_job.reload }.to raise_error(ActiveRecord::RecordNotFound)
    expect { old_pending_job.reload }.to raise_error(ActiveRecord::RecordNotFound)
  end

  it 'schedules DeleteOrphanedDeployments background jobs' do
    Sidekiq::Testing.fake! do
      freeze_time do
        migrate!

        expect(BackgroundMigrationWorker.jobs.size).to eq(7)
        table(:deployments).find_each do |deployment|
          expect(described_class::MIGRATION).to be_scheduled_migration(deployment.id, deployment.id)
        end
      end
    end
  end

  def create_deployment!(environment_id, project_id)
    table(:deployments).create!(
      environment_id: environment_id,
      project_id: project_id,
      ref: 'master',
      tag: false,
      sha: 'x',
      status: 1,
      iid: table(:deployments).count + 1)
  end
end