nixos/tests/home-assistant: improve reload/restart test cases

Wait until home-assistant is fully reloaded or restarted to spot
possible errors during startup.

Swap out bluetooth_tracker for esphome, since the bluetooth tracker
causes errors, when it does not find a bluetooth device.

Drop mosquitto from the environment. It wasn't used since the 2022.3.0
release when MQTT stopped being configurable from the YAML config.

Changed files
+19 -6
nixos
+19 -6
nixos/tests/home-assistant.nix
···
meta.maintainers = lib.teams.home-assistant.members;
nodes.hass = { pkgs, ... }: {
-
environment.systemPackages = with pkgs; [ mosquitto ];
-
services.postgresql = {
enable = true;
ensureDatabases = [ "hass" ];
···
# Cause a configuration change that requires a service restart as we added a new runtime dependency
specialisation.newFeature = {
inheritParentConfig = true;
-
configuration.services.home-assistant.config.device_tracker = [
-
{ platform = "bluetooth_tracker"; }
-
];
};
};
···
in
''
import re
start_all()
···
assert match
package = match.group('path')
hass.wait_for_unit("home-assistant.service")
with subtest("Check that YAML configuration file is in place"):
hass.succeed("test -L ${configDir}/configuration.yaml")
···
hass.succeed(f"grep -q 'wake_on_lan' {package}/extra_components")
with subtest("Check that Home Assistant's web interface and API can be reached"):
-
hass.wait_until_succeeds("journalctl -u home-assistant.service | grep -q 'Home Assistant initialized in'")
hass.wait_for_open_port(8123)
hass.succeed("curl --fail http://localhost:8123/lovelace")
···
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")
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"
with subtest("check service restarts when package changes"):
pid = new_pid
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"
with subtest("Check that no errors were logged"):
output_log = hass.succeed("cat ${configDir}/home-assistant.log")
···
meta.maintainers = lib.teams.home-assistant.members;
nodes.hass = { pkgs, ... }: {
services.postgresql = {
enable = true;
ensureDatabases = [ "hass" ];
···
# Cause a configuration change that requires a service restart as we added a new runtime dependency
specialisation.newFeature = {
inheritParentConfig = true;
+
configuration.services.home-assistant.config.esphome = {};
};
};
···
in
''
import re
+
import json
start_all()
···
assert match
package = match.group('path')
+
+
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'")
+
+
hass.wait_for_unit("home-assistant.service")
+
cursor = get_journal_cursor(hass)
with subtest("Check that YAML configuration file is in place"):
hass.succeed("test -L ${configDir}/configuration.yaml")
···
hass.succeed(f"grep -q 'wake_on_lan' {package}/extra_components")
with subtest("Check that Home Assistant's web interface and API can be reached"):
+
wait_for_homeassistant(hass, cursor)
hass.wait_for_open_port(8123)
hass.succeed("curl --fail http://localhost:8123/lovelace")
···
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")
+
cursor = get_journal_cursor(hass)
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(hass, cursor)
with subtest("check service restarts when package changes"):
pid = new_pid
+
cursor = get_journal_cursor(hass)
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"
+
wait_for_homeassistant(hass, cursor)
with subtest("Check that no errors were logged"):
output_log = hass.succeed("cat ${configDir}/home-assistant.log")