Skip to content

Commit

Permalink
Merge pull request #119 from basnijholt/wrong-secrets-path
Browse files Browse the repository at this point in the history
add failing test where secrets.yaml is not found and fix it
  • Loading branch information
robmarkcole authored Mar 8, 2020
2 parents 00140df + 53bc26c commit db57f29
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
26 changes: 24 additions & 2 deletions detective/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
Helper functions for config.
"""
import os
from pathlib import Path
from typing import Optional

from ruamel.yaml import YAML
from ruamel.yaml.constructor import SafeConstructor


_CONFIGURATION_PATH: Optional[Path] = None


def default_hass_config_dir():
"""Put together the default configuration directory based on the OS."""
data_dir = os.getenv("APPDATA") if os.name == "nt" else os.path.expanduser("~")
Expand Down Expand Up @@ -35,10 +40,25 @@ class HassSafeConstructor(SafeConstructor):

def _secret_yaml(loader, node):
"""Load secrets and embed it into the configuration YAML."""
fname = os.path.join(os.path.dirname(loader.name), "secrets.yaml")
dirpath = Path(os.path.dirname(loader.name))

# Recursively search for the secrets.yaml file
# this might be needed when a yaml file is included like
# `!included folder/file.yaml`. Same rules as
# https://www.home-assistant.io/docs/configuration/secrets/#debugging-secrets
fname = dirpath / "secrets.yaml"
for _ in range(len(dirpath.parts)):
if fname.exists() or (
_CONFIGURATION_PATH is not None
and _CONFIGURATION_PATH.exists()
and dirpath.samefile(_CONFIGURATION_PATH)
):
break
dirpath = dirpath.parent
fname = dirpath / "secrets.yaml"

try:
with open(fname, encoding="utf-8") as secret_file:
with fname.open() as secret_file:
secrets = YAML(typ="safe").load(secret_file)
except FileNotFoundError:
raise ValueError("Secrets file {} not found".format(fname)) from None
Expand Down Expand Up @@ -103,6 +123,8 @@ def load_yaml(fname):

def db_url_from_hass_config(path):
"""Find the recorder database url from a HASS config dir."""
global _CONFIGURATION_PATH
_CONFIGURATION_PATH = Path(path).resolve()
config = load_hass_config(path)
default_path = os.path.join(path, "home-assistant_v2.db")
default_url = "sqlite:///{}".format(default_path)
Expand Down
28 changes: 17 additions & 11 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for config package."""
import os
import tempfile
from pathlib import Path
from unittest.mock import patch

import pytest
Expand All @@ -27,11 +28,14 @@ def test_find_hass_config():
def test_load_hass_config():
"""Test loading hass config."""
with tempfile.TemporaryDirectory() as tmpdir:
with open(os.path.join(tmpdir, "configuration.yaml"), "wt") as fp:
p = Path(tmpdir)
with (p / "configuration.yaml").open("wt") as fp:
fp.write(
"""
mock_secret: !secret some_secret
included: !include included.yaml
nested_with_secret: !include includes/with_secret.yaml
nested_without_secret: !include includes/without_secret.yaml
mock_env: !env_var MOCK_ENV
mock_env: !env_var MOCK_ENV
mock_dir_list: !include_dir_list ./zxc
Expand All @@ -43,25 +47,28 @@ def test_load_hass_config():
"""
)

with open(os.path.join(tmpdir, "secrets.yaml"), "wt") as fp:
with (p / "secrets.yaml").open("wt") as fp:
fp.write(
"""
some_secret: test-some-secret
other_secret: test-other-secret
another_secret: test-another-secret
"""
)

with open(os.path.join(tmpdir, "included.yaml"), "wt") as fp:
fp.write(
"""
some: value
"""
)

with (p / "included.yaml").open("wt") as fp:
fp.write("some: value")
includes_dir = p / "includes"
includes_dir.mkdir(exist_ok=True)
with (includes_dir / "with_secret.yaml").open("wt") as fp:
fp.write("some: !secret another_secret")
with (includes_dir / "without_secret.yaml").open("wt") as fp:
fp.write("some: value")
configuration = config.load_hass_config(tmpdir)

# assert configuration["mock_secret"] == "test-other-secret" # TODO: fix
assert configuration["included"] == {"some": "value"}
assert configuration["nested_with_secret"] == {"some": "test-another-secret"}
assert configuration["nested_without_secret"] == {"some": "value"}


def test_db_url_from_hass_config():
Expand All @@ -84,4 +91,3 @@ def test_db_url_from_hass_config():
return_value={"recorder": {"db_url": "mock-url"}},
):
assert config.db_url_from_hass_config("mock-path") == "mock-url"

0 comments on commit db57f29

Please sign in to comment.