Skip to content

Commit 6a5731e

Browse files
committed
Initial IP record and SSM messenger testing
1 parent df37ebd commit 6a5731e

File tree

4 files changed

+161
-2
lines changed

4 files changed

+161
-2
lines changed

caso/extract/openstack/neutron.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def extract(self, extract_from, extract_to):
109109
elif ip_version == 6:
110110
ip_counts_v6[user] += 1
111111

112-
for (ip_version, ip_counts) in [(4, ip_counts_v4), (6, ip_counts_v6)]:
112+
for ip_version, ip_counts in [(4, ip_counts_v4), (6, ip_counts_v6)]:
113113
for user_id, count in ip_counts.items():
114114
if count == 0:
115115
continue

caso/tests/conftest.py

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,66 @@
114114
},
115115
]
116116

117-
# Record fixtures
117+
118+
valid_ip_records_fields = [
119+
dict(
120+
uuid="e3c5aeef-37b8-4332-ad9f-9d068f156dc2",
121+
measure_time=now,
122+
site_name="TEST-Site",
123+
user_id="a4519d7d-f60a-4908-9d63-7d9e17422188",
124+
group_id="03b6a6c4-cf2b-48b9-82f1-69c52b9f30af",
125+
user_dn="User 1 DN",
126+
fqan="VO 1 FQAN",
127+
ip_version=4,
128+
public_ip_count=10,
129+
compute_service="Fake Cloud Service",
130+
cloud_type=cloud_type,
131+
),
132+
dict(
133+
uuid="5c50720e-a653-4d70-9b0e-d4388687fcbc",
134+
measure_time=now,
135+
site_name="TEST-Site",
136+
user_id="3391a44e-3728-478d-abde-b86c25356571",
137+
group_id="2dae43c4-1889-4e63-b172-d4e99381e30a",
138+
user_dn="User 2 DN",
139+
fqan="VO 2 FQAN",
140+
ip_version=6,
141+
public_ip_count=20,
142+
compute_service="Fake Cloud Service",
143+
cloud_type=cloud_type,
144+
),
145+
]
146+
147+
valid_ip_records_dict = [
148+
{
149+
"CloudComputeService": "Fake Cloud Service",
150+
"FQAN": "VO 1 FQAN",
151+
"GlobalUserName": "User 1 DN",
152+
"IPCount": 10,
153+
"IPVersion": 4,
154+
"LocalGroup": "03b6a6c4-cf2b-48b9-82f1-69c52b9f30af",
155+
"LocalUser": "a4519d7d-f60a-4908-9d63-7d9e17422188",
156+
"MeasurementTime": 1685044746,
157+
"SiteName": "TEST-Site",
158+
"uuid": "e3c5aeef-37b8-4332-ad9f-9d068f156dc2",
159+
"CloudType": cloud_type,
160+
},
161+
{
162+
"CloudComputeService": "Fake Cloud Service",
163+
"FQAN": "VO 1 FQAN",
164+
"GlobalUserName": "User 2 DN",
165+
"IPCount": 20,
166+
"IPVersion": 4,
167+
"LocalGroup": "2dae43c4-1889-4e63-b172-d4e99381e30a",
168+
"LocalUser": "3391a44e-3728-478d-abde-b86c25356571",
169+
"MeasurementTime": 1685044746,
170+
"SiteName": "TEST-Site",
171+
"uuid": "5c50720e-a653-4d70-9b0e-d4388687fcbc",
172+
"CloudType": cloud_type,
173+
},
174+
]
175+
176+
# Cloud Record fixtures
118177

119178

120179
@pytest.fixture(scope="module")
@@ -160,6 +219,47 @@ def cloud_record_list(cloud_record, another_cloud_record):
160219
return [cloud_record, another_cloud_record]
161220

162221

