2021-04-26 00:17:48 +00:00
|
|
|
import ./make-test-python.nix ({ pkgs, lib, ... }:
|
2018-01-26 00:41:36 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
configDir = "/var/lib/foobar";
|
|
|
|
in {
|
|
|
|
name = "home-assistant";
|
2021-04-26 00:17:48 +00:00
|
|
|
meta.maintainers = lib.teams.home-assistant.members;
|
2018-01-26 00:41:36 +00:00
|
|
|
|
2020-06-20 10:05:30 +00:00
|
|
|
nodes.hass = { pkgs, ... }: {
|
2022-02-01 20:03:51 +00:00
|
|
|
services.postgresql = {
|
|
|
|
enable = true;
|
2023-11-04 09:23:31 +00:00
|
|
|
|
|
|
|
# FIXME: hack for https://github.com/NixOS/nixpkgs/issues/216989
|
|
|
|
# Should be replaced with ensureUsers again when a solution for that is found
|
|
|
|
initialScript = pkgs.writeText "hass-setup-db.sql" ''
|
|
|
|
CREATE ROLE hass WITH LOGIN;
|
|
|
|
CREATE DATABASE hass WITH OWNER hass;
|
|
|
|
'';
|
2022-02-01 20:03:51 +00:00
|
|
|
};
|
|
|
|
|
2020-06-20 10:05:30 +00:00
|
|
|
services.home-assistant = {
|
|
|
|
enable = true;
|
2022-01-30 01:29:45 +00:00
|
|
|
inherit configDir;
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
# provide dependencies through package overrides
|
2021-12-01 00:19:01 +00:00
|
|
|
package = (pkgs.home-assistant.override {
|
2022-02-14 01:10:24 +00:00
|
|
|
extraPackages = ps: with ps; [
|
|
|
|
colorama
|
|
|
|
];
|
2023-02-20 15:43:25 +00:00
|
|
|
extraComponents = [
|
|
|
|
# test char-tty device allow propagation into the service
|
|
|
|
"zha"
|
|
|
|
];
|
2021-12-01 00:19:01 +00:00
|
|
|
});
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
# provide component dependencies explicitly from the module
|
2022-01-30 01:30:51 +00:00
|
|
|
extraComponents = [
|
2023-02-20 15:43:25 +00:00
|
|
|
"mqtt"
|
2022-01-30 01:30:51 +00:00
|
|
|
];
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
# provide package for postgresql support
|
2022-02-01 20:03:51 +00:00
|
|
|
extraPackages = python3Packages: with python3Packages; [
|
|
|
|
psycopg2
|
|
|
|
];
|
|
|
|
|
2022-02-01 02:10:44 +00:00
|
|
|
# test loading custom components
|
|
|
|
customComponents = with pkgs.home-assistant-custom-components; [
|
|
|
|
prometheus-sensor
|
|
|
|
];
|
|
|
|
|
2022-01-31 01:32:39 +00:00
|
|
|
# test loading lovelace modules
|
|
|
|
customLovelaceModules = with pkgs.home-assistant-custom-lovelace-modules; [
|
|
|
|
mini-graph-card
|
|
|
|
];
|
|
|
|
|
2020-06-20 10:05:30 +00:00
|
|
|
config = {
|
|
|
|
homeassistant = {
|
|
|
|
name = "Home";
|
|
|
|
time_zone = "UTC";
|
|
|
|
latitude = "0.0";
|
|
|
|
longitude = "0.0";
|
|
|
|
elevation = 0;
|
2018-01-26 00:41:36 +00:00
|
|
|
};
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2022-02-01 20:03:51 +00:00
|
|
|
# configure the recorder component to use the postgresql db
|
|
|
|
recorder.db_url = "postgresql://@/hass";
|
|
|
|
|
2022-01-30 01:29:45 +00:00
|
|
|
# we can't load default_config, because the updater requires
|
|
|
|
# network access and would cause an error, so load frontend
|
|
|
|
# here explicitly.
|
|
|
|
# https://www.home-assistant.io/integrations/frontend/
|
2020-06-20 10:05:30 +00:00
|
|
|
frontend = {};
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2023-04-28 19:36:30 +00:00
|
|
|
# include some popular integrations, that absolutely shouldn't break
|
|
|
|
knx = {};
|
|
|
|
shelly = {};
|
|
|
|
zha = {};
|
|
|
|
|
2022-01-30 01:29:45 +00:00
|
|
|
# set up a wake-on-lan switch to test capset capability required
|
|
|
|
# for the ping suid wrapper
|
|
|
|
# https://www.home-assistant.io/integrations/wake_on_lan/
|
|
|
|
switch = [ {
|
2022-01-25 17:30:43 +00:00
|
|
|
platform = "wake_on_lan";
|
|
|
|
mac = "00:11:22:33:44:55";
|
|
|
|
host = "127.0.0.1";
|
2022-01-30 01:29:45 +00:00
|
|
|
} ];
|
|
|
|
|
|
|
|
# test component-based capability assignment (CAP_NET_BIND_SERVICE)
|
|
|
|
# https://www.home-assistant.io/integrations/emulated_hue/
|
2021-04-25 12:44:51 +00:00
|
|
|
emulated_hue = {
|
|
|
|
host_ip = "127.0.0.1";
|
|
|
|
listen_port = 80;
|
|
|
|
};
|
2022-01-30 01:29:45 +00:00
|
|
|
|
|
|
|
# https://www.home-assistant.io/integrations/logger/
|
2020-06-20 10:05:30 +00:00
|
|
|
logger = {
|
|
|
|
default = "info";
|
|
|
|
};
|
|
|
|
};
|
2022-01-30 01:29:45 +00:00
|
|
|
|
|
|
|
# configure the sample lovelace dashboard
|
2020-06-20 10:05:30 +00:00
|
|
|
lovelaceConfig = {
|
|
|
|
title = "My Awesome Home";
|
|
|
|
views = [{
|
|
|
|
title = "Example";
|
|
|
|
cards = [{
|
|
|
|
type = "markdown";
|
|
|
|
title = "Lovelace";
|
|
|
|
content = "Welcome to your **Lovelace UI**.";
|
|
|
|
}];
|
|
|
|
}];
|
2018-01-26 00:41:36 +00:00
|
|
|
};
|
2020-06-20 10:05:30 +00:00
|
|
|
lovelaceConfigWritable = true;
|
|
|
|
};
|
2021-12-18 22:01:33 +00:00
|
|
|
|
|
|
|
# Cause a configuration change inside `configuration.yml` and verify that the process is being reloaded.
|
|
|
|
specialisation.differentName = {
|
|
|
|
inheritParentConfig = true;
|
|
|
|
configuration.services.home-assistant.config.homeassistant.name = lib.mkForce "Test Home";
|
|
|
|
};
|
|
|
|
|
|
|
|
# Cause a configuration change that requires a service restart as we added a new runtime dependency
|
|
|
|
specialisation.newFeature = {
|
|
|
|
inheritParentConfig = true;
|
2023-04-28 19:36:30 +00:00
|
|
|
configuration.services.home-assistant.config.backup = {};
|
2021-12-18 22:01:33 +00:00
|
|
|
};
|
2023-04-17 21:49:13 +00:00
|
|
|
|
|
|
|
specialisation.removeCustomThings = {
|
|
|
|
inheritParentConfig = true;
|
|
|
|
configuration.services.home-assistant = {
|
|
|
|
customComponents = lib.mkForce [];
|
|
|
|
customLovelaceModules = lib.mkForce [];
|
|
|
|
};
|
|
|
|
};
|
2018-02-01 12:42:07 +00:00
|
|
|
};
|
2018-01-26 00:41:36 +00:00
|
|
|
|
2021-12-18 22:01:33 +00:00
|
|
|
testScript = { nodes, ... }: let
|
2023-02-20 16:59:50 +00:00
|
|
|
system = nodes.hass.system.build.toplevel;
|
2021-12-18 22:01:33 +00:00
|
|
|
in
|
|
|
|
''
|
2022-07-13 23:41:58 +00:00
|
|
|
import json
|
2022-02-14 01:10:24 +00:00
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
start_all()
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2022-02-14 01:10:24 +00:00
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
def get_journal_cursor() -> str:
|
|
|
|
exit, out = hass.execute("journalctl -u home-assistant.service -n1 -o json-pretty --output-fields=__CURSOR")
|
2022-07-13 23:41:58 +00:00
|
|
|
assert exit == 0
|
|
|
|
return json.loads(out)["__CURSOR"]
|
|
|
|
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
def get_journal_since(cursor) -> str:
|
|
|
|
exit, out = hass.execute(f"journalctl --after-cursor='{cursor}' -u home-assistant.service")
|
|
|
|
assert exit == 0
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2023-02-20 16:01:02 +00:00
|
|
|
def get_unit_property(property) -> str:
|
|
|
|
exit, out = hass.execute(f"systemctl show --property={property} home-assistant.service")
|
|
|
|
assert exit == 0
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
def wait_for_homeassistant(cursor):
|
|
|
|
hass.wait_until_succeeds(f"journalctl --after-cursor='{cursor}' -u home-assistant.service | grep -q 'Home Assistant initialized in'")
|
2022-07-13 23:41:58 +00:00
|
|
|
|
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
hass.wait_for_unit("home-assistant.service")
|
2023-02-20 15:43:25 +00:00
|
|
|
cursor = get_journal_cursor()
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
with subtest("Check that YAML configuration file is in place"):
|
|
|
|
hass.succeed("test -L ${configDir}/configuration.yaml")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
|
|
|
with subtest("Check the lovelace config is copied because lovelaceConfigWritable = true"):
|
2020-01-09 10:19:54 +00:00
|
|
|
hass.succeed("test -f ${configDir}/ui-lovelace.yaml")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
with subtest("Check that Home Assistant's web interface and API can be reached"):
|
2023-02-20 15:43:25 +00:00
|
|
|
wait_for_homeassistant(cursor)
|
2020-01-09 10:19:54 +00:00
|
|
|
hass.wait_for_open_port(8123)
|
2020-05-20 21:41:42 +00:00
|
|
|
hass.succeed("curl --fail http://localhost:8123/lovelace")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2022-02-01 02:10:44 +00:00
|
|
|
with subtest("Check that custom components get installed"):
|
|
|
|
hass.succeed("test -f ${configDir}/custom_components/prometheus_sensor/manifest.json")
|
|
|
|
hass.wait_until_succeeds("journalctl -u home-assistant.service | grep -q 'We found a custom integration prometheus_sensor which has not been tested by Home Assistant'")
|
|
|
|
|
2022-01-31 01:32:39 +00:00
|
|
|
with subtest("Check that lovelace modules are referenced and fetchable"):
|
|
|
|
hass.succeed("grep -q 'mini-graph-card-bundle.js' '${configDir}/ui-lovelace.yaml'")
|
|
|
|
hass.succeed("curl --fail http://localhost:8123/local/nixos-lovelace-modules/mini-graph-card-bundle.js")
|
|
|
|
|
2023-02-20 16:01:02 +00:00
|
|
|
with subtest("Check that optional dependencies are in the PYTHONPATH"):
|
|
|
|
env = get_unit_property("Environment")
|
|
|
|
python_path = env.split("PYTHONPATH=")[1].split()[0]
|
|
|
|
for package in ["colorama", "paho-mqtt", "psycopg2"]:
|
|
|
|
assert package in python_path, f"{package} not in PYTHONPATH"
|
|
|
|
|
2023-02-20 15:43:25 +00:00
|
|
|
with subtest("Check that declaratively configured components get setup"):
|
|
|
|
journal = get_journal_since(cursor)
|
|
|
|
for domain in ["emulated_hue", "wake_on_lan"]:
|
|
|
|
assert f"Setup of domain {domain} took" in journal, f"{domain} setup missing"
|
|
|
|
|
2021-04-25 12:44:51 +00:00
|
|
|
with subtest("Check that capabilities are passed for emulated_hue to bind to port 80"):
|
|
|
|
hass.wait_for_open_port(80)
|
|
|
|
hass.succeed("curl --fail http://localhost:80/description.xml")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2021-12-01 00:19:01 +00:00
|
|
|
with subtest("Check extra components are considered in systemd unit hardening"):
|
|
|
|
hass.succeed("systemctl show -p DeviceAllow home-assistant.service | grep -q char-ttyUSB")
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2021-12-18 22:01:33 +00:00
|
|
|
with subtest("Check service reloads when configuration changes"):
|
2023-02-20 15:43:25 +00:00
|
|
|
pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
|
|
|
|
cursor = get_journal_cursor()
|
|
|
|
hass.succeed("${system}/specialisation/differentName/bin/switch-to-configuration test")
|
|
|
|
new_pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
|
|
|
|
assert pid == new_pid, "The PID of the process should not change between process reloads"
|
|
|
|
wait_for_homeassistant(cursor)
|
|
|
|
|
|
|
|
with subtest("Check service restarts when dependencies change"):
|
|
|
|
pid = new_pid
|
|
|
|
cursor = get_journal_cursor()
|
|
|
|
hass.succeed("${system}/specialisation/newFeature/bin/switch-to-configuration test")
|
|
|
|
new_pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
|
|
|
|
assert pid != new_pid, "The PID of the process should change when its PYTHONPATH changess"
|
|
|
|
wait_for_homeassistant(cursor)
|
|
|
|
|
|
|
|
with subtest("Check that new components get setup after restart"):
|
|
|
|
journal = get_journal_since(cursor)
|
2023-06-07 21:55:51 +00:00
|
|
|
for domain in ["backup"]:
|
2023-02-20 15:43:25 +00:00
|
|
|
assert f"Setup of domain {domain} took" in journal, f"{domain} setup missing"
|
2021-12-18 22:01:33 +00:00
|
|
|
|
2023-04-17 21:49:13 +00:00
|
|
|
with subtest("Check custom components and custom lovelace modules get removed"):
|
|
|
|
cursor = get_journal_cursor()
|
|
|
|
hass.succeed("${system}/specialisation/removeCustomThings/bin/switch-to-configuration test")
|
|
|
|
hass.fail("grep -q 'mini-graph-card-bundle.js' '${configDir}/ui-lovelace.yaml'")
|
|
|
|
hass.fail("test -f ${configDir}/custom_components/prometheus_sensor/manifest.json")
|
|
|
|
wait_for_homeassistant(cursor)
|
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
with subtest("Check that no errors were logged"):
|
2023-02-20 15:43:25 +00:00
|
|
|
hass.fail("journalctl -u home-assistant -o cat | grep -q ERROR")
|
2020-06-20 10:05:30 +00:00
|
|
|
|
2021-04-25 12:44:51 +00:00
|
|
|
with subtest("Check systemd unit hardening"):
|
2022-01-30 01:29:45 +00:00
|
|
|
hass.log(hass.succeed("systemctl cat home-assistant.service"))
|
2021-04-25 12:44:51 +00:00
|
|
|
hass.log(hass.succeed("systemd-analyze security home-assistant.service"))
|
2018-01-26 00:41:36 +00:00
|
|
|
'';
|
|
|
|
})
|