Skip to content

Commit 4fd1d20

Browse files
authored
Faster China - tests (#169)
1 parent 285982f commit 4fd1d20

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

src/lumigo_tracer/lumigo_utils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,6 @@ def _send_data_to_kinesis(
371371
if not boto3:
372372
get_logger().error("boto3 is missing. Unable to send to Kinesis.")
373373
return None
374-
if not botocore:
375-
get_logger().error("botocore is missing. Unable to send to Kinesis.")
376-
return None
377374
client = _get_edge_kinesis_boto_client(
378375
region=region,
379376
aws_access_key_id=aws_access_key_id,

src/test/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def cancel_timeout_mechanism(monkeypatch):
2828
@pytest.fixture(autouse=True)
2929
def remove_caches(monkeypatch):
3030
get_omitting_regex.cache_clear()
31+
monkeypatch.setattr(lumigo_utils, "edge_kinesis_boto_client", None)
3132

3233

3334
@pytest.yield_fixture(autouse=True)

src/test/unit/test_lumigo_utils.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import importlib.util
12
import inspect
3+
import logging
24
from collections import OrderedDict
35
from decimal import Decimal
46
import datetime
57
import http.client
6-
from mock import Mock
8+
9+
import boto3
10+
from mock import Mock, MagicMock
711

812
import pytest
913
from lumigo_tracer import lumigo_utils
@@ -427,6 +431,53 @@ def test_report_json_china_no_boto(monkeypatch, reporter_mock, caplog):
427431
)
428432

429433

434+
def test_report_json_china_on_error_no_exception_and_notify_user(capsys, monkeypatch):
435+
monkeypatch.setattr(Configuration, "should_report", True)
436+
monkeypatch.setattr(Configuration, "edge_kinesis_aws_access_key_id", "my_value")
437+
monkeypatch.setattr(Configuration, "edge_kinesis_aws_secret_access_key", "my_value")
438+
monkeypatch.setattr(boto3, "client", MagicMock(side_effect=Exception))
439+
lumigo_utils.get_logger().setLevel(logging.CRITICAL)
440+
441+
report_json(CHINA_REGION, [{"a": "b"}])
442+
443+
assert "Failed to send spans" in capsys.readouterr().out
444+
445+
446+
def test_china_shouldnt_establish_http_connection(monkeypatch):
447+
monkeypatch.setenv("AWS_REGION", CHINA_REGION)
448+
# Reload a duplicate of lumigo_utils
449+
spec = importlib.util.find_spec("lumigo_tracer.lumigo_utils")
450+
lumigo_utils_reloaded = importlib.util.module_from_spec(spec)
451+
spec.loader.exec_module(lumigo_utils_reloaded)
452+
453+
assert lumigo_utils_reloaded.edge_connection is None
454+
455+
456+
def test_china_with_env_variable_shouldnt_reuse_boto3_connection(monkeypatch):
457+
monkeypatch.setenv("LUMIGO_KINESIS_SHOULD_REUSE_CONNECTION", "false")
458+
monkeypatch.setattr(Configuration, "should_report", True)
459+
monkeypatch.setattr(Configuration, "edge_kinesis_aws_access_key_id", "my_value")
460+
monkeypatch.setattr(Configuration, "edge_kinesis_aws_secret_access_key", "my_value")
461+
monkeypatch.setattr(boto3, "client", MagicMock())
462+
463+
report_json(CHINA_REGION, [{"a": "b"}])
464+
report_json(CHINA_REGION, [{"a": "b"}])
465+
466+
assert boto3.client.call_count == 2
467+
468+
469+
def test_china_reuse_boto3_connection(monkeypatch):
470+
monkeypatch.setattr(Configuration, "should_report", True)
471+
monkeypatch.setattr(Configuration, "edge_kinesis_aws_access_key_id", "my_value")
472+
monkeypatch.setattr(Configuration, "edge_kinesis_aws_secret_access_key", "my_value")
473+
monkeypatch.setattr(boto3, "client", MagicMock())
474+
475+
report_json(CHINA_REGION, [{"a": "b"}])
476+
report_json(CHINA_REGION, [{"a": "b"}])
477+
478+
boto3.client.assert_called_once()
479+
480+
430481
@pytest.mark.parametrize("env, expected", [("True", True), ("other", False), ("123", False)])
431482
def test_is_kill_switch_on(monkeypatch, env, expected):
432483
monkeypatch.setenv(KILL_SWITCH, env)

src/test/unit/test_tracer.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77
from functools import wraps
88
import logging
9-
from unittest.mock import MagicMock
9+
from unittest.mock import MagicMock, ANY
1010

1111
import boto3
1212
import pytest
@@ -419,9 +419,10 @@ def lambda_test_function(event, context):
419419
span_sent = json.loads(records[1]["Data"].decode())[0]
420420
assert span_sent["event"] == json.dumps(event)
421421
# Used the client from the decorator params
422-
# boto3.client.assert_called_with(
423-
# "kinesis",
424-
# region_name=china_region_for_test,
425-
# aws_access_key_id=access_key_id,
426-
# aws_secret_access_key=secret_access_key,
427-
# )
422+
boto3.client.assert_called_with(
423+
"kinesis",
424+
region_name=china_region_for_test,
425+
aws_access_key_id=access_key_id,
426+
aws_secret_access_key=secret_access_key,
427+
config=ANY,
428+
)

0 commit comments

Comments
 (0)