Esempio n. 1
0
def test_fetch_npm_source_resolve_fails(mock_rn, mock_pnfjr, mock_srs,
                                        mock_vnf, mock_rbd):
    request_id = 6
    request = {"id": request_id}
    mock_srs.return_value = request
    mock_rn.side_effect = CachitoError("Some error")

    with pytest.raises(CachitoError, match="Some error"):
        npm.fetch_npm_source(request_id)

    assert mock_srs.call_count == 2
    mock_pnfjr.assert_called_once_with("cachito-npm-6")
Esempio n. 2
0
def test_fetch_npm_source(
    mock_update_request_env_vars,
    mock_urwcf,
    mock_gnc,
    mock_gcc,
    mock_fnfjr,
    mock_rn,
    mock_pnfjr,
    mock_get_request,
    mock_srs,
    mock_vnf,
    get_worker_config,
    ca_file,
    lock_file,
    package_json,
    package_subpath,
    subpath_as_path_component,
    reverse_path_component,
    task_passes_state_check,
    tmpdir,
):
    get_worker_config.return_value = mock.Mock(cachito_bundles_dir=tmpdir)
    request_id = 6
    request = {"id": request_id}
    mock_get_request.return_value = request
    package = {"name": "han-solo", "type": "npm", "version": "5.0.0"}
    deps = [
        {"dev": False, "name": "@angular/animations", "type": "npm", "version": "8.2.14"},
        {"dev": False, "name": "tslib", "type": "npm", "version": "1.11.1"},
    ]
    mock_rn.return_value = {
        "deps": deps,
        "downloaded_deps": {"@angular/[email protected]", "[email protected]"},
        "lock_file": lock_file,
        "lock_file_name": "package-lock.json",
        "package": package,
        "package.json": package_json,
    }
    username = f"cachito-npm-{request_id}"
    password = "******"
    mock_fnfjr.return_value = password
    mock_gcc.return_value = ca_file
    mock_gnc.return_value = "some npmrc"

    if package_subpath:
        package_configs = [{"path": package_subpath}]
    else:
        package_configs = None

    npm.fetch_npm_source(request_id, package_configs=package_configs)

    bundle_dir = RequestBundleDir(request_id)
    mock_vnf.assert_called_once_with(bundle_dir, [package_subpath or "."])
    assert mock_srs.call_count == 3
    assert mock_get_request.called_once_with(request_id)
    mock_pnfjr.assert_called_once_with("cachito-npm-6")
    lock_file_path = str(bundle_dir.app_subpath(package_subpath or ".").source_dir)
    mock_rn.assert_called_once_with(lock_file_path, request, skip_deps=set())
    if ca_file:
        mock_gnc.assert_called_once_with(
            "http://nexus:8081/repository/cachito-npm-6/",
            username,
            password,
            custom_ca_path=f"{reverse_path_component}registry-ca.pem",
        )
    else:
        mock_gnc.assert_called_once_with(
            "http://nexus:8081/repository/cachito-npm-6/", username, password, custom_ca_path=None
        )

    expected_config_files = []
    if package_json:
        expected_config_files.append(
            {
                "content": "ewogICJuYW1lIjogImhhbi1zb2xvIgp9",
                "path": f"app/{subpath_as_path_component}package.json",
                "type": "base64",
            }
        )

    if lock_file:
        expected_config_files.append(
            {
                "content": "ewogICJkZXBlbmRlbmNpZXMiOiBbXQp9",
                "path": f"app/{subpath_as_path_component}package-lock.json",
                "type": "base64",
            }
        )

    if ca_file:
        expected_config_files.append(
            {
                "content": "c29tZSBDQSBmaWxlIGNvbnRlbnRz",
                "path": "app/registry-ca.pem",
                "type": "base64",
            }
        )

    expected_config_files.append(
        {
            "content": "c29tZSBucG1yYw==",
            "path": f"app/{subpath_as_path_component}.npmrc",
            "type": "base64",
        }
    )
    mock_urwcf.assert_called_once_with(request_id, expected_config_files)

    mock_update_request_env_vars.assert_called_once_with(
        request_id,
        {
            "CHROMEDRIVER_SKIP_DOWNLOAD": {"value": "true", "kind": "literal"},
            "SKIP_SASS_BINARY_DOWNLOAD_FOR_CI": {"value": "true", "kind": "literal"},
        },
    )

    pkg_info = package.copy()
    pkg_info["dependencies"] = deps
    if package_subpath and package_subpath != os.curdir:
        pkg_info["path"] = package_subpath
    assert {"packages": [pkg_info]} == json.loads(bundle_dir.npm_packages_data.read_bytes())