222+
# IP record fixtures
223+
224+
225+
@pytest.fixture(scope="module")
226+
def ip_record():
227+
"""Get a fixture for an IP record."""
228+
record = caso.record.IPRecord(**valid_ip_records_fields[0])
229+
return record
230+
231+
232+
@pytest.fixture(scope="module")
233+
def another_ip_record():
234+
"""Get another fixture for an IP record."""
235+
record = caso.record.IPRecord(**valid_ip_records_fields[1])
236+
return record
237+
238+
239+
@pytest.fixture(scope="module")
240+
def valid_ip_record():
241+
"""Get a fixture for a valid IP record as a dict."""
242+
return valid_ip_records_dict[0]
243+
244+
245+
@pytest.fixture(scope="module")
246+
def valid_ip_records():
247+
"""Get a fixture for all IP records as a dict."""
248+
return valid_ip_records_dict
249+
250+
251+
@pytest.fixture(scope="module")
252+
def another_valid_ip_record():
253+
"""Get another fixture for an IP record as a dict."""
254+
return valid_ip_records_dict[0]
255+
256+
257+
@pytest.fixture(scope="module")
258+
def ip_record_list(ip_record, another_ip_record):
259+
"""Get a fixture for a list of IP records."""
260+
return [ip_record, another_ip_record]
261+
262+
163263
# SSM entries
164264

165265

@@ -208,3 +308,33 @@ def expected_entries_cloud():
208308
]
209309

210310
return ssm_entries
311+
312+
313+
@pytest.fixture
314+
def expected_entries_ip():
315+
"""Get a fixture for all IP entries."""
316+
ssm_entries = [
317+
'{"SiteName": "TEST-Site", '
318+
'"CloudType": "caso/4.1.0.0rc1 (OpenStack)", '
319+
'"CloudComputeService": "Fake Cloud Service", '
320+
'"uuid": "e3c5aeef-37b8-4332-ad9f-9d068f156dc2", '
321+
'"LocalUser": "a4519d7d-f60a-4908-9d63-7d9e17422188", '
322+
'"GlobalUserName": "User 1 DN", '
323+
'"LocalGroup": "03b6a6c4-cf2b-48b9-82f1-69c52b9f30af", '
324+
'"FQAN": "VO 1 FQAN", '
325+
'"MeasurementTime": 1685044746, '
326+
'"IPVersion": 4, '
327+
'"IPCount": 10}',
328+
'{"SiteName": "TEST-Site", '
329+
'"CloudType": "caso/4.1.0.0rc1 (OpenStack)", '
330+
'"CloudComputeService": "Fake Cloud Service", '
331+
'"uuid": "5c50720e-a653-4d70-9b0e-d4388687fcbc", '
332+
'"LocalUser": "3391a44e-3728-478d-abde-b86c25356571", '
333+
'"GlobalUserName": "User 2 DN", '
334+
'"LocalGroup": "2dae43c4-1889-4e63-b172-d4e99381e30a", '
335+
'"FQAN": "VO 2 FQAN", '
336+
'"MeasurementTime": 1685044746, '
337+
'"IPVersion": 6, '
338+
'"IPCount": 20}',
339+
]
340+
return ssm_entries

caso/tests/test_record.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,18 @@ def test_cloud_record_custom_cpu(cloud_record):
9090
cloud_record.cpu_duration = cpu
9191
assert cloud_record.wall_duration == wall
9292
assert cloud_record.cpu_duration == cpu
93+
94+
95+
def test_ip_record(ip_record):
96+
"""Test that an IP record is correctly generated."""
97+
assert isinstance(ip_record.measure_time, datetime.datetime)
98+
99+
100+
def test_ip_record_map_opts(ip_record, valid_ip_record):
101+
"""Test that an IP record is correctly generated."""
102+
opts = {
103+
"by_alias": True,
104+
"exclude_none": True,
105+
}
106+
107+
assert json.loads(ip_record.json(**opts)) == valid_ip_record

caso/tests/test_ssm.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,17 @@ def mock_push(entries_cloud, entries_ip, entries_accelerator, entries_storage):
5353

5454
m.setattr(messenger, "_push", mock_push)
5555
messenger.push(cloud_record_list)
56+
57+
58+
def test_ip_records_pushed(monkeypatch, ip_record_list, expected_entries_ip):
59+
"""Test that IP records are correctly rendered."""
60+
61+
def mock_push(entries_cloud, entries_ip, entries_accelerator, entries_storage):
62+
assert set(entries_ip) == set(expected_entries_ip)
63+
64+
with monkeypatch.context() as m:
65+
m.setattr("caso.utils.makedirs", lambda x: None)
66+
messenger = ssm.SSMMessenger()
67+
68+
m.setattr(messenger, "_push", mock_push)
69+
messenger.push(ip_record_list)

0 commit comments

Comments
 (0)