From cada9acced3f6f645d606504498e4fe1f4236386 Mon Sep 17 00:00:00 2001 From: Douglas Viroel Date: Wed, 30 Jul 2025 17:02:03 -0300 Subject: [PATCH] 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] https://github.com/openstack/watcher/commit/cb6fb1609706495a6187b973a55e270257be64e1 Related-Bug: #2098984 Assisted-By: Cursor (claude-3.5-sonnet) Change-Id: Icf1e5d4c83862c848d11dae994842ad0ee62ba12 Signed-off-by: Douglas Viroel --- .../strategies/test_zone_migration.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/watcher/tests/decision_engine/strategy/strategies/test_zone_migration.py b/watcher/tests/decision_engine/strategy/strategies/test_zone_migration.py index e478b4fe9..6e36138d0 100644 --- a/watcher/tests/decision_engine/strategy/strategies/test_zone_migration.py +++ b/watcher/tests/decision_engine/strategy/strategies/test_zone_migration.py @@ -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"],