diff --git a/mlrun/runtimes/base.py b/mlrun/runtimes/base.py index 9b327a5ca49..e8b1e3a127f 100644 --- a/mlrun/runtimes/base.py +++ b/mlrun/runtimes/base.py @@ -792,7 +792,7 @@ def with_requirements(self, requirements: Union[str, List[str]]): """ if isinstance(requirements, str): with open(requirements, "r") as fp: - requirements = fp.readlines() + requirements = fp.read().splitlines() commands = self.spec.build.commands or [] commands.append("python -m pip install " + " ".join(requirements)) self.spec.build.commands = commands diff --git a/tests/api/runtimes/assets/requirements.txt b/tests/api/runtimes/assets/requirements.txt new file mode 100644 index 00000000000..cfb01f5a40f --- /dev/null +++ b/tests/api/runtimes/assets/requirements.txt @@ -0,0 +1,2 @@ +faker +python-dotenv \ No newline at end of file diff --git a/tests/api/runtimes/base.py b/tests/api/runtimes/base.py index ab3adf5c9f6..f085c6f354f 100644 --- a/tests/api/runtimes/base.py +++ b/tests/api/runtimes/base.py @@ -34,6 +34,7 @@ def setup_method(self, method): self.artifact_path = "/tmp" self.function_name_label = "mlrun/name" self.code_filename = str(self.assets_path / "sample_function.py") + self.requirements_file = str(self.assets_path / "requirements.txt") self.vault_secrets = ["secret1", "secret2", "AWS_KEY"] self.vault_secret_value = "secret123!@" diff --git a/tests/api/runtimes/test_kubejob.py b/tests/api/runtimes/test_kubejob.py index 772b2e99011..f5e26fb0192 100644 --- a/tests/api/runtimes/test_kubejob.py +++ b/tests/api/runtimes/test_kubejob.py @@ -1,5 +1,6 @@ import os +import deepdiff import pytest from fastapi.testclient import TestClient from sqlalchemy.orm import Session @@ -241,3 +242,14 @@ def test_set_label(self, db: Session, client: TestClient): runtime = self._generate_runtime() self._execute_run(runtime, runspec=task) self._assert_pod_creation_config(expected_labels=labels) + + def test_with_requirements(self, db: Session, client: TestClient): + runtime = self._generate_runtime() + runtime.with_requirements(self.requirements_file) + expected_commands = ["python -m pip install faker python-dotenv"] + assert ( + deepdiff.DeepDiff( + expected_commands, runtime.spec.build.commands, ignore_order=True, + ) + == {} + )