summaryrefslogtreecommitdiff
path: root/spec/workers/hashed_storage/project_migrate_worker_spec.rb
blob: fd4608889323ac746de660c60f38a54de9f34d84 (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
78
79
80
81
82
83
84
85
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe HashedStorage::ProjectMigrateWorker, :clean_gitlab_redis_shared_state do
  include ExclusiveLeaseHelpers

  let(:migration_service) { ::Projects::HashedStorage::MigrationService }
  let(:lease_timeout) { described_class::LEASE_TIMEOUT }

  describe '#perform' do
    it 'skips when project no longer exists' do
      stub_exclusive_lease(lease_key(-1), 'uuid', timeout: lease_timeout)

      expect(migration_service).not_to receive(:new)

      subject.perform(-1)
    end

    it 'skips when project is pending delete' do
      pending_delete_project = create(:project, :empty_repo, pending_delete: true)
      stub_exclusive_lease(lease_key(pending_delete_project.id), 'uuid', timeout: lease_timeout)

      expect(migration_service).not_to receive(:new)

      subject.perform(pending_delete_project.id)
    end

    it 'skips when project is already migrated' do
      migrated_project = create(:project, :empty_repo)
      stub_exclusive_lease(lease_key(migrated_project.id), 'uuid', timeout: lease_timeout)

      expect(migration_service).not_to receive(:new)

      subject.perform(migrated_project.id)
    end

    context 'with exclusive lease available' do
      it 'delegates migration to service class' do
        project = create(:project, :empty_repo, :legacy_storage)
        stub_exclusive_lease(lease_key(project.id), 'uuid', timeout: lease_timeout)

        service_spy = spy

        allow(migration_service)
          .to receive(:new).with(project, project.full_path, logger: subject.logger)
                .and_return(service_spy)

        subject.perform(project.id)

        expect(service_spy).to have_received(:execute)
      end

      it 'delegates migration to service class with correct path in a partially migrated project' do
        project = create(:project, :empty_repo, storage_version: 1)
        stub_exclusive_lease(lease_key(project.id), 'uuid', timeout: lease_timeout)

        service_spy = spy

        allow(migration_service)
          .to receive(:new).with(project, project.full_path, logger: subject.logger)
                .and_return(service_spy)

        subject.perform(project.id)

        expect(service_spy).to have_received(:execute)
      end
    end

    context 'with exclusive lease taken' do
      it 'skips when it cant acquire the exclusive lease' do
        project = create(:project, :empty_repo, :legacy_storage)
        stub_exclusive_lease_taken(lease_key(project.id), timeout: lease_timeout)

        expect(migration_service).not_to receive(:new)

        subject.perform(project.id)
      end
    end
  end

  def lease_key(key)
    "project_migrate_hashed_storage_worker:#{key}"
  end
end