Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_repository_move_shared_examples.rb « workers « shared_examples « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb3cd81f5ce5a1d4e407a5959da17b6871230ee1 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# frozen_string_literal: true

RSpec.shared_examples 'an update storage move worker' do
  let(:worker) { described_class.new }

  it 'has the `until_executed` deduplicate strategy' do
    expect(described_class.get_deduplicate_strategy).to eq(:until_executed)
  end

  describe '#perform', :clean_gitlab_redis_shared_state do
    let(:service) { double(:update_repository_storage_service) }

    before do
      allow(Gitlab.config.repositories.storages).to receive(:keys).and_return(%w[default test_second_storage])
    end

    describe 'deprecated method signature' do
      # perform(container_id, new_repository_storage_key, repository_storage_move_id = nil)
      subject { worker.perform(container.id, 'test_second_storage', repository_storage_move_id) }

      context 'without repository storage move' do
        let(:repository_storage_move_id) { nil }

        it 'calls the update repository storage service' do
          expect(service_klass).to receive(:new).and_return(service)
          expect(service).to receive(:execute)

          expect do
            worker.perform(container.id, 'test_second_storage')
          end.to change { repository_storage_move_klass.count }.by(1)

          storage_move = container.repository_storage_moves.last
          expect(storage_move).to have_attributes(
            source_storage_name: 'default',
            destination_storage_name: 'test_second_storage'
          )
        end
      end

      context 'with repository storage move' do
        let(:repository_storage_move_id) { repository_storage_move.id }

        before do
          allow(service_klass).to receive(:new).and_return(service)
        end

        it 'calls the update repository storage service' do
          expect(service).to receive(:execute)

          expect do
            subject
          end.not_to change { repository_storage_move_klass.count }
        end

        context 'when repository storage move raises an exception' do
          let(:exception) { RuntimeError.new('boom') }

          it 'releases the exclusive lock' do
            expect(service).to receive(:execute).and_raise(exception)

            allow_next_instance_of(Gitlab::ExclusiveLease) do |lease|
              expect(lease).to receive(:cancel)
            end

            expect { subject }.to raise_error(exception)
          end
        end

        context 'when exclusive lease already set' do
          let(:lease_key) { [described_class.name.underscore, container.id].join(':') }
          let(:exclusive_lease) { Gitlab::ExclusiveLease.new(lease_key, uuid: uuid, timeout: 1.minute) }
          let(:uuid) { 'other_worker_jid' }

          it 'does not call the update repository storage service' do
            expect(exclusive_lease.try_obtain).to eq(uuid)
            expect(service).not_to receive(:execute)

            subject

            expect(repository_storage_move.reload).to be_failed
          end

          context 'when exclusive lease was taken by the current worker' do
            let(:uuid) { 'existing_worker_jid' }

            before do
              allow(worker).to receive(:jid).and_return(uuid)
            end

            it 'marks storage migration as failed' do
              expect(exclusive_lease.try_obtain).to eq(worker.jid)
              expect(service).not_to receive(:execute)

              subject

              expect(repository_storage_move.reload).to be_failed
            end
          end
        end
      end
    end

    describe 'new method signature' do
      # perform(repository_storage_move_id)
      subject { worker.perform(repository_storage_move.id) }

      before do
        allow(service_klass).to receive(:new).and_return(service)
      end

      it 'calls the update repository storage service' do
        expect(service).to receive(:execute)

        expect do
          subject
        end.not_to change { repository_storage_move_klass.count }
      end

      context 'when repository storage move raises an exception' do
        let(:exception) { RuntimeError.new('boom') }

        it 'releases the exclusive lock' do
          expect(service).to receive(:execute).and_raise(exception)

          allow_next_instance_of(Gitlab::ExclusiveLease) do |lease|
            expect(lease).to receive(:cancel)
          end

          expect { subject }.to raise_error(exception)
        end
      end

      context 'when exclusive lease already set' do
        let(:lease_key) { [described_class.name.underscore, repository_storage_move.container_id].join(':') }
        let(:exclusive_lease) { Gitlab::ExclusiveLease.new(lease_key, uuid: uuid, timeout: 1.minute) }
        let(:uuid) { 'other_worker_jid' }

        it 'does not call the update repository storage service' do
          expect(exclusive_lease.try_obtain).to eq(uuid)
          expect(service).not_to receive(:execute)

          subject

          expect(repository_storage_move.reload).to be_failed
        end

        context 'when exclusive lease was taken by the current worker' do
          let(:uuid) { 'existing_worker_jid' }

          before do
            allow(worker).to receive(:jid).and_return(uuid)
          end

          it 'marks storage migration as failed' do
            expect(exclusive_lease.try_obtain).to eq(worker.jid)
            expect(service).not_to receive(:execute)

            subject

            expect(repository_storage_move.reload).to be_failed
          end
        end
      end
    end
  end
end