Esempio n. 3
0
def test_fetch_npm_source_multiple_paths(
    mock_update_request_env_vars,
    mock_urwcf,
    mock_gnc,
    mock_gcc,
    mock_fnfjr,
    mock_rn,
    mock_pnfjr,
    mock_get_request,
    mock_srs,
    mock_vnf,
    mock_rbd,
    task_passes_state_check,
):
    request_id = 6
    request = {"id": request_id}
    mock_get_request.return_value = request
    package = {"name": "han-solo", "type": "npm", "version": "5.0.0"}
    package_two = {"name": "han-solo", "type": "npm", "version": "6.0.0"}
    deps = [
        {"dev": False, "name": "@angular/animations", "type": "npm", "version": "8.2.14"},
        {"dev": False, "name": "tslib", "type": "npm", "version": "1.11.1"},
    ]
    # The package.json and package-lock.json mock values are not actually valid,
    # they just need to be valid JSON
    mock_rn.side_effect = [
        {
            "deps": deps,
            "downloaded_deps": {"@angular/[email protected]", "[email protected]"},
            "lock_file": {"dependencies": []},
            "lock_file_name": "package-lock.json",
            "package": package,
            "package.json": {"name": "han-solo", "version": "5.0.0"},
        },
        {
            "deps": deps,
            "downloaded_deps": {"@angular/[email protected]", "[email protected]"},
            "lock_file": {"dependencies": []},
            "lock_file_name": "package-lock.json",
            "package": package_two,
            "package.json": {"name": "han-solo", "version": "6.0.0"},
        },
    ]
    ca_file = "some CA file contents"
    mock_gcc.return_value = ca_file
    mock_gnc.return_value = "some npmrc"

    npm.fetch_npm_source(request_id, [{"path": "old-client"}, {"path": "new-client/client"}])

    mock_vnf.assert_called_once_with(mock_rbd.return_value, ["old-client", "new-client/client"])
    mock_pnfjr.assert_called_once()
    mock_rn.assert_has_calls(
        (
            mock.call(
                str(mock_rbd().app_subpath("old-client").source_dir), request, skip_deps=set()
            ),
            mock.call(
                str(mock_rbd().app_subpath("new-client/client").source_dir),
                request,
                skip_deps={"@angular/[email protected]", "[email protected]"},
            ),
        )
    )
    mock_gnc.assert_has_calls(
        (
            mock.call(mock.ANY, mock.ANY, mock.ANY, custom_ca_path="../registry-ca.pem"),
            mock.call(mock.ANY, mock.ANY, mock.ANY, custom_ca_path="../../registry-ca.pem"),
        )
    )

    expected_config_files = [
        {
            "content": "ewogICJuYW1lIjogImhhbi1zb2xvIiwKICAidmVyc2lvbiI6ICI1LjAuMCIKfQ==",
            "path": "app/old-client/package.json",
            "type": "base64",
        },
        {
            "content": "ewogICJkZXBlbmRlbmNpZXMiOiBbXQp9",
            "path": "app/old-client/package-lock.json",
            "type": "base64",
        },
        {
            "content": "ewogICJuYW1lIjogImhhbi1zb2xvIiwKICAidmVyc2lvbiI6ICI2LjAuMCIKfQ==",
            "path": "app/new-client/client/package.json",
            "type": "base64",
        },
        {
            "content": "ewogICJkZXBlbmRlbmNpZXMiOiBbXQp9",
            "path": "app/new-client/client/package-lock.json",
            "type": "base64",
        },
        {
            "content": "c29tZSBDQSBmaWxlIGNvbnRlbnRz",
            "path": "app/registry-ca.pem",
            "type": "base64",
        },
        {"content": "c29tZSBucG1yYw==", "path": "app/old-client/.npmrc", "type": "base64"},
        {"content": "c29tZSBucG1yYw==", "path": "app/new-client/client/.npmrc", "type": "base64"},
    ]

    mock_urwcf.assert_called_once_with(request_id, expected_config_files)

    mock_update_request_env_vars.assert_called_once_with(
        request_id,
        {
            "CHROMEDRIVER_SKIP_DOWNLOAD": {"kind": "literal", "value": "true"},
            "SKIP_SASS_BINARY_DOWNLOAD_FOR_CI": {"kind": "literal", "value": "true"},
        },
    )
