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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::GithubImport::StageMethods do
let(:project) { create(:project) }
let(:worker) do
Class.new do
def self.name
'DummyStage'
end
include(Gitlab::GithubImport::StageMethods)
end.new
end
describe '#perform' do
let(:project) { create(:project, import_url: 'https://t0ken@github.com/repo/repo.git') }
it 'returns if no project could be found' do
expect(worker).not_to receive(:try_import)
worker.perform(-1)
end
it 'imports the data when the project exists' do
allow(worker)
.to receive(:find_project)
.with(project.id)
.and_return(project)
expect(worker)
.to receive(:try_import)
.with(
an_instance_of(Gitlab::GithubImport::Client),
an_instance_of(Project)
)
expect_next_instance_of(Gitlab::Import::Logger) do |logger|
expect(logger)
.to receive(:info)
.with(
message: 'starting stage',
import_source: :github,
project_id: project.id,
import_stage: 'DummyStage'
)
expect(logger)
.to receive(:info)
.with(
message: 'stage finished',
import_source: :github,
project_id: project.id,
import_stage: 'DummyStage'
)
end
worker.perform(project.id)
end
it 'logs error when import fails' do
exception = StandardError.new('some error')
allow(worker)
.to receive(:find_project)
.with(project.id)
.and_return(project)
expect(worker)
.to receive(:try_import)
.and_raise(exception)
expect_next_instance_of(Gitlab::Import::Logger) do |logger|
expect(logger)
.to receive(:info)
.with(
message: 'starting stage',
import_source: :github,
project_id: project.id,
import_stage: 'DummyStage'
)
expect(logger)
.to receive(:error)
.with(
message: 'stage failed',
import_source: :github,
project_id: project.id,
import_stage: 'DummyStage',
'error.message': 'some error'
)
end
expect(Gitlab::ErrorTracking)
.to receive(:track_and_raise_exception)
.with(
exception,
import_source: :github,
project_id: project.id,
import_stage: 'DummyStage'
)
.and_call_original
expect { worker.perform(project.id) }.to raise_error(exception)
end
end
describe '#try_import' do
it 'imports the project' do
client = double(:client)
expect(worker)
.to receive(:import)
.with(client, project)
worker.try_import(client, project)
end
it 'reschedules the worker if RateLimitError was raised' do
client = double(:client, rate_limit_resets_in: 10)
expect(worker)
.to receive(:import)
.with(client, project)
.and_raise(Gitlab::GithubImport::RateLimitError)
expect(worker.class)
.to receive(:perform_in)
.with(10, project.id)
worker.try_import(client, project)
end
end
describe '#find_project' do
let(:import_state) { create(:import_state, project: project) }
it 'returns a Project for an existing ID' do
import_state.update_column(:status, 'started')
expect(worker.find_project(project.id)).to eq(project)
end
it 'returns nil for a project that failed importing' do
import_state.update_column(:status, 'failed')
expect(worker.find_project(project.id)).to be_nil
end
it 'returns nil for a non-existing project ID' do
expect(worker.find_project(-1)).to be_nil
end
end
end
|