Esempio n. 1
0
def test_phase_2(db_conn, caplog, monkeypatch):
    """Test add cves and delete content set."""
    monkeypatch.setattr(FileDownloadThread, '_download', download_mock)

    # Test store cve info
    cve_ctr = CveRepoController()
    cve_ctr.add_repos()
    cve_ctr.store()

    with db_conn.cursor() as cur:
        cur.execute("""select * from cve""")
        rows = cur.fetchall()
        assert len(rows) == 13

        cur.execute("""select * from metadata""")
        rows = cur.fetchall()
        assert len(rows) == 20

    for year in range(2002, 2020):
        assert f"Syncing CVE list: {year}" in caplog.messages
        assert f"File nvdcve-1.0-{year}.meta mock-downloaded." in caplog.messages

    # 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
Esempio n. 2
0
 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"
     return "OK"
Esempio n. 3
0
def main():
    """Main function."""
    parser = ArgumentParser()
    parser.add_argument("-d", "--db-name", action="store", help="Database name (default 'vmaas').",
                        default="vmaas")
    parser.add_argument("-U", "--db-user", action="store", help="Database user (default 'vmaas_user').",
                        default="vmaas_user")
    parser.add_argument("-P", "--db-pass", action="store", help="Database password (default 'vmaas_passwd').",
                        default="vmaas_passwd")
    parser.add_argument("-H", "--db-host", action="store", help="Database host (default 'localhost').",
                        default="localhost")
    parser.add_argument("-p", "--db-port", action="store", help="Database port (default '5432').",
                        default="5432")
    parser.add_argument("-r", "--repo", action="append",
                        help="Sync given repository (can be specifed multiple times).")
    parser.add_argument("--repofile", action="store", help="Read repository list from file.")
    args = parser.parse_args()

    # Setup DB connection parameters
    DatabaseHandler.db_name = args.db_name
    DatabaseHandler.db_user = args.db_user
    DatabaseHandler.db_pass = args.db_pass
    DatabaseHandler.db_host = args.db_host
    DatabaseHandler.db_port = args.db_port

    repository_controller = RepositoryController()
    if args.repo:
        for repo_url in args.repo:
            repository_controller.add_repository(repo_url)
    if args.repofile:
        with open(args.repofile, "r") as repo_file:
            for line in repo_file.readlines():
                repository_controller.add_repository(line)
    repository_controller.store()
Esempio n. 4
0
def repo_sync_task(products=None, repos=None):
    """Function to start syncing all repositories from input list or from database."""
    try:
        init_db()
        repository_controller = RepositoryController()
        if products:
            product_store = ProductStore()
            product_store.store(products)
            # Reference imported content set to associate with repositories
            repository_controller.repo_store.set_content_set_db_mapping(
                product_store.cs_to_dbid)

        if repos:
            # Sync repos from input
            for repo in repos:
                repo_label, repo_url, content_set, cert_name, ca_cert, cert, key = repo
                repository_controller.add_repository(repo_label,
                                                     repo_url,
                                                     content_set=content_set,
                                                     cert_name=cert_name,
                                                     ca_cert=ca_cert,
                                                     cert=cert,
                                                     key=key)
        else:
            # Re-sync repos in DB
            repository_controller.add_synced_repositories()
        repository_controller.store()
    except:  # pylint: disable=bare-except
        LOGGER.log(traceback.format_exc())
        DatabaseHandler.rollback()
        return "ERROR"
    return "OK"
Esempio n. 5
0
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
Esempio n. 6
0
    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)
            init_logging()
            init_db()

            if products:
                product_store = ProductStore()
                product_store.store(products)

            if repos:
                repository_controller = RepositoryController()
                # 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)
                repository_controller.import_repositories()
        except Exception as err:  # pylint: disable=broad-except
            msg = "Internal server error <%s>" % err.__hash__()
            LOGGER.exception(msg)
            DatabaseHandler.rollback()
            return "ERROR"
        return "OK"
Esempio n. 7
0
    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))))
                repository_controller.import_repositories()
        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"
Esempio n. 8
0
 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()
         return "ERROR"
     return "OK"
Esempio n. 9
0
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: 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
Esempio n. 10
0
import sys

from repodata.repository_controller import RepositoryController

if __name__ == '__main__':
    repository_controller = RepositoryController()
    for repo_url in sys.argv[1:]:
        repository_controller.add_repository(repo_url)
    repository_controller.store()