From 05b58d76b9446d29164fddaa028197a34bd7fbc1 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Sun, 18 Nov 2018 12:08:46 +0100 Subject: [PATCH] Add tests for hass.io (#817) * Add tests for hass.io * Fix folder * Fix test command --- tests/__init__.py | 1 + tests/addons/__init__.py | 1 + tests/addons/test_config.py | 41 ++++++++++++++++++++++++++ tests/common.py | 9 ++++++ tests/fixtures/basic-addon-config.json | 14 +++++++++ tox.ini | 8 ++++- 6 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/addons/__init__.py create mode 100644 tests/addons/test_config.py create mode 100644 tests/common.py create mode 100644 tests/fixtures/basic-addon-config.json diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..7a37e62ba --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +"""Hass.io Testframework.""" \ No newline at end of file diff --git a/tests/addons/__init__.py b/tests/addons/__init__.py new file mode 100644 index 000000000..5221b9624 --- /dev/null +++ b/tests/addons/__init__.py @@ -0,0 +1 @@ +"""Add-ons tests.""" \ No newline at end of file diff --git a/tests/addons/test_config.py b/tests/addons/test_config.py new file mode 100644 index 000000000..6e444d901 --- /dev/null +++ b/tests/addons/test_config.py @@ -0,0 +1,41 @@ +"""Validate Add-on configs.""" + +import voluptuous as vol +import pytest + +from hassio.addons import validate as vd + +from ..common import load_json_fixture + + +def test_basic_config(): + """Validate basic config and check the default values.""" + config = load_json_fixture("basic-addon-config.json") + + valid_config = vd.SCHEMA_ADDON_CONFIG(config) + + assert valid_config['name'] == "Test Add-on" + assert valid_config['image'] == "test/{arch}-my-custom-addon" + + # Check defaults + assert not valid_config['host_network'] + assert not valid_config['host_ipc'] + assert not valid_config['host_dbus'] + assert not valid_config['host_pid'] + + assert not valid_config['hassio_api'] + assert not valid_config['homeassistant_api'] + assert not valid_config['docker_api'] + + +def test_invalid_repository(): + """Validate basic config with invalid repository.""" + config = load_json_fixture("basic-addon-config.json") + + config['image'] = "home-assistant/no-valid-repo" + with pytest.raises(vol.Invalid): + vd.SCHEMA_ADDON_CONFIG(config) + + config['image'] = "homeassistant/no-valid-repo:no-tag-allow" + with pytest.raises(vol.Invalid): + vd.SCHEMA_ADDON_CONFIG(config) diff --git a/tests/common.py b/tests/common.py new file mode 100644 index 000000000..c432bc037 --- /dev/null +++ b/tests/common.py @@ -0,0 +1,9 @@ +"""Common test functions.""" +import json +from pathlib import Path + + +def load_json_fixture(filename): + """Load a fixture.""" + path = Path(Path(__file__).parent.joinpath("fixtures"), filename) + return json.loads(path.read_text()) diff --git a/tests/fixtures/basic-addon-config.json b/tests/fixtures/basic-addon-config.json new file mode 100644 index 000000000..18b4890c8 --- /dev/null +++ b/tests/fixtures/basic-addon-config.json @@ -0,0 +1,14 @@ +{ + "name": "Test Add-on", + "version": "1.0.1", + "slug": "test_addon", + "description": "This is a basic Test Add-on", + "arch": ["amd64"], + "url": "https://www.home-assistant.io/", + "startup": "application", + "boot": "auto", + "map": ["config:rw", "ssl"], + "options": {}, + "schema": {}, + "image": "test/{arch}-my-custom-addon" +} \ No newline at end of file diff --git a/tox.ini b/tox.ini index 16c09e69f..70f3d55f4 100644 --- a/tox.ini +++ b/tox.ini @@ -1,10 +1,11 @@ [tox] -envlist = lint +envlist = lint, tests [testenv] deps = flake8==3.6.0 pylint==2.1.1 + pytest==4.0.0 -r{toxinidir}/requirements.txt [testenv:lint] @@ -13,3 +14,8 @@ ignore_errors = True commands = flake8 hassio pylint --rcfile pylintrc hassio + +[testenv:tests] +basepython = python3 +commands = + pytest --duration=10 tests