summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/populate_namespace_statistics_spec.rb
blob: 98b2bc437f30fa1bec700773ea2455c9145b1ac6 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::PopulateNamespaceStatistics do
  let_it_be(:namespaces) { table(:namespaces) }
  let_it_be(:namespace_statistics) { table(:namespace_statistics) }
  let_it_be(:dependency_proxy_manifests) { table(:dependency_proxy_manifests) }
  let_it_be(:dependency_proxy_blobs) { table(:dependency_proxy_blobs) }

  let!(:group1) { namespaces.create!(id: 10, type: 'Group', name: 'group1', path: 'group1') }
  let!(:group2) { namespaces.create!(id: 20, type: 'Group', name: 'group2', path: 'group2') }

  let!(:group1_manifest) do
    dependency_proxy_manifests.create!(group_id: 10, size: 20, file_name: 'test-file', file: 'test', digest: 'abc123')
  end

  let!(:group2_manifest) do
    dependency_proxy_manifests.create!(group_id: 20, size: 20, file_name: 'test-file', file: 'test', digest: 'abc123')
  end

  let!(:group1_stats) { namespace_statistics.create!(id: 10, namespace_id: 10) }

  let(:ids) { namespaces.pluck(:id) }
  let(:statistics) { [] }

  subject(:perform) { described_class.new.perform(ids, statistics) }

  it 'creates/updates all namespace_statistics and updates root storage statistics', :aggregate_failures do
    expect(Namespaces::ScheduleAggregationWorker).to receive(:perform_async).with(group1.id)
    expect(Namespaces::ScheduleAggregationWorker).to receive(:perform_async).with(group2.id)

    expect { perform }.to change(namespace_statistics, :count).from(1).to(2)

    namespace_statistics.all.each do |stat|
      expect(stat.dependency_proxy_size).to eq 20
      expect(stat.storage_size).to eq 20
    end
  end

  context 'when just a stat is passed' do
    let(:statistics) { [:dependency_proxy_size] }

    it 'calls the statistics update service with just that stat' do
      expect(Groups::UpdateStatisticsService)
        .to receive(:new)
        .with(anything, statistics: [:dependency_proxy_size])
        .twice.and_call_original

      perform
    end
  end

  context 'when a statistics update fails' do
    before do
      error_response = instance_double(ServiceResponse, message: 'an error', error?: true)

      allow_next_instance_of(Groups::UpdateStatisticsService) do |instance|
        allow(instance).to receive(:execute).and_return(error_response)
      end
    end

    it 'logs an error' do
      expect_next_instance_of(Gitlab::BackgroundMigration::Logger) do |instance|
        expect(instance).to receive(:error).twice
      end

      perform
    end
  end
end