summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/requests/api/container_repositories_shared_examples.rb
blob: c1eccafa987403efd31367b3b764b7867f3775b1 (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
# frozen_string_literal: true

RSpec.shared_examples 'rejected container repository access' do |user_type, status|
  context "for #{user_type}" do
    let(:api_user) { users[user_type] }

    it "returns #{status}" do
      subject

      expect(response).to have_gitlab_http_status(status)
    end
  end
end

RSpec.shared_examples 'returns repositories for allowed users' do |user_type, scope|
  context "for #{user_type}" do
    it 'returns a list of repositories' do
      subject

      expect(json_response.length).to eq(2)
      expect(json_response.map { |repository| repository['id'] }).to contain_exactly(
        root_repository.id, test_repository.id)
      expect(response.body).not_to include('tags')
    end

    it 'returns a matching schema' do
      subject

      expect(response).to have_gitlab_http_status(:ok)
      expect(response).to match_response_schema('registry/repositories')
    end

    context 'with tags param' do
      let(:url) { "/#{scope}s/#{object.id}/registry/repositories?tags=true" }

      before do
        stub_container_registry_tags(repository: root_repository.path, tags: %w(rootA latest), with_manifest: true)
        stub_container_registry_tags(repository: test_repository.path, tags: %w(rootA latest), with_manifest: true)
      end

      it 'returns a list of repositories and their tags' do
        subject

        expect(json_response.length).to eq(2)
        expect(json_response.map { |repository| repository['id'] }).to contain_exactly(
          root_repository.id, test_repository.id)
        expect(response.body).to include('tags')
      end

      it 'returns a matching schema' do
        subject

        expect(response).to have_gitlab_http_status(:ok)
        expect(response).to match_response_schema('registry/repositories')
      end
    end

    context 'with tags_count param' do
      let(:url) { "/#{scope}s/#{object.id}/registry/repositories?tags_count=true" }

      before do
        stub_container_registry_tags(repository: root_repository.path, tags: %w(rootA latest), with_manifest: true)
        stub_container_registry_tags(repository: test_repository.path, tags: %w(rootA latest), with_manifest: true)
      end

      it 'returns a list of repositories and their tags_count' do
        subject

        expect(response.body).to include('tags_count')
        expect(json_response[0]['tags_count']).to eq(2)
      end

      it 'returns a matching schema' do
        subject

        expect(response).to have_gitlab_http_status(:ok)
        expect(response).to match_response_schema('registry/repositories')
      end
    end
  end
end

RSpec.shared_examples 'handling network errors with the container registry' do
  before do
    stub_container_registry_network_error(client_method: :repository_tags)
  end

  it 'returns a connection error' do
    subject

    expect(response).to have_gitlab_http_status(:service_unavailable)
    expect(json_response['message']).to include('We are having trouble connecting to the Container Registry')
  end
end

RSpec.shared_examples 'handling graphql network errors with the container registry' do
  before do
    stub_container_registry_network_error(client_method: :repository_tags)
  end

  it 'returns a connection error' do
    subject

    expect_graphql_errors_to_include('We are having trouble connecting to the Container Registry')
  end
end

RSpec.shared_examples 'not hitting graphql network errors with the container registry' do
  before do
    stub_container_registry_network_error(client_method: :repository_tags)
  end

  it 'does not return any error' do
    subject

    expect_graphql_errors_to_be_empty
  end
end

RSpec.shared_examples 'reconciling migration_state' do
  shared_examples 'enforcing states coherence to' do |expected_migration_state|
    it 'leaves the repository in the expected migration_state' do
      expect(repository.gitlab_api_client).not_to receive(:pre_import_repository)
      expect(repository.gitlab_api_client).not_to receive(:import_repository)

      subject

      expect(repository.reload.migration_state).to eq(expected_migration_state)
    end
  end

  shared_examples 'retrying the pre_import' do
    it 'retries the pre_import' do
      expect(repository).to receive(:migration_pre_import).and_return(:ok)

      expect { subject }.to change { repository.reload.migration_state }.to('pre_importing')
    end
  end

  shared_examples 'retrying the import' do
    it 'retries the import' do
      expect(repository).to receive(:migration_import).and_return(:ok)

      expect { subject }.to change { repository.reload.migration_state }.to('importing')
    end
  end

  context 'native response' do
    let(:status) { 'native' }

    it 'finishes the import' do
      expect { subject }
        .to change { repository.reload.migration_state }.to('import_done')
        .and change { repository.reload.migration_skipped_reason }.to('native_import')
    end
  end

  context 'import_in_progress response' do
    let(:status) { 'import_in_progress' }

    it_behaves_like 'enforcing states coherence to', 'importing'
  end

  context 'import_complete response' do
    let(:status) { 'import_complete' }

    it 'finishes the import' do
      expect { subject }.to change { repository.reload.migration_state }.to('import_done')
    end
  end

  context 'import_failed response' do
    let(:status) { 'import_failed' }

    it_behaves_like 'retrying the import'
  end

  context 'pre_import_in_progress response' do
    let(:status) { 'pre_import_in_progress' }

    it_behaves_like 'enforcing states coherence to', 'pre_importing'
  end

  context 'pre_import_complete response' do
    let(:status) { 'pre_import_complete' }

    it 'finishes the pre_import and starts the import' do
      expect(repository).to receive(:finish_pre_import).and_call_original
      expect(repository).to receive(:migration_import).and_return(:ok)

      expect { subject }.to change { repository.reload.migration_state }.to('importing')
    end
  end

  context 'pre_import_failed response' do
    let(:status) { 'pre_import_failed' }

    it_behaves_like 'retrying the pre_import'
  end

  %w[pre_import_canceled import_canceled].each do |canceled_status|
    context "#{canceled_status} response" do
      let(:status) { canceled_status }

      it_behaves_like 'enforcing states coherence to', 'import_skipped'
    end
  end
end