fix timezone-dependent http tests

The devcontainer mounts the host's /etc/localtime over zoneinfo/Etc/UTC, so the API's ZoneInfo("UTC") used the host's local zone and the recordings summary and event time_range tests failed at any hour. The review summary tests also built their expected day key from the local date, so they failed whenever the local day differed from the UTC day. BaseTestHttp now writes a minimal UTC zone file to a temp dir and puts it first on the zoneinfo search path, which works without the tzdata package, and the review tests convert their expected day keys to UTC.
This commit is contained in:
Josh Hawkins
2026-11-18 16:05:36 -06:00
parent 7208d7e810
commit d071afe3ba
2 changed files with 41 additions and 9 deletions
+27
View File
@@ -1,7 +1,10 @@
import datetime import datetime
import logging import logging
import os import os
import struct
import tempfile
import unittest import unittest
import zoneinfo
from unittest.mock import patch from unittest.mock import patch
from fastapi import Request from fastapi import Request
@@ -20,6 +23,13 @@ from frigate.models import Event, Recordings, ReviewSegment
from frigate.review.types import SeverityEnum from frigate.review.types import SeverityEnum
from frigate.test.const import TEST_DB, TEST_DB_CLEANUPS, TEST_DB_PRAGMAS from frigate.test.const import TEST_DB, TEST_DB_CLEANUPS, TEST_DB_PRAGMAS
# Minimal TZif file for a zone fixed at UTC+0 named "UTC".
UTC_TZIF = (
struct.pack(">4s16x6l", b"TZif", 0, 0, 0, 0, 1, 4)
+ struct.pack(">lBB", 0, 0, 0)
+ b"UTC\0"
)
class AuthTestClient(TestClient): class AuthTestClient(TestClient):
"""TestClient that automatically adds auth headers to all requests.""" """TestClient that automatically adds auth headers to all requests."""
@@ -36,6 +46,23 @@ class AuthTestClient(TestClient):
class BaseTestHttp(unittest.TestCase): class BaseTestHttp(unittest.TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Docker mounts of the host's /etc/localtime can overwrite the system
# zoneinfo/Etc/UTC file with a local zone. Shadow "UTC" with a known
# good file so the API's ZoneInfo("UTC") is real UTC.
tz_dir = tempfile.TemporaryDirectory()
with open(os.path.join(tz_dir.name, "UTC"), "wb") as f:
f.write(UTC_TZIF)
zoneinfo.reset_tzpath(to=[tz_dir.name, *zoneinfo.TZPATH])
zoneinfo.ZoneInfo.clear_cache()
cls.addClassCleanup(tz_dir.cleanup)
cls.addClassCleanup(zoneinfo.ZoneInfo.clear_cache)
cls.addClassCleanup(zoneinfo.reset_tzpath)
def setUp(self, models): def setUp(self, models):
# setup clean database for each test run # setup clean database for each test run
migrate_db = SqliteDatabase(TEST_DB, pragmas=TEST_DB_PRAGMAS) migrate_db = SqliteDatabase(TEST_DB, pragmas=TEST_DB_PRAGMAS)
+14 -9
View File
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta from datetime import UTC, datetime, timedelta
from fastapi import Request from fastapi import Request
from peewee import DoesNotExist from peewee import DoesNotExist
@@ -9,6 +9,11 @@ from frigate.review.types import SeverityEnum
from frigate.test.http_api.base_http_test import AuthTestClient, BaseTestHttp from frigate.test.http_api.base_http_test import AuthTestClient, BaseTestHttp
def utc_day(dt: datetime) -> str:
"""Return the UTC calendar day the summary endpoint buckets dt into."""
return dt.astimezone(UTC).strftime("%Y-%m-%d")
class TestHttpReview(BaseTestHttp): class TestHttpReview(BaseTestHttp):
def setUp(self): def setUp(self):
super().setUp([Event, Recordings, ReviewSegment, UserReviewStatus]) super().setUp([Event, Recordings, ReviewSegment, UserReviewStatus])
@@ -362,7 +367,7 @@ class TestHttpReview(BaseTestHttp):
assert response_json["last24Hours"]["total_alert"] == 1 assert response_json["last24Hours"]["total_alert"] == 1
assert response_json["last24Hours"]["total_detection"] == 0 assert response_json["last24Hours"]["total_detection"] == 0
today_formatted = datetime.today().strftime("%Y-%m-%d") today_formatted = utc_day(datetime.now())
assert response_json[today_formatted]["total_alert"] == 1 assert response_json[today_formatted]["total_alert"] == 1
assert response_json[today_formatted]["total_detection"] == 0 assert response_json[today_formatted]["total_detection"] == 0
@@ -379,7 +384,7 @@ class TestHttpReview(BaseTestHttp):
assert response.status_code == 200 assert response.status_code == 200
response_json = response.json() response_json = response.json()
# e.g. '2024-11-24' # e.g. '2024-11-24'
today_formatted = datetime.today().strftime("%Y-%m-%d") today_formatted = utc_day(datetime.now())
expected_response = { expected_response = {
"last24Hours": { "last24Hours": {
"reviewed_alert": 0, "reviewed_alert": 0,
@@ -404,7 +409,7 @@ class TestHttpReview(BaseTestHttp):
assert response.status_code == 200 assert response.status_code == 200
response_json = response.json() response_json = response.json()
# e.g. '2024-11-24' # e.g. '2024-11-24'
today_formatted = datetime.today().strftime("%Y-%m-%d") today_formatted = utc_day(datetime.now())
expected_response = { expected_response = {
"last24Hours": { "last24Hours": {
"reviewed_alert": 0, "reviewed_alert": 0,
@@ -439,9 +444,9 @@ class TestHttpReview(BaseTestHttp):
assert response.status_code == 200 assert response.status_code == 200
response_json = response.json() response_json = response.json()
# e.g. '2024-11-24' # e.g. '2024-11-24'
today_formatted = now.strftime("%Y-%m-%d") today_formatted = utc_day(now)
# e.g. '2024-11-19' # e.g. '2024-11-19'
five_days_ago_formatted = five_days_ago.strftime("%Y-%m-%d") five_days_ago_formatted = utc_day(five_days_ago)
expected_response = { expected_response = {
"last24Hours": { "last24Hours": {
"reviewed_alert": 0, "reviewed_alert": 0,
@@ -491,9 +496,9 @@ class TestHttpReview(BaseTestHttp):
assert response.status_code == 200 assert response.status_code == 200
response_json = response.json() response_json = response.json()
# e.g. '2024-11-24' # e.g. '2024-11-24'
today_formatted = now.strftime("%Y-%m-%d") today_formatted = utc_day(now)
# e.g. '2024-11-19' # e.g. '2024-11-19'
five_days_ago_formatted = five_days_ago.strftime("%Y-%m-%d") five_days_ago_formatted = utc_day(five_days_ago)
expected_response = { expected_response = {
"last24Hours": { "last24Hours": {
"reviewed_alert": 0, "reviewed_alert": 0,
@@ -549,7 +554,7 @@ class TestHttpReview(BaseTestHttp):
assert response.status_code == 200 assert response.status_code == 200
response_json = response.json() response_json = response.json()
# e.g. '2024-11-19' # e.g. '2024-11-19'
five_days_ago_formatted = five_days_ago.strftime("%Y-%m-%d") five_days_ago_formatted = utc_day(five_days_ago)
expected_response = { expected_response = {
"last24Hours": { "last24Hours": {
"reviewed_alert": None, "reviewed_alert": None,