Skip to content

Commit

Permalink
Update test suite to give more helpful error message and fix test by …
Browse files Browse the repository at this point in the history
…enabling built in bridge

Signed-off-by: James Rhodes <jarhodes314@gmail.com>
  • Loading branch information
jarhodes314 committed May 29, 2024
1 parent df59548 commit 299627a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 54 deletions.
109 changes: 55 additions & 54 deletions tests/RobotFramework/libraries/ThinEdgeIO/ThinEdgeIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
from DeviceLibrary import DeviceLibrary, DeviceAdapter
from Cumulocity import Cumulocity, retry


relativetime_ = Union[datetime, str]


devices_lib = DeviceLibrary()
c8y_lib = Cumulocity()


logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s %(module)s -%(levelname)s- %(message)s"
)
Expand Down Expand Up @@ -56,11 +53,11 @@ class ThinEdgeIO(DeviceLibrary):
"""ThinEdgeIO Library"""

def __init__(
self,
image: str = DeviceLibrary.DEFAULT_IMAGE,
adapter: str = None,
bootstrap_script: str = DeviceLibrary.DEFAULT_BOOTSTRAP_SCRIPT,
**kwargs,
self,
image: str = DeviceLibrary.DEFAULT_IMAGE,
adapter: str = None,
bootstrap_script: str = DeviceLibrary.DEFAULT_BOOTSTRAP_SCRIPT,
**kwargs,
):
super().__init__(
image=image, adapter=adapter, bootstrap_script=bootstrap_script, **kwargs
Expand Down Expand Up @@ -147,7 +144,7 @@ def get_debian_architecture(self):

@keyword("Get Logs")
def get_logs(
self, name: str = None, date_from: Union[datetime, float] = None, show=True
self, name: str = None, date_from: Union[datetime, float] = None, show=True
):
"""Get device logs (override base class method to add additional debug info)
Expand Down Expand Up @@ -224,11 +221,14 @@ def _hide_sensitive_factory(self):
# This is fragile and should be improved upon once a more suitable/robust method of logging and querying
# the mqtt messages is found.
token_replace_pattern = re.compile(r"\{.+$")

def _hide(line: str) -> str:
if C8Y_TOKEN_TOPIC in line and "71," in line:
line_sensitive = token_replace_pattern.sub(f"(redacted log entry): Received token: topic={C8Y_TOKEN_TOPIC}, message=71,<redacted>", line)
line_sensitive = token_replace_pattern.sub(
f"(redacted log entry): Received token: topic={C8Y_TOKEN_TOPIC}, message=71,<redacted>", line)
return line_sensitive
return line

return _hide

def log_operations(self, mo_id: str, status: str = None):
Expand All @@ -250,7 +250,7 @@ def log_operations(self, mo_id: str, status: str = None):
log_method = (
log.info
if operation.status
in (operation.Status.SUCCESSFUL, operation.Status.FAILED)
in (operation.Status.SUCCESSFUL, operation.Status.FAILED)
else log.warning
)
log_method(
Expand Down Expand Up @@ -429,12 +429,12 @@ def tedge_disconnect_connect(self, mapper: str = "c8y", sleep: float = 0.0):
# Assert presence of a topic (with timeout)
#
def mqtt_match_messages(
self,
topic: str,
message_pattern: str = None,
date_from: relativetime_ = None,
date_to: relativetime_ = None,
**kwargs,
self,
topic: str,
message_pattern: str = None,
date_from: relativetime_ = None,
date_to: relativetime_ = None,
**kwargs,
) -> List[Dict[str, Any]]:
"""Match mqtt messages using different types of filters
Expand Down Expand Up @@ -464,7 +464,7 @@ def mqtt_match_messages(
message = json.loads(line)
if "message" in message:
if message_pattern_re is None or message_pattern_re.match(
message["message"]["payload"]
message["message"]["payload"]
):
messages.append(message)
except Exception as ex:
Expand All @@ -477,7 +477,7 @@ def mqtt_match_messages(
item
for item in messages
if not topic
or (topic and mqtt_topic_match(mqtt_matcher, item["message"]["topic"]))
or (topic and mqtt_topic_match(mqtt_matcher, item["message"]["topic"]))
]
return matching

Expand All @@ -501,7 +501,7 @@ def assert_service_health_status_down(self, service: str, device: str = "main")

@keyword("Service Health Status Should Be Equal")
def assert_service_health_status_equal(
self, service: str, status: str, device: str = "main"
self, service: str, status: str, device: str = "main"
) -> Dict[str, Any]:
return self._assert_health_status(service, status=status, device=device)

Expand Down Expand Up @@ -541,11 +541,11 @@ def _assert_health_status(self, service: str, status: str, device: str = "main")

@keyword("Setup")
def setup(
self,
skip_bootstrap: bool = None,
cleanup: bool = None,
adapter: str = None,
wait_for_healthy: bool = True,
self,
skip_bootstrap: bool = None,
cleanup: bool = None,
adapter: str = None,
wait_for_healthy: bool = True,
) -> str:
serial_sn = super().setup(skip_bootstrap, cleanup, adapter)

Expand All @@ -554,15 +554,15 @@ def setup(
return serial_sn

def _assert_mqtt_topic_messages(
self,
topic: str,
date_from: relativetime_ = None,
date_to: relativetime_ = None,
minimum: int = 1,
maximum: int = None,
message_pattern: str = None,
message_contains: str = None,
**kwargs,
self,
topic: str,
date_from: relativetime_ = None,
date_to: relativetime_ = None,
minimum: int = 1,
maximum: int = None,
message_pattern: str = None,
message_contains: str = None,
**kwargs,
) -> List[Dict[str, Any]]:
# log.info("Checking mqtt messages for topic: %s", topic)
if message_contains:
Expand All @@ -583,13 +583,13 @@ def _assert_mqtt_topic_messages(

if minimum is not None:
assert (
len(messages) >= minimum
), f"Matching messages is less than minimum.\nwanted: {minimum}\ngot: {len(messages)}\n\nmessages:\n{messages}"
len(messages) >= minimum
), f"Matching messages on topic '{topic}' is less than minimum.\nwanted: {minimum}\ngot: {len(messages)}\n\nmessages:\n{messages}"

if maximum is not None:
assert (
len(messages) <= maximum
), f"Matching messages is greater than maximum.\nwanted: {maximum}\ngot: {len(messages)}\n\nmessages:\n{messages}"
len(messages) <= maximum
), f"Matching messages on topic '{topic}' is greater than maximum.\nwanted: {maximum}\ngot: {len(messages)}\n\nmessages:\n{messages}"

return messages

Expand All @@ -613,15 +613,15 @@ def log_mqtt_messages(self, topic: str = "#", date_from: Union[datetime, float]

@keyword("Should Have MQTT Messages")
def mqtt_should_have_topic(
self,
topic: str,
date_from: relativetime_ = None,
date_to: relativetime_ = None,
message_pattern: str = None,
message_contains: str = None,
minimum: int = 1,
maximum: int = None,
**kwargs,
self,
topic: str,
date_from: relativetime_ = None,
date_to: relativetime_ = None,
message_pattern: str = None,
message_contains: str = None,
minimum: int = 1,
maximum: int = None,
**kwargs,
) -> List[Dict[str, Any]]:
"""
Check for the presence of a topic
Expand Down Expand Up @@ -660,11 +660,11 @@ def mqtt_should_have_topic(

@keyword("Register Child Device")
def register_child(
self,
parent_name: str,
child_name: str,
supported_operations: Union[List[str], str] = None,
name: str = None,
self,
parent_name: str,
child_name: str,
supported_operations: Union[List[str], str] = None,
name: str = None,
):
"""
Register a child device to a parent along with a given list of supported operations
Expand Down Expand Up @@ -700,7 +700,7 @@ def set_restart_command(self, command: str, **kwargs):
)

@keyword("Set Restart Timeout")
def set_restart_timeout(self, value: Union[str,int], **kwargs):
def set_restart_timeout(self, value: Union[str, int], **kwargs):
"""Set the restart timeout interval in seconds for how long thin-edge.io
should wait to for a device restart to happen.
Expand All @@ -714,7 +714,7 @@ def set_restart_timeout(self, value: Union[str,int], **kwargs):
command = "sed -i -e '/reboot_timeout_seconds/d' /etc/tedge/system.toml"
else:
command = f"sed -i -e '/reboot_timeout_seconds/d' -e '/reboot =/a reboot_timeout_seconds = {value}' /etc/tedge/system.toml",
self.execute_command(command, **kwargs,)
self.execute_command(command, **kwargs, )

@keyword("Escape Pattern")
def regexp_escape(self, pattern: str, is_json: bool = False):
Expand All @@ -733,6 +733,7 @@ def regexp_escape(self, pattern: str, is_json: bool = False):

return value


def to_date(value: relativetime_) -> datetime:
if isinstance(value, datetime):
return value
Expand Down
2 changes: 2 additions & 0 deletions tests/RobotFramework/tests/cumulocity/jwt/jwt_request.robot
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ Custom Setup
${DEVICE_SN}= Setup
Set Suite Variable $DEVICE_SN
Device Should Exist ${DEVICE_SN}
Execute Command tedge config set mqtt.bridge.built_in true
Execute Command tedge reconnect c8y
Should Have MQTT Messages te/device/main/service/tedge-mapper-bridge-c8y/status/health
Sleep 1s wait just in case that the server responds to already sent messages

0 comments on commit 299627a

Please sign in to comment.