def generate_all_mocks(self): """will generate netconf obj with all need mocks""" netconf = netconf_connection.connection() netconf.ssh = mock.Mock() netconf.ssh.close = mock.MagicMock() netconf.chan = mock.Mock() netconf.chan.close = mock.MagicMock() netconf.chan.send = mock.MagicMock() netconf.chan.recv = mock.MagicMock( return_value=netconf_connection.NETCONF_1_0_END) return netconf
def test_connect_with_key(self): """connect call""" # ssh mock will_be_ssh = mock.Mock() will_be_ssh.set_missing_host_key_policy = mock.MagicMock() will_be_ssh.connect = mock.MagicMock() # channel mock channel_mock = mock.Mock() channel_mock.recv = mock.MagicMock( return_value=("ok" + netconf_connection.NETCONF_1_0_END)) channel_mock.send = mock.MagicMock() channel_mock.invoke_subsystem = mock.MagicMock() # transport mock transport_mock = mock.MagicMock() transport_mock.open_session = mock.MagicMock(return_value=channel_mock) will_be_ssh.get_transport = mock.MagicMock(return_value=transport_mock) with mock.patch.object(paramiko, 'SSHClient', return_value=will_be_ssh) as mock_ssh_client: with mock.patch.object(paramiko, 'AutoAddPolicy', return_value="I'm policy") as mock_policy: with mock.patch.object(paramiko.RSAKey, 'from_private_key', return_value="secret"): netconf = netconf_connection.connection() message = netconf.connect("127.0.0.100", "me", hello_string="hi", key_content="unknow") # check calls self.assertEqual(message, "ok") mock_ssh_client.assert_called_with() mock_policy.assert_called_with() will_be_ssh.set_missing_host_key_policy.assert_called_with( "I'm policy") will_be_ssh.connect.assert_called_with('127.0.0.100', username='******', pkey='secret', port=830, allow_agent=False) will_be_ssh.get_transport.assert_called_with() transport_mock.open_session.assert_called_with() channel_mock.invoke_subsystemassert_called_with('netconf') channel_mock.send.assert_called_with( "hi" + netconf_connection.NETCONF_1_0_END)
def run(**kwargs): """main entry point for all calls""" calls = kwargs.get('calls', []) template = kwargs.get('template') templates = [] if template: templates = ctx.get_resource(template).split("]]>]]>") if not calls and not templates: ctx.logger.info("Please provide calls or template") return # credentials properties = ctx.node.properties netconf_auth = properties.get('netconf_auth', {}) netconf_auth.update(kwargs.get('netconf_auth', {})) user = netconf_auth.get('user') password = netconf_auth.get('password') key_content = netconf_auth.get('key_content') port = int(netconf_auth.get('port', 830)) ip_list = netconf_auth.get('ip') if isinstance(ip_list, basestring): ip_list = [ip_list] # save logs to debug file log_file_name = None if netconf_auth.get('store_logs'): log_file_name = "/tmp/netconf-%s_%s_%s.log" % (str( ctx.execution_id), str(ctx.instance.id), str(ctx.workflow_id)) ctx.logger.info("Communication logs will be saved to %s" % log_file_name) # if node contained in some other node, try to overwrite ip if not ip_list: ip_list = [ctx.instance.host_ip] ctx.logger.info("Used host from container: %s" % str(ip_list)) # check minimal amout of credentials if not port or not ip_list or not user or (not password and not key_content): raise cfy_exc.NonRecoverableError("please check your credentials") # some random initial message id, for have different between calls message_id = int((time.time() * 100) % 100 * 1000) # xml namespaces and capabilities xmlns = properties.get('metadata', {}).get('xmlns', {}) # override by system namespaces xmlns = _merge_ns(xmlns, properties.get('base_xmlns', {})) netconf_namespace, xmlns = utils.update_xmlns(xmlns) capabilities = properties.get('metadata', {}).get('capabilities') # connect ctx.logger.info("use %s@%s:%s for login" % (user, ip_list, port)) hello_string = _generate_hello(xmlns, netconf_namespace, capabilities) ctx.logger.info("i sent: " + hello_string) _write_to_log(log_file_name, hello_string) netconf = netconf_connection.connection() for ip in ip_list: try: capabilities = netconf.connect(ip, user, hello_string, password, key_content, port) ctx.logger.info("Will be used: " + ip) break except Exception as ex: ctx.logger.info("Can't connect to %s with %s" % (repr(ip), str(ex))) else: raise cfy_exc.NonRecoverableError("please check your ip list") ctx.logger.info("i recieved: " + capabilities) _write_to_log(log_file_name, capabilities) if _server_support_1_1(xmlns, netconf_namespace, capabilities): ctx.logger.info("i will use version 1.1 of netconf protocol") netconf.current_level = netconf_connection.NETCONF_1_1_CAPABILITY else: ctx.logger.info("i will use version 1.0 of netconf protocol") strict_check = kwargs.get('strict_check', True) if 'lock' in kwargs: message_id = message_id + 1 for name in kwargs['lock']: _lock(name, True, netconf, message_id, netconf_namespace, xmlns, strict_check, log_file_name) if 'back_database' in kwargs and 'front_database' in kwargs: message_id = message_id + 1 _copy(kwargs['front_database'], kwargs['back_database'], netconf, message_id, netconf_namespace, xmlns, strict_check, log_file_name=log_file_name) if calls: dsdl = properties.get('metadata', {}).get('dsdl') _run_calls(netconf, message_id, netconf_namespace, xmlns, calls, kwargs.get('back_database'), dsdl, strict_check, log_file_name=log_file_name) elif templates: template_params = kwargs.get('params') deep_error_check = kwargs.get('deep_error_check') ctx.logger.info("Params for template %s" % str(template_params)) _run_templates(netconf, templates, template_params, netconf_namespace, xmlns, strict_check, deep_error_check, log_file_name=log_file_name) if 'back_database' in kwargs and 'front_database' in kwargs: message_id = message_id + 1 _copy(kwargs['back_database'], kwargs['front_database'], netconf, message_id, netconf_namespace, xmlns, strict_check, log_file_name=log_file_name) if 'lock' in kwargs: message_id = message_id + 1 for name in kwargs['lock']: _lock(name, False, netconf, message_id, netconf_namespace, xmlns, strict_check, log_file_name=log_file_name) # goodbye ctx.logger.info("connection close") message_id = message_id + 1 goodbye_string = _generate_goodbye(xmlns, netconf_namespace, message_id) ctx.logger.info("i sent: " + goodbye_string) response = netconf.close(goodbye_string) ctx.logger.info("i recieved: " + response)