From 60f7a6abb54fa056869f03a63831200aace2767b Mon Sep 17 00:00:00 2001 From: Bodong Yang Date: Thu, 29 Feb 2024 07:55:07 +0000 Subject: [PATCH] fix test --- tests/test_aws_iot_logger.py | 6 +++--- tests/test_configs.py | 5 +++++ tests/test_log_proxy_server.py | 31 +++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/tests/test_aws_iot_logger.py b/tests/test_aws_iot_logger.py index 8dd9fe9..29dfc47 100644 --- a/tests/test_aws_iot_logger.py +++ b/tests/test_aws_iot_logger.py @@ -14,14 +14,14 @@ from __future__ import annotations -from collections import defaultdict -import os -from queue import Queue import logging +import os import random import time +from collections import defaultdict from datetime import datetime +from queue import Queue from uuid import uuid1 import pytest diff --git a/tests/test_configs.py b/tests/test_configs.py index 73e26ea..9d12f29 100644 --- a/tests/test_configs.py +++ b/tests/test_configs.py @@ -50,6 +50,7 @@ "MAX_LOGS_BACKLOG": 4096, "MAX_LOGS_PER_MERGE": 512, "UPLOAD_INTERVAL": 60, + "ALLOWED_ECUS": {"autoware"}, }, ), # test#1: frequently changed settings @@ -58,6 +59,7 @@ "LISTEN_ADDRESS": "172.16.1.1", "SERVER_LOGGING_LEVEL": "ERROR", "UPLOAD_INTERVAL": "30", + "ALLOWED_ECUS": '["main_ecu", "sub_ecu"]', }, { "GREENGRASS_V1_CONFIG": "/greengrass/config/config.json", @@ -72,6 +74,7 @@ "MAX_LOGS_BACKLOG": 4096, "MAX_LOGS_PER_MERGE": 512, "UPLOAD_INTERVAL": 30, + "ALLOWED_ECUS": {"main_ecu", "sub_ecu"}, }, ), # test#2: change everything @@ -89,6 +92,7 @@ "MAX_LOGS_BACKLOG": "1024", "MAX_LOGS_PER_MERGE": "128", "UPLOAD_INTERVAL": "10", + "ALLOWED_ECUS": '["main_ecu", "sub_ecu"]', }, { "GREENGRASS_V1_CONFIG": "ggv1_cfg.json", @@ -103,6 +107,7 @@ "MAX_LOGS_BACKLOG": 1024, "MAX_LOGS_PER_MERGE": 128, "UPLOAD_INTERVAL": 10, + "ALLOWED_ECUS": {"main_ecu", "sub_ecu"}, }, ), ], diff --git a/tests/test_log_proxy_server.py b/tests/test_log_proxy_server.py index fdcf0fe..a15a550 100644 --- a/tests/test_log_proxy_server.py +++ b/tests/test_log_proxy_server.py @@ -18,12 +18,16 @@ import logging import os import random -from dataclasses import dataclass +from dataclasses import dataclass, field +from http import HTTPStatus from queue import Queue +from typing import Set from urllib.parse import urljoin import aiohttp +import aiohttp.client_exceptions import pytest +from pytest_mock import MockerFixture from aiohttp import web import otaclient_iot_logging_server.log_proxy_server as log_server_module @@ -41,6 +45,9 @@ class _ServerConfig: LISTEN_ADDRESS: str = "127.0.0.1" LISTEN_PORT: int = 8083 + ALLOWED_ECUS: Set[str] = field( + default_factory=lambda: {"main_ecu", "sub_ecu0", "sub_ecu1", "sub_ecu2"} + ) _test_server_cfg = _ServerConfig() @@ -76,11 +83,13 @@ class TestLogProxyServer: TOTAL_MSG_NUM = 4096 @pytest.fixture(autouse=True) - async def launch_server(self): + async def launch_server(self, mocker: MockerFixture): """ See https://docs.aiohttp.org/en/stable/web_advanced.html#custom-resource-implementation for more details. """ + mocker.patch(f"{MODULE}.server_cfg", _test_server_cfg) + queue: LogsQueue = Queue() self._queue = queue @@ -138,3 +147,21 @@ async def test_server(self, client_sesion: aiohttp.ClientSession): assert _ecu_id == item.ecu_id assert _log_msg["message"] == item.message assert self._queue.empty() + + @pytest.mark.parametrize( + "_ecu_id, _data", + [ + # unknowned ECU's request will be dropped + ("bad_ecu_id", "valid_msg"), + # empty message will be dropped + ("main_ecu", ""), + ], + ) + async def test_reject_invalid_request( + self, _ecu_id: str, _data: str, client_sesion: aiohttp.ClientSession + ): + with pytest.raises(aiohttp.client_exceptions.ClientResponseError) as exc_info: + _log_upload_endpoint_url = urljoin(self.SERVER_URL, _ecu_id) + async with client_sesion.post(_log_upload_endpoint_url, data=_data): + pass # raise_for_status is set on session + assert exc_info.value.status == HTTPStatus.BAD_REQUEST