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;
|
|
|
|
ensureDatabases = [ "hass" ];
|
|
|
|
ensureUsers = [{
|
|
|
|
name = "hass";
|
|
|
|
ensurePermissions = {
|
|
|
|
"DATABASE hass" = "ALL PRIVILEGES";
|
|
|
|
};
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
|
2020-06-20 10:05:30 +00:00
|
|
|
services.home-assistant = {
|
|
|
|
enable = true;
|
2022-01-30 01:29:45 +00:00
|
|
|
inherit configDir;
|
|
|
|
|
|
|
|
# tests loading components by overriding the package
|
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
|
|
|
|
];
|
2021-12-01 00:19:01 +00:00
|
|
|
extraComponents = [ "zha" ];
|
|
|
|
}).overrideAttrs (oldAttrs: {
|
|
|
|
doInstallCheck = false;
|
|
|
|
});
|
2022-01-30 01:29:45 +00:00
|
|
|
|
2022-01-30 01:30:51 +00:00
|
|
|
# tests loading components from the module
|
|
|
|
extraComponents = [
|
|
|
|
"wake_on_lan"
|
|
|
|
];
|
|
|
|
|
2022-02-01 20:03:51 +00:00
|
|
|
# test extra package passing from the module
|
|
|
|
extraPackages = python3Packages: with python3Packages; [
|
|
|
|
psycopg2
|
|
|
|
];
|
|
|
|
|
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
|
|
|
|
|
|
|
# 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;
|
2022-07-13 23:41:58 +00:00
|
|
|
configuration.services.home-assistant.config.esphome = {};
|
2021-12-18 22:01:33 +00:00
|
|
|
};
|
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
|
|
|
|
system = nodes.hass.config.system.build.toplevel;
|
|
|
|
in
|
|
|
|
''
|
2022-02-14 01:10:24 +00:00
|
|
|
import re
|
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
|
|
|
# Parse the package path out of the systemd unit, as we cannot
|
2022-12-18 00:31:14 +00:00
|
|
|
# access the final package, that is overridden inside the module,
|
2022-02-14 01:10:24 +00:00
|
|
|
# by any other means.
|
|
|
|
pattern = re.compile(r"path=(?P<path>[\/a-z0-9-.]+)\/bin\/hass")
|
|
|
|
response = hass.execute("systemctl show -p ExecStart home-assistant.service")[1]
|
|
|
|
match = pattern.search(response)
|
2022-06-04 00:29:55 +00:00
|
|
|
assert match
|
2022-02-14 01:10:24 +00:00
|
|
|
package = match.group('path')
|
|
|
|
|
2022-07-13 23:41:58 +00:00
|
|
|
|
|
|
|
def get_journal_cursor(host) -> str:
|
|
|
|
exit, out = host.execute("journalctl -u home-assistant.service -n1 -o json-pretty --output-fields=__CURSOR")
|
|
|
|
assert exit == 0
|
|
|
|
return json.loads(out)["__CURSOR"]
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_homeassistant(host, cursor):
|
|
|
|
host.wait_until_succeeds(f"journalctl --after-cursor='{cursor}' -u home-assistant.service | grep -q 'Home Assistant initialized in'")
|
|
|
|
|
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
hass.wait_for_unit("home-assistant.service")
|
2022-07-13 23:41:58 +00:00
|
|
|
cursor = get_journal_cursor(hass)
|
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
|
|
|
|
2022-02-14 01:10:24 +00:00
|
|
|
with subtest("Check extraComponents and extraPackages are considered from the package"):
|
|
|
|
hass.succeed(f"grep -q 'colorama' {package}/extra_packages")
|
|
|
|
hass.succeed(f"grep -q 'zha' {package}/extra_components")
|
|
|
|
|
|
|
|
with subtest("Check extraComponents and extraPackages are considered from the module"):
|
|
|
|
hass.succeed(f"grep -q 'psycopg2' {package}/extra_packages")
|
|
|
|
hass.succeed(f"grep -q 'wake_on_lan' {package}/extra_components")
|
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
with subtest("Check that Home Assistant's web interface and API can be reached"):
|
2022-07-13 23:41:58 +00:00
|
|
|
wait_for_homeassistant(hass, 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
|
|
|
|
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"):
|
|
|
|
# store the old pid of the process
|
|
|
|
pid = hass.succeed("systemctl show --property=MainPID home-assistant.service")
|
2022-07-13 23:41:58 +00:00
|
|
|
cursor = get_journal_cursor(hass)
|
2021-12-18 22:01:33 +00:00
|
|
|
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"
|
2022-07-13 23:41:58 +00:00
|
|
|
wait_for_homeassistant(hass, cursor)
|
2021-12-18 22:01:33 +00:00
|
|
|
|
|
|
|
with subtest("check service restarts when package changes"):
|
|
|
|
pid = new_pid
|
2022-07-13 23:41:58 +00:00
|
|
|
cursor = get_journal_cursor(hass)
|
2021-12-18 22:01:33 +00:00
|
|
|
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 shoudl change when the HA binary changes"
|
2022-07-13 23:41:58 +00:00
|
|
|
wait_for_homeassistant(hass, cursor)
|
2021-12-18 22:01:33 +00:00
|
|
|
|
2020-01-09 10:19:54 +00:00
|
|
|
with subtest("Check that no errors were logged"):
|
2022-06-22 14:23:26 +00:00
|
|
|
output_log = hass.succeed("cat ${configDir}/home-assistant.log")
|
2020-01-09 10:19:54 +00:00
|
|
|
assert "ERROR" not in output_log
|
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
|
|
|
'';
|
|
|
|
})
|