Skip to content

Commit

Permalink
Streamline and update to latest testing practices
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckierDodge committed Mar 22, 2024
1 parent eb7c7ca commit 72ea78e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 61 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ jobs:
steps:
- uses: actions/checkout@v3
name: Checkout code
- name: Test makefile
run: make init .env
- name: Check .env file
run: cat .env
- name: Create paths
run: make paths
- name: Initialize, Build, and Test with Docker
run: make test
shell: bash
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ COPY ./tests camera_module/tests
RUN --mount=type=cache,target=/root/.cache \
pip install -e ./camera_module

CMD ["python", "camera_module/src/camera_rest_node.py"]
CMD ["python", "-m", "camera_rest_node"]

# Add user to video group to access camera
RUN usermod -a -G video app
Expand Down
24 changes: 9 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
# Python Configuration
PYPROJECT_TOML := pyproject.toml
PROJECT_VERSION := $(shell grep -oP '(?<=version = ")[^"]+' $(PYPROJECT_TOML) | head -n 1)

.DEFAULT_GOAL := init

.PHONY += init paths checks test clean

init: # Do the initial configuration of the project
@test -e .env || cp example.env .env
@sed -i 's/^USER_ID=.*/USER_ID=$(shell id -u)/' .env
@sed -i 's/^GROUP_ID=.*/GROUP_ID=$(shell id -g)/' .env
@sed -i 's/^PROJECT_VERSION=.*/PROJECT_VERSION=$(PROJECT_VERSION)/' .env
ifeq ($(shell uname),Darwin)
@sed -i '' 's|^PROJECT_PATH=.*|PROJECT_PATH=$(shell pwd | sed 's/\//\\\//g')|' .env
else
@sed -i 's/^PROJECT_PATH=.*/PROJECT_PATH=$(shell pwd | sed 's/\//\\\//g')/' .env
endif

.env: init

paths: .env # Create the necessary data directories
@mkdir -p $(shell grep -E '^WEI_DATA_DIR=' .env | cut -d '=' -f 2)
@mkdir -p $(shell grep -E '^REDIS_DIR=' .env | cut -d '=' -f 2)

checks: # Runs all the pre-commit checks
@pre-commit install
@pre-commit run --all-files || { echo "Checking fixes\n" ; pre-commit run --all-files; }

test: init .env paths # Runs all the tests
@docker compose -f wei.compose.yaml --env-file .env up --build -d
@docker compose -f wei.compose.yaml --env-file .env exec camera_module pytest -p no:cacheprovider camera_module
@docker compose -f wei.compose.yaml --env-file .env down
test: init .env # Runs all the tests
@docker compose -f tests/wei.compose.yaml --env-file .env up --build -d
@docker compose -f tests/wei.compose.yaml --env-file .env exec camera_module pytest -p no:cacheprovider camera_module
@docker compose -f tests/wei.compose.yaml --env-file .env down

clean:
@rm .env
7 changes: 3 additions & 4 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ services:
dockerfile: Dockerfile
tags:
- ${IMAGE}:latest
- ${IMAGE}:${PROJECT_VERSION}
- ${IMAGE}:dev
command: python camera_module/src/camera_rest_node.py --port 2000
command: python -m camera_rest_node --port 2000
privileged: true
env_file: .env
volumes:
- ${DEVICE}:/dev/video0
- ./src:/home/app/camera_module/src
- ./tests:/home/app/camera_module/tests
# - ./src:/home/app/camera_module/src
# - ./tests:/home/app/camera_module/tests
ports:
- 2000:2000
7 changes: 1 addition & 6 deletions example.env
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# Note: all paths are relative to the docker compose file
USER_ID=
GROUP_ID=
DEVICE=/dev/video0
# The PROJECT_PATH should be the absolute path to the root of the repo
PROJECT_PATH=
PROJECT_VERSION=1.2.0
WEI_DATA_DIR=~/.wei
WORKCELL_FILENAME=test_workcell.yaml
WORKCELLS_DIR=${PROJECT_PATH}/tests/workcell_defs
IMAGE=ghcr.io/ad-sdl/camera_module
REDIS_DIR=~/.wei/redis
23 changes: 23 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Base module tests."""

import unittest


class TestModule_Base(unittest.TestCase):
"""Base test class for this module."""

pass


class TestImports(TestModule_Base):
"""Test the imports of the module are working correctly"""

def test_driver_import(self):
"""Test the driver and rest node imports"""
import camera_rest_node

assert camera_rest_node


if __name__ == "__main__":
unittest.main()
33 changes: 19 additions & 14 deletions tests/test_module.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Tests the basic functionality of the Camera Module."""
"""Tests the basic functionality of the Module."""

