summaryrefslogtreecommitdiff
path: root/spec/migrations/schedule_migrate_security_scans_spec.rb
blob: 29e4e2b5cacf22cb42fd5fd7ef61ff102986d1b3 (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
# frozen_string_literal: true

require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200217225719_schedule_migrate_security_scans.rb')

# rubocop: disable RSpec/FactoriesInMigrationSpecs
describe ScheduleMigrateSecurityScans, :sidekiq do
  let(:migration) { described_class.new }
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:builds) { table(:ci_builds) }
  let(:job_artifacts) { table(:ci_job_artifacts) }

  let(:namespace) { namespaces.create!(name: "foo", path: "bar") }
  let(:project) { projects.create!(namespace_id: namespace.id) }
  let(:build) { builds.create! }

  before do
    stub_const("#{described_class.name}::BATCH_SIZE", 1)
    stub_const("#{described_class.name}::INTERVAL", 5.minutes.to_i)
  end

  context 'no security job artifacts' do
    before do
      table(:ci_job_artifacts)
    end

    it 'does not schedule migration' do
      Sidekiq::Testing.fake! do
        migrate!

        expect(BackgroundMigrationWorker.jobs).to be_empty
      end
    end
  end

  context 'has security job artifacts' do
    let!(:job_artifact_1) { job_artifacts.create!(project_id: project.id, job_id: build.id, file_type: 5) }
    let!(:job_artifact_2) { job_artifacts.create!(project_id: project.id, job_id: build.id, file_type: 8) }

    it 'schedules migration of security scans' do
      Sidekiq::Testing.fake! do
        Timecop.freeze do
          migration.up

          expect(described_class::MIGRATION).to be_scheduled_delayed_migration(5.minutes, job_artifact_1.id, job_artifact_1.id)
          expect(described_class::MIGRATION).to be_scheduled_delayed_migration(10.minutes, job_artifact_2.id, job_artifact_2.id)
          expect(BackgroundMigrationWorker.jobs.size).to eq(2)
        end
      end
    end
  end

  context 'has non-security job artifacts' do
    let!(:job_artifact_1) { job_artifacts.create!(project_id: project.id, job_id: build.id, file_type: 4) }
    let!(:job_artifact_2) { job_artifacts.create!(project_id: project.id, job_id: build.id, file_type: 9) }

    it 'schedules migration of security scans' do
      Sidekiq::Testing.fake! do
        Timecop.freeze do
          migration.up

          expect(BackgroundMigrationWorker.jobs).to be_empty
        end
      end
    end
  end
end