Esempio n. 1
0
def test_products_get_no_product_in_cache(mocker):
    mocker.patch("sps.cache.load", autospec=True)
    mocker.patch("sps.request.fetch", autospec=True)
    cache.load.return_value = {}
    products.get(None, __file__, False, False)
    request.fetch.assert_called_with(
        "https://scc.suse.com/api/package_search/products")
Esempio n. 2
0
def test_products_get_pattern_multiple_match(data, mocker):
    """
    get will return a list of all products matching pattern
    """
    mocker.patch("sps.request.fetch", autospec=True)
    request.fetch.return_value = data
    assert products.get("1", "fake-file-name", False, False) == data["data"]
    assert products.get("SUSE", "fake-file-name", False, False) == data["data"]
    assert products.get("x86", "fake-file-name", False, False) == data["data"]
Esempio n. 3
0
def test_products_update_cache(data, mocker):
    """
    get wtil retrieve products from cache file
    """
    mocker.patch("sps.cache.save", autospec=True)
    mocker.patch("sps.request.fetch", autospec=True)
    request.fetch.return_value = data
    products.get(None, "fake-file-name", False, True)
    cache.save.assert_called_with("product", "fake-file-name", data["data"])
Esempio n. 4
0
def test_products_get(data, mocker):
    """
    get will exit if no data field is found in response
    """
    mocker.patch("sps.request.fetch", autospec=True)
    wrongdata = {}
    wrongdata["bad"] = data["data"]
    request.fetch.return_value = wrongdata
    with pytest.raises(SystemExit):
        products.get(None, "fake-file-name", False, False)
Esempio n. 5
0
def test_products_get_pattern_single_match(data, mocker):
    """
    get will return a list of all products matching pattern
    """
    mocker.patch("sps.request.fetch", autospec=True)
    request.fetch.return_value = data
    assert products.get("Manager", "fake-file-name", False,
                        False) == [data["data"][0]]
    assert products.get("Desktop", "fake-file-name", False,
                        False) == [data["data"][1]]
Esempio n. 6
0
def test_products_get_cache(data, mocker):
    """
    get wtil retrieve products from cache file
    """
    mocker.patch("sps.cache.load", autospec=True)
    cache.load.return_value = {"product": data["data"]}
    assert products.get(None, __file__, False, False) == data["data"]
Esempio n. 7
0
def test_products_get_pattern_no_match(data, mocker):
    """
    get will return a list of all products matching pattern
    """
    mocker.patch("sps.request.fetch", autospec=True)
    request.fetch.return_value = data
    assert products.get("foo-bar-search", "fake-file-name", False, False) == []
Esempio n. 8
0
def test_products_get(mocker, data):
    """
    get will return a list of all products
    """
    mocker.patch("sps.request.fetch", autospec=True)
    request.fetch.return_value = data
    assert products.get(None, "fake-file-name", False, False)
    request.fetch.assert_called_with(
        "https://scc.suse.com/api/package_search/products")
Esempio n. 9
0
File: cli.py Progetto: SweBarre/sps
def main():
    """The main program logic"""

    parser = create_parser()
    args = parser.parse_args()

    if args.command in ["product", "package", "patchproduct", "patch"]:
        table = PrettyTable()
        if args.command == "product":
            products_data = products.get(args.pattern, args.cache_file,
                                         args.no_cache, args.update_cache)
            table.field_names = ["id", "Name", "Edition", "Identifier", "Arch"]
            for product in products_data:
                table.add_row([
                    product["id"],
                    product["name"],
                    product["edition"],
                    product["identifier"],
                    product["architecture"],
                ])

        if args.command == "package":
            package_data = packages.get(args.product, args.pattern,
                                        args.cache_file)
            table.field_names = [
                "Name", "Version", "Release", "Arch", "Module"
            ]
            for package in package_data:
                module_line = ""
                for product in package["products"]:
                    module_line = "{},{}".format(module_line, product["name"])
                if args.exact_match and package["name"] == args.pattern:
                    table.add_row([
                        package["name"],
                        package["version"],
                        package["release"],
                        package["arch"],
                        module_line[1:],
                    ])
                elif not args.exact_match:
                    table.add_row([
                        package["name"],
                        package["version"],
                        package["release"],
                        package["arch"],
                        module_line[1:],
                    ])
        if args.command == "patchproduct":
            products_data = patchproducts.get(args.pattern, args.cache_file,
                                              args.no_cache, args.update_cache)
            table.field_names = ["Name", "Version", "Arch"]
            for product in products_data:
                table.add_row([
                    product["name"],
                    product["version"],
                    product["architecture"],
                ])
        if args.command == "patch":
            patches = patch.get(args)
            if args.detail:
                for p in patches["hits"]:
                    print(patch.format_detail(p))

                if patches["meta"]["total_hits"] > PATCH_WARN_NUMBER:
                    print_warn(
                        f"Your query has {patches['meta']['total_hits']} hits, you might want to refine your search criteria"
                    )
                print(
                    f"Page {patches['meta']['current_page']}/{patches['meta']['total_pages']}\t Hits: {patches['meta']['total_hits']}"
                )
                sys.exit(0)
            else:
                table.hrules = ALL
                table.field_names = [
                    "Severity",
                    "Name",
                    "Product",
                    "Arch",
                    "id",
                    "Released",
                ]
                for p in patches["hits"]:
                    table.add_row([
                        p["severity"],
                        p["title"],
                        "\n".join(p["product_friendly_names"]),
                        "\n".join(p["product_architectures"]),
                        p["ibs_id"],
                        p["issued_at"][:p["issued_at"].find("T")],
                    ])
                if patches["meta"]["total_hits"] > PATCH_WARN_NUMBER:
                    print("\n")
                    print_warn(
                        f"Your query has {patches['meta']['total_hits']} hits, you might want to refine your search criteria"
                    )
                print(
                    f"\nPage {patches['meta']['current_page']}/{patches['meta']['total_pages']}\t Hits: {patches['meta']['total_hits']}"
                )

        for name in table.field_names:
            table.align[name] = "l"
        table.border = not args.no_borders
        table.header = not args.no_header
        table.sortby = args.sort_table
        print(table)

    if args.command == "completion":
        print(completion.get(args.cache_file, args.shell))

    cacheages = cache.age(args.cache_file, args.cache_age)
    for key, value in cacheages.items():
        print_warn(f"The {key} cache is old, last updated {value}")