|
1
|
|
|
"""Test kytos.core.db module.""" |
|
2
|
|
|
# pylint: disable=invalid-name |
|
3
|
|
|
|
|
4
|
1 |
|
from unittest.mock import MagicMock, call, patch |
|
5
|
|
|
|
|
6
|
1 |
|
import pytest |
|
7
|
1 |
|
from pymongo.errors import OperationFailure |
|
8
|
|
|
|
|
9
|
1 |
|
from kytos.core.db import (Mongo, _log_pymongo_thread_traceback, |
|
10
|
|
|
_mongo_conn_wait, db_conn_wait) |
|
11
|
1 |
|
from kytos.core.db import mongo_client as _mongo_client |
|
12
|
1 |
|
from kytos.core.exceptions import KytosDBInitException |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
1 |
|
class TestDb: |
|
16
|
|
|
"""TestDB.""" |
|
17
|
|
|
|
|
18
|
1 |
|
def setup_method(self): |
|
19
|
|
|
"""setup_method""" |
|
20
|
1 |
|
self.client = MagicMock() |
|
21
|
1 |
|
Mongo.client = self.client |
|
22
|
|
|
|
|
23
|
1 |
|
@patch("kytos.core.db.MongoClient") |
|
24
|
1 |
|
def test_default_args(self, mock_client) -> None: |
|
25
|
|
|
"""test default args.""" |
|
26
|
1 |
|
_mongo_client( |
|
27
|
|
|
host_seeds="mongo1:27017,mongo2:27018,mongo3:27019", |
|
28
|
|
|
username="invalid_user", |
|
29
|
|
|
password="invalid_password", |
|
30
|
|
|
database="napps", |
|
31
|
|
|
maxpoolsize=300, |
|
32
|
|
|
minpoolsize=30, |
|
33
|
|
|
serverselectiontimeoutms=30000, |
|
34
|
|
|
) |
|
35
|
1 |
|
assert mock_client.call_count == 1 |
|
36
|
1 |
|
mock_client.assert_called_with( |
|
37
|
|
|
["mongo1:27017", "mongo2:27018", "mongo3:27019"], |
|
38
|
|
|
username="invalid_user", |
|
39
|
|
|
password="invalid_password", |
|
40
|
|
|
connect=False, |
|
41
|
|
|
authsource="napps", |
|
42
|
|
|
retrywrites=True, |
|
43
|
|
|
retryreads=True, |
|
44
|
|
|
readpreference="primaryPreferred", |
|
45
|
|
|
w="majority", |
|
46
|
|
|
maxpoolsize=300, |
|
47
|
|
|
minpoolsize=30, |
|
48
|
|
|
readconcernlevel="majority", |
|
49
|
|
|
serverselectiontimeoutms=30000, |
|
50
|
|
|
) |
|
51
|
|
|
|
|
52
|
1 |
|
@staticmethod |
|
53
|
1 |
|
def test_mongo_conn_wait() -> None: |
|
54
|
|
|
"""test _mongo_conn_wait.""" |
|
55
|
1 |
|
client = MagicMock() |
|
56
|
1 |
|
mongo_client = MagicMock(return_value=client) |
|
57
|
1 |
|
_mongo_conn_wait(mongo_client) |
|
58
|
1 |
|
mongo_client.assert_called_with(maxpoolsize=6, minpoolsize=3, |
|
59
|
|
|
serverselectiontimeoutms=10000) |
|
60
|
1 |
|
assert client.db.command.mock_calls == [call("hello")] |
|
61
|
|
|
|
|
62
|
1 |
|
@patch("kytos.core.db.time.sleep") |
|
63
|
1 |
|
def test_mongo_conn_wait_retries(self, mock_sleep) -> None: |
|
64
|
|
|
"""test _mongo_conn_wait with retries.""" |
|
65
|
1 |
|
client = MagicMock() |
|
66
|
1 |
|
mongo_client = MagicMock(return_value=client) |
|
67
|
1 |
|
client.db.command.side_effect = OperationFailure("err") |
|
68
|
1 |
|
retries = 2 |
|
69
|
1 |
|
with pytest.raises(KytosDBInitException): |
|
70
|
1 |
|
_mongo_conn_wait(mongo_client, retries=retries) |
|
71
|
1 |
|
assert mongo_client.call_count == retries |
|
72
|
1 |
|
mock_sleep.assert_called() |
|
73
|
|
|
|
|
74
|
1 |
|
@staticmethod |
|
75
|
1 |
|
@patch("kytos.core.db._mongo_conn_wait") |
|
76
|
1 |
|
def test_db_conn_wait(mock_mongo_conn_wait) -> None: |
|
77
|
|
|
"""test db_conn_wait.""" |
|
78
|
1 |
|
db_conn_wait("mongodb") |
|
79
|
1 |
|
assert mock_mongo_conn_wait.call_count == 1 |
|
80
|
|
|
|
|
81
|
1 |
|
def test_db_conn_wait_unsupported_backend(self) -> None: |
|
82
|
|
|
"""test db_conn_wait unsupported backend.""" |
|
83
|
1 |
|
with pytest.raises(KytosDBInitException): |
|
84
|
1 |
|
db_conn_wait("invalid") |
|
85
|
|
|
|
|
86
|
1 |
|
def test_boostrap_index(self) -> None: |
|
87
|
|
|
"""test_boostrap_index.""" |
|
88
|
1 |
|
db = self.client[Mongo().db_name] |
|
89
|
1 |
|
coll = "switches" |
|
90
|
|
|
|
|
91
|
1 |
|
db[coll].index_information.return_value = {} |
|
92
|
1 |
|
keys = [("interfaces.id", 1)] |
|
93
|
1 |
|
Mongo().bootstrap_index(coll, keys) |
|
94
|
1 |
|
assert db[coll].create_index.call_count == 1 |
|
95
|
1 |
|
db[coll].create_index.assert_called_with(keys, |
|
96
|
|
|
background=True, |
|
97
|
|
|
maxTimeMS=30000) |
|
98
|
|
|
|
|
99
|
1 |
|
keys = [("interfaces.id", 1), ("interfaces.name", 1)] |
|
100
|
1 |
|
Mongo().bootstrap_index(coll, keys) |
|
101
|
1 |
|
assert db[coll].create_index.call_count == 2 |
|
102
|
1 |
|
db[coll].create_index.assert_called_with(keys, |
|
103
|
|
|
background=True, |
|
104
|
|
|
maxTimeMS=30000) |
|
105
|
|
|
|
|
106
|
1 |
|
@staticmethod |
|
107
|
1 |
|
@patch("kytos.core.db.LOG") |
|
108
|
1 |
|
@patch("kytos.core.db.sys") |
|
109
|
1 |
|
def test_log_pymongo_thread_traceback(mock_sys, mock_log) -> None: |
|
110
|
|
|
"""test _log_pymongo_thread_traceback.""" |
|
111
|
|
|
|
|
112
|
1 |
|
mock_sys.stderr = 1 |
|
113
|
1 |
|
_log_pymongo_thread_traceback() |
|
114
|
1 |
|
mock_sys.exc_info.assert_called() |
|
115
|
|
|
mock_log.error.assert_called() |
|
116
|
|
|
|