Add unit tests for instance and volume not found in model

The Zone Migration strategy was implemented to list all
instances and volumes from clients (nova and cinder) and
check if they exist in the models. But the code is not
properly treating model exceptions, taking audit to a failure
state when the model doesn't have the requested element.
This patch adds unit tests to validate this scenario, which
should be fixed in a follow up change.
The additional check for volumes in the model was recently
added in [1]

[1] cb6fb16097

Related-Bug: #2098984

Assisted-By: Cursor (claude-3.5-sonnet)

Change-Id: Icf1e5d4c83862c848d11dae994842ad0ee62ba12
Signed-off-by: Douglas Viroel <viroel@gmail.com>
This commit is contained in:
Douglas Viroel
2025-07-30 17:02:03 -03:00
parent 8211475478
commit cada9acced

View File

@@ -17,6 +17,7 @@ from unittest import mock
import cinderclient
import novaclient
from watcher.common import exception as watcher_exception
from watcher.common import utils
from watcher.decision_engine.strategy import strategies
from watcher.tests.decision_engine.model import faker_cluster_state
@@ -157,6 +158,40 @@ class TestZoneMigration(TestBaseStrategy):
self.assertIn(instance_on_src2, instances)
self.assertNotIn(instance_on_src3, instances)
def test_get_instances_with_instance_not_found(self):
# Create a test instance without a known id
# so we expect it to not be in the model
instance_on_src1 = self.fake_instance(
host="src1",
name="INSTANCE_1")
# Mock nova helper to return our test instance
self.m_n_helper.get_instance_list.return_value = [instance_on_src1]
# FIXME(dviroel): Fix this test together with bug #2098984
# It should return an empty list and not raise an exception
# Verify that the instance does not exist in the model
# instances = self.strategy.get_instances()
# self.assertEqual([], instances)
self.assertRaises(
watcher_exception.InstanceNotFound,
self.strategy.get_instances)
def test_get_volumes_with_volume_not_found(self):
# Create a test volume without an known id
# so we expect it to not be in the model
volume_on_src1 = self.fake_volume(
host="src1@back1#pool1",
name="volume_1")
# Mock cinder helper to return our tets volume
self.m_c_helper.get_volume_list.return_value = [volume_on_src1]
# Call get_volumes and verify the volume does not exist in the model
volumes = self.strategy.get_volumes()
self.assertEqual([], volumes)
def test_get_volumes(self):
volume_on_src1 = self.fake_volume(host="src1@back1#pool1",
id=volume_uuid_mapping["volume_1"],