summaryrefslogtreecommitdiff
path: root/spec/migrations/20211217174331_mark_recalculate_finding_signatures_as_completed_spec.rb
blob: 2d808adf578759ead8cf7fb23318f6dd0225565e (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
# frozen_string_literal: true
require 'spec_helper'
require_migration!

def create_background_migration_jobs(ids, status, created_at)
  proper_status = case status
                  when :pending
                    Gitlab::Database::BackgroundMigrationJob.statuses['pending']
                  when :succeeded
                    Gitlab::Database::BackgroundMigrationJob.statuses['succeeded']
                  else
                    raise ArgumentError
                  end

  background_migration_jobs.create!(
    class_name: 'RecalculateVulnerabilitiesOccurrencesUuid',
    arguments: Array(ids),
    status: proper_status,
    created_at: created_at
  )
end

RSpec.describe MarkRecalculateFindingSignaturesAsCompleted, :migration, feature_category: :vulnerability_management do
  let!(:background_migration_jobs) { table(:background_migration_jobs) }

  context 'when RecalculateVulnerabilitiesOccurrencesUuid jobs are present' do
    before do
      create_background_migration_jobs([1, 2, 3], :succeeded, DateTime.new(2021, 5, 5, 0, 2))
      create_background_migration_jobs([4, 5, 6], :pending, DateTime.new(2021, 5, 5, 0, 4))

      create_background_migration_jobs([1, 2, 3], :succeeded, DateTime.new(2021, 8, 18, 0, 0))
      create_background_migration_jobs([4, 5, 6], :pending, DateTime.new(2021, 8, 18, 0, 2))
      create_background_migration_jobs([7, 8, 9], :pending, DateTime.new(2021, 8, 18, 0, 4))
    end

    describe 'gitlab.com' do
      before do
        allow(::Gitlab).to receive(:com?).and_return(true)
      end

      it 'marks all jobs as succeeded' do
        expect(background_migration_jobs.where(status: 1).count).to eq(2)

        migrate!

        expect(background_migration_jobs.where(status: 1).count).to eq(5)
      end
    end

    describe 'self managed' do
      before do
        allow(::Gitlab).to receive(:com?).and_return(false)
      end

      it 'does not change job status' do
        expect(background_migration_jobs.where(status: 1).count).to eq(2)

        migrate!

        expect(background_migration_jobs.where(status: 1).count).to eq(2)
      end
    end
  end
end