89 lines
2.4 KiB
Python
89 lines
2.4 KiB
Python
"""Tests for the configuration management."""
|
|
|
|
import logging
|
|
import os
|
|
import socket
|
|
from typing import Any, Dict
|
|
|
|
import pytest
|
|
|
|
from hasskiosk import __version__
|
|
from hasskiosk.config import configure_logging, read_config
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def mock_env(monkeypatch):
|
|
"""Environment mock to test values."""
|
|
monkeypatch.setenv("HASSKIOSK_MQTT_SUBSCRIBE_TOPIC", "home/test/#")
|
|
monkeypatch.setenv("HASSKIOSK_MQTT_HOST", "ha.example.com")
|
|
monkeypatch.setenv("HASSKIOSK_MQTT_USERNAME", "testymctesterson")
|
|
monkeypatch.setenv("HASSKIOSK_MQTT_PASSWORD", "hunter2")
|
|
|
|
|
|
@pytest.fixture
|
|
def logging_config() -> Dict[str, Any]:
|
|
"""logging configuration fixture."""
|
|
config = {
|
|
"version": 1,
|
|
"disable_existing_loggers": False,
|
|
"formatters": {
|
|
"simple": {
|
|
"format": "%(asctime)s %(levelname)s (%(name)s) %(message)s",
|
|
"converter": "time.gmtime",
|
|
},
|
|
},
|
|
"handlers": {
|
|
"stdout": {
|
|
"class": "logging.StreamHandler",
|
|
"level": "DEBUG",
|
|
"formatter": "simple",
|
|
"stream": "ext://sys.stdout",
|
|
},
|
|
},
|
|
"loggers": {
|
|
"hasskiosk": {
|
|
"level": "DEBUG",
|
|
"propagate": "yes",
|
|
},
|
|
},
|
|
"root": {
|
|
"level": "WARN",
|
|
"handlers": ["stdout"],
|
|
},
|
|
}
|
|
return config
|
|
|
|
|
|
def test_read_config():
|
|
"""Test the read_config function."""
|
|
config = read_config()
|
|
hostname = socket.gethostname()
|
|
pid = os.getpid()
|
|
assert config["version"] == __version__
|
|
assert config["sysname"] == "hasskiosk"
|
|
assert config["client_id"] == f"hasskiosk-{hostname}-{pid}"
|
|
assert config["mqtt"] == {
|
|
"host": "ha.example.com",
|
|
"username": "testymctesterson",
|
|
"password": "hunter2",
|
|
"port": 1883,
|
|
"keepalive": 60,
|
|
"subscription": "home/test/#",
|
|
"screen_state_topic": "#/presence",
|
|
}
|
|
|
|
|
|
def test_configure_logging(logging_config):
|
|
"""Test the logging configuration."""
|
|
config = {
|
|
"version": "1.2.3.test",
|
|
"sysname": "testkiosk",
|
|
"logging": logging_config,
|
|
}
|
|
configure_logging(config)
|
|
log = logging.getLogger(__name__)
|
|
rootlog = log
|
|
while rootlog.parent:
|
|
rootlog = rootlog.parent
|
|
assert len(rootlog.handlers) == 1
|