File: test_lock.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 (96 lines) | stat: -rw-r--r-- 4,013 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
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
from ncclient.operations.lock import *
import unittest
try:
    from unittest.mock import patch  # Python 3.4 and later
except ImportError:
    from mock import patch
from ncclient import manager
import ncclient.manager
import ncclient.transport
from ncclient.xml_ import *
from ncclient.operations import RaiseMode
from xml.etree import ElementTree


class TestLock(unittest.TestCase):

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

    @patch('ncclient.transport.SSHSession')
    @patch('ncclient.operations.RPC._request')
    def test_lock_default_param(self, mock_request, mock_session):
        session = ncclient.transport.SSHSession(self.device_handler)
        obj = Lock(session, self.device_handler, raise_mode=RaiseMode.ALL)
        obj.request()
        node = new_ele("lock")
        sub_ele(sub_ele(node, "target"), "candidate")
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)

    @patch('ncclient.transport.SSHSession')
    @patch('ncclient.operations.RPC._request')
    def test_lock(self, mock_request, mock_session):
        session = ncclient.transport.SSHSession(self.device_handler)
        obj = Lock(session, self.device_handler, raise_mode=RaiseMode.ALL)
        obj.request(target="running")
        node = new_ele("lock")
        sub_ele(sub_ele(node, "target"), "running")
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)

    @patch('ncclient.transport.SSHSession')
    @patch('ncclient.operations.RPC._request')
    def test_unlock_default_param(self, mock_request, mock_session):
        session = ncclient.transport.SSHSession(self.device_handler)
        obj = Unlock(session, self.device_handler, raise_mode=RaiseMode.ALL)
        obj.request()
        node = new_ele("unlock")
        sub_ele(sub_ele(node, "target"), "candidate")
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)

    @patch('ncclient.transport.SSHSession')
    @patch('ncclient.operations.RPC._request')
    def test_unlock(self, mock_request, mock_session):
        session = ncclient.transport.SSHSession(self.device_handler)
        obj = Unlock(session, self.device_handler, raise_mode=RaiseMode.ALL)
        obj.request(target="running")
        node = new_ele("unlock")
        sub_ele(sub_ele(node, "target"), "running")
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)

    @patch('ncclient.transport.SSHSession')
    @patch('ncclient.operations.RPC._request')
    def test_lock_context_enter(self, mock_request, mock_session):
        session = ncclient.transport.SSHSession(self.device_handler)
        obj = LockContext(session, self.device_handler, "candidate")
        self.assertEqual(obj.__enter__(), obj)
        node = new_ele("lock")
        sub_ele(sub_ele(node, "target"), "candidate")
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)

    @patch('ncclient.transport.SSHSession')
    @patch('ncclient.operations.RPC._request')
    def test_lock_context_exit(self, mock_request, mock_session):
        session = ncclient.transport.SSHSession(self.device_handler)
        obj = LockContext(session, self.device_handler, "running")
        self.assertFalse(obj.__exit__())
        node = new_ele("unlock")
        sub_ele(sub_ele(node, "target"), "running")
        xml = ElementTree.tostring(node)
        call = mock_request.call_args_list[0][0][0]
        call = ElementTree.tostring(call)
        self.assertEqual(call, xml)