summaryrefslogtreecommitdiff
path: root/spec/workers/namespaces/schedule_aggregation_worker_spec.rb
blob: d4a49a3f53a0429dec8391b075b31f91303f6b9a (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
72
73
74
75
76
77
# frozen_string_literal: true

require 'spec_helper'

describe Namespaces::ScheduleAggregationWorker, '#perform', :clean_gitlab_redis_shared_state do
  let(:group) { create(:group) }

  subject(:worker) { described_class.new }

  context 'when group is the root ancestor' do
    context 'when aggregation schedule exists' do
      it 'does not create a new one' do
        stub_aggregation_schedule_statistics

        Namespace::AggregationSchedule.safe_find_or_create_by!(namespace_id: group.id)

        expect do
          worker.perform(group.id)
        end.not_to change(Namespace::AggregationSchedule, :count)
      end
    end

    context 'when aggregation schedule does not exist' do
      it 'creates one' do
        stub_aggregation_schedule_statistics

        expect do
          worker.perform(group.id)
        end.to change(Namespace::AggregationSchedule, :count).by(1)

        expect(group.aggregation_schedule).to be_present
      end
    end

    context 'when update_statistics_namespace is off' do
      it 'does not create a new one' do
        stub_feature_flags(update_statistics_namespace: false, namespace: group)

        expect do
          worker.perform(group.id)
        end.not_to change(Namespace::AggregationSchedule, :count)
      end
    end
  end

  context 'when group is not the root ancestor' do
    let(:parent_group) { create(:group) }
    let(:group) { create(:group, parent: parent_group) }

    it 'creates an aggregation schedule for the root' do
      stub_aggregation_schedule_statistics

      worker.perform(group.id)

      expect(parent_group.aggregation_schedule).to be_present
    end
  end

  context 'when namespace does not exist' do
    it 'logs the error' do
      expect(Gitlab::SidekiqLogger).to receive(:error).once

      worker.perform(12345)
    end
  end

  def stub_aggregation_schedule_statistics
    # Namespace::Aggregations are deleted by
    # Namespace::AggregationSchedule::schedule_root_storage_statistics,
    # which is executed async. Stubing the service so instances are not deleted
    # while still running the specs.
    expect_next_instance_of(Namespace::AggregationSchedule) do |aggregation_schedule|
      expect(aggregation_schedule)
        .to receive(:schedule_root_storage_statistics)
    end
  end
end