Skip to content

Commit 6cc7d74

Browse files
authored
Merge pull request #199 from stfc/fix-slottifier
Change Slottifier to use the Query Library to fix the slot calculations
2 parents 61e011c + 7248819 commit 6cc7d74

File tree

3 files changed

+137
-62
lines changed

3 files changed

+137
-62
lines changed

MonitoringTools/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
openstacksdk
2+
https://github.com/stfc/openstack-query-library/releases/download/v0.1.6/openstackquery-0.1.6-py3-none-any.whl
23
pytest
34
pylint
45
pytest-cov

MonitoringTools/tests/test_slottifier.py

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,33 @@ def mock_hypervisors_fixture():
2121
"""fixture for setting up various mock hvs"""
2222
return {
2323
"hv1": {
24-
"name": "hv1",
25-
"status": "enabled",
26-
"vcpus": 8,
27-
"vcpus_used": 2,
28-
"memory_size": 8192,
29-
"memory_used": 2048,
24+
"hypervisor_name": "hv1",
25+
"hypervisor_status": "enabled",
26+
"hypervisor_vcpus": 8,
27+
"hypervisor_vcpus_used": 2,
28+
"hypervisor_memory_size": 8192,
29+
"hypervisor_memory_used": 2048,
3030
},
3131
"hv2": {
32-
"name": "hv2",
33-
"status": "enabled",
34-
"vcpus": 4,
35-
"vcpus_used": 6,
36-
"memory_size": 2048,
37-
"memory_used": 4096,
32+
"hypervisor_name": "hv2",
33+
"hypervisor_status": "enabled",
34+
"hypervisor_vcpus": 4,
35+
"hypervisor_vcpus_used": 6,
36+
"hypervisor_memory_size": 2048,
37+
"hypervisor_memory_used": 4096,
3838
},
39-
"hv3": {"name": "hv3", "status": "disabled"},
39+
"hv3": {
40+
"hypervisor_name": "hv3",
41+
"hypervisor_status": "disabled",
42+
},
43+
"hv4": {
44+
"hypervisor_name": "hv4",
45+
"hypervisor_status": "enabled",
46+
"hypervisor_vcpus": "Not Found",
47+
"hypervisor_vcpus_used": "Not Found",
48+
"hypervisor_memory_size": "Not Found",
49+
"hypervisor_memory_used": "Not Found",
50+
}
4051
}
4152

4253

@@ -106,10 +117,10 @@ def test_get_hv_info_exists_and_enabled(mock_hypervisors, mock_aggregate):
106117
assert get_hv_info(
107118
mock_hypervisors["hv1"], mock_aggregate(gpu_num="1"), {"status": "enabled"}
108119
) == {
109-
"cores_available": 6,
120+
"vcpus_available": 6,
110121
"mem_available": 6144,
111122
"gpu_capacity": 1,
112-
"core_capacity": 8,
123+
"vcpus_capacity": 8,
113124
"mem_capacity": 8192,
114125
"compute_service_status": "enabled",
115126
}
@@ -124,10 +135,10 @@ def test_get_hv_info_negative_results_floored(mock_hypervisors, mock_aggregate):
124135
assert get_hv_info(
125136
mock_hypervisors["hv2"], mock_aggregate(), {"status": "enabled"}
126137
) == {
127-
"cores_available": 0,
138+
"vcpus_available": 0,
128139
"mem_available": 0,
129140
"gpu_capacity": 0,
130-
"core_capacity": 4,
141+
"vcpus_capacity": 4,
131142
"mem_capacity": 2048,
132143
"compute_service_status": "enabled",
133144
}
@@ -140,14 +151,29 @@ def test_get_hv_info_exists_but_disabled(mock_hypervisors, mock_aggregate):
140151
assert get_hv_info(
141152
mock_hypervisors["hv3"], mock_aggregate(), {"status": "disabled"}
142153
) == {
143-
"cores_available": 0,
154+
"vcpus_available": 0,
144155
"mem_available": 0,
145156
"gpu_capacity": 0,
146-
"core_capacity": 0,
157+
"vcpus_capacity": 0,
147158
"mem_capacity": 0,
148159
"compute_service_status": "disabled",
149160
}
150161

162+
def test_get_hv_info_but_values_are_not_found(mock_hypervisors, mock_aggregate):
163+
"""
164+
tests strings that contain values of "Not_Found" - should return all "Not Found" values as 0
165+
"""
166+
assert get_hv_info(
167+
mock_hypervisors["hv4"], mock_aggregate(), {"status": "enabled"}
168+
) == {
169+
"vcpus_available": 0,
170+
"mem_available": 0,
171+
"gpu_capacity": 0,
172+
"vcpus_capacity": 0,
173+
"mem_capacity": 0,
174+
"compute_service_status": "enabled",
175+
}
176+
151177

