From df3a606ef78af83543ddc142432b91cd80baf8e4 Mon Sep 17 00:00:00 2001 From: e Date: Sat, 19 Sep 2020 14:41:57 +0800 Subject: [PATCH] =?UTF-8?q?pkgship=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6?= =?UTF-8?q?=E5=AF=B9=E4=BA=8E=E4=B9=8B=E5=89=8D=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E5=A4=84=E7=90=86=E4=B8=8D=E5=90=88?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apps/lifecycle/function/download_yaml.py | 3 +- .../application/apps/lifecycle/view.py | 4 +- .../apps/package/function/constants.py | 2 + .../apps/package/function/searchdb.py | 35 ++--- .../application/apps/package/view.py | 62 ++++---- .../application/initsystem/data_import.py | 143 +++++++++++------- .../packageship/application/models/package.py | 10 ++ .../get_repodatas.json | 4 +- .../test/common_files/dbs/lifecycle.db | Bin 9990144 -> 9990144 bytes .../common_files/operate_dbs/lifecycle.db | Bin 19021824 -> 19021824 bytes .../init_system_tests/test_importdata.py | 104 ++++++------- .../repodatas_test/test_delete_repodatas.py | 7 +- 12 files changed, 206 insertions(+), 168 deletions(-) diff --git a/packageship/packageship/application/apps/lifecycle/function/download_yaml.py b/packageship/packageship/application/apps/lifecycle/function/download_yaml.py index 500e23b9..41b67000 100644 --- a/packageship/packageship/application/apps/lifecycle/function/download_yaml.py +++ b/packageship/packageship/application/apps/lifecycle/function/download_yaml.py @@ -200,7 +200,8 @@ def update_pkg_info(pkg_info_update=True): # Open thread pool pool = ThreadPoolExecutor(max_workers=pool_workers) with DBHelper(db_name="lifecycle") as database: - for table_name in filter(lambda x: x not in ['packages_issue', 'packages_maintainer'], + for table_name in filter(lambda x: x not in ['packages_issue', 'packages_maintainer', + 'databases_info'], database.engine.table_names()): cls_model = Packages.package_meta(table_name) diff --git a/packageship/packageship/application/apps/lifecycle/view.py b/packageship/packageship/application/apps/lifecycle/view.py index a5525b43..2c7521a8 100644 --- a/packageship/packageship/application/apps/lifecycle/view.py +++ b/packageship/packageship/application/apps/lifecycle/view.py @@ -245,6 +245,7 @@ class LifeTables(Resource): all_table_names = database_name.engine.table_names() all_table_names.remove("packages_issue") all_table_names.remove("packages_maintainer") + all_table_names.remove("databases_info") return jsonify( ResponseCode.response_json( ResponseCode.SUCCESS, data=all_table_names) @@ -458,7 +459,8 @@ class IssueCatch(Resource): pool_workers = 10 pool = ThreadPoolExecutor(max_workers=pool_workers) with DBHelper(db_name="lifecycle") as database: - for table_name in filter(lambda x: x not in ['packages_issue', 'packages_maintainer'], + for table_name in filter(lambda x: x not in ['packages_issue', 'packages_maintainer', + 'databases_info'], database.engine.table_names()): cls_model = Packages.package_meta(table_name) for package_item in database.session.query(cls_model).filter( diff --git a/packageship/packageship/application/apps/package/function/constants.py b/packageship/packageship/application/apps/package/function/constants.py index 083881ce..49ff91ae 100644 --- a/packageship/packageship/application/apps/package/function/constants.py +++ b/packageship/packageship/application/apps/package/function/constants.py @@ -44,6 +44,7 @@ class ResponseCode(): UPDATA_OR_ADD_DATA_FAILED = "4008" TABLE_NAME_NOT_EXIST = "4009" UPDATA_DATA_FAILED = "4010" + NOT_FOUND_DATABASE_INFO = "4011" # Database operation module error status code DELETE_DB_ERROR = "40051" SERVICE_ERROR = "50000" @@ -79,6 +80,7 @@ class ResponseCode(): SPECIFIED_FILE_NOT_EXIST: "The specified file does not exist", NO_PACKAGES_TABLE: "There is no packages table in the database", UPDATA_OR_ADD_DATA_FAILED: "Failed to update or add data", + NOT_FOUND_DATABASE_INFO: "Unable to get the generated database information", DATABASE_NOT_FOUND: "There is no such database in the system", TABLE_NAME_NOT_EXIST: "There is no such table in the database", UPDATA_DATA_FAILED: "Failed to update data", diff --git a/packageship/packageship/application/apps/package/function/searchdb.py b/packageship/packageship/application/apps/package/function/searchdb.py index 1624e0d0..2f8b777a 100644 --- a/packageship/packageship/application/apps/package/function/searchdb.py +++ b/packageship/packageship/application/apps/package/function/searchdb.py @@ -6,18 +6,19 @@ functions: db_priority """ from collections import namedtuple, Counter -import yaml from flask import current_app from sqlalchemy import text -from sqlalchemy.exc import SQLAlchemyError, DisconnectionError +from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy.exc import DisconnectionError from sqlalchemy.sql import literal_column from sqlalchemy import exists from packageship.libs.dbutils import DBHelper +from packageship.libs.exception import Error from packageship.libs.log import Log -from packageship.application.models.package import BinPack, SrcPack -from packageship.libs.exception import ContentNoneException, Error -from packageship.system_config import DATABASE_FILE_INFO +from packageship.application.models.package import BinPack +from packageship.application.models.package import SrcPack +from packageship.application.models.package import DatabaseInfo from packageship.application.apps.package.function.constants import ResponseCode LOGGER = Log(__name__) @@ -919,26 +920,20 @@ class SearchDB(): def db_priority(): """ - Description: Read yaml file, return database name, according to priority + Description: Read the databases_info table to return the table + name according to the priority of the database Args: Returns: db_list: database name list Raises: - FileNotFoundError: file cannot be found + SQLAlchemyError: file cannot be found Error: abnormal error """ try: - with open(DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context: - - init_database_date = yaml.load( - file_context.read(), Loader=yaml.FullLoader) - if init_database_date is None: - raise ContentNoneException( - "The content of the database initialization configuration file cannot be empty") - init_database_date.sort(key=lambda x: x['priority'], reverse=False) - db_list = [item.get('database_name') - for item in init_database_date] - return db_list - except (FileNotFoundError, Error) as file_not_found: - current_app.logger.error(file_not_found) + with DBHelper(db_name='lifecycle') as data_name: + name_list = data_name.session.query( + DatabaseInfo.name).order_by(DatabaseInfo.priority).all() + return [name[0] for name in name_list] + except (SQLAlchemyError, DisconnectionError, Error) as error: + current_app.logger.error(error) return None diff --git a/packageship/packageship/application/apps/package/view.py b/packageship/packageship/application/apps/package/view.py index 24c1f183..c405bd28 100644 --- a/packageship/packageship/application/apps/package/view.py +++ b/packageship/packageship/application/apps/package/view.py @@ -4,22 +4,22 @@ description: Interface processing class: BeDepend, BuildDepend, InitSystem, InstallDepend, Packages, Repodatas, SelfDepend, SinglePack """ -import yaml +from sqlalchemy.exc import DisconnectionError +from sqlalchemy.exc import SQLAlchemyError from flask import request from flask import jsonify from flask import current_app from flask_restful import Resource -from sqlalchemy.exc import DisconnectionError from packageship import system_config from packageship.application.initsystem.data_import import InitDataBase from packageship.libs.configutils.readconfig import ReadConfig +from packageship.libs.dbutils import DBHelper from packageship.libs.exception import Error from packageship.libs.exception import ContentNoneException from packageship.libs.exception import DataMergeException from packageship.libs.exception import ConfigurationException from packageship.libs.log import Log -from packageship.system_config import DATABASE_FILE_INFO from .function.constants import ResponseCode from .function.packages import get_all_package_info from .function.packages import sing_pack @@ -37,6 +37,7 @@ from .serialize import InstallDependSchema from .serialize import BuildDependSchema from .serialize import SelfDependSchema from .serialize import have_err_db_name +from ...models.package import DatabaseInfo LOGGER = Log(__name__) @@ -201,7 +202,7 @@ class InstallDepend(Resource): if not db_pri: return jsonify( ResponseCode.response_json( - ResponseCode.FILE_NOT_FIND_ERROR + ResponseCode.NOT_FOUND_DATABASE_INFO ) ) @@ -277,7 +278,7 @@ class BuildDepend(Resource): if not db_pri: return jsonify( ResponseCode.response_json( - ResponseCode.FILE_NOT_FIND_ERROR + ResponseCode.NOT_FOUND_DATABASE_INFO ) ) @@ -356,7 +357,7 @@ class SelfDepend(Resource): if not db_pri: return jsonify( ResponseCode.response_json( - ResponseCode.FILE_NOT_FIND_ERROR + ResponseCode.NOT_FOUND_DATABASE_INFO ) ) db_list = data.get("db_list") if data.get("db_list") \ @@ -459,7 +460,7 @@ class Repodatas(Resource): def get(self): """ - get all database + get all database info Returns: for @@ -468,37 +469,32 @@ class Repodatas(Resource): "code": "", "data": [ { - "database_name": "", + "name": "", "priority": "", - "status": "" } ], "msg": "" } Raises: - FileNotFoundError: File not found exception - TypeError: Exception of wrong type - Error: abnormal Error + SQLAlchemyError: sql error + Error: Error: abnormal error + DisconnectionError: Unable to connect to the database + """ try: - with open(DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context: - init_database_date = yaml.load( - file_context.read(), Loader=yaml.FullLoader) - if init_database_date is None: - raise ContentNoneException( - "The content of the database initialization configuration " - "file cannot be empty ") - init_database_date.sort( - key=lambda x: x['priority'], reverse=False) + with DBHelper(db_name='lifecycle') as data_name: + name_list = data_name.session.query( + DatabaseInfo.name, DatabaseInfo.priority).order_by(DatabaseInfo.priority).all() + data_list = [dict(zip(ven.keys(), ven)) for ven in name_list] return jsonify( ResponseCode.response_json( ResponseCode.SUCCESS, - data=init_database_date)) - except (FileNotFoundError, TypeError, Error) as file_not_found: - current_app.logger.error(file_not_found) + data=data_list)) + except (SQLAlchemyError, Error, DisconnectionError) as data_info_error: + current_app.logger.error(data_info_error) return jsonify( - ResponseCode.response_json(ResponseCode.FILE_NOT_FOUND) - ) + ResponseCode.response_json( + ResponseCode.NOT_FOUND_DATABASE_INFO)) def delete(self): """ @@ -527,8 +523,8 @@ class Repodatas(Resource): db_list = db_priority() if db_list is None: return jsonify( - ResponseCode.response_json(ResponseCode.FILE_NOT_FOUND) - ) + ResponseCode.response_json( + ResponseCode.NOT_FOUND_DATABASE_INFO)) if db_name not in db_priority(): return jsonify( ResponseCode.response_json(ResponseCode.DB_NAME_ERROR) @@ -536,13 +532,13 @@ class Repodatas(Resource): try: drop_db = InitDataBase() del_result = drop_db.delete_db(db_name) - if del_result is False: + if not del_result: return jsonify( ResponseCode.response_json(ResponseCode.DELETE_DB_ERROR)) return jsonify( ResponseCode.response_json(ResponseCode.SUCCESS) ) - except (FileNotFoundError, TypeError, Error) as error: + except (SQLAlchemyError, TypeError, Error, DisconnectionError) as error: current_app.logger.error(error) return jsonify( ResponseCode.response_json(ResponseCode.DELETE_DB_ERROR) @@ -605,14 +601,16 @@ class InitSystem(Resource): except ConfigurationException as error: LOGGER.logger.error(error) abnormal = error - return jsonify(ResponseCode.response_json('5000', msg=abnormal.message)) + return jsonify( + ResponseCode.response_json( + '5000', msg=abnormal.message)) except DataMergeException as data_merge_error: LOGGER.logger.error(data_merge_error) abnormal = ResponseCode.DATA_MERGE_ERROR except FileNotFoundError as file_not_found_error: LOGGER.logger.error(file_not_found_error) abnormal = ResponseCode.FILE_NOT_FIND_ERROR - except Error as error: + except (Error, SQLAlchemyError) as error: LOGGER.logger.error(error) abnormal = ResponseCode.FAILED_CREATE_DATABASE_TABLE if abnormal is not None: diff --git a/packageship/packageship/application/initsystem/data_import.py b/packageship/packageship/application/initsystem/data_import.py index a5846bd8..34f7288a 100644 --- a/packageship/packageship/application/initsystem/data_import.py +++ b/packageship/packageship/application/initsystem/data_import.py @@ -5,8 +5,8 @@ Description: Initialization of data import Class: InitDataBase,MysqlDatabaseOperations,SqliteDatabaseOperations """ import os -import pathlib import yaml +from sqlalchemy import text from sqlalchemy.exc import SQLAlchemyError, InternalError from packageship.libs.dbutils.sqlalchemy_helper import DBHelper from packageship.libs.exception import ContentNoneException @@ -15,7 +15,7 @@ from packageship.libs.exception import Error from packageship.libs.exception import ConfigurationException from packageship.libs.configutils.readconfig import ReadConfig from packageship.libs.log import Log -from packageship.application.models.package import SrcPack +from packageship.application.models.package import SrcPack, DatabaseInfo from packageship.application.models.package import BinPack from packageship.application.models.package import BinRequires from packageship.application.models.package import SrcRequires @@ -65,7 +65,8 @@ class InitDataBase(): # Create life cycle related databases and tables if not self.create_database(db_name='lifecycle', tables=['packages_issue', - 'packages_maintainer'], + 'packages_maintainer', + 'databases_info'], storage=True): raise SQLAlchemyError( 'Failed to create the specified database and table:lifecycle') @@ -129,9 +130,18 @@ class InitDataBase(): if self.__exists_repeat_database(): raise DatabaseRepeatException( 'There is a duplicate database configuration') - if not InitDataBase.delete_settings_file(): - raise IOError( - 'An error occurred while deleting the database configuration file') + + if not self.__clear_database(): + raise SQLAlchemyError( + 'Failed to clear database data in database_folder folder') + + if not InitDataBase.__clear_database_name_table(): + raise SQLAlchemyError( + 'Failed to clear data in database_name') + + if not InitDataBase.__clear_lifecycle_table(): + raise SQLAlchemyError( + 'Failed to delete table in lifecycle database') for database_config in self.config_file_datas: if not database_config.get('dbname'): @@ -215,8 +225,7 @@ class InitDataBase(): 'database_name': _db_name, 'priority': database_config.get('priority'), } - InitDataBase.__updata_settings_file( - database_content=database_content) + InitDataBase.__update_data_name(database_content) except (SQLAlchemyError, ContentNoneException, TypeError, Error, FileNotFoundError) as error_msg: @@ -498,53 +507,82 @@ class InitDataBase(): return False @staticmethod - def __updata_settings_file(**Kwargs): + def __update_data_name(database_content): """ - update some configuration files related to the database in the system + Update the database_name table Args: - **Kwargs: data related to configuration file nodes - database_name: Name database + database_content: + Dictionary of database names and priorities Returns: - Raises: - FileNotFoundError: The specified file was not found - IOError: File or network operation io abnormal """ try: - if not os.path.exists(system_config.DATABASE_FILE_INFO): - pathlib.Path(system_config.DATABASE_FILE_INFO).touch() - with open(system_config.DATABASE_FILE_INFO, 'a+', encoding='utf8') as file_context: - setting_content = [] - if 'database_content' in Kwargs.keys(): - content = Kwargs.get('database_content') - if content: - setting_content.append(content) - yaml.dump(setting_content, file_context) - - except FileNotFoundError as not_found: - LOGGER.logger.error(not_found) - except IOError as exception_msg: - LOGGER.logger.error(exception_msg) + with DBHelper(db_name="lifecycle") as database_name: + name = database_content.get("database_name") + priority = database_content.get("priority") + database_name.add(DatabaseInfo( + name=name, priority=priority + )) + database_name.session.commit() + except (SQLAlchemyError, Error, AttributeError) as error: + LOGGER.logger.error(error) + @staticmethod - def delete_settings_file(): + def __clear_database_name_table(): """ - Delete the configuration file of the database - - Args: + Clear the data in the databases_info table + Returns: + True or False + """ + try: + with DBHelper(db_name="lifecycle") as database_name: + clear_sql = """delete from databases_info;""" + database_name.session.execute(text(clear_sql)) + database_name.session.commit() + except SQLAlchemyError as error: + LOGGER.logger.error(error) + return False + else: + return True + @staticmethod + def __clear_lifecycle_table(): + """ + Delete the tables in the lifecycle except for the specific three tables Returns: - True if the deletion is successful, otherwise false - Raises: - IOError: File or network operation io abnormal + + """ + try: + with DBHelper(db_name="lifecycle") as database_name: + table_list = database_name.engine.table_names() + for item in table_list: + if item not in ['packages_maintainer', 'databases_info', 'packages_issue']: + drop_sql = '''DROP TABLE if exists `{table_name}`'''.format(table_name=item) + database_name.session.execute(text(drop_sql)) + database_name.session.commit() + except SQLAlchemyError as sql_error: + LOGGER.logger.error(sql_error) + return False + else: + return True + + def __clear_database(self): """ + Delete database + Returns: + """ try: - if os.path.exists(system_config.DATABASE_FILE_INFO): - os.remove(system_config.DATABASE_FILE_INFO) - except (IOError, Error) as exception_msg: - LOGGER.logger.error(exception_msg) + with DBHelper(db_name='lifecycle') as data_name: + name_data_list = data_name.session.query( + DatabaseInfo.name).order_by(DatabaseInfo.priority).all() + name_list = [name[0] for name in name_data_list] + for item in name_list: + self.__del_database(item) + except (SQLAlchemyError, Error, IOError) as error: + LOGGER.logger.error(error) return False else: return True @@ -562,25 +600,18 @@ class InitDataBase(): """ try: del_result = True - file_read = open( - system_config.DATABASE_FILE_INFO, 'r', encoding='utf-8') - _databases = yaml.load( - file_read.read(), Loader=yaml.FullLoader) - for database in _databases: - if database.get('database_name') == db_name: - _databases.remove(database) - # Delete the successfully imported database configuration node - with open(system_config.DATABASE_FILE_INFO, 'w+', encoding='utf-8') as file_context: - yaml.safe_dump(_databases, file_context) - except (IOError, Error) as del_config_error: - LOGGER.logger.error(del_config_error) + with DBHelper(db_name='lifecycle') as database_name: + database_name.session.query(DatabaseInfo).filter \ + (DatabaseInfo.name == db_name).delete() + drop_sql = '''DROP TABLE if exists `{table_name}`''' \ + .format(table_name=db_name) + database_name.session.execute(text(drop_sql)) + database_name.session.commit() + except SQLAlchemyError as sql_error: + LOGGER.logger.error(sql_error) del_result = False - finally: - file_read.close() - if del_result: del_result = self.__del_database(db_name) - return del_result def create_database(self, db_name, tables=None, storage=True): diff --git a/packageship/packageship/application/models/package.py b/packageship/packageship/application/models/package.py index f95bfcc2..3fbd6718 100644 --- a/packageship/packageship/application/models/package.py +++ b/packageship/packageship/application/models/package.py @@ -202,3 +202,13 @@ class PackagesMaintainer(DBHelper.BASE): name = Column(String(200), nullable=True) maintainer = Column(String(200), nullable=True) maintainlevel = Column(Integer, nullable=True) + + +class DatabaseInfo(DBHelper.BASE): + """ + Save the name and priority of the database + """ + __tablename__ = 'databases_info' + id = Column(Integer, primary_key=True) + name = Column(String(200), nullable=True) + priority = Column(Integer, nullable=True) \ No newline at end of file diff --git a/packageship/test/common_files/correct_test_result_json/get_repodatas.json b/packageship/test/common_files/correct_test_result_json/get_repodatas.json index 8b8d0124..c977baa4 100644 --- a/packageship/test/common_files/correct_test_result_json/get_repodatas.json +++ b/packageship/test/common_files/correct_test_result_json/get_repodatas.json @@ -1,10 +1,10 @@ [ { - "database_name": "mainline", + "name": "mainline", "priority": 1 }, { - "database_name": "fedora30", + "name": "fedora30", "priority": 2 } ] \ No newline at end of file diff --git a/packageship/test/common_files/dbs/lifecycle.db b/packageship/test/common_files/dbs/lifecycle.db index 264f168a630bc3e8485bdf9e464d8a56e409b808..457902d611737b640179d876a3a9e5c67b68c3fa 100644 GIT binary patch delta 748 zcmY++O;^kT9L4eff0~-9rp5?S;#-8040+B|Wu!DA<(W*H7*b8;A&(VqD3l@AUP0%y zXr)(RV`a<98(?SWw{7t`_uO;Oxx+tyd56tpVQ9v2GRGXpmuK1LI8OOgMOD#^Q(65d zC(Y+%xKStjpZ|Bp4ez`g^@iN28%>SmexHrHUtKqAt~fmIbL-0kzWlN>cQiN{>*$Vm z#w=n3@qzY4Pgn2a=BaCm)U-xut*LE_P_lSTg#+Q#w?I~?uRqb-pBNk}p`p1oQXgqa zCDRLL=e@Pr)8VAo?+GqA!N0+U*$_X1ML8a?Q{;BVJA3SWWQr`2EdnA(%NaiJqDx#CH$+0*6t_gTxGj1_ujrd5>2o{g{3J)yJ^n9a6QxPhZwAbu zxnu5{d*;3wG7ro{^T<3lPs~#@Y(~ted1juQ7e;2xyfm-OYcp;p%p3F8yfc$#%Dguz R^TB*H)8^CMMCs?j#{c^l@QMHc delta 1786 zcmciB?Nd~B6bJBKU|AkSS~pTM^NU3RkuCTTMP!7VqCATzm9CfF3vBEIuoqnDK^FCk z3JMAeWkWB7nWiSpu?a=YzoD1S)J)BA7kgE28{c#mgIyu(8tVr0E##$ZSuFc{2MSsM%nL+<(fEgNPGn|J(_7H>9~jFpDezmgUbTN8dTR^I=O zTH=4aUTM5-w9l@!+l?pNG^NR-I@%Ryo6@2N9PU7%OLh1?E=QrQ&}J(t-1?|o+~_Z+ zb;tf)vk5sV$!-_QtLp8$>~*NBu1D4Wii&*5_RT<}cDuLK^q_*}n(%0rc&D#=t$5AukPv|$TNt?}c zPvn_QPG_hnZG6%@@%>V`DjKFn!<1;491W9}LTjXhd9@)FUy_j-innH1GcDs;hbH!= z_}f)qc|mpUfueC^TSrM&Z>!R(lr`8ROEBp3g|;WgmSsG=Y-3AInb#XSWLhq&m9rjR zRJ{9-`JwRe$im!Ic=CjPYc4!?H9QmwpPSZ)ujs=Q;mhBJuk?k#nD)1OW4bprg^nb| zbkF>c?t34*dq<@E;{4q2pUy_2?(Wc$_(vXKi#)30$3H-O@POXl(BZh{1F9_<%Xe^W zZe5|K=#5UIo} zB8^y0q!UjN7Ge!yC7vWQh^L6P#5y9Ac$!#GJVR_Co+UOC(m#vXL_9}4Ph=A>5HAur zL@tp>yhP*^FB6-IR|p$XKok;Nh^@pnVmt9FQAE5(yiODoZxAKKo5T*{EuxfonQeqDyp( zqarAJM6dWzd?Y>=pNM0kPlQCjI4(|zlY(MEoD!$S88Ik^#947p42uzQUR)5PVoY2V Nm&Ew&Q1*nj=Wo0@tn~l@ diff --git a/packageship/test/common_files/operate_dbs/lifecycle.db b/packageship/test/common_files/operate_dbs/lifecycle.db index 9053816339c706c36913a234b7e928c439529b21..2979e19f647503f5c425f40dc178bec09b925993 100644 GIT binary patch delta 1293 zcmY+;X;_qH7{&4T9cN`2#sQT@!2?o>8M#zewxOYxTB&G@k&Q9b6ao!onOGyqA+rL* zqRph-ca0J)&30RCPi@gkvBgp=D_XSWJbm-S@BeJqbze_BKHC?U#*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}mtqvRlC9(@xk{drue4GAzi6TURHPIuB}%DM frnFVsDdkFgrGrwTbW}PiqEtE#v=nr91bX}feUQ=~ diff --git a/packageship/test/test_module/init_system_tests/test_importdata.py b/packageship/test/test_module/init_system_tests/test_importdata.py index 681be9c8..c1c16533 100644 --- a/packageship/test/test_module/init_system_tests/test_importdata.py +++ b/packageship/test/test_module/init_system_tests/test_importdata.py @@ -9,8 +9,10 @@ import unittest import warnings from configparser import ConfigParser import yaml + from packageship import system_config -from packageship.libs.exception import Error,ConfigurationException +from packageship.libs.exception import Error +from packageship.libs.exception import ConfigurationException try: @@ -36,11 +38,13 @@ try: except Error: raise Error +from sqlalchemy.exc import SQLAlchemyError from packageship.application.initsystem.data_import import InitDataBase from packageship.libs.exception import ContentNoneException from packageship.libs.exception import DatabaseRepeatException from packageship.libs.configutils.readconfig import ReadConfig - +from packageship.application.models.package import DatabaseInfo +from packageship.libs.dbutils import DBHelper class ImportData(unittest.TestCase): """ @@ -64,7 +68,8 @@ class ImportData(unittest.TestCase): # Yaml file exists but the content is empty try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'w', encoding='utf-8') as w_f: @@ -83,7 +88,8 @@ class ImportData(unittest.TestCase): # Yaml file exists but DB exists_ The same with name try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -118,7 +124,8 @@ class ImportData(unittest.TestCase): config.set("SYSTEM", "init_conf_path", "D:\\Users\\conf.yaml") config.write(open(system_config.SYS_CONFIG_PATH, "w")) - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') InitDataBase(config_file_path=_config_path).init_data() except FileNotFoundError as error: self.assertEqual( @@ -135,7 +142,8 @@ class ImportData(unittest.TestCase): def test_dbname(self): """test dbname""" try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -159,7 +167,8 @@ class ImportData(unittest.TestCase): def test_src_db_file(self): """test src db file""" try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -183,7 +192,8 @@ class ImportData(unittest.TestCase): def test_priority(self): """test priority""" try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -192,12 +202,15 @@ class ImportData(unittest.TestCase): with open(_config_path, 'w', encoding='utf-8') as w_f: yaml.dump(origin_yaml, w_f) InitDataBase(config_file_path=_config_path).init_data() - with open(system_config.DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context: - init_database_date = yaml.load( - file_context.read(), Loader=yaml.FullLoader) + + with DBHelper(db_name='lifecycle') as data_name: + name_list = data_name.session.query( + DatabaseInfo.name).order_by( + DatabaseInfo.priority).all() + init_database_date = [name[0] for name in name_list] self.assertEqual( init_database_date, - None, + [], msg=" Priority must be a positive integer between 0 and 100 ") except FileNotFoundError: return @@ -211,43 +224,30 @@ class ImportData(unittest.TestCase): Initialization of system data """ # Normal configuration - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') - InitDataBase(config_file_path=_config_path).init_data() - - # In the correct case, an import will be generated under the initsystem - # directory_ success_ databse.yaml - path = system_config.DATABASE_FILE_INFO - - self.assertTrue( - os.path.exists(path), - msg="Import was not generated successfully " - "after initialization_ success_ databse.yaml file") - - # And there is data in this file, and it comes from the yaml file of - # conf - with open(_config_path, 'r', encoding='utf-8') as file: - yaml_config = yaml.load(file.read(), Loader=yaml.FullLoader) - - with open(path, 'r', encoding='utf-8') as files: - yaml_success = yaml.load(files.read(), Loader=yaml.FullLoader) - - self.assertEqual( - len(yaml_config), - len(yaml_success), - msg="The success file is inconsistent with the original yaml file") - - # Compare name and priority - success_name_priority = dict() - config_name_priority = dict() - len_con = len(yaml_config) - for i in range(len_con): - success_name_priority[yaml_success[i]["database_name"]] = \ - yaml_success[i]["priority"] - config_name_priority[yaml_config[i]["dbname"]] = \ - yaml_config[i]["priority"] - - self.assertEqual( - success_name_priority, - config_name_priority, - msg="The database and priority after initialization are" - "inconsistent with the original file") + try: + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + InitDataBase(config_file_path=_config_path).init_data() + with DBHelper(db_name='lifecycle') as data_name: + name_list = data_name.session.query( + DatabaseInfo.name, DatabaseInfo.priority).order_by( + DatabaseInfo.priority).all() + data_list = [dict(zip(ven.keys(), ven)) for ven in name_list] + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + with open(_config_path, 'r', encoding='utf-8') as file: + origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) + origin_list = list() + for item in origin_yaml: + data_dict = dict() + data_dict['name'] = item.get("dbname") + data_dict['priority'] = item.get("priority") + origin_list.append(data_dict) + + self.assertEqual( + data_list, + origin_list, + msg="The name and priority of the data generated by the initialization are correct") + + except (Error, SQLAlchemyError, FileNotFoundError, yaml.YAMLError): + return None diff --git a/packageship/test/test_module/repodatas_test/test_delete_repodatas.py b/packageship/test/test_module/repodatas_test/test_delete_repodatas.py index 0cd640ae..6476fea3 100644 --- a/packageship/test/test_module/repodatas_test/test_delete_repodatas.py +++ b/packageship/test/test_module/repodatas_test/test_delete_repodatas.py @@ -6,6 +6,8 @@ test delete repodatas import os import shutil +from sqlalchemy.exc import SQLAlchemyError + from test.base_code.operate_data_base import OperateTestBase import unittest import json @@ -70,7 +72,6 @@ class TestDeleteRepodatas(OperateTestBase): Returns: """ try: - shutil.copyfile(system_config.DATABASE_FILE_INFO, system_config.DATABASE_FILE_INFO + '.bak') shutil.copytree(system_config.DATABASE_FOLDER_PATH, system_config.DATABASE_FOLDER_PATH + '.bak') resp = self.client.delete("/repodatas?dbName=fedora30") resp_dict = json.loads(resp.data) @@ -90,10 +91,8 @@ class TestDeleteRepodatas(OperateTestBase): self.assertIsNone( resp_dict.get("data"), msg="Error in data information return") - except Error: + except (SQLAlchemyError, FileExistsError, Error): return None finally: - os.remove(system_config.DATABASE_FILE_INFO) - os.rename(system_config.DATABASE_FILE_INFO + '.bak', system_config.DATABASE_FILE_INFO) shutil.rmtree(system_config.DATABASE_FOLDER_PATH) os.rename(system_config.DATABASE_FOLDER_PATH + '.bak', system_config.DATABASE_FOLDER_PATH) -- Gitee