Skip to content

Commit

Permalink
feat(core): add qrcode_input utility function to let developers eas…
Browse files Browse the repository at this point in the history
…ily take input using qrcode through camera, wireless flow now also uses this function, in dev environment it reads the text from `/tmp/qrcode_input.txt` instead

feat(docker): add `environment_variables` and `command` to image description, both allowing functions as their values, these functions get evaluated when the image is being created
refactor(core): improve `load_services` so that `ubo_handle.py` files are enforced to be pure and can't import anything, services can start importing once their thread is started.
fix(image): add `apt remove orca` to image creation scripts #48
refactor(test): stability fixture now stops after 4 seconds
  • Loading branch information
sassanh committed Mar 30, 2024
1 parent 9d2de98 commit 9175e7a
Show file tree
Hide file tree
Showing 64 changed files with 1,368 additions and 750 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/integration_delivery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v4
name: Checkout

- name: Save Cached Poetry
- name: Load Cached Poetry
id: cached-poetry
uses: actions/cache@v4
with:
Expand Down Expand Up @@ -125,7 +125,7 @@ jobs:

- name: Run Tests
run: |
poetry run poe test --make-screenshots --cov-report=xml --cov-report=html -n auto
poetry run poe test --make-screenshots --cov-report=xml --cov-report=html -n auto --log-level=DEBUG
- name: Collect Window Screenshots
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -186,7 +186,9 @@ jobs:

- name: Add SENTRY_DSN to .env
run: |
echo "SENTRY_DSN=${{ secrets.SENTRY_DSN }}" >> .env
echo "SENTRY_DSN=${{ secrets.SENTRY_DSN }}" >> ubo_app/.env
pwd
cat ubo_app/.env
- name: Build
run: poetry build
Expand Down Expand Up @@ -306,9 +308,6 @@ jobs:
name: binary
path: /build/dist

- run: |
ls -l /build/dist
- name: Generate Image URL and Checksum
id: generate_image_url
run: |
Expand Down
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## Version 0.12.0

- feat(core): add `qrcode_input` utility function to let developers easily take input
using qrcode through camera, wireless flow now also uses this function, in dev
environment it reads the text from `/tmp/qrcode_input.txt` instead
- feat(docker): add `environment_variables` and `command` to image description,
both allowing functions as their values, these functions get evaluated when the
image is being created
- refactor(core): improve `load_services` so that `ubo_handle.py` files are enforced
to be pure and can't import anything, services can start importing once their
thread is started.
- fix(image): add `apt remove orca` to image creation scripts #48
- fix(image):
- refactor(test): stability fixture now stops after 4 seconds
- feat(test): introduce `UBO_DEBUG_TEST_UUID` environment variable for tracking
the sequence of uuid generations in the tests, it prints the traceback for each
call to `uuid.uuid4` if it is set

## Version 0.11.7

