|
| 1 | +import importlib.util |
1 | 2 | import inspect |
| 3 | +import logging |
2 | 4 | from collections import OrderedDict |
3 | 5 | from decimal import Decimal |
4 | 6 | import datetime |
5 | 7 | import http.client |
6 | | -from mock import Mock |
| 8 | + |
| 9 | +import boto3 |
| 10 | +from mock import Mock, MagicMock |
7 | 11 |
|
8 | 12 | import pytest |
9 | 13 | from lumigo_tracer import lumigo_utils |
@@ -427,6 +431,53 @@ def test_report_json_china_no_boto(monkeypatch, reporter_mock, caplog): |
427 | 431 | ) |
428 | 432 |
|
429 | 433 |
|
| 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 | + |
430 | 481 | @pytest.mark.parametrize("env, expected", [("True", True), ("other", False), ("123", False)]) |
431 | 482 | def test_is_kill_switch_on(monkeypatch, env, expected): |
432 | 483 | monkeypatch.setenv(KILL_SWITCH, env) |
|
0 commit comments