import time
import unittest
from pathlib import Path

import requests
from wei import ExperimentClient
from wei.core.data_classes import ModuleAbout, WorkcellData, WorkflowStatus
from wei.core.data_classes import ModuleAbout, Workcell, WorkflowStatus


class TestWEI_Base(unittest.TestCase):
Expand All @@ -19,9 +19,14 @@ def __init__(self, *args, **kwargs):
self.workcell_file = self.root_dir / Path(
"tests/workcell_defs/test_workcell.yaml"
)
self.workcell = WorkcellData.from_yaml(self.workcell_file)
self.workcell = Workcell.from_yaml(self.workcell_file)
self.server_host = self.workcell.config.server_host
self.server_port = self.workcell.config.server_port
self.experiment = ExperimentClient(
self.server_host,
self.server_port,
"TestExperiment",
)
self.url = f"http://{self.server_host}:{self.server_port}"
self.module_url = "http://camera_module:2000"
self.redis_host = self.workcell.config.redis_host
Expand All @@ -30,7 +35,7 @@ def __init__(self, *args, **kwargs):
start_time = time.time()
while True:
try:
if requests.get(self.url + "/wc/state").status_code == 200:
if requests.get(self.url + "/wc/state").ok:
break
except Exception:
pass
Expand All @@ -39,7 +44,7 @@ def __init__(self, *args, **kwargs):
raise TimeoutError("Server did not start in 60 seconds")
while True:
try:
if requests.get(self.module_url + "/state").status_code == 200:
if requests.get(self.module_url + "/state").ok:
break
except Exception:
pass
Expand All @@ -48,33 +53,33 @@ def __init__(self, *args, **kwargs):
raise TimeoutError("Module did not start in 60 seconds")


class TestCameraModule(TestWEI_Base):
"""Tests the basic functionality of the Sleep Module."""
class TestModuleInterfaces(TestWEI_Base):
"""Tests the basic functionality of the Module."""

def test_take_picture_action(self):
"""Tests that the take_picture action works"""
exp = ExperimentClient(self.server_host, self.server_port, "camera_module_test")

result = exp.start_run(
result = self.experiment.start_run(
Path(self.root_dir) / Path("tests/workflow_defs/test_workflow.yaml"),
simulate=False,
blocking=True,
)
assert result["status"] == WorkflowStatus.COMPLETED
output_path = Path("~/.wei/temp/test_image.jpg").expanduser()
output_path.parent.mkdir(parents=True, exist_ok=True)
exp.get_file(
input_filepath=result["hist"]["Take Picture"]["action_msg"],
self.experiment.get_wf_result_file(
run_id=result["run_id"],
filename=result["hist"]["Take Picture"]["action_msg"],
output_filepath=output_path,
)
assert Path("~/.wei/temp/test_image.jpg").expanduser().exists()

def test_camera_about(self):
"""Tests that the camera module's /about works"""
def test_module_about(self):
"""Tests that the module's /about endpoint works"""
response = requests.get(self.module_url + "/about")
assert response.status_code == 200
ModuleAbout(**response.json())


if __name__ == "__main__":
unittest.main()

18 changes: 3 additions & 15 deletions wei.compose.yaml → tests/wei.compose.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include: [compose.yaml]
include: [../compose.yaml]
#####################
# WEI Core Services #
#####################
Expand All @@ -8,13 +8,11 @@ services:
container_name: wei_server
ports:
- 8000:8000
env_file: .env
env_file: ../.env
environment:
- PYTHONUNBUFFERED=1 # Fix weird bug with empty logging
volumes:
- ${WORKCELLS_DIR}:/workcell_defs
- ${WEI_DATA_DIR}:/home/app/.wei
- diaspora_config:/home/app/.diaspora
command: python3 -m wei.server --workcell /workcell_defs/${WORKCELL_FILENAME}
depends_on:
- wei_redis
Expand All @@ -23,8 +21,7 @@ services:
container_name: wei_engine
volumes:
- ${WORKCELLS_DIR}:/workcell_defs
- ${WEI_DATA_DIR}:/home/app/.wei
env_file: .env
env_file: ../.env
environment:
- PYTHONUNBUFFERED=1 # Fix weird bug with empty logging
command: python3 -m wei.engine --workcell /workcell_defs/${WORKCELL_FILENAME}
Expand All @@ -36,13 +33,4 @@ services:
container_name: wei_redis
ports:
- 6379:6379
volumes:
- ${REDIS_DIR}:/data
command: redis-server --save 60 1 --loglevel warning

################
# Data Storage #
################
volumes:
diaspora_config:
driver: local

0 comments on commit 72ea78e

Please sign in to comment.