summaryrefslogtreecommitdiff
path: root/spec/services/container_expiration_policy_service_spec.rb
blob: dfce51d73add54041b322004b2a4c0f8cddb59f8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ContainerExpirationPolicyService do
  let_it_be(:user) { create(:user) }
  let_it_be(:container_expiration_policy) { create(:container_expiration_policy, :runnable) }
  let(:project) { container_expiration_policy.project }
  let(:container_repository) { create(:container_repository, project: project) }

  before do
    project.add_maintainer(user)
  end

  describe '#execute' do
    subject { described_class.new(project, user).execute(container_expiration_policy) }

    it 'kicks off a cleanup worker for the container repository' do
      expect(CleanupContainerRepositoryWorker).to receive(:perform_async)
        .with(nil, container_repository.id, hash_including(container_expiration_policy: true))

      subject
    end

    it 'sets next_run_at on the container_expiration_policy' do
      subject

      expect(container_expiration_policy.next_run_at).to be > Time.zone.now
    end

    context 'with an invalid container expiration policy' do
      before do
        allow(container_expiration_policy).to receive(:valid?).and_return(false)
      end

      it 'disables it' do
        expect(container_expiration_policy).not_to receive(:schedule_next_run!)
        expect(CleanupContainerRepositoryWorker).not_to receive(:perform_async)

        expect { subject }
          .to change { container_expiration_policy.reload.enabled }.from(true).to(false)
          .and raise_error(ContainerExpirationPolicyService::InvalidPolicyError)
      end
    end
  end
end