Browse Source

Merge "fix cellv2 delete_host" into stable/queens

tags/17.0.12
Zuul 1 month ago
parent
commit
62ae97e63a
2 changed files with 37 additions and 2 deletions
  1. 4
    1
      nova/cmd/manage.py
  2. 33
    1
      nova/tests/unit/test_nova_manage.py

+ 4
- 1
nova/cmd/manage.py View File

@@ -1593,7 +1593,10 @@ class CellV2Commands(object):
1593 1593
 
1594 1594
         with context.target_cell(ctxt, cell_mapping) as cctxt:
1595 1595
             instances = objects.InstanceList.get_by_host(cctxt, host)
1596
-            nodes = objects.ComputeNodeList.get_all_by_host(cctxt, host)
1596
+            try:
1597
+                nodes = objects.ComputeNodeList.get_all_by_host(cctxt, host)
1598
+            except exception.ComputeHostNotFound:
1599
+                nodes = []
1597 1600
 
1598 1601
         if instances:
1599 1602
             print(_('There are instances on the host %s.') % host)

+ 33
- 1
nova/tests/unit/test_nova_manage.py View File

@@ -2075,7 +2075,7 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
2075 2075
     @mock.patch.object(objects.ComputeNodeList, 'get_all_by_host')
2076 2076
     def test_delete_host_success(self, mock_get_cn, mock_destroy,
2077 2077
                                  mock_get_by_host):
2078
-        """Tests trying to delete a host that has not instances."""
2078
+        """Tests trying to delete a host that has no instances."""
2079 2079
         ctxt = context.get_admin_context()
2080 2080
         # create the cell mapping
2081 2081
         cm1 = objects.CellMapping(
@@ -2100,6 +2100,38 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
2100 2100
             self.assertEqual(0, node.mapped)
2101 2101
             node.save.assert_called_once_with()
2102 2102
 
2103
+    @mock.patch.object(objects.InstanceList, 'get_by_host',
2104
+                       return_value=[])
2105
+    @mock.patch.object(objects.HostMapping, 'destroy')
2106
+    @mock.patch.object(objects.ComputeNodeList, 'get_all_by_host',
2107
+        side_effect=exception.ComputeHostNotFound(host='fake-host'))
2108
+    def test_delete_host_success_compute_host_not_found(self, mock_get_cn,
2109
+                                                        mock_destroy,
2110
+                                                        mock_get_by_host):
2111
+        """Tests trying to delete a host that has no instances, but cannot
2112
+           be found by ComputeNodeList.get_all_by_host.
2113
+        """
2114
+        ctxt = context.get_admin_context()
2115
+        # create the cell mapping
2116
+        cm1 = objects.CellMapping(
2117
+            context=ctxt, uuid=uuidsentinel.cell1,
2118
+            database_connection='fake:///db', transport_url='fake:///mq')
2119
+        cm1.create()
2120
+        # create a host mapping in the cell
2121
+        hm = objects.HostMapping(
2122
+            context=ctxt, host='fake-host', cell_mapping=cm1)
2123
+        hm.create()
2124
+
2125
+        self.assertEqual(0, self.commands.delete_host(uuidsentinel.cell1,
2126
+                                                      'fake-host'))
2127
+        output = self.output.getvalue().strip()
2128
+        self.assertEqual('', output)
2129
+        mock_get_by_host.assert_called_once_with(
2130
+            test.MatchType(context.RequestContext), 'fake-host')
2131
+        mock_destroy.assert_called_once_with()
2132
+        mock_get_cn.assert_called_once_with(
2133
+            test.MatchType(context.RequestContext), 'fake-host')
2134
+
2103 2135
 
2104 2136
 class TestNovaManageMain(test.NoDBTestCase):
2105 2137
     """Tests the nova-manage:main() setup code."""

Loading…
Cancel
Save