Skip to content

Commit

Permalink
Fix add validation with tests and path check (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
spillai authored Oct 21, 2023
1 parent 6a38ac6 commit 84c4a20
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
29 changes: 21 additions & 8 deletions agipack/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from dataclasses import asdict, field
from pathlib import Path
from typing import Dict, List, Optional, Union
from typing import Dict, List, Optional, Tuple, Union

import yaml
from pydantic import validator
Expand Down Expand Up @@ -82,7 +82,7 @@ class ImageConfig:
"""List of Python requirements files to install (via `pip install -r`)."""

add: Optional[List[str]] = field(default_factory=list)
"""List of files to copy into the image."""
"""List of files to add into the image (`./entrypoint.sh:/app/entrytpoint.sh`)."""

workdir: Optional[str] = field(default=None)
"""Working directory for the image (defaults to /app/${AGIPACK_ENV} if not set)."""
Expand Down Expand Up @@ -114,15 +114,28 @@ def is_base_image(self) -> bool:
return ":" in self.base

@validator("python", pre=True)
def validate_python_version(cls, python):
def validate_python_version(cls, python) -> str:
"""Validate the python version."""
python = str(python)
if not python.startswith("3."):
raise ValueError(f"Python version must be >= 3.6 (found {python})")
return python

@validator("add", pre=True)
def validate_add(cls, items) -> Tuple[str, str]:
"""Validate the add command."""
for item in items:
if ":" not in item:
raise ValueError(
f"`add` must have a colon to separate from and to paths `from_path:to_path`, provided {item}"
)
from_path, to_path = item.split(":")
if not Path(from_path).exists():
raise ValueError(f"`add` {from_path} does not exist")
return items

@validator("command", pre=True)
def validate_command_version(cls, cmd):
def validate_command_version(cls, cmd) -> List[str]:
"""Validate the command."""
if isinstance(cmd, str):
cmd = cmd.split(" ")
Expand Down Expand Up @@ -178,14 +191,14 @@ def is_prod(self) -> bool:
@validator("images")
def validate_python_dependencies_for_nonbase_images(cls, images):
"""Validate that all images have the same python dependency as the base image."""
version = None
py_version = None
for target, config in images.items():
if config.is_base_image():
version = config.python
py_version = config.python
else:
if config.python != version:
if config.python != py_version:
logger.debug(f"Ignoring python version for non-base image [{target}]")
config.python = version
config.python = py_version
return images

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion agipack/templates/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ WORKDIR /app/$AGIPACK_PYENV

# Add project files
{%- for item in add %}
ADD {{ item }} {{ item }}
ADD {{ item.split(":")[0] }} {{ item.split(":")[1] }}
{%- endfor %}

{%- endif %}
Expand Down
2 changes: 1 addition & 1 deletion agipack/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.12"
__version__ = "0.1.13"
1 change: 1 addition & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_configs(test_data_dir):
def test_poorly_formatted_configs(test_data_dir):
poorly_formatted_configs = [
test_data_dir / "agibuild-no-base.yaml",
test_data_dir / "agibuild-malformed-add.yaml",
]
for filename in poorly_formatted_configs:
with pytest.raises(ValueError):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_data/agibuild-malformed-add.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
images:
base-cpu:
system:
- wget
python: 3.8.10
pip:
- scikit-learn
add:
- "path_without_colon.txt"
run:
- echo "Hello, world!"

0 comments on commit 84c4a20

Please sign in to comment.