Skip to content

Commit fa70e5e

Browse files
author
elay
committed
update assertions and logger
1 parent 5ade542 commit fa70e5e

File tree

2 files changed

+50
-55
lines changed

2 files changed

+50
-55
lines changed

pcfuncs/ipban/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
from .config import settings
1010
from .models import UpdateBannedIPTask
1111

12+
logger = logging.getLogger(__name__)
13+
1214

1315
def main(mytimer: func.TimerRequest) -> None:
1416
utc_timestamp: str = (
1517
datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat()
1618
)
17-
logging.info("Updating the ip ban list at %s", utc_timestamp)
19+
logger.info("Updating the ip ban list at %s", utc_timestamp)
1820
credential: DefaultAzureCredential = DefaultAzureCredential()
1921
logs_query_client: LogsQueryClient = LogsQueryClient(credential)
2022
table_service_client: TableServiceClient = TableServiceClient(

pcfuncs/tests/ipban/test_ipban.py

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
import uuid
13
from typing import Any, Dict, Generator, List, Tuple
24
from unittest.mock import MagicMock
35

@@ -12,47 +14,38 @@
1214
from pytest_mock import MockerFixture
1315

1416
MOCK_LOGS_QUERY_RESULT = [("192.168.1.1", 8000), ("192.168.1.4", 12000)]
15-
TEST_BANNED_IP_TABLE = "testblobstoragebannedip"
16-
17-
18-
def populate_banned_ip_table(table_client: TableClient) -> List[Dict[str, Any]]:
19-
print("Populating the table")
20-
entities: List[Dict[str, Any]] = [
21-
{
22-
"PartitionKey": "192.168.1.1",
23-
"RowKey": "192.168.1.1",
24-
"ReadCount": 647,
25-
"Threshold": settings.threshold_read_count_in_gb,
26-
"TimeWindow": settings.time_window_in_hours,
27-
},
28-
{
29-
"PartitionKey": "192.168.1.2",
30-
"RowKey": "192.168.1.2",
31-
"ReadCount": 214,
32-
"Threshold": settings.threshold_read_count_in_gb,
33-
"TimeWindow": settings.time_window_in_hours,
34-
},
35-
{
36-
"PartitionKey": "192.168.1.3",
37-
"RowKey": "192.168.1.3",
38-
"ReadCount": 550,
39-
"Threshold": settings.threshold_read_count_in_gb,
40-
"TimeWindow": settings.time_window_in_hours,
41-
},
42-
]
43-
for entity in entities:
17+
TEST_ID = uuid.uuid4()
18+
TEST_BANNED_IP_TABLE = f"testblobstoragebannedip-{TEST_ID}"
19+
20+
logger = logging.getLogger(__name__)
21+
PREPOPULATED_ENTITIES = [
22+
{
23+
"PartitionKey": "192.168.1.1",
24+
"RowKey": "192.168.1.1",
25+
"ReadCount": 647,
26+
"Threshold": settings.threshold_read_count_in_gb,
27+
"TimeWindow": settings.time_window_in_hours,
28+
},
29+
{
30+
"PartitionKey": "192.168.1.2",
31+
"RowKey": "192.168.1.2",
32+
"ReadCount": 214,
33+
"Threshold": settings.threshold_read_count_in_gb,
34+
"TimeWindow": settings.time_window_in_hours,
35+
},
36+
{
37+
"PartitionKey": "192.168.1.3",
38+
"RowKey": "192.168.1.3",
39+
"ReadCount": 550,
40+
"Threshold": settings.threshold_read_count_in_gb,
41+
"TimeWindow": settings.time_window_in_hours,
42+
},
43+
]
44+
45+
46+
def populate_banned_ip_table(table_client: TableClient) -> None:
47+
for entity in PREPOPULATED_ENTITIES:
4448
table_client.create_entity(entity)
45-
return entities
46-
47-
48-
def clear_table(table_client: TableClient) -> None:
49-
entities = list(table_client.list_entities())
50-
for entity in entities:
51-
table_client.delete_entity(
52-
partition_key=entity["PartitionKey"], row_key=entity["RowKey"]
53-
)
54-
entities = list(table_client.list_entities())
55-
assert len(entities) == 0
5649

5750

5851
@pytest.fixture
@@ -70,19 +63,18 @@ def mock_clients(
7063
"TableEndpoint=http://azurite:10002/devstoreaccount1;"
7164
)
7265
# Use Azurite for unit tests and populate the table with initial data
73-
table_service: TableServiceClient = TableServiceClient.from_connection_string(
74-
CONNECTION_STRING
66+
table_service_client: TableServiceClient = (
67+
TableServiceClient.from_connection_string(CONNECTION_STRING)
7568
)
76-
table_client: TableClient = table_service.create_table_if_not_exists(
69+
table_client: TableClient = table_service_client.create_table_if_not_exists(
7770
table_name=TEST_BANNED_IP_TABLE
7871
)
79-
8072
# Pre-populate the banned ip table
8173
populate_banned_ip_table(table_client)
8274
yield logs_query_client, table_client
8375

84-
# Clear all entities from the table
85-
clear_table(table_client)
76+
# Delete the test table
77+
table_service_client.delete_table(TEST_BANNED_IP_TABLE)
8678

8779

8880
@pytest.fixture
@@ -100,21 +92,22 @@ def integration_clients(
10092
# Pre-populate the banned ip table
10193
populate_banned_ip_table(table_client)
10294
yield logs_query_client, table_client
103-
# Clear all entities from the table
104-
clear_table(table_client)
95+
96+
# Delete the test table
97+
table_service_client.delete_table(TEST_BANNED_IP_TABLE)
10598

10699

107100
@pytest.mark.integration
108101
def test_update_banned_ip_integration(
109102
integration_clients: Tuple[LogsQueryClient, TableClient]
110103
) -> None:
111-
print("Integration test is running")
104+
logger.info(f"Test id: {TEST_ID} - integration test is running")
112105
logs_query_client, table_client = integration_clients
106+
assert len(list(table_client.list_entities())) == len(PREPOPULATED_ENTITIES)
113107
task: UpdateBannedIPTask = UpdateBannedIPTask(logs_query_client, table_client)
114108
# retrieve the logs query result from pc-api-loganalytics
115109
logs_query_result: List[LogsTableRow] = task.run()
116-
entities = list(table_client.list_entities())
117-
assert len(logs_query_result) == len(entities)
110+
assert len(list(table_client.list_entities())) == len(logs_query_result)
118111
for ip, expected_read_count in logs_query_result:
119112
entity: TableEntity = table_client.get_entity(ip, ip)
120113
assert entity["ReadCount"] == expected_read_count
@@ -123,12 +116,12 @@ def test_update_banned_ip_integration(
123116

124117

125118
def test_update_banned_ip(mock_clients: Tuple[MagicMock, TableClient]) -> None:
126-
print("Unit test is running")
119+
logger.info(f"Test id: {TEST_ID} - unit test is running")
127120
mock_logs_query_client, table_client = mock_clients
121+
assert len(list(table_client.list_entities())) == len(PREPOPULATED_ENTITIES)
128122
task: UpdateBannedIPTask = UpdateBannedIPTask(mock_logs_query_client, table_client)
129123
task.run()
130-
entities = list(table_client.list_entities())
131-
assert len(entities) == len(MOCK_LOGS_QUERY_RESULT)
124+
assert len(list(table_client.list_entities())) == len(MOCK_LOGS_QUERY_RESULT)
132125
for ip, expected_read_count in MOCK_LOGS_QUERY_RESULT:
133126
entity = table_client.get_entity(ip, ip)
134127
assert entity["ReadCount"] == expected_read_count

0 commit comments

Comments
 (0)