Skip to content

Commit

Permalink
TST: Add test for collecting vm stats
Browse files Browse the repository at this point in the history
Update the main method for collecting stats and add a test for checking
an error is raised if a param is missing.
  • Loading branch information
meoflynn committed Feb 6, 2024
1 parent 8ada504 commit a467ee1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
29 changes: 19 additions & 10 deletions MonitoringTools/usr/local/bin/collect_vm_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,34 @@ def number_servers_shutoff(conn):
return instance_shutoff


def collect_stats(cloud):
def collect_stats(cloud, prod):
"""
Collects the stats for vms and returns a dict
:param cloud: OpenStack Cloud connection
:return: Dictionary of VM Stats
:param prod: Boolean to determine whether Prod or Dev Cloud used
:return: A comma separated string containing VM states.
"""
# raise error if cloud connection not given
if not cloud:
raise ValueError("An OpenStack Connection is required")

# collect stats in order: total, active, build, error, shutoff
total_vms = number_servers_total(cloud)
active_vms = number_servers_active(cloud)
build_vms = number_servers_build(cloud)
error_vms = number_servers_error(cloud)
shutoff_vms = number_servers_shutoff(cloud)

if prod:
cloud_env = "Prod"
else:
cloud_env = "PreProd"

vm_stats = {
"total_vms": number_servers_total(cloud),
"active_vms": number_servers_active(cloud),
"build_vms": number_servers_build(cloud),
"error_vms": number_servers_error(cloud),
"shutoff_vms": number_servers_shutoff(cloud),
}
vm_stats = f"VMStats, instance={cloud_env} totalVM={total_vms}i,activeVM={active_vms}i,buildVM={build_vms}i,errorVM={error_vms}i,shutoffVM={shutoff_vms}i"

return vm_stats


if __name__ == "__main__":
cloud_conn = openstack.connect(cloud="openstack")
print(collect_stats(cloud_conn))
print(collect_stats(cloud_conn, prod=False))
20 changes: 5 additions & 15 deletions MonitoringTools/usr/local/bin/test_collect_vm_stats.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import unittest
from unittest import mock
from unittest.mock import NonCallableMock, Mock

import vm_stats
from vm_stats import (
number_servers_active,
number_servers_build,
Expand Down Expand Up @@ -75,20 +72,13 @@ def test_number_servers_shutoff(self):
num_returned = number_servers_shutoff(mock_conn)
assert num_returned == 3

# WIP Unit Test
def test_collect_stats(self):
def test_collect_stats_raise_error(self):
"""
Test that stats are collected and stats methods are called
Tests that if no cloud connection was given, a value error is raised
"""
mock_cloud_conn = Mock()

mock_vm_stats = {
"total_vms": NonCallableMock(),
"active_vms": NonCallableMock(),
"build_vms": NonCallableMock(),
"error_vms": NonCallableMock(),
"shutoff_vms": NonCallableMock(),
}
mock_cloud = None
with self.assertRaises(ValueError):
collect_stats(mock_cloud, prod=False)


if __name__ == "__main__":
Expand Down

0 comments on commit a467ee1

Please sign in to comment.