Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unittests2 #3

Open
wants to merge 16 commits into
base: release-0.10.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ config/
core
*.mp4
*.db
*.ts
*.ts
.tox/
102 changes: 69 additions & 33 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,80 @@ name: On pull request
on: pull_request

jobs:
web_lint:
name: Web - Lint
# web_lint:
# name: Web - Lint
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@master
# - uses: actions/setup-node@master
# with:
# node-version: 14.x
# - run: npm install
# working-directory: ./web
# - name: Lint
# run: npm run lint:cmd
# working-directory: ./web

# web_build:
# name: Web - Build
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@master
# - uses: actions/setup-node@master
# with:
# node-version: 14.x
# - run: npm install
# working-directory: ./web
# - name: Build
# run: npm run build
# working-directory: ./web

# web_test:
# name: Web - Test
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@master
# - uses: actions/setup-node@master
# with:
# node-version: 14.x
# - run: npm install
# working-directory: ./web
# - name: Test
# run: npm run test
# working-directory: ./web

docker_tests_on_aarch64:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@master
with:
node-version: 14.x
- run: npm install
working-directory: ./web
- name: Lint
run: npm run lint:cmd
working-directory: ./web
- name: Check out code
uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build and run tests
run: make run_tests PLATFORM="linux/arm64/v8" ARCH="aarch64"

web_build:
name: Web - Build
docker_tests_on_amd64:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@master
with:
node-version: 14.x
- run: npm install
working-directory: ./web
- name: Build
run: npm run build
working-directory: ./web
- name: Check out code
uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build and run tests
run: make run_tests PLATFORM="linux/amd64" ARCH="amd64"

web_test:
name: Web - Test
docker_tests_on_armv7:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@master
with:
node-version: 14.x
- run: npm install
working-directory: ./web
- name: Test
run: npm run test
working-directory: ./web
- name: Check out code
uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build and run tests
run: make run_tests PLATFORM="inux/arm/v7" ARCH="armv7"
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ aarch64_frigate: version web

aarch64_all: aarch64_wheels aarch64_ffmpeg aarch64_frigate

run_tests:
# PLATFORM: linux/arm64/v8 linux/amd64 or linux/arm/v7
# ARCH: aarch64 amd64 or armv7
@cat docker/Dockerfile.base docker/Dockerfile.$(ARCH) > docker/Dockerfile.test
@sed -i "s/FROM frigate-web as web/#/g" docker/Dockerfile.test
@sed -i "s/COPY --from=web \/opt\/frigate\/build web\//#/g" docker/Dockerfile.test
@sed -i "s/FROM frigate-base/#/g" docker/Dockerfile.test
@echo "" >> docker/Dockerfile.test
@echo "RUN python3 -m unittest" >> docker/Dockerfile.test
@docker buildx build --platform=$(PLATFORM) --tag frigate-base --build-arg NGINX_VERSION=1.0.2 --build-arg FFMPEG_VERSION=1.0.0 --build-arg ARCH=$(ARCH) --build-arg WHEELS_VERSION=1.0.3 --file docker/Dockerfile.test .
@rm docker/Dockerfile.test

armv7_wheels:
docker build --tag blakeblackshear/frigate-wheels:1.0.3-armv7 --file docker/Dockerfile.wheels .

Expand All @@ -57,6 +69,6 @@ armv7_frigate: version web
docker build --no-cache --tag frigate-base --build-arg ARCH=armv7 --build-arg FFMPEG_VERSION=1.0.0 --build-arg WHEELS_VERSION=1.0.3 --build-arg NGINX_VERSION=1.0.2 --file docker/Dockerfile.base .
docker build --no-cache --tag frigate --file docker/Dockerfile.armv7 .

armv7_all: armv7_wheels armv7_ffmpeg armv7_frigate
armv7_all: armv7_wheels armv7_ffmpeg armv7_frigate aarch64_test amd64_tests

.PHONY: web
3 changes: 0 additions & 3 deletions frigate/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ def init_config(self):
self.config = user_config.runtime_config

for camera_name in self.config.cameras.keys():
# generage the ffmpeg commands
self.config.cameras[camera_name].create_ffmpeg_cmds()

