-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathtest_link_all_repos.py
More file actions
300 lines (258 loc) · 11.4 KB
/
test_link_all_repos.py
File metadata and controls
300 lines (258 loc) · 11.4 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
from unittest.mock import MagicMock, patch
import pytest
import responses
from django.db import IntegrityError
from sentry.constants import ObjectStatus
from sentry.integrations.github.integration import GitHubIntegrationProvider
from sentry.integrations.github.tasks.link_all_repos import link_all_repos
from sentry.integrations.source_code_management.metrics import LinkAllReposHaltReason
from sentry.integrations.types import EventLifecycleOutcome
from sentry.models.repository import Repository
from sentry.silo.base import SiloMode
from sentry.taskworker.retry import RetryTaskError
from sentry.testutils.asserts import assert_failure_metric, assert_halt_metric, assert_slo_metric
from sentry.testutils.cases import IntegrationTestCase
from sentry.testutils.silo import assume_test_silo_mode, control_silo_test
@control_silo_test
@patch("sentry.integrations.github.client.get_jwt", return_value="jwt_token_1")
class LinkAllReposTestCase(IntegrationTestCase):
provider = GitHubIntegrationProvider
base_url = "https://api.github.com"
key = "github"
def _add_responses(self):
responses.add(
responses.GET,
self.base_url + "/installation/repositories?per_page=100",
status=200,
json={
"total_count": 2,
"repositories": [
{
"id": 1,
"full_name": "getsentry/sentry",
},
{
"id": 2,
"full_name": "getsentry/snuba",
},
],
},
)
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_link_all_repos_inactive_integration(
self, mock_record: MagicMock, _: MagicMock
) -> None:
self.integration.update(status=ObjectStatus.DISABLED)
link_all_repos(
integration_key=self.key,
integration_id=self.integration.id,
organization_id=self.organization.id,
)
assert_slo_metric(mock_record, EventLifecycleOutcome.FAILURE)
assert_failure_metric(mock_record, LinkAllReposHaltReason.MISSING_INTEGRATION.value)
@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_link_all_repos(self, mock_record: MagicMock, _: MagicMock) -> None:
self._add_responses()
link_all_repos(
integration_key=self.key,
integration_id=self.integration.id,
organization_id=self.organization.id,
)
with assume_test_silo_mode(SiloMode.REGION):
repos = Repository.objects.all()
assert len(repos) == 2
for repo in repos:
assert repo.organization_id == self.organization.id
assert repo.provider == "integrations:github"
assert repos[0].name == "getsentry/sentry"
assert repos[1].name == "getsentry/snuba"
assert len(mock_record.mock_calls) == 4
start1, start2, end2, end1 = mock_record.mock_calls
assert start1.args[0] == EventLifecycleOutcome.STARTED
assert start2.args[0] == EventLifecycleOutcome.STARTED
assert end2.args[0] == EventLifecycleOutcome.SUCCESS
assert end1.args[0] == EventLifecycleOutcome.SUCCESS
@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_link_all_repos_api_response_keyerror(
self, mock_record: MagicMock, _: MagicMock
) -> None:
responses.add(
responses.GET,
self.base_url + "/installation/repositories?per_page=100",
status=200,
json={
"total_count": 2,
"repositories": [
{
"full_name": "getsentry/sentry",
},
{
"id": 2,
"full_name": "getsentry/snuba",
},
],
},
)
link_all_repos(
integration_key=self.key,
integration_id=self.integration.id,
organization_id=self.organization.id,
)
with assume_test_silo_mode(SiloMode.REGION):
repos = Repository.objects.all()
assert len(repos) == 1
assert repos[0].organization_id == self.organization.id
assert repos[0].provider == "integrations:github"
assert repos[0].name == "getsentry/snuba"
assert len(mock_record.mock_calls) == 4
start1, start2, end2, end1 = mock_record.mock_calls
assert start1.args[0] == EventLifecycleOutcome.STARTED
assert start2.args[0] == EventLifecycleOutcome.STARTED
assert end2.args[0] == EventLifecycleOutcome.SUCCESS
assert end1.args[0] == EventLifecycleOutcome.HALTED
assert_halt_metric(
mock_record, LinkAllReposHaltReason.REPOSITORY_NOT_CREATED.value
) # should be halt because it didn't complete successfully
@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_link_all_repos_api_response_keyerror_single_repo(
self, mock_record: MagicMock, _: MagicMock
) -> None:
responses.add(
responses.GET,
self.base_url + "/installation/repositories?per_page=100",
status=200,
json={
"total_count": 2,
"repositories": [
{
"full_name": "getsentry/sentry",
},
],
},
)
link_all_repos(
integration_key=self.key,
integration_id=self.integration.id,
organization_id=self.organization.id,
)
with assume_test_silo_mode(SiloMode.REGION):
repos = Repository.objects.all()
assert len(repos) == 0
assert len(mock_record.mock_calls) == 4
start1, start2, end2, end1 = mock_record.mock_calls
assert start1.args[0] == EventLifecycleOutcome.STARTED
assert start2.args[0] == EventLifecycleOutcome.STARTED
assert end2.args[0] == EventLifecycleOutcome.SUCCESS
assert end1.args[0] == EventLifecycleOutcome.HALTED
assert_halt_metric(mock_record, LinkAllReposHaltReason.REPOSITORY_NOT_CREATED.value)
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_link_all_repos_missing_integration(self, mock_record: MagicMock, _: MagicMock) -> None:
link_all_repos(
integration_key=self.key,
integration_id=0,
organization_id=self.organization.id,
)
assert_slo_metric(mock_record, EventLifecycleOutcome.FAILURE)
assert_failure_metric(mock_record, LinkAllReposHaltReason.MISSING_INTEGRATION.value)
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_link_all_repos_missing_organization(
self, mock_record: MagicMock, _: MagicMock
) -> None:
link_all_repos(
integration_key=self.key,
integration_id=self.integration.id,
organization_id=0,
)
assert_slo_metric(mock_record, EventLifecycleOutcome.FAILURE)
assert_failure_metric(mock_record, LinkAllReposHaltReason.MISSING_ORGANIZATION.value)
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
@responses.activate
def test_link_all_repos_api_error(self, mock_record: MagicMock, _: MagicMock) -> None:
responses.add(
responses.GET,
self.base_url + "/installation/repositories?per_page=100",
status=400,
)
with pytest.raises(RetryTaskError):
link_all_repos(
integration_key=self.key,
integration_id=self.integration.id,
organization_id=self.organization.id,
)
assert len(mock_record.mock_calls) == 4
start1, start2, end2, end1 = mock_record.mock_calls
assert start1.args[0] == EventLifecycleOutcome.STARTED
assert start2.args[0] == EventLifecycleOutcome.STARTED
assert end2.args[0] == EventLifecycleOutcome.FAILURE
assert end1.args[0] == EventLifecycleOutcome.FAILURE
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
@responses.activate
def test_link_all_repos_api_error_rate_limited(
self, mock_record: MagicMock, _: MagicMock
) -> None:
responses.add(
responses.GET,
self.base_url + "/installation/repositories?per_page=100",
status=400,
json={
"message": "API rate limit exceeded",
"documentation_url": "https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting",
},
)
link_all_repos(
integration_key=self.key,
integration_id=self.integration.id,
organization_id=self.organization.id,
)
assert len(mock_record.mock_calls) == 4
start1, start2, end2, end1 = mock_record.mock_calls
assert start1.args[0] == EventLifecycleOutcome.STARTED
assert start2.args[0] == EventLifecycleOutcome.STARTED
assert end2.args[0] == EventLifecycleOutcome.FAILURE
assert end1.args[0] == EventLifecycleOutcome.HALTED
assert_halt_metric(mock_record, LinkAllReposHaltReason.RATE_LIMITED.value)
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
@patch("sentry.models.Repository.objects.create")
@responses.activate
def test_link_all_repos_repo_creation_error(
self, mock_repo: MagicMock, mock_record: MagicMock, _: MagicMock
) -> None:
mock_repo.side_effect = IntegrityError
self._add_responses()
link_all_repos(
integration_key=self.key,
integration_id=self.integration.id,
organization_id=self.organization.id,
)
assert len(mock_record.mock_calls) == 4
start1, start2, end2, end1 = mock_record.mock_calls
assert start1.args[0] == EventLifecycleOutcome.STARTED
assert start2.args[0] == EventLifecycleOutcome.STARTED
assert end2.args[0] == EventLifecycleOutcome.SUCCESS
assert end1.args[0] == EventLifecycleOutcome.HALTED
assert_halt_metric(mock_record, LinkAllReposHaltReason.REPOSITORY_NOT_CREATED.value)
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
@patch("sentry.integrations.services.repository.repository_service.create_repository")
@patch("sentry.plugins.providers.IntegrationRepositoryProvider.on_delete_repository")
@responses.activate
def test_link_all_repos_repo_creation_exception(
self, mock_delete_repo, mock_create_repository, mock_record, _
):
mock_create_repository.return_value = None
mock_delete_repo.side_effect = Exception
self._add_responses()
with pytest.raises(Exception):
link_all_repos(
integration_key=self.key,
integration_id=self.integration.id,
organization_id=self.organization.id,
)
assert len(mock_record.mock_calls) == 4
start1, start2, end2, end1 = mock_record.mock_calls
assert start1.args[0] == EventLifecycleOutcome.STARTED
assert start2.args[0] == EventLifecycleOutcome.STARTED
assert end2.args[0] == EventLifecycleOutcome.SUCCESS
assert end1.args[0] == EventLifecycleOutcome.FAILURE