Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
Add unit tests for new build and delete server actions/workflows
  • Loading branch information
gmatthews20 committed Jan 24, 2025
1 parent 7ad4793 commit 033a68f
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/workflows/hv_create_test_server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import random

from openstack.connection import Connection
from openstack_api.openstack_server import build_server, delete_server
from openstack_api.openstack_hypervisor import get_avaliable_flavors
from openstack_api.openstack_server import build_server, delete_server


def create_test_server(conn: Connection, hypervisor_name: str, test_all_flavors: bool):
flavors = get_avaliable_flavors(conn, hypervisor_name)
images = conn.image.images(status="active")

if not test_all_flavors:
flavors = [random.choice(flavors)]
Expand Down
47 changes: 46 additions & 1 deletion tests/lib/openstack_api/test_openstack_hypervisor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from unittest.mock import MagicMock
from enums.hypervisor_states import HypervisorState
from openstack_api.openstack_hypervisor import get_hypervisor_state
from openstack_api.openstack_hypervisor import (
get_avaliable_flavors,
get_hypervisor_state,
)
import pytest


Expand Down Expand Up @@ -182,3 +186,44 @@ def test_missing():
"state": True,
}
assert HypervisorState(mock_hv_state) == HypervisorState.UNKNOWN


def test_avaliable_flavors():
"""
Test avaliable flavors
"""
mock_conn = MagicMock()
mock_flavor_1 = MagicMock()
mock_flavor_2 = MagicMock()
mock_aggregate_1 = MagicMock()
mock_aggregate_2 = MagicMock()

mock_aggregate_1.metadata.return_value = {
"hosttype": "amdlocal",
"local-storage-type": "nvme",
}
mock_aggregate_1.hosts = ["hvabc.nubes.rl.ac.uk"]
mock_aggregate_2.metadata.return_value = {
"hosttype": "amdlocal",
"local-storage-type": "sas-ssd",
}
mock_aggregate_2.hosts = ["hvxyz.nubes.rl.ac.uk"]

mock_conn.compute.aggregates.return_value = [mock_aggregate_1, mock_aggregate_2]
mock_conn.compute.flavors.return_value = [mock_flavor_1, mock_flavor_2]

res = get_avaliable_flavors(mock_conn, "hvxyz.nubes.rl.ac.uk")

mock_conn.compute.aggregates.assert_called_once()

mock_aggregate_1.metadata.get.assert_any_call("hosttype")
mock_aggregate_1.metadata.get.assert_any_call("local-storage-type")
mock_aggregate_2.metadata.get.assert_any_call("hosttype")
mock_aggregate_2.metadata.get.assert_any_call("local-storage-type")

assert mock_aggregate_1.metadata.get.call_count == 2
assert mock_aggregate_2.metadata.get.call_count == 2

mock_conn.compute.flavors.assert_called_once()

assert res == [mock_flavor_1.name, mock_flavor_2.name]
78 changes: 77 additions & 1 deletion tests/lib/openstack_api/test_openstack_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from datetime import datetime
from unittest.mock import MagicMock, patch
import pytest
from openstack_api.openstack_server import snapshot_and_migrate_server, snapshot_server
from openstack_api.openstack_server import (
build_server,
delete_server,
snapshot_and_migrate_server,
snapshot_server,
)


@pytest.mark.parametrize("dest_host", [None, "hv01"])
Expand Down Expand Up @@ -146,3 +151,74 @@ def test_block_gpu_migration():
dest_host=None,
snapshot=True,
)


def test_build_server():
mock_conn = MagicMock()

mock_conn.compute.find_flavor.return_value = MagicMock()
mock_conn.image.find_image.return_value = MagicMock()
mock_conn.network.find_network.return_value = MagicMock()

mock_server = MagicMock()
mock_conn.compute.create_server.return_value = mock_server

res = build_server(
mock_conn,
"test-server",
"test-flavor",
"test-image",
"test-network",
"hvxyz.nubes.rl.ac.uk",
)

mock_conn.compute.find_flavor.assert_called_once_with("test-flavor")
mock_conn.image.find_image.assert_called_once_with("test-image")
mock_conn.network.find_network.assert_called_once_with("test-network")

