summaryrefslogtreecommitdiff
path: root/spec/workers/dependency_proxy/image_ttl_group_policy_worker_spec.rb
blob: 6a2fdfbe8f5814b26134d6d5777da25274dcfcaa (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe DependencyProxy::ImageTtlGroupPolicyWorker do
  let(:worker) { described_class.new }

  describe '#perform' do
    let_it_be(:policy) { create(:image_ttl_group_policy) }
    let_it_be(:group) { policy.group }

    subject { worker.perform }

    context 'when there are images to expire' do
      let_it_be_with_reload(:old_blob) { create(:dependency_proxy_blob, group: group, read_at: 1.year.ago) }
      let_it_be_with_reload(:old_manifest) { create(:dependency_proxy_manifest, group: group, read_at: 1.year.ago) }
      let_it_be_with_reload(:new_blob) { create(:dependency_proxy_blob, group: group) }
      let_it_be_with_reload(:new_manifest) { create(:dependency_proxy_manifest, group: group) }

      it 'updates the old images to pending_destruction' do
        expect { subject }
          .to change { old_blob.reload.status }.from('default').to('pending_destruction')
          .and change { old_manifest.reload.status }.from('default').to('pending_destruction')
          .and not_change { new_blob.reload.status }
          .and not_change { new_manifest.reload.status }
      end
    end

    context 'counts logging' do
      let_it_be(:expired_blob) { create(:dependency_proxy_blob, :pending_destruction, group: group) }
      let_it_be(:expired_blob2) { create(:dependency_proxy_blob, :pending_destruction, group: group) }
      let_it_be(:expired_manifest) { create(:dependency_proxy_manifest, :pending_destruction, group: group) }
      let_it_be(:processing_blob) { create(:dependency_proxy_blob, status: :processing, group: group) }
      let_it_be(:processing_manifest) { create(:dependency_proxy_manifest, status: :processing, group: group) }
      let_it_be(:error_blob) { create(:dependency_proxy_blob, status: :error, group: group) }
      let_it_be(:error_manifest) { create(:dependency_proxy_manifest, status: :error, group: group) }

      it 'logs all the counts', :aggregate_failures do
        expect(worker).to receive(:log_extra_metadata_on_done).with(:expired_dependency_proxy_blob_count, 2)
        expect(worker).to receive(:log_extra_metadata_on_done).with(:expired_dependency_proxy_manifest_count, 1)
        expect(worker).to receive(:log_extra_metadata_on_done).with(:processing_dependency_proxy_blob_count, 1)
        expect(worker).to receive(:log_extra_metadata_on_done).with(:processing_dependency_proxy_manifest_count, 1)
        expect(worker).to receive(:log_extra_metadata_on_done).with(:error_dependency_proxy_blob_count, 1)
        expect(worker).to receive(:log_extra_metadata_on_done).with(:error_dependency_proxy_manifest_count, 1)

        subject
      end

      context 'with load balancing enabled', :db_load_balancing do
        it 'reads the counts from the replica' do
          expect(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_replicas_for_read_queries).and_call_original

          subject
        end
      end
    end
  end
end