diff --git a/nova/cmd/status.py b/nova/cmd/status.py index 20caab01b5ec..1163733883eb 100644 --- a/nova/cmd/status.py +++ b/nova/cmd/status.py @@ -287,9 +287,10 @@ class UpgradeCommands(object): cell_mappings = self._get_non_cell0_mappings() ctxt = nova_context.get_admin_context() num_computes = 0 - for cell_mapping in cell_mappings: - with nova_context.target_cell(ctxt, cell_mapping): - num_computes += self._count_compute_nodes(ctxt) + if cell_mappings: + for cell_mapping in cell_mappings: + with nova_context.target_cell(ctxt, cell_mapping): + num_computes += self._count_compute_nodes(ctxt) else: # There are no cell mappings, cells v2 was maybe not deployed in # Newton, but placement might have been, so let's check the single diff --git a/nova/tests/unit/cmd/test_status.py b/nova/tests/unit/cmd/test_status.py index ce742f4ea947..eb95208162d3 100644 --- a/nova/tests/unit/cmd/test_status.py +++ b/nova/tests/unit/cmd/test_status.py @@ -632,6 +632,17 @@ class TestUpgradeCheckResourceProviders(test.NoDBTestCase): # create an externally shared IP allocation pool resource provider self._create_resource_provider(FAKE_IP_POOL_INVENTORY) + # Stub out _count_compute_nodes to make sure we never call it without + # a cell-targeted context. + original_count_compute_nodes = ( + status.UpgradeCommands._count_compute_nodes) + + def stub_count_compute_nodes(_self, context=None): + self.assertIsNotNone(context.db_connection) + return original_count_compute_nodes(_self, context=context) + self.stub_out('nova.cmd.status.UpgradeCommands._count_compute_nodes', + stub_count_compute_nodes) + result = self.cmd._check_resource_providers() self.assertEqual(status.UpgradeCheckCode.SUCCESS, result.code) self.assertIsNone(result.details)