1
|
|
|
"""Test kytos.core.kytosd module.""" |
2
|
|
|
from unittest import TestCase |
3
|
|
|
from unittest.mock import MagicMock, patch |
4
|
|
|
|
5
|
|
|
from kytos.core.kytosd import _create_pid_dir, async_main, main, start_shell |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestKytosd(TestCase): |
9
|
|
|
"""Kytosd tests.""" |
10
|
|
|
|
11
|
|
|
@staticmethod |
12
|
|
|
@patch('os.makedirs') |
13
|
|
|
@patch('kytos.core.kytosd.BASE_ENV', '/tmp/') |
14
|
|
|
def test_create_pid_dir__env(mock_mkdirs): |
15
|
|
|
"""Test _create_pid_dir method with env.""" |
16
|
|
|
_create_pid_dir() |
17
|
|
|
|
18
|
|
|
mock_mkdirs.assert_called_with('/tmp/var/run/kytos', exist_ok=True) |
19
|
|
|
|
20
|
|
|
@staticmethod |
21
|
|
|
@patch('os.chmod') |
22
|
|
|
@patch('os.makedirs') |
23
|
|
|
@patch('kytos.core.kytosd.BASE_ENV', '/') |
24
|
|
|
def test_create_pid_dir__system(*args): |
25
|
|
|
"""Test _create_pid_dir method with system dir.""" |
26
|
|
|
(mock_mkdirs, mock_chmod) = args |
27
|
|
|
_create_pid_dir() |
28
|
|
|
|
29
|
|
|
mock_mkdirs.assert_called_with('/var/run/kytos', exist_ok=True) |
30
|
|
|
mock_chmod.assert_called_with('/var/run/kytos', 0o1777) |
31
|
|
|
|
32
|
|
|
@staticmethod |
33
|
|
|
@patch('kytos.core.kytosd.InteractiveShellEmbed') |
34
|
|
|
def test_start_shell(mock_interactive_shell): |
35
|
|
|
"""Test stop_api_server method.""" |
36
|
|
|
start_shell(MagicMock()) |
37
|
|
|
|
38
|
|
|
mock_interactive_shell.assert_called() |
39
|
|
|
|
40
|
|
|
@staticmethod |
41
|
|
|
@patch('kytos.core.kytosd.async_main') |
42
|
|
|
@patch('kytos.core.kytosd._create_pid_dir') |
43
|
|
|
@patch('kytos.core.kytosd.KytosConfig') |
44
|
|
|
def test_main__foreground(*args): |
45
|
|
|
"""Test main method in foreground.""" |
46
|
|
|
(mock_kytos_config, mock_create_pid_dir, mock_async_main) = args |
47
|
|
|
config = MagicMock(foreground=True) |
48
|
|
|
options = {'daemon': config} |
49
|
|
|
mock_kytos_config.return_value.options = options |
50
|
|
|
|
51
|
|
|
main() |
52
|
|
|
|
53
|
|
|
mock_create_pid_dir.assert_called() |
54
|
|
|
mock_async_main.assert_called() |
55
|
|
|
|
56
|
|
|
@staticmethod |
57
|
|
|
@patch('kytos.core.kytosd.daemon.DaemonContext') |
58
|
|
|
@patch('kytos.core.kytosd.async_main') |
59
|
|
|
@patch('kytos.core.kytosd._create_pid_dir') |
60
|
|
|
@patch('kytos.core.kytosd.KytosConfig') |
61
|
|
|
def test_main__background(*args): |
62
|
|
|
"""Test main method in background.""" |
63
|
|
|
(mock_kytos_config, mock_create_pid_dir, mock_async_main, _) = args |
64
|
|
|
config = MagicMock(foreground=False) |
65
|
|
|
options = {'daemon': config} |
66
|
|
|
mock_kytos_config.return_value.options = options |
67
|
|
|
|
68
|
|
|
main() |
69
|
|
|
|
70
|
|
|
mock_create_pid_dir.assert_called() |
71
|
|
|
mock_async_main.assert_called() |
72
|
|
|
|
73
|
|
|
@staticmethod |
74
|
|
|
@patch('kytos.core.kytosd.asyncio') |
75
|
|
|
@patch('kytos.core.kytosd.InteractiveShellEmbed') |
76
|
|
|
@patch('kytos.core.kytosd.Controller') |
77
|
|
|
def test_async_main(*args): |
78
|
|
|
"""Test async_main method.""" |
79
|
|
|
(mock_controller, _, mock_asyncio) = args |
80
|
|
|
controller = MagicMock() |
81
|
|
|
controller.options.debug = True |
82
|
|
|
controller.options.foreground = True |
83
|
|
|
mock_controller.return_value = controller |
84
|
|
|
|
85
|
|
|
event_loop = MagicMock() |
86
|
|
|
mock_asyncio.get_event_loop.return_value = event_loop |
87
|
|
|
|
88
|
|
|
async_main(MagicMock()) |
89
|
|
|
|
90
|
|
|
event_loop.call_soon.assert_called_with(controller.start) |
91
|
|
|
|