Migrate hassio tests from coroutine to async/await (#30363)

This commit is contained in:
Franck Nijhof 2020-01-02 00:13:43 +01:00 committed by Andrew Sayre
parent 3a4db2fae7
commit 0fb5fbd85c
2 changed files with 56 additions and 70 deletions

View File

@ -5,35 +5,32 @@ from unittest.mock import patch
import pytest
@asyncio.coroutine
def test_forward_request(hassio_client, aioclient_mock):
async def test_forward_request(hassio_client, aioclient_mock):
"""Test fetching normal path."""
aioclient_mock.post("http://127.0.0.1/beer", text="response")
resp = yield from hassio_client.post("/api/hassio/beer")
resp = await hassio_client.post("/api/hassio/beer")
# Check we got right response
assert resp.status == 200
body = yield from resp.text()
body = await resp.text()
assert body == "response"
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
@asyncio.coroutine
@pytest.mark.parametrize(
"build_type", ["supervisor/info", "homeassistant/update", "host/info"]
)
def test_auth_required_forward_request(hassio_noauth_client, build_type):
async def test_auth_required_forward_request(hassio_noauth_client, build_type):
"""Test auth required for normal request."""
resp = yield from hassio_noauth_client.post("/api/hassio/{}".format(build_type))
resp = await hassio_noauth_client.post("/api/hassio/{}".format(build_type))
# Check we got right response
assert resp.status == 401
@asyncio.coroutine
@pytest.mark.parametrize(
"build_type",
[
@ -45,61 +42,60 @@ def test_auth_required_forward_request(hassio_noauth_client, build_type):
"app/app.js",
],
)
def test_forward_request_no_auth_for_panel(hassio_client, build_type, aioclient_mock):
async def test_forward_request_no_auth_for_panel(
hassio_client, build_type, aioclient_mock
):
"""Test no auth needed for ."""
aioclient_mock.get("http://127.0.0.1/{}".format(build_type), text="response")
resp = yield from hassio_client.get("/api/hassio/{}".format(build_type))
resp = await hassio_client.get("/api/hassio/{}".format(build_type))
# Check we got right response
assert resp.status == 200
body = yield from resp.text()
body = await resp.text()
assert body == "response"
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
@asyncio.coroutine
def test_forward_request_no_auth_for_logo(hassio_client, aioclient_mock):
async def test_forward_request_no_auth_for_logo(hassio_client, aioclient_mock):
"""Test no auth needed for ."""
aioclient_mock.get("http://127.0.0.1/addons/bl_b392/logo", text="response")
resp = yield from hassio_client.get("/api/hassio/addons/bl_b392/logo")
resp = await hassio_client.get("/api/hassio/addons/bl_b392/logo")
# Check we got right response
assert resp.status == 200
body = yield from resp.text()
body = await resp.text()
assert body == "response"
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
@asyncio.coroutine
def test_forward_log_request(hassio_client, aioclient_mock):
async def test_forward_log_request(hassio_client, aioclient_mock):
"""Test fetching normal log path doesn't remove ANSI color escape codes."""
aioclient_mock.get("http://127.0.0.1/beer/logs", text="\033[32mresponse\033[0m")
resp = yield from hassio_client.get("/api/hassio/beer/logs")
resp = await hassio_client.get("/api/hassio/beer/logs")
# Check we got right response
assert resp.status == 200
body = yield from resp.text()
body = await resp.text()
assert body == "\033[32mresponse\033[0m"
# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
@asyncio.coroutine
def test_bad_gateway_when_cannot_find_supervisor(hassio_client):
async def test_bad_gateway_when_cannot_find_supervisor(hassio_client):
"""Test we get a bad gateway error if we can't find supervisor."""
with patch(
"homeassistant.components.hassio.http.async_timeout.timeout",
side_effect=asyncio.TimeoutError,
):
resp = yield from hassio_client.get("/api/hassio/addons/test/info")
resp = await hassio_client.get("/api/hassio/addons/test/info")
assert resp.status == 502

View File

@ -1,5 +1,4 @@
"""The tests for the hassio component."""
import asyncio
import os
from unittest.mock import Mock, patch
@ -30,11 +29,10 @@ def mock_all(aioclient_mock):
)
@asyncio.coroutine
def test_setup_api_ping(hass, aioclient_mock):
async def test_setup_api_ping(hass, aioclient_mock):
"""Test setup with API ping."""
with patch.dict(os.environ, MOCK_ENVIRON):
result = yield from async_setup_component(hass, "hassio", {})
result = await async_setup_component(hass, "hassio", {})
assert result
assert aioclient_mock.call_count == 5
@ -68,11 +66,10 @@ async def test_setup_api_panel(hass, aioclient_mock):
}
@asyncio.coroutine
def test_setup_api_push_api_data(hass, aioclient_mock):
async def test_setup_api_push_api_data(hass, aioclient_mock):
"""Test setup with API push."""
with patch.dict(os.environ, MOCK_ENVIRON):
result = yield from async_setup_component(
result = await async_setup_component(
hass, "hassio", {"http": {"server_port": 9999}, "hassio": {}}
)
assert result
@ -83,11 +80,10 @@ def test_setup_api_push_api_data(hass, aioclient_mock):
assert aioclient_mock.mock_calls[1][2]["watchdog"]
@asyncio.coroutine
def test_setup_api_push_api_data_server_host(hass, aioclient_mock):
async def test_setup_api_push_api_data_server_host(hass, aioclient_mock):
"""Test setup with API push with active server host."""
with patch.dict(os.environ, MOCK_ENVIRON):
result = yield from async_setup_component(
result = await async_setup_component(
hass,
"hassio",
{"http": {"server_port": 9999, "server_host": "127.0.0.1"}, "hassio": {}},
@ -175,45 +171,41 @@ async def test_setup_core_push_timezone(hass, aioclient_mock):
assert aioclient_mock.mock_calls[-1][2]["timezone"] == "America/New_York"
@asyncio.coroutine
def test_setup_hassio_no_additional_data(hass, aioclient_mock):
async def test_setup_hassio_no_additional_data(hass, aioclient_mock):
"""Test setup with API push default data."""
with patch.dict(os.environ, MOCK_ENVIRON), patch.dict(
os.environ, {"HASSIO_TOKEN": "123456"}
):
result = yield from async_setup_component(hass, "hassio", {"hassio": {}})
result = await async_setup_component(hass, "hassio", {"hassio": {}})
assert result
assert aioclient_mock.call_count == 5
assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456"
@asyncio.coroutine
def test_fail_setup_without_environ_var(hass):
async def test_fail_setup_without_environ_var(hass):
"""Fail setup if no environ variable set."""
with patch.dict(os.environ, {}, clear=True):
result = yield from async_setup_component(hass, "hassio", {})
result = await async_setup_component(hass, "hassio", {})
assert not result
@asyncio.coroutine
def test_warn_when_cannot_connect(hass, caplog):
async def test_warn_when_cannot_connect(hass, caplog):
"""Fail warn when we cannot connect."""
with patch.dict(os.environ, MOCK_ENVIRON), patch(
"homeassistant.components.hassio.HassIO.is_connected",
Mock(return_value=mock_coro(None)),
):
result = yield from async_setup_component(hass, "hassio", {})
result = await async_setup_component(hass, "hassio", {})
assert result
assert hass.components.hassio.is_hassio()
assert "Not connected with Hass.io / system to busy!" in caplog.text
@asyncio.coroutine
def test_service_register(hassio_env, hass):
async def test_service_register(hassio_env, hass):
"""Check if service will be setup."""
assert (yield from async_setup_component(hass, "hassio", {}))
assert await async_setup_component(hass, "hassio", {})
assert hass.services.has_service("hassio", "addon_start")
assert hass.services.has_service("hassio", "addon_stop")
assert hass.services.has_service("hassio", "addon_restart")
@ -227,10 +219,9 @@ def test_service_register(hassio_env, hass):
assert hass.services.has_service("hassio", "restore_partial")
@asyncio.coroutine
def test_service_calls(hassio_env, hass, aioclient_mock):
async def test_service_calls(hassio_env, hass, aioclient_mock):
"""Call service and check the API calls behind that."""
assert (yield from async_setup_component(hass, "hassio", {}))
assert await async_setup_component(hass, "hassio", {})
aioclient_mock.post("http://127.0.0.1/addons/test/start", json={"result": "ok"})
aioclient_mock.post("http://127.0.0.1/addons/test/stop", json={"result": "ok"})
@ -247,30 +238,30 @@ def test_service_calls(hassio_env, hass, aioclient_mock):
"http://127.0.0.1/snapshots/test/restore/partial", json={"result": "ok"}
)
yield from hass.services.async_call("hassio", "addon_start", {"addon": "test"})
yield from hass.services.async_call("hassio", "addon_stop", {"addon": "test"})
yield from hass.services.async_call("hassio", "addon_restart", {"addon": "test"})
yield from hass.services.async_call(
await hass.services.async_call("hassio", "addon_start", {"addon": "test"})
await hass.services.async_call("hassio", "addon_stop", {"addon": "test"})
await hass.services.async_call("hassio", "addon_restart", {"addon": "test"})
await hass.services.async_call(
"hassio", "addon_stdin", {"addon": "test", "input": "test"}
)
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert aioclient_mock.call_count == 7
assert aioclient_mock.mock_calls[-1][2] == "test"
yield from hass.services.async_call("hassio", "host_shutdown", {})
yield from hass.services.async_call("hassio", "host_reboot", {})
yield from hass.async_block_till_done()
await hass.services.async_call("hassio", "host_shutdown", {})
await hass.services.async_call("hassio", "host_reboot", {})
await hass.async_block_till_done()
assert aioclient_mock.call_count == 9
yield from hass.services.async_call("hassio", "snapshot_full", {})
yield from hass.services.async_call(
await hass.services.async_call("hassio", "snapshot_full", {})
await hass.services.async_call(
"hassio",
"snapshot_partial",
{"addons": ["test"], "folders": ["ssl"], "password": "123456"},
)
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert aioclient_mock.call_count == 11
assert aioclient_mock.mock_calls[-1][2] == {
@ -279,8 +270,8 @@ def test_service_calls(hassio_env, hass, aioclient_mock):
"password": "123456",
}
yield from hass.services.async_call("hassio", "restore_full", {"snapshot": "test"})
yield from hass.services.async_call(
await hass.services.async_call("hassio", "restore_full", {"snapshot": "test"})
await hass.services.async_call(
"hassio",
"restore_partial",
{
@ -291,7 +282,7 @@ def test_service_calls(hassio_env, hass, aioclient_mock):
"password": "123456",
},
)
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert aioclient_mock.call_count == 13
assert aioclient_mock.mock_calls[-1][2] == {
@ -302,29 +293,28 @@ def test_service_calls(hassio_env, hass, aioclient_mock):
}
@asyncio.coroutine
def test_service_calls_core(hassio_env, hass, aioclient_mock):
async def test_service_calls_core(hassio_env, hass, aioclient_mock):
"""Call core service and check the API calls behind that."""
assert (yield from async_setup_component(hass, "hassio", {}))
assert await async_setup_component(hass, "hassio", {})
aioclient_mock.post("http://127.0.0.1/homeassistant/restart", json={"result": "ok"})
aioclient_mock.post("http://127.0.0.1/homeassistant/stop", json={"result": "ok"})
yield from hass.services.async_call("homeassistant", "stop")
yield from hass.async_block_till_done()
await hass.services.async_call("homeassistant", "stop")
await hass.async_block_till_done()
assert aioclient_mock.call_count == 4
yield from hass.services.async_call("homeassistant", "check_config")
yield from hass.async_block_till_done()
await hass.services.async_call("homeassistant", "check_config")
await hass.async_block_till_done()
assert aioclient_mock.call_count == 4
with patch(
"homeassistant.config.async_check_ha_config_file", return_value=mock_coro()
) as mock_check_config:
yield from hass.services.async_call("homeassistant", "restart")
yield from hass.async_block_till_done()
await hass.services.async_call("homeassistant", "restart")
await hass.async_block_till_done()
assert mock_check_config.called
assert aioclient_mock.call_count == 5