-
Notifications
You must be signed in to change notification settings - Fork 389
/
test_retrieve.py
222 lines (202 loc) · 9.22 KB
/
test_retrieve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from ncclient.operations.retrieve import *
import unittest
from mock import patch
from ncclient import manager
import ncclient.manager
import ncclient.transport
from ncclient.capabilities import Capabilities
from ncclient.xml_ import *
from ncclient.operations import RaiseMode
from ncclient.operations.errors import MissingCapabilityError
from xml.etree import ElementTree
from lxml import etree
import copy
class TestRetrieve(unittest.TestCase):
def setUp(self):
self.device_handler = manager.make_device_handler({'name': 'junos'})
@patch('ncclient.operations.retrieve.RPC._request')
def test_get(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
obj = Get(session, self.device_handler, raise_mode=RaiseMode.ALL)
root_filter = new_ele('filter')
config_filter = sub_ele(root_filter, 'configuration')
system_filter = sub_ele(config_filter, 'system')
sub_ele(system_filter, 'services')
obj.request(copy.deepcopy(root_filter))
node = new_ele("get")
node.append(util.build_filter(root_filter))
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
@patch('ncclient.operations.retrieve.RPC._request')
def test_get_with_defaults_basic_mode(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = Capabilities([
"urn:ietf:params:netconf:capability:with-defaults:1.0"
"?basic-mode=explicit"
])
obj = Get(session, self.device_handler, raise_mode=RaiseMode.ALL)
obj.request(with_defaults='explicit')
expected_xml = etree.fromstring(
'<nc:get xmlns:nc="{base}">'
'<ns0:with-defaults xmlns:ns0="{defaults}">explicit</ns0:with-defaults>'
'</nc:get>'.format(
base=BASE_NS_1_0,
defaults=NETCONF_WITH_DEFAULTS_NS
)
)
call = mock_request.call_args_list[0][0][0]
self.assertEqual(etree.tostring(call), etree.tostring(expected_xml))
@patch('ncclient.operations.retrieve.RPC._request')
def test_get_with_defaults_also_supported(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = Capabilities([
"urn:ietf:params:netconf:capability:with-defaults:1.0"
"?basic-mode=explicit"
"&also-supported=report-all,trim"
])
obj = Get(session, self.device_handler, raise_mode=RaiseMode.ALL)
obj.request(with_defaults='report-all')
expected_xml = etree.fromstring(
'<nc:get xmlns:nc="{base}">'
'<ns0:with-defaults xmlns:ns0="{defaults}">report-all</ns0:with-defaults>'
'</nc:get>'.format(
base=BASE_NS_1_0,
defaults=NETCONF_WITH_DEFAULTS_NS
)
)
call = mock_request.call_args_list[0][0][0]
self.assertEqual(etree.tostring(call), etree.tostring(expected_xml))
@patch('ncclient.operations.retrieve.RPC._request')
def test_get_with_defaults_not_supported(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = Capabilities([
"urn:ietf:params:netconf:capability:with-defaults:1.0"
"?basic-mode=explicit"
"&also-supported=report-all,trim"
])
obj = Get(session, self.device_handler, raise_mode=RaiseMode.ALL)
expected_error = (
"Invalid 'with-defaults' mode 'report-all-tagged'; the server "
"only supports the following: explicit, report-all, trim"
)
self.assertRaisesRegexp(
WithDefaultsError,
expected_error,
obj.request,
with_defaults='report-all-tagged'
)
@patch('ncclient.operations.retrieve.RPC._request')
def test_get_with_defaults_missing_capability(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = Capabilities([])
obj = Get(session, self.device_handler, raise_mode=RaiseMode.ALL)
self.assertRaises(
MissingCapabilityError,
obj.request,
with_defaults='report-all'
)
@patch('ncclient.operations.retrieve.RPC._request')
def test_get_config(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
obj = GetConfig(session, self.device_handler, raise_mode=RaiseMode.ALL)
source = "candidate"
obj.request(source)
node = new_ele("get-config")
node.append(util.datastore_or_url("source", source))
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
@patch('ncclient.operations.retrieve.RPC._request')
def test_get_config_with_defaults(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = Capabilities([
"urn:ietf:params:netconf:capability:with-defaults:1.0"
"?basic-mode=explicit"
])
obj = GetConfig(session, self.device_handler, raise_mode=RaiseMode.ALL)
source = 'candidate'
obj.request(source, with_defaults='explicit')
expected_xml = etree.fromstring(
'<nc:get-config xmlns:nc="{base}">'
'<nc:source><nc:candidate /></nc:source>'
'<ns0:with-defaults xmlns:ns0="{defaults}">explicit</ns0:with-defaults>'
'</nc:get-config>'.format(
base=BASE_NS_1_0,
defaults=NETCONF_WITH_DEFAULTS_NS
)
)
call = mock_request.call_args_list[0][0][0]
self.assertEqual(etree.tostring(call), etree.tostring(expected_xml))
@patch('ncclient.operations.retrieve.RPC._request')
def test_get_config_with_defaults_missing_capability(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = Capabilities([])
obj = GetConfig(session, self.device_handler, raise_mode=RaiseMode.ALL)
source = 'candidate'
self.assertRaises(
MissingCapabilityError,
obj.request,
source,
with_defaults='explicit'
)
@patch('ncclient.operations.retrieve.RPC._request')
def test_get_schema(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
obj = GetSchema(session, self.device_handler, raise_mode=RaiseMode.ALL)
identifier = "foo"
version = "1.0"
reqformat = "xsd"
obj.request(identifier, version, reqformat)
node = etree.Element(qualify("get-schema", NETCONF_MONITORING_NS))
id = etree.SubElement(node,
qualify("identifier", NETCONF_MONITORING_NS))
id.text = identifier
ver = etree.SubElement(node, qualify("version", NETCONF_MONITORING_NS))
ver.text = version
formt = etree.SubElement(node,
qualify("format", NETCONF_MONITORING_NS))
formt.text = reqformat
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
@patch('ncclient.operations.retrieve.RPC._request')
def test_dispatch(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
obj = Dispatch(session, self.device_handler, raise_mode=RaiseMode.ALL)
rpc = 'get-software-information'
source = "candidate"
root_filter = new_ele('filter')
config_filter = sub_ele(root_filter, 'configuration')
system_filter = sub_ele(config_filter, 'system')
sub_ele(system_filter, 'services')
a = copy.deepcopy(root_filter)
obj.request(rpc, source=source, filter=a)
node = new_ele(rpc)
node.append(util.datastore_or_url("source", source))
node.append(util.build_filter(root_filter))
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
@patch('ncclient.operations.retrieve.RPC._request')
def test_dispatch_2(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
obj = Dispatch(session, self.device_handler, raise_mode=RaiseMode.ALL)
node = new_ele('get-software-information')
source = "candidate"
root_filter = new_ele('filter')
config_filter = sub_ele(root_filter, 'configuration')
system_filter = sub_ele(config_filter, 'system')
sub_ele(system_filter, 'services')
a = copy.deepcopy(root_filter)
obj.request(node, source=source, filter=a)
node.append(util.datastore_or_url("source", source))
node.append(util.build_filter(root_filter))
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)