1
1
import platform
2
2
from datetime import timedelta
3
+ from types import SimpleNamespace
3
4
from unittest .mock import patch
4
5
from uuid import uuid4
5
6
18
19
19
20
20
21
@no_silo_test
22
+ @patch ("psutil.cpu_count" , return_value = 8 )
23
+ @patch ("psutil.cpu_percent" , return_value = 50 )
24
+ @patch (
25
+ "psutil.virtual_memory" ,
26
+ return_value = SimpleNamespace (
27
+ total = 34359738368 ,
28
+ percent = 50 ,
29
+ ),
30
+ )
21
31
class SendBeaconTest (OutcomesSnubaTest ):
22
32
def setUp (self ):
23
33
super ().setUp ()
@@ -87,7 +97,15 @@ def setUp(self):
87
97
@patch ("sentry.tasks.beacon.safe_urlopen" )
88
98
@patch ("sentry.tasks.beacon.safe_urlread" )
89
99
@responses .activate
90
- def test_simple (self , safe_urlread , safe_urlopen , mock_get_all_package_versions ):
100
+ def test_simple (
101
+ self ,
102
+ safe_urlread ,
103
+ safe_urlopen ,
104
+ mock_get_all_package_versions ,
105
+ mock_cpu_count ,
106
+ mock_cpu_percent ,
107
+ mock_virtual_memory ,
108
+ ):
91
109
self .organization
92
110
self .project
93
111
self .team
@@ -96,6 +114,7 @@ def test_simple(self, safe_urlread, safe_urlopen, mock_get_all_package_versions)
96
114
97
115
assert options .
set (
"system.admin-email" ,
"[email protected] " )
98
116
assert options .set ("beacon.anonymous" , False )
117
+ assert options .set ("beacon.record_cpu_ram_usage" , True )
99
118
send_beacon ()
100
119
101
120
install_id = options .get ("sentry:install-id" )
@@ -119,6 +138,10 @@ def test_simple(self, safe_urlread, safe_urlopen, mock_get_all_package_versions)
119
138
"replays.24h" : 1 ,
120
139
"profiles.24h" : 3 ,
121
140
"monitors.24h" : 0 ,
141
+ "cpu_cores_available" : 8 ,
142
+ "cpu_percentage_utilized" : 50 ,
143
+ "ram_available_gb" : 32 ,
144
+ "ram_percentage_utilized" : 50 ,
122
145
},
123
146
"anonymous" : False ,
124
147
"admin_email" :
"[email protected] " ,
@@ -134,7 +157,75 @@ def test_simple(self, safe_urlread, safe_urlopen, mock_get_all_package_versions)
134
157
@patch ("sentry.tasks.beacon.safe_urlopen" )
135
158
@patch ("sentry.tasks.beacon.safe_urlread" )
136
159
@responses .activate
137
- def test_anonymous (self , safe_urlread , safe_urlopen , mock_get_all_package_versions ):
160
+ def test_no_cpu_ram_usage (
161
+ self ,
162
+ safe_urlread ,
163
+ safe_urlopen ,
164
+ mock_get_all_package_versions ,
165
+ mock_cpu_count ,
166
+ mock_cpu_percent ,
167
+ mock_virtual_memory ,
168
+ ):
169
+ self .organization
170
+ self .project
171
+ self .team
172
+ mock_get_all_package_versions .return_value = {"foo" : "1.0" }
173
+ safe_urlread .return_value = json .dumps ({"notices" : [], "version" : {"stable" : "1.0.0" }})
174
+
175
+ assert options .
set (
"system.admin-email" ,
"[email protected] " )
176
+ assert options .set ("beacon.anonymous" , False )
177
+ assert options .set ("beacon.record_cpu_ram_usage" , False )
178
+ send_beacon ()
179
+
180
+ install_id = options .get ("sentry:install-id" )
181
+ assert install_id and len (install_id ) == 40
182
+
183
+ safe_urlopen .assert_called_once_with (
184
+ BEACON_URL ,
185
+ json = {
186
+ "install_id" : install_id ,
187
+ "version" : sentry .get_version (),
188
+ "docker" : sentry .is_docker (),
189
+ "python_version" : platform .python_version (),
190
+ "data" : {
191
+ "organizations" : 2 ,
192
+ "users" : 1 ,
193
+ "projects" : 2 ,
194
+ "teams" : 2 ,
195
+ "events.24h" : 8 , # We expect the number of events to be the sum of events from two orgs. First org has 5 events while the second org has 3 events.
196
+ "errors.24h" : 8 ,
197
+ "transactions.24h" : 2 ,
198
+ "replays.24h" : 1 ,
199
+ "profiles.24h" : 3 ,
200
+ "monitors.24h" : 0 ,
201
+ "cpu_cores_available" : None ,
202
+ "cpu_percentage_utilized" : None ,
203
+ "ram_available_gb" : None ,
204
+ "ram_percentage_utilized" : None ,
205
+ },
206
+ "anonymous" : False ,
207
+ "admin_email" :
"[email protected] " ,
208
+ "packages" : mock_get_all_package_versions .return_value ,
209
+ },
210
+ timeout = 5 ,
211
+ )
212
+ safe_urlread .assert_called_once_with (safe_urlopen .return_value )
213
+
214
+ assert options .get ("sentry:latest_version" ) == "1.0.0"
215
+
216
+ @patch ("sentry.tasks.beacon.get_all_package_versions" )
217
+ @patch ("sentry.tasks.beacon.safe_urlopen" )
218
+ @patch ("sentry.tasks.beacon.safe_urlread" )
219
+ @responses .activate
220
+ def test_anonymous (
221
+ self ,
222
+ safe_urlread ,
223
+ safe_urlopen ,
224
+ mock_get_all_package_versions ,
225
+ mock_cpu_count ,
226
+ mock_cpu_percent ,
227
+ mock_virtual_memory ,
228
+ ):
138
229
self .organization
139
230
self .project
140
231
self .team
@@ -143,6 +234,7 @@ def test_anonymous(self, safe_urlread, safe_urlopen, mock_get_all_package_versio
143
234
144
235
assert options .
set (
"system.admin-email" ,
"[email protected] " )
145
236
assert options .set ("beacon.anonymous" , True )
237
+ assert options .set ("beacon.record_cpu_ram_usage" , True )
146
238
send_beacon ()
147
239
148
240
install_id = options .get ("sentry:install-id" )
@@ -166,6 +258,10 @@ def test_anonymous(self, safe_urlread, safe_urlopen, mock_get_all_package_versio
166
258
"replays.24h" : 1 ,
167
259
"profiles.24h" : 3 ,
168
260
"monitors.24h" : 0 ,
261
+ "cpu_cores_available" : 8 ,
262
+ "cpu_percentage_utilized" : 50 ,
263
+ "ram_available_gb" : 32 ,
264
+ "ram_percentage_utilized" : 50 ,
169
265
},
170
266
"anonymous" : True ,
171
267
"packages" : mock_get_all_package_versions .return_value ,
@@ -180,7 +276,15 @@ def test_anonymous(self, safe_urlread, safe_urlopen, mock_get_all_package_versio
180
276
@patch ("sentry.tasks.beacon.safe_urlopen" )
181
277
@patch ("sentry.tasks.beacon.safe_urlread" )
182
278
@responses .activate
183
- def test_with_broadcasts (self , safe_urlread , safe_urlopen , mock_get_all_package_versions ):
279
+ def test_with_broadcasts (
280
+ self ,
281
+ safe_urlread ,
282
+ safe_urlopen ,
283
+ mock_get_all_package_versions ,
284
+ mock_cpu_count ,
285
+ mock_cpu_percent ,
286
+ mock_virtual_memory ,
287
+ ):
184
288
broadcast_id = uuid4 ().hex
185
289
mock_get_all_package_versions .return_value = {}
186
290
safe_urlread .return_value = json .dumps (
@@ -236,7 +340,15 @@ def test_with_broadcasts(self, safe_urlread, safe_urlopen, mock_get_all_package_
236
340
@patch ("sentry.tasks.beacon.safe_urlopen" )
237
341
@patch ("sentry.tasks.beacon.safe_urlread" )
238
342
@responses .activate
239
- def test_disabled (self , safe_urlread , safe_urlopen , mock_get_all_package_versions ):
343
+ def test_disabled (
344
+ self ,
345
+ safe_urlread ,
346
+ safe_urlopen ,
347
+ mock_get_all_package_versions ,
348
+ mock_cpu_count ,
349
+ mock_cpu_percent ,
350
+ mock_virtual_memory ,
351
+ ):
240
352
mock_get_all_package_versions .return_value = {"foo" : "1.0" }
241
353
242
354
with self .settings (SENTRY_BEACON = False ):
@@ -248,7 +360,15 @@ def test_disabled(self, safe_urlread, safe_urlopen, mock_get_all_package_version
248
360
@patch ("sentry.tasks.beacon.safe_urlopen" )
249
361
@patch ("sentry.tasks.beacon.safe_urlread" )
250
362
@responses .activate
251
- def test_debug (self , safe_urlread , safe_urlopen , mock_get_all_package_versions ):
363
+ def test_debug (
364
+ self ,
365
+ safe_urlread ,
366
+ safe_urlopen ,
367
+ mock_get_all_package_versions ,
368
+ mock_cpu_count ,
369
+ mock_cpu_percent ,
370
+ mock_virtual_memory ,
371
+ ):
252
372
mock_get_all_package_versions .return_value = {"foo" : "1.0" }
253
373
254
374
with self .settings (DEBUG = True ):
@@ -258,7 +378,13 @@ def test_debug(self, safe_urlread, safe_urlopen, mock_get_all_package_versions):
258
378
259
379
@patch ("sentry.tasks.beacon.safe_urlopen" )
260
380
@responses .activate
261
- def test_metrics (self , safe_urlopen ):
381
+ def test_metrics (
382
+ self ,
383
+ safe_urlopen ,
384
+ mock_cpu_count ,
385
+ mock_cpu_percent ,
386
+ mock_virtual_memory ,
387
+ ):
262
388
metrics = [
263
389
{
264
390
"description" : "SentryApp" ,
0 commit comments