2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Updater component."""
|
2016-10-20 19:30:44 +00:00
|
|
|
from datetime import datetime, timedelta
|
2015-11-29 20:13:06 +00:00
|
|
|
import unittest
|
|
|
|
from unittest.mock import patch
|
2016-10-20 19:30:44 +00:00
|
|
|
import os
|
2015-11-29 20:13:06 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2016-09-26 21:20:36 +00:00
|
|
|
from homeassistant.bootstrap import setup_component
|
2015-11-29 20:13:06 +00:00
|
|
|
from homeassistant.components import updater
|
2016-10-20 19:30:44 +00:00
|
|
|
|
|
|
|
from tests.common import (
|
|
|
|
assert_setup_component, fire_time_changed, get_test_home_assistant)
|
2015-11-29 20:13:06 +00:00
|
|
|
|
|
|
|
NEW_VERSION = '10000.0'
|
|
|
|
|
2016-04-07 01:46:48 +00:00
|
|
|
# We need to use a 'real' looking version number to load the updater component
|
|
|
|
MOCK_CURRENT_VERSION = '10.0'
|
|
|
|
|
2015-11-29 20:13:06 +00:00
|
|
|
|
|
|
|
class TestUpdater(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the Updater component."""
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2016-10-20 19:30:44 +00:00
|
|
|
hass = None
|
|
|
|
|
|
|
|
def setup_method(self, _):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2016-10-20 19:30:44 +00:00
|
|
|
def teardown_method(self, _):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-11-29 20:13:06 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
@patch('homeassistant.components.updater.get_newest_version')
|
2016-10-20 19:30:44 +00:00
|
|
|
def test_new_version_shows_entity_on_start( # pylint: disable=invalid-name
|
|
|
|
self, mock_get_newest_version):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if new entity is created if new version is available."""
|
2016-10-20 19:30:44 +00:00
|
|
|
mock_get_newest_version.return_value = (NEW_VERSION, '')
|
2016-04-07 01:46:48 +00:00
|
|
|
updater.CURRENT_VERSION = MOCK_CURRENT_VERSION
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2016-10-20 19:30:44 +00:00
|
|
|
with assert_setup_component(1) as config:
|
|
|
|
setup_component(self.hass, updater.DOMAIN, {updater.DOMAIN: {}})
|
|
|
|
_dt = datetime.now() + timedelta(hours=1)
|
|
|
|
assert config['updater'] == {'opt_out': False}
|
|
|
|
|
|
|
|
for secs in [-1, 0, 1]:
|
|
|
|
fire_time_changed(self.hass, _dt + timedelta(seconds=secs))
|
|
|
|
self.hass.block_till_done()
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
self.assertTrue(self.hass.states.is_state(
|
|
|
|
updater.ENTITY_ID, NEW_VERSION))
|
2015-11-29 20:13:06 +00:00
|
|
|
|
|
|
|
@patch('homeassistant.components.updater.get_newest_version')
|
2016-10-20 19:30:44 +00:00
|
|
|
def test_no_entity_on_same_version( # pylint: disable=invalid-name
|
|
|
|
self, mock_get_newest_version):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if no entity is created if same version."""
|
2016-10-20 19:30:44 +00:00
|
|
|
mock_get_newest_version.return_value = (MOCK_CURRENT_VERSION, '')
|
2016-04-07 01:46:48 +00:00
|
|
|
updater.CURRENT_VERSION = MOCK_CURRENT_VERSION
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2016-10-20 19:30:44 +00:00
|
|
|
with assert_setup_component(1) as config:
|
|
|
|
assert setup_component(
|
|
|
|
self.hass, updater.DOMAIN, {updater.DOMAIN: {}})
|
|
|
|
_dt = datetime.now() + timedelta(hours=1)
|
|
|
|
assert config['updater'] == {'opt_out': False}
|
2015-11-29 20:13:06 +00:00
|
|
|
|
|
|
|
self.assertIsNone(self.hass.states.get(updater.ENTITY_ID))
|
|
|
|
|
2016-10-20 19:30:44 +00:00
|
|
|
mock_get_newest_version.return_value = (NEW_VERSION, '')
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2016-10-20 19:30:44 +00:00
|
|
|
for secs in [-1, 0, 1]:
|
|
|
|
fire_time_changed(self.hass, _dt + timedelta(seconds=secs))
|
|
|
|
self.hass.block_till_done()
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
self.assertTrue(self.hass.states.is_state(
|
|
|
|
updater.ENTITY_ID, NEW_VERSION))
|
2015-11-29 20:13:06 +00:00
|
|
|
|
2016-10-20 19:30:44 +00:00
|
|
|
@patch('homeassistant.components.updater.requests.post')
|
|
|
|
def test_errors_while_fetching_new_version( # pylint: disable=invalid-name
|
|
|
|
self, mock_get):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for errors while fetching the new version."""
|
2015-11-29 20:13:06 +00:00
|
|
|
mock_get.side_effect = requests.RequestException
|
2016-10-20 19:30:44 +00:00
|
|
|
uuid = '0000'
|
|
|
|
self.assertIsNone(updater.get_newest_version(uuid))
|
2015-11-29 20:13:06 +00:00
|
|
|
|
|
|
|
mock_get.side_effect = ValueError
|
2016-10-20 19:30:44 +00:00
|
|
|
self.assertIsNone(updater.get_newest_version(uuid))
|
2015-11-29 20:13:06 +00:00
|
|
|
|
|
|
|
mock_get.side_effect = KeyError
|
2016-10-20 19:30:44 +00:00
|
|
|
self.assertIsNone(updater.get_newest_version(uuid))
|
2016-04-07 01:46:48 +00:00
|
|
|
|
|
|
|
def test_updater_disabled_on_dev(self):
|
|
|
|
"""Test if the updater component is disabled on dev."""
|
|
|
|
updater.CURRENT_VERSION = MOCK_CURRENT_VERSION + 'dev'
|
|
|
|
|
2016-10-20 19:30:44 +00:00
|
|
|
with assert_setup_component(1) as config:
|
|
|
|
assert not setup_component(
|
|
|
|
self.hass, updater.DOMAIN, {updater.DOMAIN: {}})
|
|
|
|
assert config['updater'] == {'opt_out': False}
|
|
|
|
|
|
|
|
def test_uuid_function(self):
|
|
|
|
"""Test if the uuid function works."""
|
|
|
|
path = self.hass.config.path(updater.UPDATER_UUID_FILE)
|
|
|
|
try:
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
uuid = updater._load_uuid(self.hass)
|
|
|
|
assert os.path.isfile(path)
|
|
|
|
uuid2 = updater._load_uuid(self.hass)
|
|
|
|
assert uuid == uuid2
|
|
|
|
os.remove(path)
|
|
|
|
uuid2 = updater._load_uuid(self.hass)
|
|
|
|
assert uuid != uuid2
|
|
|
|
finally:
|
|
|
|
os.remove(path)
|