-
Notifications
You must be signed in to change notification settings - Fork 3
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
✨ Allowing pytest cli arguments for host and port #8
Merged
amotl
merged 2 commits into
mqtt-tools:main
from
zedfmario:feature/mqtt-host-configurable
Mar 29, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright (c) 2020-2022 Andreas Motl <[email protected]> | ||
# Copyright (c) 2020-2022 Richard Pobering <[email protected]> | ||
# | ||
|
@@ -20,6 +21,7 @@ | |
from pytest_docker_fixtures import images | ||
from pytest_docker_fixtures.containers._base import BaseImage | ||
|
||
from pytest_mqtt.model import MqttSettings | ||
from pytest_mqtt.util import probe_tcp_connect | ||
|
||
images.settings["mosquitto"] = { | ||
|
@@ -36,7 +38,10 @@ | |
class Mosquitto(BaseImage): | ||
|
||
name = "mosquitto" | ||
port = 1883 | ||
|
||
def __init__(self, host: str = "localhost", port: int = 1883) -> None: | ||
self.host = host | ||
self.port = port | ||
|
||
def check(self): | ||
# TODO: Add real implementation. | ||
|
@@ -71,16 +76,18 @@ | |
mosquitto_image = Mosquitto() | ||
|
||
|
||
def is_mosquitto_running() -> bool: | ||
return probe_tcp_connect("localhost", 1883) | ||
def is_mosquitto_running(host: str, port: int) -> bool: | ||
return probe_tcp_connect(host, port) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def mosquitto(): | ||
def mosquitto(mqtt_settings: MqttSettings): | ||
|
||
host, port = mqtt_settings.host, mqtt_settings.port | ||
|
||
# Gracefully skip spinning up the Docker container if Mosquitto is already running. | ||
if is_mosquitto_running(): | ||
yield "localhost", 1883 | ||
if is_mosquitto_running(host, port): | ||
yield host, port | ||
return | ||
|
||
# Spin up Mosquitto container. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pytest | ||
|
||
from pytest_mqtt.model import MqttSettings | ||
|
||
|
||
def pytest_addoption(parser) -> None: | ||
parser.addoption("--mqtt-host", action="store", type=str, default="localhost", help="MQTT host name") | ||
parser.addoption("--mqtt-port", action="store", type=int, default=1883, help="MQTT port number") | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def mqtt_settings(pytestconfig) -> MqttSettings: | ||
return MqttSettings( | ||
host=pytestconfig.getoption("--mqtt-host"), | ||
port=pytestconfig.getoption("--mqtt-port"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When acquiring configuration options like this, they can be provided through three different means, see usage of
capmqtt_decode_utf8
.Acquiring
capmqtt_host
andcapmqtt_port
could be implemented in the very same way, so they are not too special?