File: test_subscribe.py

package info (click to toggle)
python-ncclient 0.6.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: python: 9,548; xml: 476; makefile: 77; sh: 5
file content (71 lines) | stat: -rw-r--r-- 2,908 bytes parent folder | download | duplicates (2)
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
from ncclient.operations.edit import *
from ncclient.operations.subscribe import *
import unittest
try:
    from unittest.mock import patch, MagicMock  # Python 3.4 and later
except ImportError:
    from mock import patch, MagicMock
from ncclient import manager
import ncclient.manager
import ncclient.transport
from ncclient.xml_ import *
from ncclient.operations import RaiseMode
from xml.etree import ElementTree
from ncclient.operations.errors import MissingCapabilityError
import copy

start_time = "1990-12-31T23:59:60Z"
stop_time = "1996-12-19T16:39:57-08:00"

class TestSubscribe(unittest.TestCase):

    def setUp(self):
        self.device_handler = manager.make_device_handler({'name': 'junos'})

    @patch('ncclient.operations.edit.RPC._request')
    def test_subscribe_all(self, mock_request):
        session = ncclient.transport.SSHSession(self.device_handler)
        session._server_capabilities = [":notification"]
        obj = CreateSubscription(
            session,
            self.device_handler,
            raise_mode=RaiseMode.ALL)
        obj.request()
        node = new_ele_ns("create-subscription", NETCONF_NOTIFICATION_NS)
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)

    @patch('ncclient.operations.edit.RPC._request')
    def test_subscribe_stream(self, mock_request):
        session = ncclient.transport.SSHSession(self.device_handler)
        session._server_capabilities = [":notification"]
        obj = CreateSubscription(
            session,
            self.device_handler,
            raise_mode=RaiseMode.ALL)
        obj.request(filter=None, stream_name="nameofstream")
        node = new_ele_ns("create-subscription", NETCONF_NOTIFICATION_NS)
        sub_ele_ns(node, "stream", NETCONF_NOTIFICATION_NS).text = "nameofstream"
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)

    @patch('ncclient.operations.edit.RPC._request')
    def test_subscribe_times(self, mock_request):
        session = ncclient.transport.SSHSession(self.device_handler)
        session._server_capabilities = [":notification"]
        obj = CreateSubscription(
            session,
            self.device_handler,
            raise_mode=RaiseMode.ALL)
        obj.request(filter=None, start_time=start_time, stop_time=stop_time)
        node = new_ele_ns("create-subscription", NETCONF_NOTIFICATION_NS)
        sub_ele_ns(node, "startTime", NETCONF_NOTIFICATION_NS).text = start_time
        sub_ele_ns(node, "stopTime", NETCONF_NOTIFICATION_NS).text = stop_time
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)