Skip to content

Commit ed4f2cf

Browse files
committed
Fix up unit tests and linter
1 parent 983f185 commit ed4f2cf

File tree

8 files changed

+96
-283
lines changed

8 files changed

+96
-283
lines changed

os_capacity/prometheus.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
#!/usr/bin/env python3
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
213

3-
import os
414
import collections
5-
import json
15+
import os
616
import time
717
import uuid
818

@@ -174,12 +184,12 @@ def get_host_details(compute_client, placement_client):
174184
counts = capacity_per_flavor.get(flavor.name, {}).values()
175185
total = 0 if not counts else sum(counts)
176186
free_by_flavor_total.add_metric([flavor.name, str(flavor.is_public)], total)
177-
# print(f'openstack_free_capacity_by_flavor{{flavor="{flavor_name}"}} {total}')
178187

179188
# capacity per host
180189
free_by_flavor_hypervisor = prom_core.GaugeMetricFamily(
181190
"openstack_free_capacity_hypervisor_by_flavor",
182-
"Free capacity for each hypervisor if you fill remaining space full of each flavor",
191+
"Free capacity for each hypervisor if you fill "
192+
"remaining space full of each flavor",
183193
labels=["hypervisor", "flavor_name", "az_aggregate", "project_aggregate"],
184194
)
185195
resource_providers, project_to_aggregate = get_resource_provider_info(
@@ -203,7 +213,8 @@ def get_host_details(compute_client, placement_client):
203213
)
204214
free_space_found = True
205215
if not free_space_found:
206-
# TODO(johngarbutt) allocation candidates only returns some not all candidates!
216+
# TODO(johngarbutt) allocation candidates only returns some,
217+
# not all candidates!
207218
print(f"# WARNING - no free spaces found for {hostname}")
208219

209220
project_filter_aggregates = prom_core.GaugeMetricFamily(
@@ -214,9 +225,6 @@ def get_host_details(compute_client, placement_client):
214225
for project, names in project_to_aggregate.items():
215226
for name in names:
216227
project_filter_aggregates.add_metric([project, name], 1)
217-
# print(
218-
# f'openstack_project_filter_aggregate{{project_id="{project}",aggregate="{name}"}} 1'
219-
# )
220228
return resource_providers, [
221229
free_by_flavor_total,
222230
free_by_flavor_hypervisor,
@@ -312,10 +320,6 @@ def get_host_usage(resource_providers, placement_client):
312320
return [usage_guage, capacity_guage]
313321

314322

315-
def print_exporter_data(app):
316-
print_host_free_details(app.compute_client, app.placement_client)
317-
318-
319323
class OpenStackCapacityCollector(object):
320324
def __init__(self):
321325
self.conn = openstack.connect()
@@ -346,25 +350,28 @@ def collect(self):
346350
host_time = time.perf_counter()
347351
host_duration = host_time - start_time
348352
print(
349-
f"1 of 3: host flavor capacity complete for {collect_id} it took {host_duration} seconds"
353+
"1 of 3: host flavor capacity complete "
354+
f"for {collect_id} it took {host_duration} seconds"
350355
)
351356

352357
if not skip_project_usage:
353358
guages += get_project_usage(conn.identity, conn.placement, conn.compute)
354359
project_time = time.perf_counter()
355360
project_duration = project_time - host_time
356361
print(
357-
f"2 of 3: project usage complete for {collect_id} it took {project_duration} seconds"
362+
"2 of 3: project usage complete "
363+
f"for {collect_id} it took {project_duration} seconds"
358364
)
359365
else:
360366
print("2 of 3: skipping project usage")
361367

362-
if not skip_project_usage:
368+
if not skip_host_usage:
363369
guages += get_host_usage(resource_providers, conn.placement)
364370
host_usage_time = time.perf_counter()
365371
host_usage_duration = host_usage_time - project_time
366372
print(
367-
f"3 of 3: host usage complete for {collect_id} it took {host_usage_duration} seconds"
373+
"3 of 3: host usage complete for "
374+
f"{collect_id} it took {host_usage_duration} seconds"
368375
)
369376
else:
370377
print("3 of 3: skipping host usage")
@@ -391,4 +398,4 @@ def main():
391398

392399

393400
if __name__ == "__main__":
394-
main()
401+
main()

os_capacity/tests/unit/__init__.py

Whitespace-only changes.

os_capacity/tests/unit/test_data.py

Lines changed: 0 additions & 188 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import unittest
14+
15+
from os_capacity import prometheus
16+
17+
18+
class FakeFlavor:
19+
def __init__(self, id, name, vcpus, ram, disk, ephemeral, extra_specs):
20+
self.id = id
21+
self.name = name
22+
self.vcpus = vcpus
23+
self.ram = ram
24+
self.disk = disk
25+
self.ephemeral = ephemeral
26+
self.extra_specs = extra_specs
27+
28+
29+
class TestFlavor(unittest.TestCase):
30+
def test_get_placement_request(self):
31+
flavor = FakeFlavor(
32+
"fake_id", "fake_name", 8, 2048, 30, 0, {"hw:cpu_policy": "dedicated"}
33+
)
34+
resources, traits = prometheus.get_placement_request(flavor)
35+
36+
self.assertEqual({"PCPU": 8, "MEMORY_MB": 2048, "DISK_GB": 30}, resources)
37+
self.assertEqual([], traits)

requirements.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
cliff>=2.8.0 # Apache
2-
os-client-config>=1.28.0 # Apache-2.0
3-
pbr>=2.0.0,!=2.1.0 # Apache-2.0
4-
prometheus-client==0.16.0
5-
six # MIT
1+
openstacksdk
2+
pbr
3+
prometheus-client

setup.cfg

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ description-file =
55
README.rst
66
author = StackHPC
77
author-email = [email protected]
8-
home-page = https://stackhpc.com
9-
classifier =
10-
Environment :: OpenStack
11-
Intended Audience :: Information Technology
12-
Intended Audience :: System Administrators
13-
Operating System :: POSIX :: Linux
14-
Programming Language :: Python
15-
Programming Language :: Python :: 3
16-
Programming Language :: Python :: 3.9
8+
url = https://github.com/stackhpc/os-capacity
9+
python-requires = >=3.9
10+
license = Apache-2
1711

1812
[files]
1913
packages =
2014
os_capacity
2115

16+
[entry_points]
17+
console_scripts=
18+
os_capacity = os_capacity.prometheus:main

0 commit comments

Comments
 (0)