1
|
|
|
"""kytos.utils.napps.NAppsManager tests.""" |
2
|
|
|
import unittest |
3
|
|
|
from unittest.mock import MagicMock, Mock, PropertyMock, patch |
4
|
|
|
|
5
|
|
|
from kytos.utils.exceptions import KytosException |
6
|
|
|
from kytos.utils.napps import NAppsManager |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class TestNapps(unittest.TestCase): |
10
|
|
|
"""Test the class APIServer.""" |
11
|
|
|
|
12
|
|
|
def setUp(self): |
13
|
|
|
"""Instantiate a APIServer.""" |
14
|
|
|
|
15
|
|
|
def test_get_napps(self): |
16
|
|
|
"""Test method get_napps used to find |
17
|
|
|
enabled and installed napps. |
18
|
|
|
""" |
19
|
|
|
napps_manager = NAppsManager() |
20
|
|
|
mock_path = Mock() |
21
|
|
|
|
22
|
|
|
def glob_side_effect(args): |
23
|
|
|
"""Path.glob to mock finding paths with kytos.json file.""" |
24
|
|
|
self.assertEqual(args, "*/*/kytos.json") |
25
|
|
|
|
26
|
|
|
mock_path1 = Mock() |
27
|
|
|
mock_path1.parts = ['kytos', 'of_core', 'kytos.json'] |
28
|
|
|
|
29
|
|
|
mock_path2 = Mock() |
30
|
|
|
mock_path2.parts = ['kytos', 'of_lldp', 'kytos.json'] |
31
|
|
|
|
32
|
|
|
return [mock_path1, mock_path2] |
33
|
|
|
|
34
|
|
|
mock_path.glob = glob_side_effect |
35
|
|
|
|
36
|
|
|
# pylint: disable=protected-access |
37
|
|
|
get_return = napps_manager._get_napps(mock_path) |
38
|
|
|
self.assertEqual(get_return[0][0], 'kytos') |
39
|
|
|
self.assertEqual(get_return[0][1], 'of_core') |
40
|
|
|
self.assertEqual(get_return[1][0], 'kytos') |
41
|
|
|
self.assertEqual(get_return[1][1], 'of_lldp') |
42
|
|
|
|
43
|
|
|
def test_get_enabled_local(self): |
44
|
|
|
"""Test get_enabled_local used to find |
45
|
|
|
enabled napps in local machine""" |
46
|
|
|
# Mock kytos.json path |
47
|
|
|
mock_path = Mock() |
48
|
|
|
|
49
|
|
|
def glob_side_effect(args): |
50
|
|
|
"""Path.glob to mock finding paths with kytos.json file.""" |
51
|
|
|
self.assertEqual(args, "*/*/kytos.json") |
52
|
|
|
|
53
|
|
|
mock_path1 = Mock() |
54
|
|
|
mock_path1.parts = ['kytos', 'of_core', 'kytos.json'] |
55
|
|
|
return [mock_path1] |
56
|
|
|
mock_path.glob = glob_side_effect |
57
|
|
|
|
58
|
|
|
# Mock _enabled Path property |
59
|
|
|
mock_prop_enabled = PropertyMock() |
60
|
|
|
with patch.object(NAppsManager, '_enabled', mock_prop_enabled): |
61
|
|
|
mock_prop_enabled.return_value = mock_path |
62
|
|
|
|
63
|
|
|
# Call the get_enabled_local to test |
64
|
|
|
napps_manager = NAppsManager() |
65
|
|
|
get_return = napps_manager.get_enabled_local() |
66
|
|
|
self.assertEqual(get_return[0][0], 'kytos') |
67
|
|
|
self.assertEqual(get_return[0][1], 'of_core') |
68
|
|
|
|
69
|
|
|
self.assertEqual(mock_prop_enabled.call_count, 1) |
70
|
|
|
|
71
|
|
|
@patch('urllib.request.urlopen') |
72
|
|
|
def test_get_installed(self, mock_urlopen): |
73
|
|
|
"""Test method get_installed to find all installed napps. |
74
|
|
|
""" |
75
|
|
|
# Mocking the API call |
76
|
|
|
mock_response = MagicMock() |
77
|
|
|
mock_response.getcode.return_value = 200 |
78
|
|
|
mock_response.read.return_value = \ |
79
|
|
|
'{"napps": [["kytos", "mef_eline"], ["kytos", "of_lldp"]]}' |
80
|
|
|
mock_response.__enter__.return_value = mock_response |
81
|
|
|
mock_urlopen.return_value = mock_response |
82
|
|
|
|
83
|
|
|
# Call the get_installed method |
84
|
|
|
napps_manager = NAppsManager() |
85
|
|
|
installed_napps = napps_manager.get_installed() |
86
|
|
|
|
87
|
|
|
self.assertEqual(len(installed_napps), 2) |
88
|
|
|
self.assertEqual(installed_napps[0], ("kytos", "mef_eline")) |
89
|
|
|
self.assertEqual(installed_napps[1], ("kytos", "of_lldp")) |
90
|
|
|
|
91
|
|
|
@patch('urllib.request.urlopen') |
92
|
|
|
def test_get_installed__error(self, mock_urlopen): |
93
|
|
|
"""Test method get_installed with API error |
94
|
|
|
""" |
95
|
|
|
# Mocking the API call |
96
|
|
|
mock_response = MagicMock() |
97
|
|
|
mock_response.getcode.return_value = 500 |
98
|
|
|
mock_urlopen.return_value = mock_response |
99
|
|
|
|
100
|
|
|
# Call the get_installed method |
101
|
|
|
napps_manager = NAppsManager() |
102
|
|
|
with self.assertRaises(KytosException) as context: |
103
|
|
|
napps_manager.get_installed() |
104
|
|
|
|
105
|
|
|
self.assertEqual('Error calling Kytos to check installed NApps.', |
106
|
|
|
str(context.exception)) |
107
|
|
|
|
108
|
|
|
@patch('urllib.request.urlopen') |
109
|
|
|
def test_get_enabled(self, mock_urlopen): |
110
|
|
|
"""Test method get_enabled to find all enabled napps. |
111
|
|
|
""" |
112
|
|
|
# Mocking the API call |
113
|
|
|
mock_response = MagicMock() |
114
|
|
|
mock_response.getcode.return_value = 200 |
115
|
|
|
mock_response.read.return_value = \ |
116
|
|
|
'{"napps": [["kytos", "mef_eline"], ' '["kytos", "of_lldp"]]}' |
117
|
|
|
mock_response.__enter__.return_value = mock_response |
118
|
|
|
mock_urlopen.return_value = mock_response |
119
|
|
|
|
120
|
|
|
# Call the get_installed method |
121
|
|
|
napps_manager = NAppsManager() |
122
|
|
|
installed_napps = napps_manager.get_enabled() |
123
|
|
|
|
124
|
|
|
self.assertEqual(len(installed_napps), 2) |
125
|
|
|
self.assertEqual(installed_napps[0], ("kytos", "mef_eline")) |
126
|
|
|
self.assertEqual(installed_napps[1], ("kytos", "of_lldp")) |
127
|
|
|
|
128
|
|
|
@patch('urllib.request.urlopen') |
129
|
|
|
def test_get_enabled__error(self, mock_urlopen): |
130
|
|
|
"""Test method get_enabled with API error |
131
|
|
|
""" |
132
|
|
|
# Mocking the API call |
133
|
|
|
mock_response = MagicMock() |
134
|
|
|
mock_response.getcode.return_value = 500 |
135
|
|
|
mock_urlopen.return_value = mock_response |
136
|
|
|
|
137
|
|
|
# Call the get_installed method |
138
|
|
|
napps_manager = NAppsManager() |
139
|
|
|
with self.assertRaises(KytosException) as context: |
140
|
|
|
napps_manager.get_enabled() |
141
|
|
|
|
142
|
|
|
self.assertEqual('Error calling Kytos to check enabled NApps.', |
143
|
|
|
str(context.exception)) |
144
|
|
|
|