Skip to content

Commit

Permalink
fix(strict-deps): fail if venv is inconsistent
Browse files Browse the repository at this point in the history
This runs `pip check` to ensure that all the packages in the charm
virtualenv have all of their required dependencies, failing the build
if they do not.

Fixes #1781
  • Loading branch information
lengau committed Aug 2, 2024
1 parent ff36d80 commit ba92c13
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
18 changes: 10 additions & 8 deletions charmcraft/charm_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020-2023 Canonical Ltd.
# Copyright 2020-2024 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,18 +61,18 @@ def __init__(
installdir: pathlib.Path,
entrypoint: pathlib.Path,
allow_pip_binary: bool = None,
binary_python_packages: list[str] = None,
python_packages: list[str] = None,
requirements: list[pathlib.Path] = None,
binary_python_packages: list[str] | None = None,
python_packages: list[str] | None = None,
requirements: list[pathlib.Path] | None = None,
strict_dependencies: bool = False,
) -> None:
self.builddir = builddir
self.installdir = installdir
self.entrypoint = entrypoint
self.allow_pip_binary = allow_pip_binary
self.binary_python_packages = binary_python_packages
self.python_packages = python_packages
self.requirement_paths = requirements
self.binary_python_packages = binary_python_packages or []
self.python_packages = python_packages or []
self.requirement_paths = requirements or []
self.strict_dependencies = strict_dependencies
self.ignore_rules = self._load_juju_ignore()
self.ignore_rules.extend_patterns([f"/{const.STAGING_VENV_DIRNAME}"])
Expand Down Expand Up @@ -227,7 +227,7 @@ def _calculate_dependencies_hash(self):
return hashlib.sha1(deps_mashup.encode("utf8")).hexdigest()

@instrum.Timer("Installing dependencies")
def _install_dependencies(self, staging_venv_dir):
def _install_dependencies(self, staging_venv_dir: pathlib.Path):
"""Install all dependencies in a specific directory."""
# create virtualenv using the host environment python
with instrum.Timer("Creating venv"):
Expand Down Expand Up @@ -309,6 +309,8 @@ def _install_strict_dependencies(self, pip_cmd: str) -> None:
binary_deps=self.binary_python_packages or [],
)
)
# Validate that the environment is consistent.
_process_run([pip_cmd, "check"])

def handle_dependencies(self):
"""Handle from-directory and virtualenv dependencies."""
Expand Down
84 changes: 84 additions & 0 deletions tests/integration/test_charm_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2024 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# For further info, check https://github.com/canonical/charmcraft
"""Integration tests for CharmBuilder."""


import pathlib

import pytest
from charmcraft import charm_builder


@pytest.mark.parametrize(
"requirements",
[
["ops==2.15.0"], # Requires pyyaml and websocket-client
]
)
def test_install_strict_dependencies_pip_check_error(
new_path: pathlib.Path, requirements: list[str]
):
build_dir = new_path / "build"
install_dir = new_path / "install"
entrypoint = build_dir / "entrypoint.py"

build_dir.mkdir()
install_dir.mkdir()

requirements_file = build_dir / "requirements.txt"
requirements_file.write_text("\n".join(requirements))

builder = charm_builder.CharmBuilder(
builddir=build_dir,
installdir=install_dir,
entrypoint=entrypoint,
requirements=[requirements_file],
strict_dependencies=True
)

with pytest.raises(RuntimeError, match="failed with retcode 1") as exc_info:
builder.handle_dependencies()


@pytest.mark.parametrize(
"requirements",
[
["craft-platforms==0.1.0"], # No dependencies
]
)
def test_install_strict_dependencies_pip_check_success(
new_path: pathlib.Path, requirements: list[str]
):
build_dir = new_path / "build"
install_dir = new_path / "install"
entrypoint = build_dir / "entrypoint.py"

build_dir.mkdir()
install_dir.mkdir()

requirements_file = build_dir / "requirements.txt"
requirements_file.write_text("\n".join(requirements))

builder = charm_builder.CharmBuilder(
builddir=build_dir,
installdir=install_dir,
entrypoint=entrypoint,
requirements=[requirements_file],
strict_dependencies=True
)

with pytest.raises(RuntimeError, match="failed with retcode 1") as exc_info:
builder.handle_dependencies()
8 changes: 6 additions & 2 deletions tests/unit/test_charm_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Canonical Ltd.
# Copyright 2023-2024 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -105,6 +105,10 @@ def test_install_strict_dependencies_success(
"--no-binary=:all:",
"--requirement=requirements.txt",
]
fake_process.register(expected_command, returncode=0)
install_cmd = fake_process.register(expected_command, returncode=0)
check_cmd = fake_process.register(["/pip", "check"], returncode=0)

builder._install_strict_dependencies("/pip")

assert install_cmd.call_count() == 1
assert check_cmd.call_count() == 1

0 comments on commit ba92c13

Please sign in to comment.