- refactor(style): update `ubo-gui` to the latest version and set placeholder
Expand Down
34 changes: 17 additions & 17 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ubo-app"
version = "0.11.7"
version = "0.12.0"
description = "Ubo main app, running on device initialization. A platform for running other apps."
authors = ["Sassan Haradji <[email protected]>"]
license = "Apache-2.0"
Expand All @@ -18,14 +18,14 @@ priority = "primary"
python = "^3.11"
psutil = "^5.9.8"
ubo-gui = [
{ version = "^0.10.3", markers = "extra=='default'", extras = [
{ version = "^0.10.4", markers = "extra=='default'", extras = [
'default',
] },
{ version = "^0.10.3", markers = "extra=='dev'", extras = [
{ version = "^0.10.4", markers = "extra=='dev'", extras = [
'dev',
] },
]
python-redux = "^0.13.2"
python-redux = "^0.14.0"
pyzbar = "^0.1.9"
sdbus-networkmanager = { version = "^2.0.0", markers = "platform_machine=='aarch64'" }
rpi_ws281x = { version = "^5.0.0", markers = "platform_machine=='aarch64'" }
Expand All @@ -45,13 +45,13 @@ optional = true

[tool.poetry.group.dev.dependencies]
poethepoet = "^0.24.4"
pyright = "^1.1.354"
pyright = "^1.1.356"
pytest = "^8.0.0"
pytest-asyncio = "^0.23.5.post1"
pytest-cov = "^4.1.0"
pytest-timeout = "^2.3.1"
pytest-xdist = "^3.5.0"
ruff = "^0.3.3"
ruff = "^0.3.4"
tenacity = "^8.2.3"
toml = "^0.10.2"
pytest-mock = "^3.14.0"
Expand Down Expand Up @@ -98,6 +98,7 @@ multiline-quotes = "double"

[tool.ruff.lint.per-file-ignores]
"tests/*" = ["S101", "PLR0913"]
"ubo_app/services/*/ubo_handle.py" = ["TCH004"]

[tool.ruff.format]
quote-style = 'single'
Expand Down
3 changes: 3 additions & 0 deletions scripts/packer/image.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ build {

provisioner "shell" {
inline = [
"ls -ld /boot",
"ls -l /boot",
"ls -l /boot/firmware/",
"chmod +x /install.sh",
"/install.sh --for-packer --with-docker --source=/ubo_app-${var.ubo_app_version}-py3-none-any.whl",
"rm /install.sh /ubo_app-${var.ubo_app_version}-py3-none-any.whl",
Expand Down
1 change: 1 addition & 0 deletions ubo_app/.test.env → tests/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
UBO_DEBUG=False
UBO_DEBUG_DOCKER=False
DOCKER_HOST=/var/run/docker.sock
UBO_DEBUG_TEST_UUID=False
93 changes: 77 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,19 @@
import atexit
import datetime
import random
import socket
import sys
import tracemalloc
import uuid
from pathlib import Path
from typing import cast

import dotenv
import pytest
from redux_pytest.fixtures import (
StoreMonitor,
Waiter,
WaitFor,
needs_finish,
store_monitor,
store_snapshot,
wait_for,
)

dotenv.load_dotenv(Path(__file__).parent / '.env')

pytest.register_assert_rewrite('tests.fixtures')

# isort: off
from tests.fixtures import ( # noqa: E402
AppContext,
LoadServices,
Expand All @@ -37,7 +30,17 @@
window_snapshot,
)

dotenv.load_dotenv(Path(__file__).parent / '.test.env')
from redux_pytest.fixtures import ( # noqa: E402
StoreMonitor,
Waiter,
WaitFor,
needs_finish,
store_monitor,
store_snapshot,
wait_for,
)
# isort: on


fixtures = (
AppContext,
Expand Down Expand Up @@ -65,6 +68,20 @@ def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption('--make-screenshots', action='store_true')


@pytest.fixture(autouse=True)
def _logger() -> None:
import logging

from ubo_app.logging import ExtraFormatter

extra_formatter = ExtraFormatter()

for handler in logging.getLogger().handlers:
if handler.formatter:
handler.formatter.format = extra_formatter.format
cast(ExtraFormatter, handler.formatter).def_keys = extra_formatter.def_keys


@pytest.fixture(autouse=True)
def _monkeypatch(monkeypatch: pytest.MonkeyPatch) -> None:
"""Mock external resources."""
Expand All @@ -73,6 +90,8 @@ def _monkeypatch(monkeypatch: pytest.MonkeyPatch) -> None:

monkeypatch.setattr(atexit, 'register', lambda _: None)

import socket

import psutil

monkeypatch.setattr(psutil, 'cpu_percent', lambda **_: 50)
Expand Down Expand Up @@ -102,6 +121,16 @@ def _monkeypatch(monkeypatch: pytest.MonkeyPatch) -> None:
'create_connection',
lambda *args, **kwargs: Fake(args, kwargs),
)
original_socket_socket = socket.socket
from ubo_app.constants import SERVER_SOCKET_PATH

monkeypatch.setattr(
socket,
'socket',
lambda *args, **kwargs: Fake(args, kwargs)
if args[0] == SERVER_SOCKET_PATH
else original_socket_socket(*args, **kwargs),
)

class FakeDockerClient:
def ping(self: FakeDockerClient) -> bool:
Expand All @@ -116,12 +145,44 @@ def now(cls: type[DateTime], tz: datetime.tzinfo | None = None) -> DateTime:
return DateTime(2023, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc)

monkeypatch.setattr(datetime, 'datetime', DateTime)
monkeypatch.setattr(uuid, 'uuid4', lambda: uuid.UUID(int=random.getrandbits(128)))

monkeypatch.setattr('importlib.metadata.version', lambda _: '0.0.0')

from ubo_app.utils.fake import Fake

counter = 0

def debug_uuid4() -> Fake:
nonlocal counter
counter += 1
import logging
import traceback

logging.debug(
'`uuid.uuid4` is being called',
extra={
'traceback': '\n'.join(traceback.format_stack()[:-1]),
'counter': counter,
},
)

result = Fake()
result.hex = f'{counter}'
return result

from ubo_app.constants import DEBUG_MODE_TEST_UUID

if DEBUG_MODE_TEST_UUID:
monkeypatch.setattr('uuid.uuid4', debug_uuid4)
else:
import uuid

monkeypatch.setattr(
uuid,
'uuid4',
lambda: uuid.UUID(int=random.getrandbits(128)),
)

monkeypatch.setattr('importlib.metadata.version', lambda _: '0.0.0')

class FakeUpdateResponse(Fake):
async def json(self: FakeUpdateResponse) -> dict[str, object]:
return {
Expand Down Expand Up @@ -152,4 +213,4 @@ class FakeSensorModule(Fake):
sys.modules['i2c'] = Fake()


_ = fixtures, _monkeypatch
_ = fixtures, _logger, _monkeypatch
Loading

0 comments on commit 9175e7a

Please sign in to comment.