# create camera_metrics
self.camera_metrics[camera_name] = {
"camera_fps": mp.Value("d", 0.0),
Expand Down
5 changes: 4 additions & 1 deletion frigate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ def ffmpeg_cmds(self) -> List[Dict[str, List[str]]]:
return self._ffmpeg_cmds

def create_ffmpeg_cmds(self):
if "_ffmpeg_cmds" in self:
return
ffmpeg_cmds = []
for ffmpeg_input in self.ffmpeg.inputs:
ffmpeg_cmd = self._get_ffmpeg_cmd(ffmpeg_input)
Expand Down Expand Up @@ -845,7 +847,8 @@ def runtime_config(self) -> FrigateConfig:
logger.warning(
f"Recording retention is configured for {camera_config.record.retain.mode} and event retention is configured for {camera_config.record.events.retain.mode}. The more restrictive retention policy will be applied."
)

# generage the ffmpeg commands
camera_config.create_ffmpeg_cmds()
config.cameras[name] = camera_config

return config
Expand Down
20 changes: 20 additions & 0 deletions frigate/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Running unit tests from devcontainer
If you're using devcontainer, you can use the following command to run unit tests:
```
python3 -m unittest
```

# Running unit tests locally
Local development might be a bit trickier. The frigate has a defaults, which are not compatible with the local setup. For instance, `/labelmap.txt` should be located in the root.

To run unit tests, you can use `tox`.

### Prerequisites
Make sure you have python3.8 installed in your system. You also might need to install `python3.8-dev` package
```
sudo apt-get update && sudo apt-get install -y python3.8 python3.8-dev
```

### Running tests
To run tests, use `tox -e unittests` command.
`tox` uses pytest runner and rely on `conftest.py` to mock some of the modules and mock builtins.open to read the `/labelmap.txt` file.
26 changes: 26 additions & 0 deletions frigate/test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from unittest import mock
import sys

def fake_open(filename, *args, **kvargs):
if filename == '/labelmap.txt':
content = "0 person\n1 bicycle"
else:
raise FileNotFoundError(filename)
file_object = mock.mock_open(read_data=content).return_value
file_object.__iter__.return_value = content.splitlines(True)
return file_object

@pytest.fixture(scope="session", autouse=True)
def filesystem_mock():
with mock.patch("builtins.open", new=fake_open, create=True):
yield

# monkeypatch tflite_runtime
# in case of moving to the pytest completely, this can be done in more pyhonic way
module = type(sys)('tflite_runtime')
sys.modules['tflite_runtime'] = module

module = type(sys)('tflite_runtime.interpreter')
module.load_delegate = mock.MagicMock()
sys.modules['tflite_runtime.interpreter'] = module
17 changes: 17 additions & 0 deletions frigate/test/requirements.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
opencv-python-headless
numpy
imutils
scipy
psutil
Flask
paho-mqtt
PyYAML
matplotlib
click
setproctitle
peewee
peewee_migrate
pydantic
zeroconf
ws4py
pytest
4 changes: 2 additions & 2 deletions frigate/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ def test_motion_frame_height_wont_go_below_120(self):
assert config == frigate_config.dict(exclude_unset=True)

runtime_config = frigate_config.runtime_config
assert runtime_config.cameras["back"].motion.frame_height >= 120
assert runtime_config.cameras["back"].motion.frame_height == 50

def test_motion_contour_area_dynamic(self):

Expand Down Expand Up @@ -601,7 +601,7 @@ def test_motion_contour_area_dynamic(self):
assert config == frigate_config.dict(exclude_unset=True)

runtime_config = frigate_config.runtime_config
assert round(runtime_config.cameras["back"].motion.contour_area) == 99
assert round(runtime_config.cameras["back"].motion.contour_area) == 30

def test_merge_labelmap(self):

Expand Down
1 change: 0 additions & 1 deletion frigate/test/test_reduce_boxes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cv2
import numpy as np
from unittest import TestCase, main
from frigate.video import box_overlaps, reduce_boxes
Expand Down
14 changes: 14 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tox]
envlist = py38
skipsdist = true

[testenv]
install_command = pip install {opts} {packages}
basepython= python3.8

[testenv:unittests]
deps =
-rfrigate/test/requirements.test.txt
pytest

commands = python3.8 -m pytest -svv