diff --git a/nova/cmd/status.py b/nova/cmd/status.py index 38a506771acb..168df57a5990 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) as cctxt: - num_computes += self._count_compute_nodes(cctxt) + if cell_mappings: + for cell_mapping in cell_mappings: + with nova_context.target_cell(ctxt, cell_mapping) as cctxt: + num_computes += self._count_compute_nodes(cctxt) 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 499a02bf18be..4fa6fd6a4a17 100644 --- a/nova/tests/unit/cmd/test_status.py +++ b/nova/tests/unit/cmd/test_status.py @@ -653,6 +653,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)