diff --git a/tests/common.py b/tests/common.py index 0751281874b9..32f64ba29491 100644 --- a/tests/common.py +++ b/tests/common.py @@ -13,9 +13,11 @@ from homeassistant.helpers.entity import ToggleEntity from homeassistant.const import ( STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, EVENT_TIME_CHANGED, EVENT_STATE_CHANGED, EVENT_PLATFORM_DISCOVERED, ATTR_SERVICE, - ATTR_DISCOVERED) + ATTR_DISCOVERED, SERVER_PORT) from homeassistant.components import sun, mqtt +_TEST_INSTANCE_PORT = SERVER_PORT + def get_test_config_dir(): """ Returns a path to a test config dir. """ @@ -43,6 +45,18 @@ def get_test_home_assistant(num_threads=None): return hass +def get_test_instance_port(): + """Return unused port for running test instance. + + The socket that holds the default port does not get released when we stop + HA in a different test case. Until I have figured out what is going on, + let's run each test on a different port. + """ + global _TEST_INSTANCE_PORT + _TEST_INSTANCE_PORT += 1 + return _TEST_INSTANCE_PORT + + def mock_service(hass, domain, service): """ Sets up a fake service. diff --git a/tests/components/device_tracker/test_locative.py b/tests/components/device_tracker/test_locative.py index b64fbda83450..32a63d0962f9 100644 --- a/tests/components/device_tracker/test_locative.py +++ b/tests/components/device_tracker/test_locative.py @@ -13,9 +13,9 @@ from homeassistant import bootstrap, const import homeassistant.components.device_tracker as device_tracker import homeassistant.components.http as http -from tests.common import get_test_home_assistant +from tests.common import get_test_home_assistant, get_test_instance_port -SERVER_PORT = 8126 +SERVER_PORT = get_test_instance_port() HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT) hass = None @@ -128,7 +128,8 @@ class TestLocative(unittest.TestCase): # Enter the Home req = requests.get(_url(data)) self.assertEqual(200, req.status_code) - state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state + state_name = hass.states.get('{}.{}'.format('device_tracker', + data['device'])).state self.assertEqual(state_name, 'home') data['id'] = 'HOME' @@ -137,7 +138,8 @@ class TestLocative(unittest.TestCase): # Exit Home req = requests.get(_url(data)) self.assertEqual(200, req.status_code) - state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state + state_name = hass.states.get('{}.{}'.format('device_tracker', + data['device'])).state self.assertEqual(state_name, 'not_home') data['id'] = 'hOmE' @@ -146,7 +148,8 @@ class TestLocative(unittest.TestCase): # Enter Home again req = requests.get(_url(data)) self.assertEqual(200, req.status_code) - state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state + state_name = hass.states.get('{}.{}'.format('device_tracker', + data['device'])).state self.assertEqual(state_name, 'home') data['trigger'] = 'exit' @@ -154,7 +157,8 @@ class TestLocative(unittest.TestCase): # Exit Home req = requests.get(_url(data)) self.assertEqual(200, req.status_code) - state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state + state_name = hass.states.get('{}.{}'.format('device_tracker', + data['device'])).state self.assertEqual(state_name, 'not_home') data['id'] = 'work' @@ -163,7 +167,8 @@ class TestLocative(unittest.TestCase): # Enter Work req = requests.get(_url(data)) self.assertEqual(200, req.status_code) - state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state + state_name = hass.states.get('{}.{}'.format('device_tracker', + data['device'])).state self.assertEqual(state_name, 'work') def test_exit_after_enter(self, update_config): @@ -181,7 +186,8 @@ class TestLocative(unittest.TestCase): req = requests.get(_url(data)) self.assertEqual(200, req.status_code) - state = hass.states.get('{}.{}'.format('device_tracker', data['device'])) + state = hass.states.get('{}.{}'.format('device_tracker', + data['device'])) self.assertEqual(state.state, 'home') data['id'] = 'Work' @@ -190,7 +196,8 @@ class TestLocative(unittest.TestCase): req = requests.get(_url(data)) self.assertEqual(200, req.status_code) - state = hass.states.get('{}.{}'.format('device_tracker', data['device'])) + state = hass.states.get('{}.{}'.format('device_tracker', + data['device'])) self.assertEqual(state.state, 'work') data['id'] = 'Home' @@ -200,7 +207,8 @@ class TestLocative(unittest.TestCase): req = requests.get(_url(data)) self.assertEqual(200, req.status_code) - state = hass.states.get('{}.{}'.format('device_tracker', data['device'])) + state = hass.states.get('{}.{}'.format('device_tracker', + data['device'])) self.assertEqual(state.state, 'work') def test_exit_first(self, update_config): @@ -218,5 +226,6 @@ class TestLocative(unittest.TestCase): req = requests.get(_url(data)) self.assertEqual(200, req.status_code) - state = hass.states.get('{}.{}'.format('device_tracker', data['device'])) + state = hass.states.get('{}.{}'.format('device_tracker', + data['device'])) self.assertEqual(state.state, 'not_home') diff --git a/tests/components/test_alexa.py b/tests/components/test_alexa.py index 60dbcbb0157c..a309bf736684 100644 --- a/tests/components/test_alexa.py +++ b/tests/components/test_alexa.py @@ -15,17 +15,17 @@ from homeassistant import bootstrap, const import homeassistant.core as ha from homeassistant.components import alexa, http +from tests.common import get_test_instance_port + API_PASSWORD = "test1234" - -# Somehow the socket that holds the default port does not get released -# when we close down HA in a different test case. Until I have figured -# out what is going on, let's run this test on a different port. -SERVER_PORT = 8119 - +SERVER_PORT = get_test_instance_port() API_URL = "http://127.0.0.1:{}{}".format(SERVER_PORT, alexa.API_ENDPOINT) - HA_HEADERS = {const.HTTP_HEADER_HA_AUTH: API_PASSWORD} +SESSION_ID = 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000' +APPLICATION_ID = 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe' +REQUEST_ID = 'amzn1.echo-api.request.0000000-0000-0000-0000-00000000000' + hass = None calls = [] @@ -53,10 +53,16 @@ def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name 'type': 'plaintext', 'text': """ - {%- if is_state('device_tracker.paulus', 'home') and is_state('device_tracker.anne_therese', 'home') -%} + {%- if is_state('device_tracker.paulus', 'home') + and is_state('device_tracker.anne_therese', + 'home') -%} You are both home, you silly {%- else -%} - Anne Therese is at {{ states("device_tracker.anne_therese") }} and Paulus is at {{ states("device_tracker.paulus") }} + Anne Therese is at {{ + states("device_tracker.anne_therese") + }} and Paulus is at {{ + states("device_tracker.paulus") + }} {% endif %} """, } @@ -105,9 +111,9 @@ class TestAlexa(unittest.TestCase): 'version': '1.0', 'session': { 'new': True, - 'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000', + 'sessionId': SESSION_ID, 'application': { - 'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe' + 'applicationId': APPLICATION_ID }, 'attributes': {}, 'user': { @@ -116,7 +122,7 @@ class TestAlexa(unittest.TestCase): }, 'request': { 'type': 'LaunchRequest', - 'requestId': 'amzn1.echo-api.request.0000000-0000-0000-0000-00000000000', + 'requestId': REQUEST_ID, 'timestamp': '2015-05-13T12:34:56Z' } } @@ -130,9 +136,9 @@ class TestAlexa(unittest.TestCase): 'version': '1.0', 'session': { 'new': False, - 'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000', + 'sessionId': SESSION_ID, 'application': { - 'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe' + 'applicationId': APPLICATION_ID }, 'attributes': { 'supportedHoroscopePeriods': { @@ -147,7 +153,7 @@ class TestAlexa(unittest.TestCase): }, 'request': { 'type': 'IntentRequest', - 'requestId': ' amzn1.echo-api.request.0000000-0000-0000-0000-00000000000', + 'requestId': REQUEST_ID, 'timestamp': '2015-05-13T12:34:56Z', 'intent': { 'name': 'GetZodiacHoroscopeIntent', @@ -162,7 +168,8 @@ class TestAlexa(unittest.TestCase): } req = _req(data) self.assertEqual(200, req.status_code) - text = req.json().get('response', {}).get('outputSpeech', {}).get('text') + text = req.json().get('response', {}).get('outputSpeech', + {}).get('text') self.assertEqual('You told us your sign is virgo.', text) def test_intent_request_with_slots_but_no_value(self): @@ -170,9 +177,9 @@ class TestAlexa(unittest.TestCase): 'version': '1.0', 'session': { 'new': False, - 'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000', + 'sessionId': SESSION_ID, 'application': { - 'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe' + 'applicationId': APPLICATION_ID }, 'attributes': { 'supportedHoroscopePeriods': { @@ -187,7 +194,7 @@ class TestAlexa(unittest.TestCase): }, 'request': { 'type': 'IntentRequest', - 'requestId': ' amzn1.echo-api.request.0000000-0000-0000-0000-00000000000', + 'requestId': REQUEST_ID, 'timestamp': '2015-05-13T12:34:56Z', 'intent': { 'name': 'GetZodiacHoroscopeIntent', @@ -201,7 +208,8 @@ class TestAlexa(unittest.TestCase): } req = _req(data) self.assertEqual(200, req.status_code) - text = req.json().get('response', {}).get('outputSpeech', {}).get('text') + text = req.json().get('response', {}).get('outputSpeech', + {}).get('text') self.assertEqual('You told us your sign is .', text) def test_intent_request_without_slots(self): @@ -209,9 +217,9 @@ class TestAlexa(unittest.TestCase): 'version': '1.0', 'session': { 'new': False, - 'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000', + 'sessionId': SESSION_ID, 'application': { - 'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe' + 'applicationId': APPLICATION_ID }, 'attributes': { 'supportedHoroscopePeriods': { @@ -226,7 +234,7 @@ class TestAlexa(unittest.TestCase): }, 'request': { 'type': 'IntentRequest', - 'requestId': ' amzn1.echo-api.request.0000000-0000-0000-0000-00000000000', + 'requestId': REQUEST_ID, 'timestamp': '2015-05-13T12:34:56Z', 'intent': { 'name': 'WhereAreWeIntent', @@ -235,16 +243,19 @@ class TestAlexa(unittest.TestCase): } req = _req(data) self.assertEqual(200, req.status_code) - text = req.json().get('response', {}).get('outputSpeech', {}).get('text') + text = req.json().get('response', {}).get('outputSpeech', + {}).get('text') - self.assertEqual('Anne Therese is at unknown and Paulus is at unknown', text) + self.assertEqual('Anne Therese is at unknown and Paulus is at unknown', + text) hass.states.set('device_tracker.paulus', 'home') hass.states.set('device_tracker.anne_therese', 'home') req = _req(data) self.assertEqual(200, req.status_code) - text = req.json().get('response', {}).get('outputSpeech', {}).get('text') + text = req.json().get('response', {}).get('outputSpeech', + {}).get('text') self.assertEqual('You are both home, you silly', text) def test_intent_request_calling_service(self): @@ -252,9 +263,9 @@ class TestAlexa(unittest.TestCase): 'version': '1.0', 'session': { 'new': False, - 'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000', + 'sessionId': SESSION_ID, 'application': { - 'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe' + 'applicationId': APPLICATION_ID }, 'attributes': {}, 'user': { @@ -263,7 +274,7 @@ class TestAlexa(unittest.TestCase): }, 'request': { 'type': 'IntentRequest', - 'requestId': ' amzn1.echo-api.request.0000000-0000-0000-0000-00000000000', + 'requestId': REQUEST_ID, 'timestamp': '2015-05-13T12:34:56Z', 'intent': { 'name': 'CallServiceIntent', @@ -285,9 +296,9 @@ class TestAlexa(unittest.TestCase): 'version': '1.0', 'session': { 'new': False, - 'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000', + 'sessionId': SESSION_ID, 'application': { - 'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe' + 'applicationId': APPLICATION_ID }, 'attributes': { 'supportedHoroscopePeriods': { @@ -302,7 +313,7 @@ class TestAlexa(unittest.TestCase): }, 'request': { 'type': 'SessionEndedRequest', - 'requestId': 'amzn1.echo-api.request.0000000-0000-0000-0000-00000000000', + 'requestId': REQUEST_ID, 'timestamp': '2015-05-13T12:34:56Z', 'reason': 'USER_INITIATED' } diff --git a/tests/components/test_api.py b/tests/components/test_api.py index e2e6341c4d61..21a8b7e19baa 100644 --- a/tests/components/test_api.py +++ b/tests/components/test_api.py @@ -17,15 +17,11 @@ from homeassistant import bootstrap, const import homeassistant.core as ha import homeassistant.components.http as http +from tests.common import get_test_instance_port + API_PASSWORD = "test1234" - -# Somehow the socket that holds the default port does not get released -# when we close down HA in a different test case. Until I have figured -# out what is going on, let's run this test on a different port. -SERVER_PORT = 8120 - +SERVER_PORT = get_test_instance_port() HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT) - HA_HEADERS = {const.HTTP_HEADER_HA_AUTH: API_PASSWORD} hass = None @@ -386,7 +382,7 @@ class TestAPI(unittest.TestCase): data=json.dumps({ 'api_password': 'bla-di-bla', 'host': '127.0.0.1', - 'port': '8125' + 'port': get_test_instance_port() }), headers=HA_HEADERS) self.assertEqual(422, req.status_code) diff --git a/tests/components/test_frontend.py b/tests/components/test_frontend.py index 0f4a2dc41347..fcd4daddadbb 100644 --- a/tests/components/test_frontend.py +++ b/tests/components/test_frontend.py @@ -16,15 +16,11 @@ import homeassistant.bootstrap as bootstrap import homeassistant.components.http as http from homeassistant.const import HTTP_HEADER_HA_AUTH +from tests.common import get_test_instance_port + API_PASSWORD = "test1234" - -# Somehow the socket that holds the default port does not get released -# when we close down HA in a different test case. Until I have figured -# out what is going on, let's run this test on a different port. -SERVER_PORT = 8121 - +SERVER_PORT = get_test_instance_port() HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT) - HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD} hass = None diff --git a/tests/components/test_group.py b/tests/components/test_group.py index 2301a15f59ec..19ca2cbf7c5b 100644 --- a/tests/components/test_group.py +++ b/tests/components/test_group.py @@ -6,7 +6,6 @@ Tests the group compoments. """ # pylint: disable=protected-access,too-many-public-methods import unittest -import logging import homeassistant.core as ha from homeassistant.const import ( @@ -14,11 +13,6 @@ from homeassistant.const import ( import homeassistant.components.group as group -def setUpModule(): # pylint: disable=invalid-name - """ Setup to ignore group errors. """ - logging.disable(logging.CRITICAL) - - class TestComponentsGroup(unittest.TestCase): """ Tests homeassistant.components.group module. """ diff --git a/tests/test_remote.py b/tests/test_remote.py index bf6a916f22cf..4f2fb75e6c97 100644 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -3,8 +3,6 @@ tests.remote ~~~~~~~~~~~~ Tests Home Assistant remote methods and classes. -Uses port 8122 for master, 8123 for slave -Uses port 8125 as a port that nothing runs on """ # pylint: disable=protected-access,too-many-public-methods import unittest @@ -16,9 +14,13 @@ import homeassistant.remote as remote import homeassistant.components.http as http from homeassistant.const import HTTP_HEADER_HA_AUTH -API_PASSWORD = "test1234" +from tests.common import get_test_instance_port -HTTP_BASE_URL = "http://127.0.0.1:8122" +API_PASSWORD = "test1234" +MASTER_PORT = get_test_instance_port() +SLAVE_PORT = get_test_instance_port() +BROKEN_PORT = get_test_instance_port() +HTTP_BASE_URL = "http://127.0.0.1:{}".format(MASTER_PORT) HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD} @@ -44,25 +46,25 @@ def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name bootstrap.setup_component( hass, http.DOMAIN, {http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD, - http.CONF_SERVER_PORT: 8122}}) + http.CONF_SERVER_PORT: MASTER_PORT}}) bootstrap.setup_component(hass, 'api') hass.start() - master_api = remote.API("127.0.0.1", API_PASSWORD, 8122) + master_api = remote.API("127.0.0.1", API_PASSWORD, MASTER_PORT) # Start slave slave = remote.HomeAssistant(master_api) bootstrap.setup_component( slave, http.DOMAIN, {http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD, - http.CONF_SERVER_PORT: 8130}}) + http.CONF_SERVER_PORT: SLAVE_PORT}}) slave.start() # Setup API pointing at nothing - broken_api = remote.API("127.0.0.1", "", 8125) + broken_api = remote.API("127.0.0.1", "", BROKEN_PORT) def tearDownModule(): # pylint: disable=invalid-name @@ -83,7 +85,7 @@ class TestRemoteMethods(unittest.TestCase): self.assertEqual( remote.APIStatus.INVALID_PASSWORD, remote.validate_api( - remote.API("127.0.0.1", API_PASSWORD + "A", 8122))) + remote.API("127.0.0.1", API_PASSWORD + "A", MASTER_PORT))) self.assertEqual( remote.APIStatus.CANNOT_CONNECT, remote.validate_api(broken_api)) @@ -210,7 +212,7 @@ class TestRemoteClasses(unittest.TestCase): # Wrong port self.assertRaises( ha.HomeAssistantError, remote.HomeAssistant, - remote.API('127.0.0.1', API_PASSWORD, 8125)) + remote.API('127.0.0.1', API_PASSWORD, BROKEN_PORT)) def test_statemachine_init(self): """ Tests if remote.StateMachine copies all states on init. """