def run_task(*args, **kwargs): """Start importing from git""" try: init_logging() init_db() if not REPOLIST_GIT_TOKEN: LOGGER.warning( "REPOLIST_GIT_TOKEN not set, skipping download of repositories from git." ) return "SKIPPED" _, repos = GitRepoListHandler.fetch_git_repolists() if not repos: return "ERROR" repository_controller = RepositoryController() repos_in_db = repository_controller.repo_store.list_repositories() for _, content_set, basearch, releasever, _, _, _, _ in repos: repos_in_db.pop((content_set, basearch, releasever), None) repository_controller.delete_repos(repos_in_db) except Exception as err: # pylint: disable=broad-except msg = "Internal server error <%s>" % err.__hash__() LOGGER.exception(msg) DatabaseHandler.rollback() if isinstance(err, DatabaseError): return "DB_ERROR" return "ERROR" finally: DatabaseHandler.close_connection() return "OK"
def run_task(*args, **kwargs): """Function to start deleting repos.""" try: repo = kwargs.get("repo", None) init_logging() init_db() repository_controller = RepositoryController() repository_controller.delete_content_set(repo) except Exception as err: # pylint: disable=broad-except msg = "Internal server error <%s>" % err.__hash__() LOGGER.exception(msg) DatabaseHandler.rollback() return "ERROR" finally: DatabaseHandler.close_connection() return "OK"
def test_phase_2(db_conn, monkeypatch): """Test add cves and delete content set.""" monkeypatch.setattr(FileDownloadThread, '_download', download_mock) # Test delete content_set rep_con = RepositoryController() rep_con.delete_content_set("content_set_1") with db_conn.cursor() as cur: cur.execute("""select * from package""") rows = cur.fetchall() assert len(rows) == 6 cur.execute("""select * from errata_refs""") rows = cur.fetchall() assert not rows
def test_phase_2(db_conn, monkeypatch): """Test delete content set.""" DatabaseHandler.connection = db_conn reset_db(db_conn) write_testing_data(db_conn) monkeypatch.setattr(FileDownloadThread, '_download', download_mock) # Test delete content_set rep_con = RepositoryController() _assert_rows_in_table(db_conn, 'content_set', 2) _assert_rows_in_table(db_conn, 'package', 7) _assert_rows_in_table(db_conn, 'repo', 2) rep_con.delete_content_set("content set 2") _assert_rows_in_table(db_conn, 'content_set', 1) _assert_rows_in_table(db_conn, 'package', 6) _assert_rows_in_table(db_conn, 'repo', 1)
def run_task(*args, **kwargs): """Function to import all repositories from input list to the DB.""" try: products = kwargs.get("products", None) repos = kwargs.get("repos", None) git_sync = kwargs.get("git_sync", False) init_logging() init_db() if products: product_store = ProductStore() product_store.store(products) if repos: repository_controller = RepositoryController() repos_in_db = repository_controller.repo_store.list_repositories( ) # Sync repos from input for repo_url, content_set, basearch, releasever, cert_name, ca_cert, cert, key in repos: repository_controller.add_repository(repo_url, content_set, basearch, releasever, cert_name=cert_name, ca_cert=ca_cert, cert=cert, key=key) repos_in_db.pop((content_set, basearch, releasever), None) if git_sync: # Warn about extra repos in DB when syncing main repolist from git for content_set, basearch, releasever in repos_in_db: LOGGER.warning( "Repository in DB but not in git repolist: %s", ", ".join( filter(None, (content_set, basearch, releasever)))) REPOS_TO_CLEANUP.set(len(repos_in_db)) repository_controller.import_repositories() except Exception as err: # pylint: disable=broad-except msg = "Internal server error <%s>" % err.__hash__() LOGGER.exception(msg) DatabaseHandler.rollback() if isinstance(err, DatabaseError): return "DB_ERROR" return "ERROR" finally: DatabaseHandler.close_connection() return "OK"
def run_task(*args, **kwargs): """Function to start syncing all repositories available from database.""" try: init_logging() init_db() repository_controller = RepositoryController() repository_controller.add_db_repositories() repository_controller.store() except Exception as err: # pylint: disable=broad-except msg = "Internal server error <%s>" % err.__hash__() LOGGER.exception(msg) DatabaseHandler.rollback() if isinstance(err, DatabaseError): return "DB_ERROR" return "ERROR" finally: DatabaseHandler.close_connection() return "OK"
def test_phase_1(db_conn, caplog, monkeypatch): """Test add product and repo.""" DatabaseHandler.connection = db_conn reset_db(db_conn) # write_testing_data(db_conn) basearch = 'x86_64' releasever = '7Server' base_url = 'http://localhost:8888/%s/%s' % (releasever, basearch) content_set = 'content_set_1' content_set_label = 'Content set 1' # Test store product. products = dict( prod1=dict(product_id=None, content_sets={content_set: { "name": content_set_label }})) prod_store = product_store.ProductStore() prod_store.store(products) monkeypatch.setattr(FileDownloadThread, '_download', download_mock) # Test store repo. rep_con = RepositoryController() rep_con.add_repository(repo_url=base_url, content_set=content_set, basearch=basearch, releasever=releasever) rep_con.import_repositories() rep_con1 = RepositoryController() rep_con1.add_db_repositories() rep_con1.store() for file in ["repomd.xml", "updateinfo.xml.gz", "primary_db.sqlite.gz"]: assert f"File {file} mock-downloaded." in caplog.messages _assert_rows_in_table(db_conn, 'content_set', 1) _assert_rows_in_table(db_conn, 'repo', 1) _assert_rows_in_table(db_conn, 'package', 18) _assert_rows_in_table(db_conn, 'errata_refs', 5) _assert_rows_in_table(db_conn, 'cve', 2)
def test_phase_1(db_conn, caplog, monkeypatch): """Test add product and repo.""" basearch = 'x86_64' releasever = '7Server' base_url = 'http://localhost:8888/%s/%s' % (releasever, basearch) content_set = 'content_set_1' content_set_label = 'Content set 1' # Test store product. products = dict( prod1=dict(product_id=None, content_sets={content_set: { "name": content_set_label }})) prod_store = product_store.ProductStore() prod_store.store(products) monkeypatch.setattr(FileDownloadThread, '_download', download_mock) # Test store repo. rep_con = RepositoryController() rep_con.add_repository(repo_url=base_url, content_set=content_set, basearch=basearch, releasever=releasever) rep_con.import_repositories() rep_con1 = RepositoryController() rep_con1.add_db_repositories() rep_con1.store() for file in ["repomd.xml", "updateinfo.xml.gz", "primary_db.sqlite.gz"]: assert f"File {file} mock-downloaded." in caplog.messages with db_conn.cursor() as cur: cur.execute("""select * from content_set""") rows = cur.fetchall() assert len(rows) == 1 cur.execute("""select * from repo""") rows = cur.fetchall() assert len(rows) == 1 cur.execute("""select * from package""") rows = cur.fetchall() assert len(rows) == 18 cur.execute("""select * from errata_refs""") rows = cur.fetchall() assert len(rows) == 5 cur.execute("""select * from cve""") rows = cur.fetchall() assert len(rows) == 2