Browse Source

Merge "Fix incorrect invocation of openstacksdk's baremetal.nodes()"

changes/10/680810/1
Zuul 1 week ago
parent
commit
ade53dc9cd
2 changed files with 50 additions and 34 deletions
  1. 32
    22
      nova/tests/unit/virt/ironic/test_driver.py
  2. 18
    12
      nova/virt/ironic/driver.py

+ 32
- 22
nova/tests/unit/virt/ironic/test_driver.py View File

@@ -190,8 +190,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
190 190
         result = self.driver._validate_instance_and_node(instance)
191 191
         self.assertEqual(result.uuid, node_id)
192 192
         self.mock_conn.nodes.assert_called_once_with(
193
-            {"instance_id": instance.uuid,
194
-             "fields": ironic_driver._NODE_FIELDS})
193
+            instance_id=instance.uuid,
194
+            fields=ironic_driver._NODE_FIELDS)
195 195
 
196 196
     def test__validate_instance_and_node_failed(self):
197 197
         self.mock_conn.nodes.return_value = iter([])
@@ -200,8 +200,18 @@ class IronicDriverTestCase(test.NoDBTestCase):
200 200
         self.assertRaises(exception.InstanceNotFound,
201 201
                           self.driver._validate_instance_and_node, instance)
202 202
         self.mock_conn.nodes.assert_called_once_with(
203
-            {"instance_id": instance.uuid,
204
-             "fields": ironic_driver._NODE_FIELDS})
203
+            instance_id=instance.uuid,
204
+            fields=ironic_driver._NODE_FIELDS)
205
+
206
+    def test__validate_instance_and_node_unexpected_many_nodes(self):
207
+        self.mock_conn.nodes.return_value = iter(['1', '2', '3'])
208
+        instance = fake_instance.fake_instance_obj(self.ctx,
209
+                                                   uuid=self.instance_id)
210
+        self.assertRaises(exception.NovaException,
211
+                          self.driver._validate_instance_and_node, instance)
212
+        self.mock_conn.nodes.assert_called_once_with(
213
+            instance_id=instance.uuid,
214
+            fields=ironic_driver._NODE_FIELDS)
205 215
 
206 216
     @mock.patch.object(objects.Instance, 'refresh')
207 217
     @mock.patch.object(ironic_driver.IronicDriver,
@@ -528,8 +538,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
528 538
 
529 539
         self.assertTrue(self.driver.instance_exists(instance))
530 540
         self.mock_conn.nodes.assert_called_once_with(
531
-            {"instance_id": instance.uuid,
532
-             "fields": ironic_driver._NODE_FIELDS})
541
+            instance_id=instance.uuid,
542
+            fields=ironic_driver._NODE_FIELDS)
533 543
 
534 544
     def test_instance_exists_fail(self):
535 545
         instance = fake_instance.fake_instance_obj(self.ctx,
@@ -538,8 +548,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
538 548
 
539 549
         self.assertFalse(self.driver.instance_exists(instance))
540 550
         self.mock_conn.nodes.assert_called_once_with(
541
-            {"instance_id": instance.uuid,
542
-             "fields": ironic_driver._NODE_FIELDS})
551
+            instance_id=instance.uuid,
552
+            fields=ironic_driver._NODE_FIELDS)
543 553
 
544 554
     def test__get_node_list(self):
545 555
         test_nodes = [ironic_utils.get_test_node(
@@ -1121,8 +1131,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
1121 1131
         self.assertEqual(hardware.InstanceInfo(state=nova_states.RUNNING),
1122 1132
                          result)
1123 1133
         self.mock_conn.nodes.assert_called_once_with(
1124
-            {"instance_id": instance.uuid,
1125
-             "fields": ironic_driver._NODE_FIELDS})
1134
+            instance_id=instance.uuid,
1135
+            fields=ironic_driver._NODE_FIELDS)
1126 1136
 
1127 1137
     @mock.patch.object(ironic_driver.IronicDriver, '_get_node_list')
1128 1138
     @mock.patch.object(objects.InstanceList, 'get_uuids_by_host')
@@ -1176,8 +1186,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
1176 1186
         self.assertEqual(hardware.InstanceInfo(state=nova_states.RUNNING),
1177 1187
                          result)
1178 1188
         self.mock_conn.nodes.assert_called_once_with(
1179
-            {"instance_id": instance.uuid,
1180
-             "fields": ironic_driver._NODE_FIELDS})
1189
+            instance_id=instance.uuid,
1190
+            fields=ironic_driver._NODE_FIELDS)
1181 1191
 
1182 1192
     @mock.patch.object(ironic_driver.IronicDriver, '_get_node_list')
1183 1193
     @mock.patch.object(objects.InstanceList, 'get_uuids_by_host')
@@ -1204,8 +1214,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
1204 1214
                          result)
1205 1215
         # verify we hit the ironic API for fresh data
