def test_sqlite_1(self, mock_spath, mock_path, mock_sys): mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') mock_spath.return_value = os.path.join(os.path.dirname(__file__), 'configs') js = JsnapSqlite("10.216.193.114", self.db) js.insert_data(self.db_dict2) def fun(): raise BaseException with patch('logging.Logger.error') as mock_log: extr = SqliteExtractXml(self.db) data, formt = extr.get_xml_using_snapname( "10.216.193.114", self.db_dict2['cli_command'], self.db_dict2['snap_name']) self.assertEqual(data, "mock_data") self.assertEqual(formt, "text") try: data, format = extr.get_xml_using_snapname( "10.216.193.114", self.db_dict2['cli_command'], "mock_ssssnap") except BaseException: err = [ "No previous snapshots exists with name = mock_ssssnap for command = show version" ] c_list = mock_log.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err[0]), -1)
def store_in_sqlite(self, db, hostname, cmd_rpc_name, reply_format, rpc_reply, snap_name, warning=False): """ Store reply in database :param db: database name :param hostname: hostname :param cmd_rpc_name: Command/RPC :param reply_format: xml / text :param rpc_reply: RPC reply :param snap_name: snap filename """ sqlite_jsnap = JsnapSqlite(hostname, db['db_name']) db_dict = dict() db_dict['cli_command'] = cmd_rpc_name db_dict['snap_name'] = snap_name db_dict['filename'] = hostname + '_' + snap_name + \ '_' + cmd_rpc_name + '.' + reply_format db_dict['format'] = reply_format if warning is False: db_dict['data'] = self._check_reply(rpc_reply, reply_format) else: db_dict['data'] = rpc_reply sqlite_jsnap.insert_data(db_dict)
def store_in_sqlite( self, db, hostname, cmd_rpc_name, reply_format, rpc_reply, snap_name, warning=False, ): """ Store reply in database :param db: database name :param hostname: hostname :param cmd_rpc_name: Command/RPC :param reply_format: xml / text :param rpc_reply: RPC reply :param snap_name: snap filename """ sqlite_jsnap = JsnapSqlite(hostname, db["db_name"]) db_dict = dict() db_dict["cli_command"] = cmd_rpc_name db_dict["snap_name"] = snap_name db_dict["filename"] = ( hostname + "_" + snap_name + "_" + cmd_rpc_name + "." + reply_format ) db_dict["format"] = reply_format if warning is False: db_dict["data"] = self._check_reply(rpc_reply, reply_format) else: db_dict["data"] = rpc_reply sqlite_jsnap.insert_data(db_dict)
def test_sqlite_store_db_not_exist(self, mock_path, mock_connect, mock_log, mock_sys): mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') mock_connect.side_effect = Exception("Connection not possible") sq = JsnapSqlite('1.1.1.1', self.db) c_list = mock_log.call_args_list[0] err = ['ERROR occurred in database'] self.assertIn((err[0]), c_list[0][0])
def test_sqlite_2(self, mock_spath, mock_path, mock_sys): mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') mock_spath.return_value = os.path.join(os.path.dirname(__file__), 'configs') def fun(): raise BaseException mock_sys.return_value = fun js = JsnapSqlite("1.1.1.1", self.db) js.insert_data(self.db_dict2) with patch('logging.Logger.error') as mock_log: extr = SqliteExtractXml(self.db) extr.get_xml_using_snapname( "10.216.193.11", self.db_dict2['cli_command'], self.db_dict2['snap_name']) err = "ERROR!! Complete message is no such table: table_10__216__193__11" c_list = mock_log.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err), -1)
def test_sqlite_3(self, mock_spath, mock_path, mock_sys): mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') mock_spath.return_value = os.path.join(os.path.dirname(__file__), 'configs') js = JsnapSqlite("1.1.1.1", self.db) js.insert_data(self.db_dict2) with patch('logging.Logger.error') as mock_log: extr = SqliteExtractXml(self.db) data, formt = extr.get_xml_using_snap_id( "1.1.1.1", self.db_dict2['cli_command'], 0) self.assertEqual(data, "mock_data") self.assertEqual(formt, "text") extr.get_xml_using_snap_id("1.1.1.1", "show vers", 0) err = [ "ERROR!! Complete message is: 'NoneType' object is not iterable"] c_list = mock_log.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err[0]), -1)
def test_sqlite_3(self, mock_spath, mock_path, mock_sys): mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') mock_spath.return_value = os.path.join(os.path.dirname(__file__), 'configs') js = JsnapSqlite("1.1.1.1", self.db) js.insert_data(self.db_dict2) with patch('logging.Logger.error') as mock_log: extr = SqliteExtractXml(self.db) data, formt = extr.get_xml_using_snap_id( "1.1.1.1", self.db_dict2['cli_command'], 0) self.assertEqual(data, "mock_data") self.assertEqual(formt, "text") extr.get_xml_using_snap_id("1.1.1.1", "show vers", 0) if sys.version_info[0] == 2 or (sys.version_info[0] == 3 and sys.version_info[1] <= 6): err = ["ERROR!! Complete message is: 'NoneType' object is not iterable"] else : err = ["ERROR!! Complete message is: cannot unpack non-iterable NoneType object"] c_list = mock_log.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err[0]), -1)
def store_in_sqlite( self, db, hostname, cmd_rpc_name, reply_format, rpc_reply, snap_name, warning=False): """ Store reply in database :param db: database name :param hostname: hostname :param cmd_rpc_name: Command/RPC :param reply_format: xml / text :param rpc_reply: RPC reply :param snap_name: snap filename """ sqlite_jsnap = JsnapSqlite(hostname, db['db_name']) db_dict = dict() db_dict['cli_command'] = cmd_rpc_name db_dict['snap_name'] = snap_name db_dict['filename'] = hostname + '_' + snap_name + \ '_' + cmd_rpc_name + '.' + reply_format db_dict['format'] = reply_format if warning is False: db_dict['data'] = self._check_reply(rpc_reply, reply_format) else: db_dict['data'] = rpc_reply sqlite_jsnap.insert_data(db_dict)
def test_sqlite_1(self, mock_spath, mock_path, mock_sys): mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') mock_spath.return_value = os.path.join(os.path.dirname(__file__), 'configs') js = JsnapSqlite("1.1.1.1", self.db) js.insert_data(self.db_dict2) def fun(): raise BaseException with patch('logging.Logger.error') as mock_log: extr = SqliteExtractXml(self.db) data, formt = extr.get_xml_using_snapname( "1.1.1.1", self.db_dict2['cli_command'], self.db_dict2['snap_name']) self.assertEqual(data, "mock_data") self.assertEqual(formt, "text") try: data, format= extr.get_xml_using_snapname( "1.1.1.1", self.db_dict2['cli_command'], "mock_ssssnap") except BaseException: err = [ "No previous snapshots exists with name = mock_ssssnap for command = show version"] c_list = mock_log.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err[0]), -1)