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
require 'spec_helper'
RSpec.describe Gitlab::GithubGistsImport::ImportGistWorker, feature_category: :importers do
subject { described_class.new }
let_it_be(:user) { create(:user) }
let(:token) { 'token' }
let(:gist_hash) do
{
id: '055b70',
git_pull_url: 'https://gist.github.com/foo/bar.git',
files: {
'random.txt': {
filename: 'random.txt',
type: 'text/plain',
language: 'Text',
raw_url: 'https://gist.githubusercontent.com/user_name/055b70/raw/66a7be0d/random.txt',
size: 166903
}
},
is_public: false,
created_at: '2022-09-06T11:38:18Z',
updated_at: '2022-09-06T11:38:18Z',
description: 'random text'
}
end
let(:importer) { instance_double('Gitlab::GithubGistsImport::Importer::GistImporter') }
let(:importer_result) { instance_double('ServiceResponse', success?: true) }
let(:gist_object) do
instance_double('Gitlab::GithubGistsImport::Representation::Gist',
gist_hash.merge(github_identifiers: { id: '055b70' }, truncated_title: 'random text', visibility_level: 0))
end
let(:log_attributes) do
{
'user_id' => user.id,
'github_identifiers' => { 'id': gist_object.id },
'class' => 'Gitlab::GithubGistsImport::ImportGistWorker',
'correlation_id' => 'new-correlation-id',
'jid' => nil,
'job_status' => 'running',
'queue' => 'github_gists_importer:github_gists_import_import_gist'
}
end
describe '#perform' do
before do
allow(Gitlab::GithubGistsImport::Representation::Gist)
.to receive(:from_json_hash)
.with(gist_hash)
.and_return(gist_object)
allow(Gitlab::GithubGistsImport::Importer::GistImporter)
.to receive(:new)
.with(gist_object, user.id)
.and_return(importer)
allow(Gitlab::ApplicationContext).to receive(:current).and_return('correlation_id' => 'new-correlation-id')
allow(described_class).to receive(:queue).and_return('github_gists_importer:github_gists_import_import_gist')
end
context 'when success' do
it 'imports gist' do
expect(Gitlab::GithubImport::Logger)
.to receive(:info)
.with(log_attributes.merge('message' => 'start importer'))
expect(importer).to receive(:execute).and_return(importer_result)
expect(Gitlab::JobWaiter).to receive(:notify).with('some_key', subject.jid)
expect(Gitlab::GithubImport::Logger)
.to receive(:info)
.with(log_attributes.merge('message' => 'importer finished'))
subject.perform(user.id, gist_hash, 'some_key')
expect_snowplow_event(
category: 'Gitlab::GithubGistsImport::ImportGistWorker',
label: 'github_gist_import',
action: 'create',
user: user,
status: 'success'
)
end
end
context 'when failure' do
context 'when importer raised an error' do
let(:exception) { StandardError.new('_some_error_') }
before do
allow(importer).to receive(:execute).and_raise(exception)
end
it 'raises an error' do
expect(Gitlab::GithubImport::Logger)
.to receive(:error)
.with(log_attributes.merge('message' => 'importer failed', 'error.message' => '_some_error_'))
expect(Gitlab::ErrorTracking).to receive(:track_exception)
expect { subject.perform(user.id, gist_hash, 'some_key') }.to raise_error(StandardError)
end
end
context 'when importer returns error' do
let(:importer_result) { instance_double('ServiceResponse', errors: 'error_message', success?: false) }
before do
allow(importer).to receive(:execute).and_return(importer_result)
end
it 'tracks and logs error' do
expect(Gitlab::GithubImport::Logger)
.to receive(:error)
.with(log_attributes.merge('message' => 'importer failed', 'error.message' => 'error_message'))
expect(Gitlab::JobWaiter).to receive(:notify).with('some_key', subject.jid)
subject.perform(user.id, gist_hash, 'some_key')
expect_snowplow_event(
category: 'Gitlab::GithubGistsImport::ImportGistWorker',
label: 'github_gist_import',
action: 'create',
user: user,
status: 'failed'
)
end
it 'persists failure' do
expect { subject.perform(user.id, gist_hash, 'some_key') }
.to change { ImportFailure.where(user: user).count }.from(0).to(1)
expect(ImportFailure.where(user_id: user.id).first).to have_attributes(
source: 'Gitlab::GithubGistsImport::Importer::GistImporter',
exception_class: 'Gitlab::GithubGistsImport::Importer::GistImporter::FileCountLimitError',
exception_message: 'Snippet maximum file count exceeded',
external_identifiers: {
'id' => '055b70'
}
)
end
end
end
describe '.sidekiq_retries_exhausted' do
subject(:sidekiq_retries_exhausted) do
described_class.sidekiq_retries_exhausted_block.call(job, StandardError.new)
end
let(:args) { [user.id, gist_hash, '1'] }
let(:job) do
{
'args' => args,
'jid' => '123',
'correlation_id' => 'abc',
'error_class' => 'StandardError',
'error_message' => 'Some error'
}
end
it 'persists failure' do
expect { sidekiq_retries_exhausted }.to change { ImportFailure.where(user: user).count }.from(0).to(1)
expect(ImportFailure.where(user_id: user.id).first).to have_attributes(
source: 'Gitlab::GithubGistsImport::Importer::GistImporter',
exception_class: 'StandardError',
exception_message: 'Some error',
correlation_id_value: 'abc',
external_identifiers: {
'id' => '055b70'
}
)
end
it 'sends snowplow event' do
sidekiq_retries_exhausted
expect_snowplow_event(
category: 'Gitlab::GithubGistsImport::ImportGistWorker',
label: 'github_gist_import',
action: 'create',
user: user,
status: 'failed'
)
end
it 'notifies the JobWaiter' do
expect(Gitlab::JobWaiter)
.to receive(:notify)
.with(job['args'].last, job['jid'])
sidekiq_retries_exhausted
end
context 'when not all arguments are given' do
let(:args) { [user.id, gist_hash] }
it 'does not notify the JobWaiter' do
expect(Gitlab::JobWaiter).not_to receive(:notify)
sidekiq_retries_exhausted
end
end
end
end
end
|