1206 1216
         self.mock_conn.nodes.assert_called_once_with(
1207
-            {"instance_id": instance.uuid,
1208
-             "fields": ironic_driver._NODE_FIELDS})
1217
+            instance_id=instance.uuid,
1218
+            fields=ironic_driver._NODE_FIELDS)
1209 1219
 
1210 1220
     @mock.patch.object(objects.InstanceList, 'get_uuids_by_host')
1211 1221
     @mock.patch.object(objects.ServiceList, 'get_all_computes_by_hv_type')
@@ -1223,8 +1233,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
1223 1233
         self.assertEqual(hardware.InstanceInfo(state=nova_states.NOSTATE),
1224 1234
                          result)
1225 1235
         self.mock_conn.nodes.assert_has_calls([mock.call(
1226
-            {"instance_id": instance.uuid,
1227
-             "fields": ironic_driver._NODE_FIELDS})])
1236
+            instance_id=instance.uuid,
1237
+            fields=ironic_driver._NODE_FIELDS)])
1228 1238
 
1229 1239
     @mock.patch.object(objects.Instance, 'save')
1230 1240
     @mock.patch.object(loopingcall, 'FixedIntervalLoopingCall')
@@ -1742,8 +1752,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
1742 1752
         self.driver.destroy(self.ctx, instance, network_info, None)
1743 1753
 
1744 1754
         self.mock_conn.nodes.assert_called_with(
1745
-            {"instance_id": instance.uuid,
1746
-             "fields": ironic_driver._NODE_FIELDS})
1755
+            instance_id=instance.uuid,
1756
+            fields=ironic_driver._NODE_FIELDS)
1747 1757
         mock_cleanup_deploy.assert_called_with(node, instance, network_info,
1748 1758
                                                remove_instance_info=False)
1749 1759
 
@@ -1870,8 +1880,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
1870 1880
         mock_node.set_provision_state.assert_called_once_with(node_id,
1871 1881
                                                               'deleted')
1872 1882
         self.mock_conn.nodes.assert_called_once_with(
1873
-            {"instance_id": instance.uuid,
1874
-             "fields": ironic_driver._NODE_FIELDS})
1883
+            instance_id=instance.uuid,
1884
+            fields=ironic_driver._NODE_FIELDS)
1875 1885
 
1876 1886
     @mock.patch.object(loopingcall, 'FixedIntervalLoopingCall')
1877 1887
     @mock.patch.object(ironic_driver.IronicDriver,
@@ -2626,8 +2636,8 @@ class IronicDriverTestCase(test.NoDBTestCase):
2626 2636
         self.driver.failed_spawn_cleanup(instance)
2627 2637
 
2628 2638
         self.mock_conn.nodes.assert_called_once_with(
2629
-            {"instance_id": instance.uuid,
2630
-             "fields": ironic_driver._NODE_FIELDS})
2639
+            instance_id=instance.uuid,
2640
+            fields=ironic_driver._NODE_FIELDS)
2631 2641
         self.assertEqual(1, mock_cleanup.call_count)
2632 2642
 
2633 2643
     @mock.patch.object(ironic_driver.IronicDriver, '_stop_firewall')

+ 18
- 12
nova/virt/ironic/driver.py View File

@@ -234,19 +234,25 @@ class IronicDriver(virt_driver.ComputeDriver):
234 234
         Check with the Ironic service that this instance is associated with a
235 235
         node, and return the node.
236 236
         """
237
-        try:
238
-            node = next(self.ironic_connection.nodes(
239
-                {"instance_id": instance.uuid, "fields": _NODE_FIELDS}))
240
-            # TODO(dustinc): Make consumers use the right fields and remove
241
-            if hasattr(node, "id"):
242
-                node.uuid = node.id
243
-            if hasattr(node, "instance_id"):
244
-                node.instance_uuid = node.instance_id
245
-            if hasattr(node, "is_maintenance"):
246
-                node.maintenance = node.is_maintenance
247
-            return node
248
-        except StopIteration:
237
+        nodes = list(self.ironic_connection.nodes(
238
+            instance_id=instance.uuid, fields=_NODE_FIELDS))
239
+        if not nodes:
249 240
             raise exception.InstanceNotFound(instance_id=instance.uuid)
241
+        if len(nodes) > 1:
242
+            # This indicates a programming error so fail.
243
+            raise exception.NovaException(
244
+                _('Ironic returned more than one node for a query '
245
+                  'that can only return zero or one: %s') % nodes)
246
+
247
+        node = nodes[0]
248
+        # TODO(dustinc): Make consumers use the right fields and remove
249
+        if hasattr(node, "id"):
250
+            node.uuid = node.id
251
+        if hasattr(node, "instance_id"):
252
+            node.instance_uuid = node.instance_id
253
+        if hasattr(node, "is_maintenance"):
254
+            node.maintenance = node.is_maintenance
255
+        return node
250 256
 
251 257
     def _node_resources_unavailable(self, node_obj):
252 258
         """Determine whether the node's resources are in an acceptable state.

Loading…
Cancel
Save