|
1
|
|
|
"""Test kytos.core.cpm module.""" |
|
2
|
1 |
|
from unittest.mock import MagicMock, patch |
|
3
|
|
|
|
|
4
|
1 |
|
import pytest |
|
5
|
|
|
|
|
6
|
1 |
|
from kytos.core.apm import ElasticAPM, begin_span, init_apm |
|
7
|
1 |
|
from kytos.core.exceptions import KytosAPMInitException |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
1 |
|
class TestElasticAPM: |
|
11
|
|
|
"""TestElasticAPM.""" |
|
12
|
|
|
|
|
13
|
1 |
|
def setup_method(self): |
|
14
|
|
|
"""setup_method.""" |
|
15
|
1 |
|
ElasticAPM._client = None |
|
16
|
|
|
|
|
17
|
1 |
|
@patch("kytos.core.apm.execution_context") |
|
18
|
1 |
|
def test_begin_span_decorator(self, context): |
|
19
|
|
|
"""Test begin span_decorator.""" |
|
20
|
|
|
|
|
21
|
1 |
|
def inner(): |
|
22
|
1 |
|
pass |
|
23
|
|
|
|
|
24
|
1 |
|
begin_span(inner)() |
|
25
|
1 |
|
assert context.get_transaction.call_count == 1 |
|
26
|
|
|
|
|
27
|
1 |
|
@patch("kytos.core.apm.ElasticAPM.init_client") |
|
28
|
1 |
|
def test_init_apm(self, mock_init): |
|
29
|
|
|
"""Test init_apm.""" |
|
30
|
1 |
|
init_apm("es") |
|
31
|
1 |
|
assert mock_init.call_count == 1 |
|
32
|
|
|
|
|
33
|
1 |
|
with pytest.raises(KytosAPMInitException): |
|
34
|
1 |
|
init_apm("unknown_apm") |
|
35
|
1 |
|
assert mock_init.call_count == 1 |
|
36
|
|
|
|
|
37
|
1 |
|
@patch("kytos.core.apm.ElasticAPM.init_client") |
|
38
|
1 |
|
def test_get_client(self, mock_init): |
|
39
|
|
|
"""Test get_client.""" |
|
40
|
1 |
|
assert not ElasticAPM._client |
|
41
|
1 |
|
ElasticAPM.get_client() |
|
42
|
1 |
|
assert mock_init.call_count == 1 |
|
43
|
1 |
|
ElasticAPM._client = MagicMock() |
|
44
|
1 |
|
ElasticAPM.get_client() |
|
45
|
1 |
|
assert mock_init.call_count == 1 |
|
46
|
|
|
|
|
47
|
1 |
|
@patch("kytos.core.apm.Client") |
|
48
|
1 |
|
def test_init_client(self, mock_client): |
|
49
|
|
|
"""Test init_client.""" |
|
50
|
1 |
|
mock_app = MagicMock() |
|
51
|
1 |
|
assert not ElasticAPM._client |
|
52
|
1 |
|
ElasticAPM.init_client(app=mock_app) |
|
53
|
1 |
|
assert mock_client.call_count == 1 |
|
54
|
|
|
assert mock_app.add_middleware.call_count == 1 |
|
55
|
|
|
|