mock_conn.compute.create_server.assert_called_once_with(
**{
"name": "test-server",
"imageRef": mock_conn.image.find_image.return_value.id,
"flavorRef": mock_conn.compute.find_flavor.return_value.id,
"networks": [{"uuid": mock_conn.network.find_network.return_value.id}],
"hypervisor": "hvxyz.nubes.rl.ac.uk",
}
)
mock_conn.compute.wait_for_server.assert_called_once_with(
mock_server, status="ACTIVE", failures=None, interval=5, wait=300
)

assert res == mock_server


def test_delete_server():
mock_conn = MagicMock()

mock_server = MagicMock()

mock_conn.compute.find_server.return_value = mock_server

delete_server(mock_conn, "test-server-id", force=False)

mock_conn.compute.find_server.assert_called_once_with("test-server-id")
mock_conn.compute.delete_server.assert_called_once_with(mock_server, False)
mock_conn.compute.wait_for_delete.assert_called_once_with(
mock_server, interval=5, wait=300
)


def test_force_delete_server():
mock_conn = MagicMock()

mock_server = MagicMock()

mock_conn.compute.find_server.return_value = mock_server

delete_server(mock_conn, "test-server-id", force=True)

mock_conn.compute.find_server.assert_called_once_with("test-server-id")
mock_conn.compute.delete_server.assert_called_once_with(mock_server, True)
mock_conn.compute.wait_for_delete.assert_called_once_with(
mock_server, interval=5, wait=300
)
67 changes: 67 additions & 0 deletions tests/lib/workflows/test_hv_create_test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from unittest.mock import MagicMock, patch

from workflows.hv_create_test_server import create_test_server


@patch("workflows.hv_create_test_server.delete_server")
@patch("workflows.hv_create_test_server.build_server")
@patch("workflows.hv_create_test_server.random")
@patch("workflows.hv_create_test_server.get_avaliable_flavors")
def test_create_single_test_server(
mock_get_avaliable_flavors, mock_random, mock_build_server, mock_delete_server
):
mock_conn = MagicMock()

mock_flavors = ["flavor1", "flavor2", "flavor3"]
mock_get_avaliable_flavors.return_value = mock_flavors

mock_random.choice.return_value = "flavor2"

mock_server = MagicMock()
mock_build_server.return_value = mock_server

create_test_server(mock_conn, "hvxyz.nubes.rl.ac.uk", False)

mock_get_avaliable_flavors.assert_called_once_with(
mock_conn, "hvxyz.nubes.rl.ac.uk"
)
mock_build_server.assert_called_once_with(
mock_conn,
"stackstorm-test-server",
"flavor2",
"ubuntu-focal-20.04-nogui",
"Internal",
"hvxyz.nubes.rl.ac.uk",
)
mock_delete_server.assert_called_once_with(mock_conn, mock_server.id)


@patch("workflows.hv_create_test_server.delete_server")
@patch("workflows.hv_create_test_server.build_server")
@patch("workflows.hv_create_test_server.get_avaliable_flavors")
def test_create_test_server_all_flavors(
mock_get_avaliable_flavors, mock_build_server, mock_delete_server
):
mock_conn = MagicMock()
mock_flavors = ["flavor1", "flavor2", "flavor3"]
mock_get_avaliable_flavors.return_value = mock_flavors

mock_server = MagicMock()
mock_build_server.return_value = mock_server

create_test_server(mock_conn, "hvxyz.nubes.rl.ac.uk", True)

mock_get_avaliable_flavors.assert_called_once_with(
mock_conn, "hvxyz.nubes.rl.ac.uk"
)

for flavor in mock_flavors:
mock_build_server.assert_any_call(
mock_conn,
"stackstorm-test-server",
flavor,
"ubuntu-focal-20.04-nogui",
"Internal",
"hvxyz.nubes.rl.ac.uk",
)
mock_delete_server.assert_any_call(mock_conn, mock_server.id)

0 comments on commit 033a68f

Please sign in to comment.