summaryrefslogtreecommitdiff
path: root/spec/migrations/schedule_delete_orphaned_deployments_spec.rb
blob: 618958a3d90b6197a3584dccb93e4fe445005ffc (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
# frozen_string_literal: true

require 'spec_helper'

require_migration!

RSpec.describe ScheduleDeleteOrphanedDeployments, :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 '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