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

repository_storage_moves_shared_examples.rb « api « requests « shared_examples « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3ca2b9fa6de4d945edf07cc08b690a769aefc971 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# frozen_string_literal: true

RSpec.shared_examples 'repository_storage_moves API' do |container_type|
  include AccessMatchersForRequest

  let_it_be(:user) { create(:admin) }

  shared_examples 'get single container repository storage move' do
    let(:repository_storage_move_id) { storage_move.id }

    def get_container_repository_storage_move
      get api(url, user)
    end

    it 'returns a container repository storage move', :aggregate_failures do
      get_container_repository_storage_move

      expect(response).to have_gitlab_http_status(:ok)
      expect(response).to match_response_schema("public_api/v4/#{container_type.singularize}_repository_storage_move")
      expect(json_response['id']).to eq(storage_move.id)
      expect(json_response['state']).to eq(storage_move.human_state_name)
    end

    context 'non-existent container repository storage move' do
      let(:repository_storage_move_id) { non_existing_record_id }

      it 'returns not found' do
        get_container_repository_storage_move

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    describe 'permissions' do
      it { expect { get_container_repository_storage_move }.to be_allowed_for(:admin) }
      it { expect { get_container_repository_storage_move }.to be_denied_for(:user) }
    end
  end

  shared_examples 'get container repository storage move list' do
    def get_container_repository_storage_moves
      get api(url, user)
    end

    it 'returns container repository storage moves', :aggregate_failures do
      get_container_repository_storage_moves

      expect(response).to have_gitlab_http_status(:ok)
      expect(response).to include_pagination_headers
      expect(response).to match_response_schema("public_api/v4/#{container_type.singularize}_repository_storage_moves")
      expect(json_response.size).to eq(1)
      expect(json_response.first['id']).to eq(storage_move.id)
      expect(json_response.first['state']).to eq(storage_move.human_state_name)
    end

    it 'avoids N+1 queries', :request_store do
      # prevent `let` from polluting the control
      get_container_repository_storage_moves

      control = ActiveRecord::QueryRecorder.new { get_container_repository_storage_moves }

      create(repository_storage_move_factory, :scheduled, container: container)

      expect { get_container_repository_storage_moves }.not_to exceed_query_limit(control)
    end

    it 'returns the most recently created first' do
      storage_move_oldest = create(repository_storage_move_factory, :scheduled, container: container, created_at: 2.days.ago)
      storage_move_middle = create(repository_storage_move_factory, :scheduled, container: container, created_at: 1.day.ago)

      get_container_repository_storage_moves

      json_ids = json_response.map {|storage_move| storage_move['id'] }
      expect(json_ids).to eq([
        storage_move.id,
        storage_move_middle.id,
        storage_move_oldest.id
      ])
    end

    describe 'permissions' do
      it { expect { get_container_repository_storage_moves }.to be_allowed_for(:admin) }
      it { expect { get_container_repository_storage_moves }.to be_denied_for(:user) }
    end
  end

  describe "GET /#{container_type}/:id/repository_storage_moves" do
    let(:container_id) { container.id }
    let(:url) { "/#{container_type}/#{container_id}/repository_storage_moves" }

    it_behaves_like 'get container repository storage move list'

    context 'non-existent container' do
      let(:container_id) { non_existing_record_id }

      it 'returns not found' do
        get api(url, user)

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe "GET /#{container_type}/:id/repository_storage_moves/:repository_storage_move_id" do
    let(:container_id) { container.id }
    let(:url) { "/#{container_type}/#{container_id}/repository_storage_moves/#{repository_storage_move_id}" }

    it_behaves_like 'get single container repository storage move'

    context 'non-existent container' do
      let(:container_id) { non_existing_record_id }
      let(:repository_storage_move_id) { storage_move.id }

      it 'returns not found' do
        get api(url, user)

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe "GET /#{container_type.singularize}_repository_storage_moves" do
    it_behaves_like 'get container repository storage move list' do
      let(:url) { "/#{container_type.singularize}_repository_storage_moves" }
    end
  end

  describe "GET /#{container_type.singularize}_repository_storage_moves/:repository_storage_move_id" do
    it_behaves_like 'get single container repository storage move' do
      let(:url) { "/#{container_type.singularize}_repository_storage_moves/#{repository_storage_move_id}" }
    end
  end

  describe "POST /#{container_type}/:id/repository_storage_moves" do
    let(:container_id) { container.id }
    let(:url) { "/#{container_type}/#{container_id}/repository_storage_moves" }
    let(:destination_storage_name) { 'test_second_storage' }

    def create_container_repository_storage_move
      post api(url, user), params: { destination_storage_name: destination_storage_name }
    end

    before do
      stub_storage_settings('test_second_storage' => { 'path' => 'tmp/tests/extra_storage' })
    end

    it 'schedules a container repository storage move', :aggregate_failures do
      create_container_repository_storage_move

      storage_move = container.repository_storage_moves.last

      expect(response).to have_gitlab_http_status(:created)
      expect(response).to match_response_schema("public_api/v4/#{container_type.singularize}_repository_storage_move")
      expect(json_response['id']).to eq(storage_move.id)
      expect(json_response['state']).to eq('scheduled')
      expect(json_response['source_storage_name']).to eq('default')
      expect(json_response['destination_storage_name']).to eq(destination_storage_name)
    end

    describe 'permissions' do
      it { expect { create_container_repository_storage_move }.to be_allowed_for(:admin) }
      it { expect { create_container_repository_storage_move }.to be_denied_for(:user) }
    end

    context 'destination_storage_name is missing', :aggregate_failures do
      let(:destination_storage_name) { nil }

      it 'schedules a container repository storage move' do
        create_container_repository_storage_move

        storage_move = container.repository_storage_moves.last

        expect(response).to have_gitlab_http_status(:created)
        expect(response).to match_response_schema("public_api/v4/#{container_type.singularize}_repository_storage_move")
        expect(json_response['id']).to eq(storage_move.id)
        expect(json_response['state']).to eq('scheduled')
        expect(json_response['source_storage_name']).to eq('default')
        expect(json_response['destination_storage_name']).to be_present
      end
    end

    context 'when container does not exist' do
      let(:container_id) { non_existing_record_id }

      it 'returns not found' do
        create_container_repository_storage_move

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe "POST /#{container_type.singularize}_repository_storage_moves" do
    let(:url) { "/#{container_type.singularize}_repository_storage_moves" }
    let(:source_storage_name) { 'default' }
    let(:destination_storage_name) { 'test_second_storage' }

    def create_container_repository_storage_moves
      post api(url, user), params: {
        source_storage_name: source_storage_name,
        destination_storage_name: destination_storage_name
      }
    end

    before do
      stub_storage_settings('test_second_storage' => { 'path' => 'tmp/tests/extra_storage' })
    end

    it 'schedules the worker' do
      expect(bulk_worker_klass).to receive(:perform_async).with(source_storage_name, destination_storage_name)

      create_container_repository_storage_moves

      expect(response).to have_gitlab_http_status(:accepted)
    end

    context 'source_storage_name is invalid' do
      let(:destination_storage_name) { 'not-a-real-storage' }

      it 'gives an error' do
        create_container_repository_storage_moves

        expect(response).to have_gitlab_http_status(:bad_request)
      end
    end

    context 'destination_storage_name is missing' do
      let(:destination_storage_name) { nil }

      it 'schedules the worker' do
        expect(bulk_worker_klass).to receive(:perform_async).with(source_storage_name, destination_storage_name)

        create_container_repository_storage_moves

        expect(response).to have_gitlab_http_status(:accepted)
      end
    end

    context 'destination_storage_name is invalid' do
      let(:destination_storage_name) { 'not-a-real-storage' }

      it 'gives an error' do
        create_container_repository_storage_moves

        expect(response).to have_gitlab_http_status(:bad_request)
      end
    end

    describe 'normal user' do
      it { expect { create_container_repository_storage_moves }.to be_denied_for(:user) }
    end
  end
end