Skip to content

Commit

Permalink
Fix up unit tests and linter
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGarbutt committed Nov 5, 2024
1 parent 983f185 commit ed4f2cf
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 283 deletions.
41 changes: 24 additions & 17 deletions os_capacity/prometheus.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#!/usr/bin/env python3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os
import collections
import json
import os
import time
import uuid

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

# capacity per host
free_by_flavor_hypervisor = prom_core.GaugeMetricFamily(
"openstack_free_capacity_hypervisor_by_flavor",
"Free capacity for each hypervisor if you fill remaining space full of each flavor",
"Free capacity for each hypervisor if you fill "
"remaining space full of each flavor",
labels=["hypervisor", "flavor_name", "az_aggregate", "project_aggregate"],
)
resource_providers, project_to_aggregate = get_resource_provider_info(
Expand All @@ -203,7 +213,8 @@ def get_host_details(compute_client, placement_client):
)
free_space_found = True
if not free_space_found:
# TODO(johngarbutt) allocation candidates only returns some not all candidates!
# TODO(johngarbutt) allocation candidates only returns some,
# not all candidates!
print(f"# WARNING - no free spaces found for {hostname}")

project_filter_aggregates = prom_core.GaugeMetricFamily(
Expand All @@ -214,9 +225,6 @@ def get_host_details(compute_client, placement_client):
for project, names in project_to_aggregate.items():
for name in names:
project_filter_aggregates.add_metric([project, name], 1)
# print(
# f'openstack_project_filter_aggregate{{project_id="{project}",aggregate="{name}"}} 1'
# )
return resource_providers, [
free_by_flavor_total,
free_by_flavor_hypervisor,
Expand Down Expand Up @@ -312,10 +320,6 @@ def get_host_usage(resource_providers, placement_client):
return [usage_guage, capacity_guage]


def print_exporter_data(app):
print_host_free_details(app.compute_client, app.placement_client)


class OpenStackCapacityCollector(object):
def __init__(self):
self.conn = openstack.connect()
Expand Down Expand Up @@ -346,25 +350,28 @@ def collect(self):
host_time = time.perf_counter()
host_duration = host_time - start_time
print(
f"1 of 3: host flavor capacity complete for {collect_id} it took {host_duration} seconds"
"1 of 3: host flavor capacity complete "
f"for {collect_id} it took {host_duration} seconds"
)

if not skip_project_usage:
guages += get_project_usage(conn.identity, conn.placement, conn.compute)
project_time = time.perf_counter()
project_duration = project_time - host_time
print(
f"2 of 3: project usage complete for {collect_id} it took {project_duration} seconds"
"2 of 3: project usage complete "
f"for {collect_id} it took {project_duration} seconds"
)
else:
print("2 of 3: skipping project usage")

if not skip_project_usage:
if not skip_host_usage:
guages += get_host_usage(resource_providers, conn.placement)
host_usage_time = time.perf_counter()
host_usage_duration = host_usage_time - project_time
print(
f"3 of 3: host usage complete for {collect_id} it took {host_usage_duration} seconds"
"3 of 3: host usage complete for "
f"{collect_id} it took {host_usage_duration} seconds"
)
else:
print("3 of 3: skipping host usage")
Expand All @@ -391,4 +398,4 @@ def main():


if __name__ == "__main__":
main()
main()
Empty file.
188 changes: 0 additions & 188 deletions os_capacity/tests/unit/test_data.py

This file was deleted.

37 changes: 37 additions & 0 deletions os_capacity/tests/unit/test_prometheus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import unittest

from os_capacity import prometheus


class FakeFlavor:
def __init__(self, id, name, vcpus, ram, disk, ephemeral, extra_specs):
self.id = id
self.name = name
self.vcpus = vcpus
self.ram = ram
self.disk = disk
self.ephemeral = ephemeral
self.extra_specs = extra_specs


class TestFlavor(unittest.TestCase):
def test_get_placement_request(self):
flavor = FakeFlavor(
"fake_id", "fake_name", 8, 2048, 30, 0, {"hw:cpu_policy": "dedicated"}
)
resources, traits = prometheus.get_placement_request(flavor)

self.assertEqual({"PCPU": 8, "MEMORY_MB": 2048, "DISK_GB": 30}, resources)
self.assertEqual([], traits)
8 changes: 3 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
cliff>=2.8.0 # Apache
os-client-config>=1.28.0 # Apache-2.0
pbr>=2.0.0,!=2.1.0 # Apache-2.0
prometheus-client==0.16.0
six # MIT
openstacksdk
pbr
prometheus-client
15 changes: 6 additions & 9 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ description-file =
README.rst
author = StackHPC
author-email = [email protected]
home-page = https://stackhpc.com
classifier =
Environment :: OpenStack
Intended Audience :: Information Technology
Intended Audience :: System Administrators
Operating System :: POSIX :: Linux
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.9
url = https://github.com/stackhpc/os-capacity
python-requires = >=3.9
license = Apache-2

[files]
packages =
os_capacity

[entry_points]
console_scripts=
os_capacity = os_capacity.prometheus:main
Loading

0 comments on commit ed4f2cf

Please sign in to comment.