Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use loopback address in all tests #841

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class PatchableAPIClient(APIClient):
pass

cli = PatchableAPIClient(
address="1.2.3.4",
address="127.0.0.1",
port=6052,
password=None,
)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async def test_finish_connection_wraps_exceptions_as_unhandled_api_error(
) -> None:
"""Verify finish_connect re-wraps exceptions as UnhandledAPIError."""

cli = APIClient("1.2.3.4", 1234, None)
cli = APIClient("127.0.0.1", 1234, None)
with patch("aioesphomeapi.client.APIConnection", PatchableAPIConnection):
await cli.start_connection()

Expand All @@ -219,7 +219,7 @@ async def test_finish_connection_wraps_exceptions_as_unhandled_api_error(
@pytest.mark.asyncio
async def test_connection_released_if_connecting_is_cancelled() -> None:
"""Verify connection is unset if connecting is cancelled."""
cli = APIClient("1.2.3.4", 1234, None)
cli = APIClient("127.0.0.1", 1234, None)
asyncio.get_event_loop()

async def _start_connection_with_delay(*args, **kwargs):
Expand Down Expand Up @@ -930,7 +930,7 @@ class PatchableAPIClient(APIClient):
pass

cli = PatchableAPIClient(
address=Estr("1.2.3.4"),
address=Estr("127.0.0.1"),
port=6052,
password=None,
noise_psk=Estr("QRTIErOb/fcE9Ukd/5qA3RGYMn0Y+p06U58SCtOXvPc="),
Expand Down Expand Up @@ -966,7 +966,7 @@ class PatchableAPIClient(APIClient):
async def test_no_noise_psk():
"""Test not using a noise_psk."""
cli = APIClient(
address=Estr("1.2.3.4"),
address=Estr("127.0.0.1"),
port=6052,
password=None,
noise_psk=None,
Expand All @@ -982,7 +982,7 @@ async def test_no_noise_psk():
async def test_empty_noise_psk_or_expected_name():
"""Test an empty noise_psk is treated as None."""
cli = APIClient(
address=Estr("1.2.3.4"),
address=Estr("127.0.0.1"),
port=6052,
password=None,
noise_psk="",
Expand Down
6 changes: 3 additions & 3 deletions tests/test_log_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PatchableAPIClient(APIClient):
async_zeroconf = get_mock_async_zeroconf()

cli = PatchableAPIClient(
address=Estr("1.2.3.4"),
address=Estr("127.0.0.1"),
port=6052,
password=None,
noise_psk=None,
Expand Down Expand Up @@ -115,7 +115,7 @@ class PatchableAPIClient(APIClient):
async_zeroconf = get_mock_async_zeroconf()

cli = PatchableAPIClient(
address=Estr("1.2.3.4"),
address=Estr("127.0.0.1"),
port=6052,
password=None,
noise_psk=None,
Expand Down Expand Up @@ -195,7 +195,7 @@ class PatchableAPIClient(APIClient):
async_zeroconf = get_mock_async_zeroconf()

cli = PatchableAPIClient(
address=Estr("1.2.3.4"),
address=Estr("127.0.0.1"),
port=6052,
password=None,
noise_psk=None,
Expand Down
36 changes: 18 additions & 18 deletions tests/test_reconnect_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async def on_connect() -> None:
async def test_reconnect_logic_name_from_address():
"""Test that the name is set correctly from the address."""
cli = APIClient(
address="1.2.3.4",
address="127.0.0.1",
port=6052,
password=None,
)
Expand All @@ -119,14 +119,14 @@ async def on_connect() -> None:
on_connect=on_connect,
zeroconf_instance=get_mock_zeroconf(),
)
assert cli.log_name == "1.2.3.4"
assert cli.log_name == "127.0.0.1"


@pytest.mark.asyncio
async def test_reconnect_logic_name_from_name():
"""Test that the name is set correctly from the address."""
cli = APIClient(
address="1.2.3.4",
address="127.0.0.1",
port=6052,
password=None,
)
Expand All @@ -144,7 +144,7 @@ async def on_connect() -> None:
zeroconf_instance=get_mock_zeroconf(),
name="mydevice",
)
assert cli.log_name == "mydevice @ 1.2.3.4"
assert cli.log_name == "mydevice @ 127.0.0.1"


@pytest.mark.asyncio
Expand Down Expand Up @@ -201,7 +201,7 @@ async def on_connect_fail(connect_exception: Exception) -> None:
name="mydevice",
on_connect_error=on_connect_fail,
)
assert cli.log_name == "mydevice @ 1.2.3.4"
assert cli.log_name == "mydevice @ 127.0.0.1"

with patch.object(cli, "start_connection", side_effect=APIConnectionError):
await rl.start()
Expand Down Expand Up @@ -274,7 +274,7 @@ async def on_connect_fail(connect_exception: Exception) -> None:
name="mydevice",
on_connect_error=on_connect_fail,
)
assert cli.log_name == "mydevice @ 1.2.3.4"
assert cli.log_name == "mydevice @ 127.0.0.1"
caplog.clear()

with patch.object(cli, "start_connection", side_effect=APIConnectionError):
Expand All @@ -288,9 +288,9 @@ async def on_connect_fail(connect_exception: Exception) -> None:
assert len(on_connect_fail_called) == 1
assert isinstance(on_connect_fail_called[-1], APIConnectionError)
assert rl._connection_state is ReconnectLogicState.DISCONNECTED
assert "connect to ESPHome API for mydevice @ 1.2.3.4" in caplog.text
assert "connect to ESPHome API for mydevice @ 127.0.0.1" in caplog.text
for record in caplog.records:
if "connect to ESPHome API for mydevice @ 1.2.3.4" in record.message:
if "connect to ESPHome API for mydevice @ 127.0.0.1" in record.message:
assert record.levelno == logging.WARNING

caplog.clear()
Expand All @@ -307,9 +307,9 @@ async def on_connect_fail(connect_exception: Exception) -> None:
assert len(on_connect_fail_called) == 2
assert isinstance(on_connect_fail_called[-1], APIConnectionError)
assert rl._connection_state is ReconnectLogicState.DISCONNECTED
assert "connect to ESPHome API for mydevice @ 1.2.3.4" in caplog.text
assert "connect to ESPHome API for mydevice @ 127.0.0.1" in caplog.text
for record in caplog.records:
if "connect to ESPHome API for mydevice @ 1.2.3.4" in record.message:
if "connect to ESPHome API for mydevice @ 127.0.0.1" in record.message:
assert record.levelno == logging.DEBUG

caplog.clear()
Expand All @@ -320,7 +320,7 @@ async def on_connect_fail(connect_exception: Exception) -> None:
await asyncio.sleep(0)
await asyncio.sleep(0)

assert "connect to ESPHome API for mydevice @ 1.2.3.4" not in caplog.text
assert "connect to ESPHome API for mydevice @ 127.0.0.1" not in caplog.text
assert len(on_disconnect_called) == 0
assert len(on_connect_called) == 1
assert len(on_connect_fail_called) == 2
Expand Down Expand Up @@ -372,7 +372,7 @@ async def on_connect_fail(connect_exception: Exception) -> None:
_TYPE_A,
_CLASS_IN,
1000,
ip_address("1.2.3.4").packed,
ip_address("127.0.0.1").packed,
),
True,
ReconnectLogicState.READY,
Expand Down Expand Up @@ -403,7 +403,7 @@ async def test_reconnect_zeroconf(
name="mydevice",
on_connect_error=AsyncMock(),
)
assert cli.log_name == "mydevice @ 1.2.3.4"
assert cli.log_name == "mydevice @ 127.0.0.1"

with patch.object(
cli, "start_connection", side_effect=quick_connect_fail
Expand Down Expand Up @@ -474,7 +474,7 @@ async def test_reconnect_zeroconf_not_while_handshaking(
name="mydevice",
on_connect_error=AsyncMock(),
)
assert cli.log_name == "mydevice @ 1.2.3.4"
assert cli.log_name == "mydevice @ 127.0.0.1"

with patch.object(
cli, "start_connection", side_effect=quick_connect_fail
Expand Down Expand Up @@ -531,7 +531,7 @@ async def test_connect_task_not_cancelled_while_handshaking(
name="mydevice",
on_connect_error=AsyncMock(),
)
assert cli.log_name == "mydevice @ 1.2.3.4"
assert cli.log_name == "mydevice @ 127.0.0.1"

with patch.object(
cli, "start_connection", side_effect=quick_connect_fail
Expand Down Expand Up @@ -591,7 +591,7 @@ async def test_connect_aborts_if_stopped(
name="mydevice",
on_connect_error=AsyncMock(),
)
assert cli.log_name == "mydevice @ 1.2.3.4"
assert cli.log_name == "mydevice @ 127.0.0.1"

with patch.object(
cli, "start_connection", side_effect=quick_connect_fail
Expand Down Expand Up @@ -694,7 +694,7 @@ class PatchableAPIClient(APIClient):
async_zeroconf = get_mock_async_zeroconf()

cli = PatchableAPIClient(
address="1.2.3.4",
address="127.0.0.1",
port=6052,
password=None,
noise_psk=None,
Expand Down Expand Up @@ -770,7 +770,7 @@ class PatchableAPIClient(APIClient):
async_zeroconf = get_mock_async_zeroconf()

cli = PatchableAPIClient(
address="1.2.3.4",
address="127.0.0.1",
port=6052,
password=None,
noise_psk="",
Expand Down