Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketmaurya committed Sep 26, 2024
1 parent 5a94cf5 commit 0ca2bde
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/litserve/docker_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
import logging
import os
from pathlib import Path

import warnings
import litserve as ls

Expand Down Expand Up @@ -58,7 +60,7 @@ def color(text, color_code, action_code=None):
# Install litserve and requirements
RUN pip install --no-cache-dir litserve=={version} {requirements}
EXPOSE {port}
CMD ["python", "/app/{server_path}"]
CMD ["python", "/app/{server_filename}"]
"""

SUCCESS_MSG = """
Expand All @@ -71,11 +73,11 @@ def color(text, color_code, action_code=None):
"""


def build(server_path: str, port: int = 8000):
def build(server_filename: str, port: int = 8000):
"""Build a Docker image from the given server code.
Args:
server_path (str): The path to the server file.
server_filename (str): The path to the server file. Example sever.py or app.py.
port (int, optional): The port to expose in the Docker container. Defaults to 8000.
"""
Expand All @@ -89,9 +91,13 @@ def build(server_path: str, port: int = 8000):
UserWarning,
)

current_dir = Path.cwd()
if not (current_dir / server_filename).is_file():
raise FileNotFoundError(f"Server file `{server_filename}` must be in the current directory: {os.getcwd()}")

version = ls.__version__
dockerfile_content = DOCKERFILE_TEMPLATE.format(
server_path=server_path, port=port, version=version, requirements=requirements
server_filename=server_filename, port=port, version=version, requirements=requirements
)
with open("Dockerfile", "w") as f:
f.write(dockerfile_content)
Expand Down
34 changes: 24 additions & 10 deletions tests/test_docker_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# 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.
import litserve as ls
from litserve import docker_builder


Expand All @@ -21,19 +22,32 @@ def test_color():
assert docker_builder.color("hi", docker_builder.RED, docker_builder.INFO) == expected


def test_build(tmp_path):
EXPECTED_CONENT = f"""FROM python:3.10-slim
####### Put any installation commands here #######
# RUN apt-get update && apt-get install -y <package-name>
WORKDIR /app
COPY . /app
# Install litserve and requirements
RUN pip install --no-cache-dir litserve=={ls.__version__} -r requirements.txt
EXPOSE 8000
CMD ["python", "/app/app.py"]
"""


def test_build(tmp_path, monkeypatch):
with open(tmp_path / "app.py", "w") as f:
f.write("print('hello')")
with open(tmp_path / "requirements.txt", "w") as f:
f.write("litserve")
f.write("lightning")

# Temporarily change the current working directory to tmp_path
monkeypatch.chdir(tmp_path)

docker_builder.build("app.py", 8000)

docker_builder.build(tmp_path / "app.py", 8000)
with open("Dockerfile") as f:
content = f.read()
assert (
"""FROM python:3.10-slim
WORKDIR /app
COPY . /app"""
in content
)
assert "EXPOSE 8000" in content
assert content == EXPECTED_CONENT

0 comments on commit 0ca2bde

Please sign in to comment.