Esempio n. 4
0
def test_fetch_npm_source(
    mock_urwd,
    mock_urwp,
    mock_urwcf,
    mock_gnc,
    mock_gcc,
    mock_fnfjr,
    mock_rn,
    mock_pnfjr,
    mock_srs,
    mock_vnf,
    mock_rbd,
    ca_file,
    lock_file,
    package_json,
    package_subpath,
    subpath_as_path_component,
    reverse_path_component,
):
    request_id = 6
    request = {"id": request_id}
    mock_srs.return_value = request
    package = {"name": "han-solo", "type": "npm", "version": "5.0.0"}
    deps = [
        {
            "dev": False,
            "name": "@angular/animations",
            "type": "npm",
            "version": "8.2.14"
        },
        {
            "dev": False,
            "name": "tslib",
            "type": "npm",
            "version": "1.11.1"
        },
    ]
    mock_rn.return_value = {
        "deps": deps,
        "downloaded_deps": {"@angular/[email protected]", "[email protected]"},
        "lock_file": lock_file,
        "lock_file_name": "package-lock.json",
        "package": package,
        "package.json": package_json,
    }
    username = f"cachito-npm-{request_id}"
    password = "******"
    mock_fnfjr.return_value = password
    mock_gcc.return_value = ca_file
    mock_gnc.return_value = "some npmrc"

    if package_subpath:
        package_configs = [{"path": package_subpath}]
    else:
        package_configs = None

    npm.fetch_npm_source(request_id, package_configs=package_configs)

    mock_vnf.assert_called_once_with(mock_rbd.return_value,
                                     [package_subpath or "."])
    assert mock_srs.call_count == 3
    mock_pnfjr.assert_called_once_with("cachito-npm-6")
    lock_file_path = str(mock_rbd().app_subpath(package_subpath
                                                or ".").source_dir)
    mock_rn.assert_called_once_with(lock_file_path, request, skip_deps=set())
    if ca_file:
        mock_gnc.assert_called_once_with(
            "http://nexus:8081/repository/cachito-npm-6/",
            username,
            password,
            custom_ca_path=f"{reverse_path_component}registry-ca.pem",
        )
    else:
        mock_gnc.assert_called_once_with(
            "http://nexus:8081/repository/cachito-npm-6/",
            username,
            password,
            custom_ca_path=None)

    expected_config_files = []
    if package_json:
        expected_config_files.append({
            "content": "ewogICJuYW1lIjogImhhbi1zb2xvIgp9",
            "path": f"app/{subpath_as_path_component}package.json",
            "type": "base64",
        })

    if lock_file:
        expected_config_files.append({
            "content": "ewogICJkZXBlbmRlbmNpZXMiOiBbXQp9",
            "path": f"app/{subpath_as_path_component}package-lock.json",
            "type": "base64",
        })

    if ca_file:
        expected_config_files.append({
            "content": "c29tZSBDQSBmaWxlIGNvbnRlbnRz",
            "path": "app/registry-ca.pem",
            "type": "base64",
        })

    expected_config_files.append({
        "content": "c29tZSBucG1yYw==",
        "path": f"app/{subpath_as_path_component}.npmrc",
        "type": "base64",
    })
    mock_urwcf.assert_called_once_with(request_id, expected_config_files)
    mock_urwp.assert_called_once_with(
        request_id,
        package,
        {
            "CHROMEDRIVER_SKIP_DOWNLOAD": {
                "value": "true",
                "kind": "literal"
            },
            "SKIP_SASS_BINARY_DOWNLOAD_FOR_CI": {
                "value": "true",
                "kind": "literal"
            },
        },
        package_subpath=package_subpath or ".",
    )
    mock_urwd.assert_called_once_with(request_id, package, deps)