152178
def test_get_flavor_requirements_with_valid_flavor():
153179
"""
@@ -296,7 +322,7 @@ def test_calculate_slots_on_hv_non_gpu_disabled():
296322
{
297323
"compute_service_status": "disabled",
298324
# can fit 10 slots, but should be 0 since compute service disabled
299-
"cores_available": 100,
325+
"vcpus_available": 100,
300326
"mem_available": 100,
301327
},
302328
)
@@ -319,7 +345,7 @@ def test_calculate_slots_on_hv_gpu_no_gpunum():
319345
{
320346
"compute_service_status": "disabled",
321347
# can fit 10 slots, but should be 0 since compute service disabled
322-
"cores_available": 100,
348+
"vcpus_available": 100,
323349
"mem_available": 100,
324350
},
325351
)
@@ -338,9 +364,9 @@ def test_calculate_slots_on_hv_gpu_disabled():
338364
{
339365
"compute_service_status": "disabled",
340366
# can fit 10 slots, but should be 0 since compute service disabled
341-
"cores_available": 100,
367+
"vcpus_available": 100,
342368
"mem_available": 100,
343-
"core_capacity": 100,
369+
"vcpus_capacity": 100,
344370
"mem_capacity": 100,
345371
"gpu_capacity": 10,
346372
},
@@ -363,7 +389,7 @@ def test_calculate_slots_on_hv_mem_available_max():
363389
{"cores_required": 10, "mem_required": 10},
364390
{
365391
"compute_service_status": "enabled",
366-
"cores_available": 100,
392+
"vcpus_available": 100,
367393
# can fit only one slot
368394
"mem_available": 10,
369395
},
@@ -385,7 +411,7 @@ def test_calculate_slots_on_hv_cores_available_max():
385411
{
386412
"compute_service_status": "enabled",
387413
# can fit 10 cpu slots
388-
"cores_available": 100,
414+
"vcpus_available": 100,
389415
"mem_available": 1000,
390416
},
391417
)
@@ -408,9 +434,9 @@ def test_calculate_slots_on_hv_gpu_available_max():
408434
"compute_service_status": "enabled",
409435
# should find only 5 slots available since gpus are the limiting factor
410436
"gpu_capacity": 5,
411-
"cores_available": 100,
437+
"vcpus_available": 100,
412438
"mem_available": 100,
413-
"core_capacity": 100,
439+
"vcpus_capacity": 100,
414440
"mem_capacity": 100,
415441
},
416442
)
@@ -432,9 +458,9 @@ def test_calculate_slots_on_hv_gpu_max_slots_calculated_properly():
432458
"compute_service_status": "enabled",
433459
# should find 3 slots since we require 2 gpus for each slot
434460
"gpu_capacity": 6,
435-
"cores_available": 100,
461+
"vcpus_available": 100,
436462
"mem_available": 100,
437-
"core_capacity": 100,
463+
"vcpus_capacity": 100,
438464
"mem_capacity": 100,
439465
},
440466
)
@@ -457,10 +483,10 @@ def test_calculate_slots_on_hv_calculates_used_gpu_capacity():
457483
"compute_service_status": "enabled",
458484
# should find only 5 slots available since gpus are the limiting factor
459485
"gpu_capacity": 5,
460-
"cores_available": 10,
486+
"vcpus_available": 10,
461487
"mem_available": 10,
462488
# there's 4 flavor slots that could have already been used
463-
"core_capacity": 50,
489+
"vcpus_capacity": 50,
464490
"mem_capacity": 50,
465491
},
466492
)
@@ -471,31 +497,43 @@ def test_calculate_slots_on_hv_calculates_used_gpu_capacity():
471497

472498

473499
@patch("slottifier.openstack")
474-
def test_get_openstack_resources(mock_openstack):
500+
@patch("slottifier.HypervisorQuery")
501+
def test_get_openstack_resources(mock_hypervisor_query, mock_openstack): # do I use self?
475502
"""
476503
tests get_openstack_resources gets all required resources via openstacksdk
477-
and outputs them properly
504+
and the query library and outputs them properly
478505
"""
479506
mock_conn = mock_openstack.connect.return_value
480507

481-
mock_conn.list_hypervisors.return_value = [{"name": "hv1", "id": 1}]
508+
#Run the mock queries
509+
mock_hv = mock_hypervisor_query.return_value
510+
511+
# Create a mock hv_props dictionary.
512+
mock_hv.to_props.return_value = {
513+
'hypervisor1': {'id': [1], 'name': ['hv1']},
514+
'hypervisor2': {'id': [2], 'name': ['hv2']}
515+
}
516+
482517
mock_conn.compute.aggregates.return_value = [{"name": "ag1", "id": 2}]
483518
mock_conn.compute.services.return_value = [{"name": "svc1", "id": 3}]
484519
mock_conn.compute.flavors.return_value = [{"name": "flv1", "id": 4}]
485520

486521
mock_instance = NonCallableMock()
487522
res = get_openstack_resources(mock_instance)
488523

524+
mock_hv.select_all.assert_called_once()
525+
mock_hv.run.assert_called_once_with(mock_instance)
526+
mock_hv.group_by.assert_called_once_with('id')
527+
489528
mock_openstack.connect.assert_called_once_with(cloud=mock_instance)
490529
mock_conn.compute.services.assert_called_once()
491530
mock_conn.compute.aggregates.assert_called_once()
492-
mock_conn.list_hypervisors.assert_called_once()
493531
mock_conn.compute.flavors.assert_called_once_with(get_extra_specs=True)
494532

495533
assert res == {
496534
"compute_services": [{"name": "svc1", "id": 3}],
497535
"aggregates": [{"name": "ag1", "id": 2}],
498-
"hypervisors": [{"name": "hv1", "id": 1}],
536+
"hypervisors": [{"name": "hv1", "id": 1}, {"name": "hv2", "id": 2}],
499537
"flavors": [{"name": "flv1", "id": 4}],
500538
}
501539

@@ -537,10 +575,10 @@ def test_get_all_hv_info_for_aggregate_with_invalid_data(
537575
"""
538576
mock_aggregate = {
539577
"hosts": [
540-
# hv4 has service but not found in list of hvs
541-
"hv4",
542-
# hv5 has no service and not in list of hvs
543-
"hv5",
578+
# hvFoo has service but not found in list of hvs
579+
"hvFoo",
580+
# hvBar has no service and not in list of hvs
581+
"hvBar",
544582
]
545583
}
546584
assert not (

0 commit comments

Comments
 (0)