1
+ import logging
2
+ import uuid
1
3
from typing import Any , Dict , Generator , List , Tuple
2
4
from unittest .mock import MagicMock
3
5
12
14
from pytest_mock import MockerFixture
13
15
14
16
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 :
44
48
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
56
49
57
50
58
51
@pytest .fixture
@@ -70,19 +63,18 @@ def mock_clients(
70
63
"TableEndpoint=http://azurite:10002/devstoreaccount1;"
71
64
)
72
65
# 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 )
75
68
)
76
- table_client : TableClient = table_service .create_table_if_not_exists (
69
+ table_client : TableClient = table_service_client .create_table_if_not_exists (
77
70
table_name = TEST_BANNED_IP_TABLE
78
71
)
79
-
80
72
# Pre-populate the banned ip table
81
73
populate_banned_ip_table (table_client )
82
74
yield logs_query_client , table_client
83
75
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 )
86
78
87
79
88
80
@pytest .fixture
@@ -100,21 +92,22 @@ def integration_clients(
100
92
# Pre-populate the banned ip table
101
93
populate_banned_ip_table (table_client )
102
94
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 )
105
98
106
99
107
100
@pytest .mark .integration
108
101
def test_update_banned_ip_integration (
109
102
integration_clients : Tuple [LogsQueryClient , TableClient ]
110
103
) -> None :
111
- print ( "Integration test is running" )
104
+ logger . info ( f"Test id: { TEST_ID } - integration test is running" )
112
105
logs_query_client , table_client = integration_clients
106
+ assert len (list (table_client .list_entities ())) == len (PREPOPULATED_ENTITIES )
113
107
task : UpdateBannedIPTask = UpdateBannedIPTask (logs_query_client , table_client )
114
108
# retrieve the logs query result from pc-api-loganalytics
115
109
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 )
118
111
for ip , expected_read_count in logs_query_result :
119
112
entity : TableEntity = table_client .get_entity (ip , ip )
120
113
assert entity ["ReadCount" ] == expected_read_count
@@ -123,12 +116,12 @@ def test_update_banned_ip_integration(
123
116
124
117
125
118
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" )
127
120
mock_logs_query_client , table_client = mock_clients
121
+ assert len (list (table_client .list_entities ())) == len (PREPOPULATED_ENTITIES )
128
122
task : UpdateBannedIPTask = UpdateBannedIPTask (mock_logs_query_client , table_client )
129
123
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 )
132
125
for ip , expected_read_count in MOCK_LOGS_QUERY_RESULT :
133
126
entity = table_client .get_entity (ip , ip )
134
127
assert entity ["ReadCount" ] == expected_read_count
0 commit comments