Skip to content

Commit a671c4a

Browse files
committed
fix(ironic): record failed HA checks in last_error, not extra
Adds tests for the HA validator.
1 parent 8f0cae5 commit a671c4a

4 files changed

Lines changed: 124 additions & 16 deletions

File tree

‎python/ironic-understack/ironic_understack/drivers/panos_inspect.py‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,8 @@ def _validate_and_pair_ha(
792792
4. Interface speed validation (hsci vs data plane)
793793
5. Pre-flight compatibility checks (resource_class, roles)
794794
795-
Sets extra.ha_operational = True only when ALL checks pass.
795+
Sets extra.ha_operational = True only when ALL checks pass;
796+
otherwise the reason goes in last_error.
796797
Does NOT modify mate node's extra fields or transition states.
797798
The enroll-fw workflow will read ha_operational from both devices
798799
and decide whether to promote.
@@ -815,7 +816,7 @@ def _validate_and_pair_ha(
815816
f"ha1-b={ha1_b_state}, hsci={hsci_state}"
816817
)
817818
LOG.info("[node:%s] %s", node.uuid, reason)
818-
extra["ha_check_reason"] = reason
819+
node.last_error = reason
819820
return
820821

821822
LOG.info("[node:%s] HA interfaces operational", node.uuid)
@@ -831,7 +832,7 @@ def _validate_and_pair_ha(
831832
f"conn={peer_conn}, peer_serial={peer_serial}"
832833
)
833834
LOG.info("[node:%s] %s", node.uuid, reason)
834-
extra["ha_check_reason"] = reason
835+
node.last_error = reason
835836
return
836837

837838
if peer_serial != mate_serial:
@@ -840,7 +841,7 @@ def _validate_and_pair_ha(
840841
f"expected {mate_serial}"
841842
)
842843
LOG.warning("[node:%s] %s", node.uuid, reason)
843-
extra["ha_check_reason"] = reason
844+
node.last_error = reason
844845
return
845846

846847
LOG.info("[node:%s] HA peer connected: %s", node.uuid, peer_serial)
@@ -850,7 +851,7 @@ def _validate_and_pair_ha(
850851
if not mate:
851852
reason = f"Mate node {mate_serial} not found in Ironic"
852853
LOG.info("[node:%s] %s", node.uuid, reason)
853-
extra["ha_check_reason"] = reason
854+
node.last_error = reason
854855
return
855856

856857
# Step 4: Pre-flight compatibility checks
@@ -865,7 +866,7 @@ def _validate_and_pair_ha(
865866
f"my serial is {my_serial}"
866867
)
867868
LOG.warning("[node:%s] %s", node.uuid, reason)
868-
extra["ha_check_reason"] = reason
869+
node.last_error = reason
869870
return
870871

871872
# Check resource class match
@@ -875,7 +876,7 @@ def _validate_and_pair_ha(
875876
f"mate={mate.resource_class}"
876877
)
877878
LOG.warning("[node:%s] %s", node.uuid, reason)
878-
extra["ha_check_reason"] = reason
879+
node.last_error = reason
879880
return
880881

881882
# Check opposite HA roles
@@ -884,13 +885,13 @@ def _validate_and_pair_ha(
884885
if not my_role or not mate_role:
885886
reason = f"Missing HA roles: self={my_role}, mate={mate_role}"
886887
LOG.warning("[node:%s] %s", node.uuid, reason)
887-
extra["ha_check_reason"] = reason
888+
node.last_error = reason
888889
return
889890

890891
if my_role == mate_role:
891892
reason = f"Same HA role on both devices: {my_role}"
892893
LOG.warning("[node:%s] %s", node.uuid, reason)
893-
extra["ha_check_reason"] = reason
894+
node.last_error = reason
894895
return
895896

896897
LOG.info(
@@ -906,7 +907,7 @@ def _validate_and_pair_ha(
906907
if not mate_address:
907908
reason = "Mate has no management_ip"
908909
LOG.warning("[node:%s] %s", node.uuid, reason)
909-
extra["ha_check_reason"] = reason
910+
node.last_error = reason
910911
return
911912

912913
mate_client = panos_api.PanosClient(mate_address)
@@ -921,13 +922,13 @@ def _validate_and_pair_ha(
921922
f"got {mate_peer_serial}, expected {my_serial}"
922923
)
923924
LOG.warning("[node:%s] %s", node.uuid, reason)
924-
extra["ha_check_reason"] = reason
925+
node.last_error = reason
925926
return
926927

927928
if mate_peer_conn != "up":
928929
reason = f"Mate peer connection not up: {mate_peer_conn}"
929930
LOG.warning("[node:%s] %s", node.uuid, reason)
930-
extra["ha_check_reason"] = reason
931+
node.last_error = reason
931932
return
932933

933934
LOG.info("[node:%s] Bidirectional HA validation passed", node.uuid)
@@ -955,7 +956,7 @@ def _validate_and_pair_ha(
955956
"HA pair must be on different switches."
956957
)
957958
LOG.error("[node:%s] %s", node.uuid, error_msg)
958-
extra["ha_check_reason"] = error_msg
959+
node.last_error = error_msg
959960
raise exception.HardwareInspectionFailure(error_msg)
960961

961962
LOG.info(
@@ -980,7 +981,7 @@ def _validate_and_pair_ha(
980981
LOG.info("[node:%s] All HA validation checks passed", node.uuid)
981982
extra["ha_operational"] = True
982983
# Clear any previous failure reason
983-
extra.pop("ha_check_reason", None)
984+
node.last_error = None
984985

985986
except Exception as e:
986987
LOG.exception(
@@ -989,7 +990,7 @@ def _validate_and_pair_ha(
989990
e,
990991
)
991992
# Set failure reason for troubleshooting
992-
extra["ha_check_reason"] = str(e)
993+
node.last_error = str(e)
993994
# Don't raise - allow inspection to complete even if pairing fails
994995
finally:
995996
# Always save ha_operational status (True if all checks passed, False otherwise)

‎python/ironic-understack/ironic_understack/tests/test_panos_inspect.py‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,107 @@ def test_warns_only_when_hsci_is_slower_than_data_ports(mocker, hsci_speed, warn
151151
panos_inspect._warn_if_hsci_slower("n1", ports, {"hsci": {"speed": hsci_speed}})
152152

153153
assert log.warning.called is warns
154+
155+
156+
def _ha_status(serial, conn):
157+
return {"peer": {"serial": serial, "conn_status": conn}}
158+
159+
160+
def _ha_pair(
161+
mocker,
162+
task,
163+
*,
164+
hsci="up",
165+
peer=("S2", "up"),
166+
mate_peer=("S1", "up"),
167+
mate_switch="leaf-2",
168+
):
169+
"""A primary, its secondary in Ironic and both devices; returns this device."""
170+
task.node.traits = ["CUSTOM_PRIMARY"]
171+
task.node.extra = {"serial": "S1", "mate_serial": "S2"}
172+
task.node.resource_class = "pa1410"
173+
task.ports = [Mock(local_link_connection={"switch_info": "leaf-1"})]
174+
mate = Mock(
175+
id=2,
176+
uuid="mate-uuid",
177+
traits=["CUSTOM_SECONDARY"],
178+
extra={"serial": "S2", "mate_serial": "S1"},
179+
resource_class="pa1410",
180+
driver_info={"management_ip": "192.0.2.11"},
181+
)
182+
mocker.patch("ironic.objects.node.Node.list", return_value=[mate])
183+
mocker.patch.object(
184+
panos_inspect.port.Port,
185+
"list_by_node_id",
186+
return_value=[Mock(local_link_connection={"switch_info": mate_switch})],
187+
)
188+
device = Mock()
189+
device.get_ha_interface_status.return_value = {
190+
"ha1-a": {"state": "up"},
191+
"ha1-b": {"state": "up"},
192+
"hsci": {"state": hsci},
193+
}
194+
device.get_ha_status.return_value = _ha_status(*peer)
195+
mate_device = mocker.patch.object(panos_api, "PanosClient").return_value
196+
mate_device.get_ha_status.return_value = _ha_status(*mate_peer)
197+
return device
198+
199+
200+
def _validate_ha(task, device):
201+
"""Run the real HA checks; return the node they saved."""
202+
node = task.node
203+
panos_inspect._validate_and_pair_ha(
204+
task, device, node, dict(node.extra), "S2", "primary"
205+
)
206+
node.save.assert_called_once()
207+
return node
208+
209+
210+
def test_a_healthy_pair_is_operational(mocker, task):
211+
device = _ha_pair(mocker, task)
212+
task.node.last_error = "stale"
213+
214+
node = _validate_ha(task, device)
215+
216+
assert node.extra["ha_operational"] is True
217+
assert node.last_error is None
218+
219+
220+
@pytest.mark.parametrize(
221+
("change", "reason"),
222+
[
223+
({"peer": ("S2", "down")}, "HA peer connection not up"),
224+
({"peer": ("S9", "up")}, "HA peer serial mismatch"),
225+
({"mate_peer": ("S9", "up")}, "Mate peer serial mismatch"),
226+
({"mate_peer": ("S1", "down")}, "Mate peer connection not up"),
227+
({"mate_switch": "leaf-1"}, "same switch"),
228+
],
229+
ids=["peer-down", "wrong-peer", "mate-sees-other", "mate-down", "same-switch"],
230+
)
231+
def test_an_unhealthy_pair_is_not_operational(mocker, task, change, reason):
232+
device = _ha_pair(mocker, task, **change)
233+
234+
node = _validate_ha(task, device)
235+
236+
assert node.extra["ha_operational"] is False
237+
assert reason in node.last_error
238+
239+
240+
def test_a_local_link_down_never_contacts_the_mate(mocker, task):
241+
device = _ha_pair(mocker, task, hsci="down")
242+
243+
node = _validate_ha(task, device)
244+
245+
assert node.extra["ha_operational"] is False
246+
assert "HA interfaces not all up" in node.last_error
247+
panos_api.PanosClient.assert_not_called()
248+
249+
250+
def test_a_device_timeout_is_recorded_not_raised(mocker, task):
251+
device = _ha_pair(mocker, task)
252+
device.get_ha_status.side_effect = requests.Timeout("read timed out")
253+
254+
node = _validate_ha(task, device)
255+
256+
assert node.extra["ha_operational"] is False
257+
assert node.last_error == "read timed out"

‎python/understack-workflows/tests/test_enroll_fw.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def _node(
3737
provision_state=provision_state,
3838
driver_info=driver_info or {},
3939
extra=extra or {},
40+
last_error=None,
4041
)
4142

4243

@@ -642,6 +643,7 @@ def _ha_node(uuid, *, serial, mate_serial, **fields):
642643
"parent_node": None,
643644
"driver_info": {},
644645
"extra": extra,
646+
"last_error": None,
645647
**fields,
646648
}
647649
)

‎python/understack-workflows/understack_workflows/main/enroll_fw.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,9 @@ def _inspect_and_provide(
246246
if _extra(node).get("ha_operational") is not True:
247247
logger.warning(
248248
"[node:%s] Left manageable: HA pair not confirmed yet. It is waiting "
249-
"for its mate, or the HA checks failed; see the conductor log",
249+
"for its mate, or the HA checks failed: %s",
250250
node.uuid,
251+
node.last_error,
251252
)
252253
return
253254
_promote_ha_pair(client, node, ha_role, physical_network, role_ports)

0 commit comments

Comments
 (0)