Created
May 1, 2020 17:13
-
-
Save eliezio/3e5ff7a8b69151e1d145fb6af2d957de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from ncclient import manager, operations | |
import settings | |
LOGGER = logging.getLogger(__name__) | |
def check_reply_ok(reply): | |
assert reply is not None | |
_log_netconf_msg("Received", reply.xml) | |
assert reply.ok is True | |
assert reply.error is None | |
def check_reply_err(reply): | |
assert reply is not None | |
_log_netconf_msg("Received", reply.xml) | |
assert reply.ok is False | |
assert reply.error is not None | |
def check_reply_data(reply): | |
check_reply_ok(reply) | |
def _log_netconf_msg(header: str, body: str): | |
"""Log a message using a format inspired by NETCONF 1.1 """ | |
LOGGER.info("%s:\n\n#%d\n%s\n##", header, len(body), body) | |
def open_ssh_session(): | |
nc = manager.connect(host=settings.HOST, | |
port=settings.SSH_PORT, | |
username=settings.SSH_USERNAME, | |
key_filename=settings.SSH_KEY_FILENAME, | |
allow_agent=False, | |
look_for_keys=False, | |
hostkey_verify=False) | |
nc.raise_mode = operations.RaiseMode.NONE | |
return nc | |
def close_ssh_session(nc): | |
nc.close_session() | |
class NCTestCase: | |
""" Base class for NETCONF test cases. Provides a NETCONF connection. """ | |
nc: manager.Manager | |
def setup(self): | |
self.nc = open_ssh_session() | |
def teardown(self): | |
close_ssh_session(self.nc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment