[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

View File

@@ -18,6 +18,7 @@
import base64
from unittest import mock
import ddt
import fixtures
from openstack.baremetal.v1 import node as _node
from openstack import exceptions as sdk_exc
@@ -96,6 +97,7 @@ def _make_compute_service(hostname):
return objects.Service(host=hostname)
@ddt.ddt
class IronicDriverTestCase(test.NoDBTestCase):
@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
self.mock_conn.set_node_provision_state.reset_mock()
def test_destroy(self):
for state in ironic_states.PROVISION_STATE_LIST:
self._test_destroy(state)
@ddt.data(*ironic_states.ALL_STATES)
def test_destroy(self, state):
self._test_destroy(state)
@mock.patch.object(ironic_driver.IronicDriver,
'_remove_instance_info_from_node')

View File

@@ -1373,7 +1373,7 @@ class IronicDriver(virt_driver.ComputeDriver):
return
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
# the things we do in _cleanup_deploy, so let's not repeat them
# here. Doing so would also race with the node cleaning process,
@@ -2240,7 +2240,7 @@ class IronicDriver(virt_driver.ComputeDriver):
try:
self.ironic_connection.set_node_provision_state(
node_id,
ironic_states.UNRESCUE,
ironic_states.VERBS['unrescue'],
)
except Exception as e:
raise exception.InstanceUnRescueFailure(reason=str(e))

View File

@@ -1,51 +1,77 @@
# Copyright (c) 2012 NTT DOCOMO, INC.
# Copyright 2010 OpenStack Foundation
# All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# 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
#
# 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
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# 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
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.
Constants for bare metal node states.
The `power_state` can also be set manually via the API. A failure to change
the state leaves the current state unchanged. The node is NOT placed into
maintenance mode in this case.
This module contains only state constant definitions with no executable code.
For the state machine implementation, see ironic.common.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
""" No state information.
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.
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'
""" Node is in a manageable state.
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
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'
""" 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'
""" 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.
"""
DEPLOYHOLD = 'deploy hold'
""" Node is being held by a deploy step. """
DELETING = 'deleting'
""" Node is actively being torn down. """
DELETED = 'deleted'
""" Node tear down was successful.
In Juno, target_provision_state was set to this value during node tear down.
In Kilo, this will be a transitory value of provision_state, and never
This is a transitory value of provision_state, and never
represented in target_provision_state.
"""
CLEANING = 'cleaning'
""" 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'
""" 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. """
CLEANHOLD = 'clean hold'
""" Node is being held by a cleaning step. """
""" Node is a holding state due to a clean step. """
ERROR = 'error'
""" 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'
""" Node is to be rebuilt.
This is not used as a state, but rather as a "verb" when changing the node's
provision_state via the REST API.
"""
INSPECTING = 'inspecting'
""" Node is under inspection.
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'
""" Node inspection failed. """
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'
""" Node is in rescue mode.
This is also used as a "verb" when changing the node's provision_state via the
REST API"""
""" Node is in rescue mode. """
RESCUEFAIL = 'rescue failed'
""" Node rescue failed. """
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'
""" Node is waiting to be 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.
"""
""" Node is in process of being rescued. """
UNRESCUEFAIL = 'unrescue failed'
""" Node unrescue failed. """
UNRESCUING = "unrescuing"
""" Node is unrescuing. """
UNRESCUING = 'unrescuing'
""" Node is being restored from rescue mode (to active state). """
DEPLOYHOLD = 'deploy hold'
""" Node is being held by a deploy step. """
SERVICE = 'service'
""" Node is being requested to be modified through a service step. """
SERVICING = 'servicing'
""" Node is actively being changed by a service step. """
@@ -178,15 +236,69 @@ SERVICEFAIL = 'service failed'
SERVICEHOLD = 'service hold'
""" 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'
""" Node driver attributes being verified.
Nova should never see a node in this state."""
"""All Node states related to servicing."""
SERVICING_STATES = frozenset((SERVICING, SERVICEWAIT,
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
##############
@@ -200,15 +312,32 @@ POWER_OFF = 'power off'
REBOOT = 'rebooting'
""" Node is rebooting. """
##################
# Helper constants
##################
SOFT_REBOOT = 'soft rebooting'
""" Node is rebooting gracefully. """
PROVISION_STATE_LIST = (NOSTATE, MANAGEABLE, AVAILABLE, ACTIVE, DEPLOYWAIT,
DEPLOYING, DEPLOYFAIL, DEPLOYDONE, DEPLOYHOLD,
DELETING, DELETED, CLEANING, CLEANWAIT, CLEANFAIL,
CLEANHOLD, ERROR, REBUILD, INSPECTING, INSPECTFAIL,
INSPECTWAIT, RESCUE, RESCUEFAIL, RESCUEWAIT, RESCUING,
SERVICING, SERVICEWAIT, SERVICEFAIL, SERVICEHOLD,
UNRESCUEFAIL, UNRESCUING, ENROLL, VERIFYING)
""" A list of all provision states. """
SOFT_POWER_OFF = 'soft power off'
""" Node is in the process of soft power off. """
###################
# Allocation 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"