Debian Codegraph @

test/unit/operations/third_party/juniper/test_rpc.py

raw
1 from ncclient.operations.third_party.juniper.rpc import *
2 import json
3 import unittest
4 try:
5 from unittest.mock import patch # Python 3.4 and later
6 except ImportError:
7 from mock import patch
8 from ncclient import manager
9 import ncclient.manager
10 import ncclient.transport
11 from ncclient.xml_ import *
12 from ncclient.operations import RaiseMode
13 from xml.etree import ElementTree
14
15
16 class TestRPC(unittest.TestCase):
17
18 @patch('ncclient.transport.SSHSession')
19 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
20 def test_command(self, mock_request, mock_session):
21 device_handler = manager.make_device_handler({'name': 'junos'})
22 session = ncclient.transport.SSHSession(device_handler)
23 obj = Command(session, device_handler, raise_mode=RaiseMode.ALL)
24 command = 'show system users'
25 format = 'text'
26 obj.request(command=command, format=format)
27 node = new_ele('command', {'format': format})
28 node.text = command
29 call = mock_request.call_args_list[0][0][0]
30 self.assertEqual(call.tag, node.tag)
31 self.assertEqual(call.text, node.text)
32
33 @patch('ncclient.transport.SSHSession')
34 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
35 def test_getconf(self, mock_request, mock_session):
36 device_handler = manager.make_device_handler({'name': 'junos'})
37 session = ncclient.transport.SSHSession(device_handler)
38 obj = GetConfiguration(
39 session,
40 device_handler,
41 raise_mode=RaiseMode.ALL)
42 root_filter = new_ele('filter')
43 config_filter = sub_ele(root_filter, 'configuration')
44 system_filter = sub_ele(config_filter, 'system')
45 obj.request(format='xml', filter=system_filter)
46 node = new_ele('get-configuration', {'format': 'xml'})
47 node.append(system_filter)
48 call = mock_request.call_args_list[0][0][0]
49 self.assertEqual(call.tag, node.tag)
50 self.assertEqual(call.attrib, node.attrib)
51
52 @patch('ncclient.transport.SSHSession')
53 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
54 def test_getconf_text_sets_huge_tree(self, mock_request, mock_session):
55 device_handler = manager.make_device_handler({'name': 'junos'})
56 session = ncclient.transport.SSHSession(device_handler)
57 obj = GetConfiguration(
58 session,
59 device_handler,
60 raise_mode=RaiseMode.ALL)
61 obj.huge_tree = False
62 obj.request(format='text')
63 self.assertTrue(obj.huge_tree)
64
65 @patch('ncclient.transport.SSHSession')
66 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
67 def test_loadconf_xml(self, mock_request, mock_session):
68 device_handler = manager.make_device_handler({'name': 'junos'})
69 session = ncclient.transport.SSHSession(device_handler)
70 obj = LoadConfiguration(
71 session,
72 device_handler,
73 raise_mode=RaiseMode.ALL)
74 root_config = new_ele('configuration')
75 system_config = sub_ele(root_config, 'system')
76 location_config = sub_ele(system_config, 'location')
77 floor_config = sub_ele(location_config, 'floor').text = "7"
78 obj.request(format='xml', config=root_config)
79 node = new_ele('load-configuration', {'format': 'xml', 'action': 'merge'})
80 node.append(root_config)
81 call = mock_request.call_args_list[0][0][0]
82 self.assertEqual(call.tag, node.tag)
83 self.assertEqual(call.attrib, node.attrib)
84
85 @patch('ncclient.transport.SSHSession')
86 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
87 def test_loadconf_json(self, mock_request, mock_session):
88 device_handler = manager.make_device_handler({'name': 'junos'})
89 session = ncclient.transport.SSHSession(device_handler)
90 obj = LoadConfiguration(
91 session,
92 device_handler,
93 raise_mode=RaiseMode.ALL)
94 location = '{ "configuration": { "system": { "location": { "floor": "7" }}}}'
95 config_json = json.loads(location)
96 config = json.dumps(config_json)
97 obj.request(format='json', action='merge', config=config)
98 node = new_ele('load-configuration', {'format': 'json', 'action': 'merge'})
99 sub_ele(node, 'configuration-json').text = config
100 call = mock_request.call_args_list[0][0][0]
101 self.assertEqual(call.tag, node.tag)
102 self.assertEqual(call.attrib, node.attrib)
103
104 @patch('ncclient.transport.SSHSession')
105 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
106 def test_loadconf_set(self, mock_request, mock_session):
107 device_handler = manager.make_device_handler({'name': 'junos'})
108 session = ncclient.transport.SSHSession(device_handler)
109 obj = LoadConfiguration(
110 session,
111 device_handler,
112 raise_mode=RaiseMode.ALL)
113 config = 'set system location floor 7'
114 obj.request(format='text', action='set', config=config)
115 node = new_ele('load-configuration', {'format': 'text', 'action': 'set'})
116 sub_ele(node, 'configuration-set').text = config
117 call = mock_request.call_args_list[0][0][0]
118 self.assertEqual(call.tag, node.tag)
119 self.assertEqual(call.attrib, node.attrib)
120
121 @patch('ncclient.transport.SSHSession')
122 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
123 def test_loadconf_text(self, mock_request, mock_session):
124 device_handler = manager.make_device_handler({'name': 'junos'})
125 session = ncclient.transport.SSHSession(device_handler)
126 obj = LoadConfiguration(
127 session,
128 device_handler,
129 raise_mode=RaiseMode.ALL)
130 config = 'system { location floor 7; }'
131 obj.request(format='text', action='merge', config=config)
132 node = new_ele('load-configuration', {'format': 'text', 'action': 'merge'})
133 sub_ele(node, 'configuration-text').text = config
134 call = mock_request.call_args_list[0][0][0]
135 self.assertEqual(call.tag, node.tag)
136 self.assertEqual(call.attrib, node.attrib)
137
138 @patch('ncclient.transport.SSHSession')
139 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
140 def test_loadconf_list(self, mock_request, mock_session):
141 device_handler = manager.make_device_handler({'name': 'junos'})
142 session = ncclient.transport.SSHSession(device_handler)
143 obj = LoadConfiguration(
144 session,
145 device_handler,
146 raise_mode=RaiseMode.ALL)
147 config = ['set system location floor 7', 'set system location rack 3']
148 obj.request(format='text', action='set', config=config)
149 node = new_ele('load-configuration', {'format': 'text', 'action': 'set'})
150 sub_ele(node, 'configuration-set').text = '\n'.join(config)
151 call = mock_request.call_args_list[0][0][0]
152 self.assertEqual(call.tag, node.tag)
153 self.assertEqual(call.attrib, node.attrib)
154
155 @patch('ncclient.transport.SSHSession')
156 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
157 def test_compare_conf(self, mock_request, mock_session):
158 device_handler = manager.make_device_handler({'name': 'junos'})
159 session = ncclient.transport.SSHSession(device_handler)
160 obj = CompareConfiguration(
161 session,
162 device_handler,
163 raise_mode=RaiseMode.ALL)
164 obj.request(rollback=2)
165 node = new_ele(
166 'get-configuration', {'compare': 'rollback', 'rollback': str(2), 'format': 'text'})
167 call = mock_request.call_args_list[0][0][0]
168 self.assertEqual(call.tag, node.tag)
169 self.assertEqual(call.attrib, node.attrib)
170
171 @patch('ncclient.transport.SSHSession')
172 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
173 def test_execute_rpc(self, mock_request, mock_session):
174 device_handler = manager.make_device_handler({'name': 'junos'})
175 session = ncclient.transport.SSHSession(device_handler)
176 obj = ExecuteRpc(session, device_handler, raise_mode=RaiseMode.ALL)
177 rpc = new_ele('get-software-information')
178 obj.request(rpc)
179 mock_request.assert_called_once_with(rpc)
180
181 @patch('ncclient.transport.SSHSession')
182 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
183 def test_execute_rpc_str(self, mock_request, mock_session):
184 device_handler = manager.make_device_handler({'name': 'junos'})
185 session = ncclient.transport.SSHSession(device_handler)
186 obj = ExecuteRpc(session, device_handler, raise_mode=RaiseMode.ALL)
187 rpc = '<get-software-information/>'
188 obj.request(rpc)
189 self.assertEqual(True, isinstance(rpc, str))
190
191 @patch('ncclient.transport.SSHSession')
192 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
193 def test_reboot(self, mock_request, mock_session):
194 device_handler = manager.make_device_handler({'name': 'junos'})
195 session = ncclient.transport.SSHSession(device_handler)
196 obj = Reboot(session, device_handler, raise_mode=RaiseMode.ALL)
197 obj.request()
198 node = new_ele('request-reboot')
199 call = mock_request.call_args_list[0][0][0]
200 self.assertEqual(call.tag, node.tag)
201
202 @patch('ncclient.transport.SSHSession')
203 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
204 def test_halt(self, mock_request, mock_session):
205 device_handler = manager.make_device_handler({'name': 'junos'})
206 session = ncclient.transport.SSHSession(device_handler)
207 obj = Halt(session, device_handler, raise_mode=RaiseMode.ALL)
208 obj.request()
209 node = new_ele('request-halt')
210 call = mock_request.call_args_list[0][0][0]
211 self.assertEqual(call.tag, node.tag)
212
213 @patch('ncclient.transport.SSHSession')
214 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
215 @patch('ncclient.operations.third_party.juniper.rpc.RPC._assert')
216 def test_commit_confirmed(self, mock_assert, mock_request, mock_session):
217 # mock_session.server_capabilities.return_value = [':candidate']
218 device_handler = manager.make_device_handler({'name': 'junos'})
219 session = ncclient.transport.SSHSession(device_handler)
220 obj = Commit(session, device_handler, raise_mode=RaiseMode.ALL)
221 obj.request(confirmed=True, comment="message", timeout="50")
222 node = new_ele_ns("commit-configuration", "")
223 sub_ele(node, "confirmed")
224 # confirm-timeout need to be provided in minutes for this Junos specific rpc
225 sub_ele(node, "confirm-timeout").text = "1"
226 sub_ele(node, "log").text = "message"
227 xml = ElementTree.tostring(node)
228 call = mock_request.call_args_list[0][0][0]
229 call = ElementTree.tostring(call)
230 self.assertEqual(call, xml)
231
232 @patch('ncclient.transport.SSHSession')
233 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
234 @patch('ncclient.operations.third_party.juniper.rpc.RPC._assert')
235 def test_commit(self, mock_assert, mock_request, mock_session):
236 # mock_session.server_capabilities.return_value = [':candidate']
237 device_handler = manager.make_device_handler({'name': 'junos'})
238 session = ncclient.transport.SSHSession(device_handler)
239 obj = Commit(session, device_handler, raise_mode=RaiseMode.ALL)
240 obj.request()
241 node = new_ele_ns("commit-configuration", "")
242 xml = ElementTree.tostring(node)
243 call = mock_request.call_args_list[0][0][0]
244 call = ElementTree.tostring(call)
245 self.assertEqual(call, xml)
246
247 obj.request(check=True)
248 self.assertEqual(call, xml)
249
250 @patch('ncclient.transport.SSHSession')
251 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
252 @patch('ncclient.operations.third_party.juniper.rpc.RPC._assert')
253 def test_commit_at_time(self, mock_assert, mock_request, mock_session):
254 # mock_session.server_capabilities.return_value = [':candidate']
255 device_handler = manager.make_device_handler({'name': 'junos'})
256 session = ncclient.transport.SSHSession(device_handler)
257 obj = Commit(session, device_handler, raise_mode=RaiseMode.ALL)
258 obj.request(at_time="1111-11-11 00:00:00", synchronize=True)
259 node = new_ele_ns("commit-configuration", "")
260 sub_ele(node, "at-time").text = "1111-11-11 00:00:00"
261 sub_ele(node, "synchronize")
262 xml = ElementTree.tostring(node)
263 call = mock_request.call_args_list[0][0][0]
264 call = ElementTree.tostring(call)
265 self.assertEqual(call, xml)
266
267 @patch('ncclient.transport.SSHSession')
268 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
269 @patch('ncclient.operations.third_party.juniper.rpc.RPC._assert')
270 def test_commit_confirmed_at_time(
271 self, mock_assert, mock_request, mock_session):
272 # mock_session.server_capabilities.return_value = [':candidate']
273 device_handler = manager.make_device_handler({'name': 'junos'})
274 session = ncclient.transport.SSHSession(device_handler)
275 obj = Commit(session, device_handler, raise_mode=RaiseMode.ALL)
276 self.assertRaises(NCClientError,
277 obj.request, at_time="1111-11-11 00:00:00", synchronize=True,
278 confirmed=True)
279
280 @patch('ncclient.transport.SSHSession')
281 @patch('ncclient.operations.third_party.juniper.rpc.RPC._request')
282 @patch('ncclient.operations.third_party.juniper.rpc.RPC._assert')
283 def test_rollback(self, mock_assert, mock_request, mock_session):
284 mock_request.return_value = 'junos'
285 expected = 'junos'
286 # mock_session.server_capabilities.return_value = [':candidate']
287 device_handler = manager.make_device_handler({'name': 'junos'})
288 session = ncclient.transport.SSHSession(device_handler)
289 obj = Rollback(session, device_handler, raise_mode=RaiseMode.ALL)
290 actual = obj.request()
291 self.assertEqual(expected, actual)
292
293 actual = obj.request(rollback=1)
294 self.assertEqual(expected, actual)