1
|
|
|
"""Module to test MaintenanceController""" |
2
|
|
|
|
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import MagicMock, patch, call |
5
|
|
|
|
6
|
|
|
from datetime import datetime, timedelta |
7
|
|
|
import pytz |
8
|
|
|
|
9
|
|
|
from napps.kytos.maintenance.controllers import MaintenanceController |
10
|
|
|
from napps.kytos.maintenance.models import MaintenanceWindow, MaintenanceWindows |
11
|
|
|
|
12
|
|
|
class TestMaintenanceController(TestCase): |
13
|
|
|
"""Test the MaintenanceController Class""" |
14
|
|
|
|
15
|
|
|
def setUp(self) -> None: |
16
|
|
|
self.controller = MaintenanceController(MagicMock()) |
17
|
|
|
self.now = datetime.now(pytz.utc) |
18
|
|
|
self.window_dict = { |
19
|
|
|
'id': 'Test Window', |
20
|
|
|
'description': '', |
21
|
|
|
'start': self.now + timedelta(hours=1), |
22
|
|
|
'end': self.now + timedelta(hours=2), |
23
|
|
|
'status': 'pending', |
24
|
|
|
'switches': [], |
25
|
|
|
'interfaces': [], |
26
|
|
|
'links': [], |
27
|
|
|
'updated_at': self.now - timedelta(days=1), |
28
|
|
|
'inserted_at': self.now - timedelta(days=1), |
29
|
|
|
} |
30
|
|
|
self.window = MaintenanceWindow.construct( |
31
|
|
|
id = 'Test Window', |
32
|
|
|
description = '', |
33
|
|
|
start = self.now + timedelta(hours=1), |
34
|
|
|
end = self.now + timedelta(hours=2), |
35
|
|
|
status = 'pending', |
36
|
|
|
switches = [], |
37
|
|
|
interfaces = [], |
38
|
|
|
links = [], |
39
|
|
|
updated_at = self.now - timedelta(days=1), |
40
|
|
|
inserted_at = self.now - timedelta(days=1), |
41
|
|
|
) |
42
|
|
|
|
43
|
|
|
def test_bootstrap_indexes(self) -> None: |
44
|
|
|
"""Check that the proper indexes were bootstrapped""" |
45
|
|
|
self.controller.bootstrap_indexes() |
46
|
|
|
windows = self.controller.windows |
47
|
|
|
expected_indexes = [ |
48
|
|
|
call("maintenance.windows", [("id", 1)], unique=True), |
49
|
|
|
] |
50
|
|
|
mock = self.controller.mongo.bootstrap_index |
51
|
|
|
indexes = mock.call_args_list |
52
|
|
|
self.assertEqual(indexes, expected_indexes) |
53
|
|
|
|
54
|
|
|
@patch('napps.kytos.maintenance.controllers.datetime') |
55
|
|
|
def test_insert_window(self, dt_class): |
56
|
|
|
"""Test inserting a window.""" |
57
|
|
|
now_func = dt_class.now |
58
|
|
|
now_func.return_value = self.now |
59
|
|
|
self.controller.insert_window(self.window) |
60
|
|
|
self.controller.windows.insert_one.assert_called_once_with( |
61
|
|
|
{ |
62
|
|
|
**self.window_dict, |
63
|
|
|
'inserted_at': self.now, |
64
|
|
|
'updated_at': self.now, |
65
|
|
|
} |
66
|
|
|
) |
67
|
|
|
|
68
|
|
|
def test_update_window(self): |
69
|
|
|
"""Test updating a window.""" |
70
|
|
|
self.controller.update_window(self.window) |
71
|
|
|
dict_copy = self.window_dict.copy() |
72
|
|
|
del dict_copy['inserted_at'] |
73
|
|
|
del dict_copy['updated_at'] |
74
|
|
|
self.controller.windows.update_one.assert_called_once_with( |
75
|
|
|
{'id': self.window.id}, |
76
|
|
|
[{ |
77
|
|
|
'$set': { |
78
|
|
|
**dict_copy, |
79
|
|
|
'updated_at': '$$NOW', |
80
|
|
|
}, |
81
|
|
|
}], |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
def test_get_window_1(self): |
85
|
|
|
"""Test getting a window that exists.""" |
86
|
|
|
mw_id = 'Test Window' |
87
|
|
|
self.controller.windows.find_one.return_value = self.window_dict |
88
|
|
|
result = self.controller.get_window(mw_id) |
89
|
|
|
self.assertEqual(result, self.window) |
90
|
|
|
|
91
|
|
|
def test_get_window_2(self): |
92
|
|
|
"""Test getting a window that does not exist.""" |
93
|
|
|
mw_id = 'Test Window' |
94
|
|
|
self.controller.windows.find_one.return_value = None |
95
|
|
|
result = self.controller.get_window(mw_id) |
96
|
|
|
self.assertEqual(result, None) |
97
|
|
|
|
98
|
|
|
def test_get_windows(self): |
99
|
|
|
"""Test getting the set of windows.""" |
100
|
|
|
self.controller.windows.find.return_value = [self.window_dict] |
101
|
|
|
expected = MaintenanceWindows.construct(__root__ = [self.window]) |
102
|
|
|
result = self.controller.get_windows() |
103
|
|
|
self.assertEqual(result, expected) |