|
1
|
|
|
"""Test kytos.core.db module.""" |
|
2
|
|
|
from unittest import TestCase |
|
3
|
|
|
from unittest.mock import call, MagicMock, patch |
|
4
|
|
|
|
|
5
|
|
|
from kytos.core.db import ( |
|
6
|
|
|
_mongo_conn_wait, |
|
7
|
|
|
Mongo, |
|
8
|
|
|
db_conn_wait, |
|
9
|
|
|
_log_pymongo_thread_traceback, |
|
10
|
|
|
) |
|
11
|
|
|
from kytos.core.exceptions import KytosDBInitException |
|
12
|
|
|
|
|
13
|
|
|
from pymongo.errors import OperationFailure |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class TestDb(TestCase): |
|
17
|
|
|
def setUp(self): |
|
18
|
|
|
"""setUp.""" |
|
19
|
|
|
self.client = MagicMock() |
|
20
|
|
|
Mongo.client = self.client |
|
21
|
|
|
|
|
22
|
|
|
@staticmethod |
|
23
|
|
|
def test_mongo_conn_wait() -> None: |
|
24
|
|
|
"""test _mongo_conn_wait.""" |
|
25
|
|
|
client = MagicMock() |
|
26
|
|
|
mongo_client = MagicMock(return_value=client) |
|
27
|
|
|
_mongo_conn_wait(mongo_client) |
|
28
|
|
|
mongo_client.assert_called_with(maxpoolsize=6, minpoolsize=3) |
|
29
|
|
|
assert client.db.command.mock_calls == [call("hello")] |
|
30
|
|
|
|
|
31
|
|
|
@patch("kytos.core.db.time.sleep") |
|
32
|
|
|
def test_mongo_conn_wait_retries(self, mock_sleep) -> None: |
|
33
|
|
|
"""test _mongo_conn_wait with retries.""" |
|
34
|
|
|
client = MagicMock() |
|
35
|
|
|
mongo_client = MagicMock(return_value=client) |
|
36
|
|
|
client.db.command.side_effect = OperationFailure("err") |
|
37
|
|
|
retries = 2 |
|
38
|
|
|
with self.assertRaises(KytosDBInitException): |
|
39
|
|
|
_mongo_conn_wait(mongo_client, retries=retries) |
|
40
|
|
|
assert mongo_client.call_count == retries |
|
41
|
|
|
mock_sleep.assert_called() |
|
42
|
|
|
|
|
43
|
|
|
@staticmethod |
|
44
|
|
|
@patch("kytos.core.db._mongo_conn_wait") |
|
45
|
|
|
def test_db_conn_wait(mock_mongo_conn_wait) -> None: |
|
46
|
|
|
"""test db_conn_wait.""" |
|
47
|
|
|
db_conn_wait("mongodb") |
|
48
|
|
|
assert mock_mongo_conn_wait.call_count == 1 |
|
49
|
|
|
|
|
50
|
|
|
def test_db_conn_wait_unsupported_backend(self) -> None: |
|
51
|
|
|
"""test db_conn_wait unsupported backend.""" |
|
52
|
|
|
with self.assertRaises(KytosDBInitException): |
|
53
|
|
|
db_conn_wait("invalid") |
|
54
|
|
|
|
|
55
|
|
|
def test_boostrap_index(self) -> None: |
|
56
|
|
|
"""test_boostrap_index.""" |
|
57
|
|
|
db = self.client[Mongo().db_name] |
|
58
|
|
|
coll = "switches" |
|
59
|
|
|
|
|
60
|
|
|
db[coll].index_information.return_value = {} |
|
61
|
|
|
Mongo().bootstrap_index(coll, "interfaces.id", 1) |
|
62
|
|
|
assert db[coll].create_index.call_count == 1 |
|
63
|
|
|
db[coll].create_index.assert_called_with([("interfaces.id", 1)]) |
|
64
|
|
|
|
|
65
|
|
|
@staticmethod |
|
66
|
|
|
@patch("kytos.core.db.LOG") |
|
67
|
|
|
@patch("kytos.core.db.sys") |
|
68
|
|
|
def test_log_pymongo_thread_traceback(mock_sys, mock_log) -> None: |
|
69
|
|
|
"""test _log_pymongo_thread_traceback.""" |
|
70
|
|
|
|
|
71
|
|
|
mock_sys.stderr = 1 |
|
72
|
|
|
_log_pymongo_thread_traceback() |
|
73
|
|
|
mock_sys.exc_info.assert_called() |
|
74
|
|
|
mock_log.error.assert_called() |
|
75
|
|
|
|