[ironic] Use constants from Ironic, test w/ddt

Follow-ups for the original fix to bug #2131960. Includes an update to
make tests match Nova style (ddt) and another to unify state constant
code between Ironic and Nova.

The new ironic_states.py was copied from ironic/common/states.py from
commit 2622864b70bc66ec711045a3a0bf30478b27b9cd in ironic.

Change-Id: I49fba64da039e5c54967b6f5c56cb44927f815c3
Signed-off-by: Jay Faulkner <jay@jvf.cc>
This commit is contained in:
Jay Faulkner
2025-12-02 13:33:41 -08:00
parent 23b462d77d
commit 21cebb7725
3 changed files with 200 additions and 69 deletions
+5 -3
View File
@@ -18,6 +18,7 @@
import base64 import base64
from unittest import mock from unittest import mock
import ddt
import fixtures import fixtures
from openstack.baremetal.v1 import node as _node from openstack.baremetal.v1 import node as _node
from openstack import exceptions as sdk_exc from openstack import exceptions as sdk_exc
@@ -96,6 +97,7 @@ def _make_compute_service(hostname):
return objects.Service(host=hostname) return objects.Service(host=hostname)
@ddt.ddt
class IronicDriverTestCase(test.NoDBTestCase): class IronicDriverTestCase(test.NoDBTestCase):
@mock.patch.object(ironic_driver.IronicDriver, '_refresh_hash_ring') @mock.patch.object(ironic_driver.IronicDriver, '_refresh_hash_ring')
@@ -1742,9 +1744,9 @@ class IronicDriverTestCase(test.NoDBTestCase):
# we call this innter function twice so we need to reset mocks # we call this innter function twice so we need to reset mocks
self.mock_conn.set_node_provision_state.reset_mock() self.mock_conn.set_node_provision_state.reset_mock()
def test_destroy(self): @ddt.data(*ironic_states.ALL_STATES)
for state in ironic_states.PROVISION_STATE_LIST: def test_destroy(self, state):
self._test_destroy(state) self._test_destroy(state)
@mock.patch.object(ironic_driver.IronicDriver, @mock.patch.object(ironic_driver.IronicDriver,
'_remove_instance_info_from_node') '_remove_instance_info_from_node')
+2 -2
View File
@@ -1373,7 +1373,7 @@ class IronicDriver(virt_driver.ComputeDriver):
return return
if (node.provision_state in _UNPROVISION_STATES or if (node.provision_state in _UNPROVISION_STATES or
node.provision_state not in ironic_states.PROVISION_STATE_LIST): node.provision_state not in ironic_states.ALL_STATES):
# NOTE(mgoddard): Ironic's node tear-down procedure includes all of # NOTE(mgoddard): Ironic's node tear-down procedure includes all of
# the things we do in _cleanup_deploy, so let's not repeat them # the things we do in _cleanup_deploy, so let's not repeat them
# here. Doing so would also race with the node cleaning process, # here. Doing so would also race with the node cleaning process,
@@ -2240,7 +2240,7 @@ class IronicDriver(virt_driver.ComputeDriver):
try: try:
self.ironic_connection.set_node_provision_state( self.ironic_connection.set_node_provision_state(
node_id, node_id,
ironic_states.UNRESCUE, ironic_states.VERBS['unrescue'],
) )
except Exception as e: except Exception as e:
raise exception.InstanceUnRescueFailure(reason=str(e)) raise exception.InstanceUnRescueFailure(reason=str(e))
+193 -64
View File
@@ -1,51 +1,77 @@
# Copyright (c) 2012 NTT DOCOMO, INC. # Licensed under the Apache License, Version 2.0 (the "License"); you may
# Copyright 2010 OpenStack Foundation # not use this file except in compliance with the License. You may obtain
# All Rights Reserved. # a copy of the License at
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # http://www.apache.org/licenses/LICENSE-2.0
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software
# # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# Unless required by applicable law or agreed to in writing, software # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # License for the specific language governing permissions and limitations
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # under the License.
# License for the specific language governing permissions and limitations
# under the License.
""" """
Mapping of bare metal node states. NOTE(JayF): This file is verbatim copied from ironic/common/states.py at
c69caf28e88565fbf5cd2b4ee71ba49fc13738c7 and should not be
modified except by copying newer versions from ironic.
Setting the node `power_state` is handled by the conductor's power Constants for bare metal node states.
synchronization thread. Based on the power state retrieved from the driver
for the node, the state is set to POWER_ON or POWER_OFF, accordingly.
Should this fail, the `power_state` value is left unchanged, and the node
is placed into maintenance mode.
The `power_state` can also be set manually via the API. A failure to change This module contains only state constant definitions with no executable code.
the state leaves the current state unchanged. The node is NOT placed into For the state machine implementation, see ironic.common.states.
maintenance mode in this case.
""" """
##################### #####################
# Provisioning states # Provisioning states
##################### #####################
VERBS = {
'active': 'deploy',
'deploy': 'deploy',
'deleted': 'delete',
'undeploy': 'delete',
'manage': 'manage',
'provide': 'provide',
'inspect': 'inspect',
'abort': 'abort',
'clean': 'clean',
'adopt': 'adopt',
'rescue': 'rescue',
'unrescue': 'unrescue',
'unhold': 'unhold',
'service': 'service',
}
""" Mapping of state-changing events that are PUT to the REST API
This is a mapping of target states which are PUT to the API, eg,
PUT /v1/node/states/provision {'target': 'active'}
The dict format is:
{target string used by the API: internal verb}
This provides a reference set of supported actions, and in the future
may be used to support renaming these actions.
"""
NOSTATE = None NOSTATE = None
""" No state information. """ No state information.
This state is used with power_state to represent a lack of knowledge of This state is used with power_state to represent a lack of knowledge of
power state, and in target_*_state fields when there is no target. power state, and in target_*_state fields when there is no target.
Prior to the Kilo release, Ironic set node.provision_state to NOSTATE
when the node was available for provisioning. During Kilo cycle, this was
changed to the AVAILABLE state.
""" """
ENROLL = 'enroll'
""" Node is enrolled.
This state indicates that Ironic is aware of a node, but is not managing it.
"""
VERIFYING = 'verifying'
""" Node power management credentials are being verified. """
MANAGEABLE = 'manageable' MANAGEABLE = 'manageable'
""" Node is in a manageable state. """ Node is in a manageable state.
This state indicates that Ironic has verified, at least once, that it had This state indicates that Ironic has verified, at least once, that it had
sufficient information to manage the hardware. While in this state, the node sufficient information to manage the hardware. While in this state, the node
is not available for provisioning (it must be in the AVAILABLE state for that). is not available for provisioning (it must be in the AVAILABLE state for that).
@@ -60,6 +86,11 @@ This state is replacing the NOSTATE state used prior to Kilo.
ACTIVE = 'active' ACTIVE = 'active'
""" Node is successfully deployed and associated with an instance. """ """ Node is successfully deployed and associated with an instance. """
DEPLOY = 'deploy'
""" Node is successfully deployed and associated with an instance.
This is an alias for ACTIVE.
"""
DEPLOYWAIT = 'wait call-back' DEPLOYWAIT = 'wait call-back'
""" Node is waiting to be deployed. """ Node is waiting to be deployed.
@@ -86,20 +117,28 @@ This is mainly a target provision state used during deployment. A successfully
deployed node should go to ACTIVE status. deployed node should go to ACTIVE status.
""" """
DEPLOYHOLD = 'deploy hold'
""" Node is being held by a deploy step. """
DELETING = 'deleting' DELETING = 'deleting'
""" Node is actively being torn down. """ """ Node is actively being torn down. """
DELETED = 'deleted' DELETED = 'deleted'
""" Node tear down was successful. """ Node tear down was successful.
In Juno, target_provision_state was set to this value during node tear down. This is a transitory value of provision_state, and never
In Kilo, this will be a transitory value of provision_state, and never
represented in target_provision_state. represented in target_provision_state.
""" """
CLEANING = 'cleaning' CLEANING = 'cleaning'
""" Node is being automatically cleaned to prepare it for provisioning. """ """ Node is being automatically cleaned to prepare it for provisioning. """
UNDEPLOY = 'undeploy'
""" Node tear down process has started.
This is an alias for DELETED.
"""
CLEANWAIT = 'clean wait' CLEANWAIT = 'clean wait'
""" Node is waiting for a clean step to be finished. """ Node is waiting for a clean step to be finished.
@@ -111,7 +150,7 @@ CLEANFAIL = 'clean failed'
""" Node failed cleaning. This requires operator intervention to resolve. """ """ Node failed cleaning. This requires operator intervention to resolve. """
CLEANHOLD = 'clean hold' CLEANHOLD = 'clean hold'
""" Node is being held by a cleaning step. """ """ Node is a holding state due to a clean step. """
ERROR = 'error' ERROR = 'error'
""" An error occurred during node processing. """ An error occurred during node processing.
@@ -121,50 +160,69 @@ The `last_error` attribute of the node details should contain an error message.
REBUILD = 'rebuild' REBUILD = 'rebuild'
""" Node is to be rebuilt. """ Node is to be rebuilt.
This is not used as a state, but rather as a "verb" when changing the node's This is not used as a state, but rather as a "verb" when changing the node's
provision_state via the REST API. provision_state via the REST API.
""" """
INSPECTING = 'inspecting' INSPECTING = 'inspecting'
""" Node is under inspection. """ Node is under inspection.
This is the provision state used when inspection is started. A successfully This is the provision state used when inspection is started. A successfully
inspected node shall transition to MANAGEABLE status. inspected node shall transition to MANAGEABLE state. For asynchronous
inspection, node shall transition to INSPECTWAIT state.
""" """
INSPECTFAIL = 'inspect failed' INSPECTFAIL = 'inspect failed'
""" Node inspection failed. """ """ Node inspection failed. """
INSPECTWAIT = 'inspect wait' INSPECTWAIT = 'inspect wait'
""" Node is waiting for inspection callback. """ """ Node is under inspection.
This is the provision state used when an asynchronous inspection is in
progress. A successfully inspected node shall transition to MANAGEABLE state.
"""
ADOPTING = 'adopting'
""" Node is being adopted.
This provision state is intended for use to move a node from MANAGEABLE to
ACTIVE state to permit designation of nodes as being "managed" by Ironic,
however "deployed" previously by external means.
"""
ADOPTFAIL = 'adopt failed'
""" Node failed to complete the adoption process.
This state is the resulting state of a node that failed to complete adoption,
potentially due to invalid or incompatible information being defined for the
node.
"""
RESCUE = 'rescue' RESCUE = 'rescue'
""" Node is in rescue mode. """ Node is in rescue mode. """
This is also used as a "verb" when changing the node's provision_state via the
REST API"""
RESCUEFAIL = 'rescue failed' RESCUEFAIL = 'rescue failed'
""" Node rescue failed. """ """ Node rescue failed. """
RESCUEWAIT = 'rescue wait' RESCUEWAIT = 'rescue wait'
""" Node is waiting for rescue callback. """ """ Node is waiting on an external callback.
This will be the node `provision_state` while the node is waiting for
the driver to finish rescuing the node.
"""
RESCUING = 'rescuing' RESCUING = 'rescuing'
""" Node is waiting to be rescued. """ """ Node is in process of being rescued. """
UNRESCUE = 'unrescue'
""" Node is to be unrescued.
This is not used as a state, but rather as a "verb" when changing the node's
provision_state via the REST API.
"""
UNRESCUEFAIL = 'unrescue failed' UNRESCUEFAIL = 'unrescue failed'
""" Node unrescue failed. """ """ Node unrescue failed. """
UNRESCUING = "unrescuing" UNRESCUING = 'unrescuing'
""" Node is unrescuing. """ """ Node is being restored from rescue mode (to active state). """
DEPLOYHOLD = 'deploy hold' SERVICE = 'service'
""" Node is being held by a deploy step. """ """ Node is being requested to be modified through a service step. """
SERVICING = 'servicing' SERVICING = 'servicing'
""" Node is actively being changed by a service step. """ """ Node is actively being changed by a service step. """
@@ -178,15 +236,69 @@ SERVICEFAIL = 'service failed'
SERVICEHOLD = 'service hold' SERVICEHOLD = 'service hold'
""" Node is being held for direct intervention from a service step. """ """ Node is being held for direct intervention from a service step. """
ENROLL = 'enroll'
""" Node being entrolled into Ironic.
Nova should never see a node in this state."""
VERIFYING = 'verifying' """All Node states related to servicing."""
""" Node driver attributes being verified. SERVICING_STATES = frozenset((SERVICING, SERVICEWAIT,
Nova should never see a node in this state.""" SERVICEFAIL, SERVICEHOLD))
# NOTE(kaifeng): INSPECTING is allowed to keep backwards compatibility,
# starting from API 1.39 node update is disallowed in this state.
UPDATE_ALLOWED_STATES = (DEPLOYFAIL, INSPECTING, INSPECTFAIL, INSPECTWAIT,
CLEANFAIL, ERROR, VERIFYING, ADOPTFAIL, RESCUEFAIL,
UNRESCUEFAIL, SERVICE, SERVICEHOLD, SERVICEFAIL)
"""Transitional states in which we allow updating a node."""
DELETE_ALLOWED_STATES = (MANAGEABLE, ENROLL, ADOPTFAIL)
"""States in which node deletion is allowed."""
STABLE_STATES = (ENROLL, MANAGEABLE, AVAILABLE, ACTIVE, ERROR, RESCUE)
"""States that will not transition unless receiving a request."""
UNSTABLE_STATES = (DEPLOYING, DEPLOYWAIT, CLEANING, CLEANWAIT, VERIFYING,
DELETING, INSPECTING, INSPECTWAIT, ADOPTING, RESCUING,
RESCUEWAIT, UNRESCUING, SERVICING, SERVICEWAIT)
"""States that can be changed without external request."""
STUCK_STATES_TREATED_AS_FAIL = (DEPLOYING, CLEANING, VERIFYING, INSPECTING,
ADOPTING, RESCUING, UNRESCUING, DELETING,
SERVICING)
"""States that cannot be resumed once a conductor dies.
If a node gets stuck with one of these states for some reason
(eg. conductor goes down when executing task), node will be moved
to fail state.
"""
_LOOKUP_ALLOWED_STATES = (DEPLOYING, DEPLOYWAIT, CLEANING, CLEANWAIT,
INSPECTING, INSPECTWAIT, RESCUING, RESCUEWAIT)
LOOKUP_ALLOWED_STATES = frozenset(_LOOKUP_ALLOWED_STATES)
"""States when API lookups are normally allowed for nodes."""
_FASTTRACK_LOOKUP_ALLOWED_STATES = (ENROLL, MANAGEABLE, AVAILABLE,
DEPLOYING, DEPLOYWAIT,
CLEANING, CLEANWAIT,
INSPECTING, INSPECTWAIT,
RESCUING, RESCUEWAIT,
SERVICING, SERVICEWAIT,
SERVICEHOLD)
FASTTRACK_LOOKUP_ALLOWED_STATES = frozenset(_FASTTRACK_LOOKUP_ALLOWED_STATES)
"""States where API lookups are permitted with fast track enabled."""
FAILURE_STATES = frozenset((DEPLOYFAIL, CLEANFAIL, INSPECTFAIL,
RESCUEFAIL, UNRESCUEFAIL, ADOPTFAIL,
SERVICEFAIL))
# NOTE(JayF) This isn't used in Ironic, but is used in Nova as a copy of this
# file will be proposed into the nova driver.
ALL_STATES = frozenset((ACTIVE, ADOPTFAIL, ADOPTING, AVAILABLE, CLEANFAIL,
CLEANHOLD, CLEANING, CLEANWAIT, DELETED, DELETING, DEPLOYDONE, DEPLOYFAIL,
DEPLOYHOLD, DEPLOYING, DEPLOYWAIT, ENROLL, ERROR, INSPECTFAIL, INSPECTING,
INSPECTWAIT, MANAGEABLE, RESCUE, RESCUEFAIL, RESCUING, RESCUEWAIT,
SERVICEFAIL, SERVICEHOLD, SERVICING, SERVICEWAIT, UNRESCUEFAIL,
UNRESCUING, VERIFYING)) # noqa
############## ##############
# Power states # Power states
############## ##############
@@ -200,15 +312,32 @@ POWER_OFF = 'power off'
REBOOT = 'rebooting' REBOOT = 'rebooting'
""" Node is rebooting. """ """ Node is rebooting. """
################## SOFT_REBOOT = 'soft rebooting'
# Helper constants """ Node is rebooting gracefully. """
##################
PROVISION_STATE_LIST = (NOSTATE, MANAGEABLE, AVAILABLE, ACTIVE, DEPLOYWAIT, SOFT_POWER_OFF = 'soft power off'
DEPLOYING, DEPLOYFAIL, DEPLOYDONE, DEPLOYHOLD, """ Node is in the process of soft power off. """
DELETING, DELETED, CLEANING, CLEANWAIT, CLEANFAIL,
CLEANHOLD, ERROR, REBUILD, INSPECTING, INSPECTFAIL, ###################
INSPECTWAIT, RESCUE, RESCUEFAIL, RESCUEWAIT, RESCUING, # Allocation states
SERVICING, SERVICEWAIT, SERVICEFAIL, SERVICEHOLD, ###################
UNRESCUEFAIL, UNRESCUING, ENROLL, VERIFYING)
""" A list of all provision states. """ ALLOCATING = 'allocating'
# States ERROR and ACTIVE are reused.
###########################
# History Event State Types
###########################
PROVISIONING = "provisioning"
TAKEOVER = "takeover"
INTROSPECTION = "introspection"
CONDUCTOR = "conductor"
TRANSITION = "transition"
STARTFAIL = "startup failure"
UNPROVISION = "unprovision"
ADOPTION = "adoption"
CONSOLE = "console"
MONITORING = "monitoring"
VERIFY = "verify"