1
|
|
|
"""Test kytos.core.db module.""" |
2
|
|
|
# pylint: disable=invalid-name |
3
|
|
|
|
4
|
|
|
from unittest import TestCase |
5
|
|
|
from unittest.mock import MagicMock, call, patch |
6
|
|
|
|
7
|
|
|
from pymongo.errors import OperationFailure |
8
|
|
|
|
9
|
|
|
from kytos.core.db import (Mongo, _log_pymongo_thread_traceback, |
10
|
|
|
_mongo_conn_wait, db_conn_wait) |
11
|
|
|
from kytos.core.exceptions import KytosDBInitException |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class TestDb(TestCase): |
15
|
|
|
"""TestDB.""" |
16
|
|
|
|
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
|
|
|
serverselectiontimeoutms=10000) |
30
|
|
|
assert client.db.command.mock_calls == [call("hello")] |
31
|
|
|
|
32
|
|
|
@patch("kytos.core.db.time.sleep") |
33
|
|
|
def test_mongo_conn_wait_retries(self, mock_sleep) -> None: |
34
|
|
|
"""test _mongo_conn_wait with retries.""" |
35
|
|
|
client = MagicMock() |
36
|
|
|
mongo_client = MagicMock(return_value=client) |
37
|
|
|
client.db.command.side_effect = OperationFailure("err") |
38
|
|
|
retries = 2 |
39
|
|
|
with self.assertRaises(KytosDBInitException): |
40
|
|
|
_mongo_conn_wait(mongo_client, retries=retries) |
41
|
|
|
assert mongo_client.call_count == retries |
42
|
|
|
mock_sleep.assert_called() |
43
|
|
|
|
44
|
|
|
@staticmethod |
45
|
|
|
@patch("kytos.core.db._mongo_conn_wait") |
46
|
|
|
def test_db_conn_wait(mock_mongo_conn_wait) -> None: |
47
|
|
|
"""test db_conn_wait.""" |
48
|
|
|
db_conn_wait("mongodb") |
49
|
|
|
assert mock_mongo_conn_wait.call_count == 1 |
50
|
|
|
|
51
|
|
|
def test_db_conn_wait_unsupported_backend(self) -> None: |
52
|
|
|
"""test db_conn_wait unsupported backend.""" |
53
|
|
|
with self.assertRaises(KytosDBInitException): |
54
|
|
|
db_conn_wait("invalid") |
55
|
|
|
|
56
|
|
|
def test_boostrap_index(self) -> None: |
57
|
|
|
"""test_boostrap_index.""" |
58
|
|
|
db = self.client[Mongo().db_name] |
59
|
|
|
coll = "switches" |
60
|
|
|
|
61
|
|
|
db[coll].index_information.return_value = {} |
62
|
|
|
keys = [("interfaces.id", 1)] |
63
|
|
|
Mongo().bootstrap_index(coll, keys) |
64
|
|
|
assert db[coll].create_index.call_count == 1 |
65
|
|
|
db[coll].create_index.assert_called_with(keys, background=True) |
66
|
|
|
|
67
|
|
|
keys = [("interfaces.id", 1), ("interfaces.name", 1)] |
68
|
|
|
Mongo().bootstrap_index(coll, keys) |
69
|
|
|
assert db[coll].create_index.call_count == 2 |
70
|
|
|
db[coll].create_index.assert_called_with(keys, |
71
|
|
|
background=True) |
72
|
|
|
|
73
|
|
|
@staticmethod |
74
|
|
|
@patch("kytos.core.db.LOG") |
75
|
|
|
@patch("kytos.core.db.sys") |
76
|
|
|
def test_log_pymongo_thread_traceback(mock_sys, mock_log) -> None: |
77
|
|
|
"""test _log_pymongo_thread_traceback.""" |
78
|
|
|
|
79
|
|
|
mock_sys.stderr = 1 |
80
|
|
|
_log_pymongo_thread_traceback() |
81
|
|
|
mock_sys.exc_info.assert_called() |
82
|
|
|
mock_log.error.assert_called() |
83
|
|
|
|