Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodong-Yang committed Feb 29, 2024
1 parent 4a78b35 commit 60f7a6a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
6 changes: 3 additions & 3 deletions tests/test_aws_iot_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"MAX_LOGS_BACKLOG": 4096,
"MAX_LOGS_PER_MERGE": 512,
"UPLOAD_INTERVAL": 60,
"ALLOWED_ECUS": {"autoware"},
},
),
# test#1: frequently changed settings
Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -103,6 +107,7 @@
"MAX_LOGS_BACKLOG": 1024,
"MAX_LOGS_PER_MERGE": 128,
"UPLOAD_INTERVAL": 10,
"ALLOWED_ECUS": {"main_ecu", "sub_ecu"},
},
),
],
Expand Down
31 changes: 29 additions & 2 deletions tests/test_log_proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 60f7a6a

Please sign in to comment.