Skip to content

Commit ea0d2db

Browse files
authored
Merge pull request #1619 from rackerlabs/PUC-1442
fix: bmc password validation fails incorrectly.
2 parents e7d61dd + 1b2fbd3 commit ea0d2db

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from unittest.mock import MagicMock
2-
31
import pytest
42

5-
from understack_workflows.bmc import RedfishRequestError
3+
from understack_workflows.bmc import AuthException
64
from understack_workflows.bmc_credentials import set_bmc_password
75

86

@@ -14,17 +12,28 @@ def mock_getsession(mocker):
1412

1513

1614
@pytest.fixture
17-
def mock_close(mocker):
18-
mock = mocker.patch("understack_workflows.bmc_credentials.Bmc.close_session")
15+
def mock_sleep(mocker):
16+
mock = mocker.patch("understack_workflows.bmc_credentials.sleep", return_value=None)
17+
return mock
18+
19+
20+
@pytest.fixture
21+
def mock_getsession_failed(mocker):
22+
mock = mocker.patch("understack_workflows.bmc_credentials.Bmc.get_session")
23+
mock.side_effect = [
24+
(None, None),
25+
(None, None),
26+
(None, None),
27+
(None, None),
28+
(None, None),
29+
] # patching 5 requests for session attempts.
1930
return mock
2031

2132

2233
@pytest.fixture
23-
def mock_fail_auth(mocker):
24-
mock_response = MagicMock()
25-
mock_response.status_code = 402
26-
mock_response.json.return_value = {"message": "Failure"}
27-
mock = mocker.patch("requests.request", return_value=mock_response)
34+
def mock_close(mocker):
35+
mock = mocker.patch("understack_workflows.bmc_credentials.Bmc.close_session")
36+
mock.return_value = None
2837
return mock
2938

3039

@@ -34,6 +43,6 @@ def test_set_bmc_password_noop(mock_getsession, mock_close):
3443
mock_close.assert_called_with(session="/path/to/session/1234", token="tOkEn")
3544

3645

37-
def test_set_bmc_password_failed(mock_fail_auth):
38-
with pytest.raises(RedfishRequestError):
46+
def test_set_bmc_password_failed(mock_getsession_failed, mock_sleep):
47+
with pytest.raises(AuthException):
3948
set_bmc_password("1.2.3.4", "qwertyuiop")

python/understack-workflows/understack_workflows/bmc_credentials.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from understack_workflows.bmc import AuthException
55
from understack_workflows.bmc import Bmc
6+
from understack_workflows.bmc import RedfishRequestError
67
from understack_workflows.helpers import setup_logger
78

89
FACTORY_B64 = b"Y2FsdmluLGNhbHZpbmNhbHZpbixjYWx2aW4xLGNhbHZpbmNhbHZpbjE="
@@ -33,7 +34,11 @@ def set_bmc_password(
3334
"""
3435
bmc = Bmc(ip_address=ip_address, username=username, password=new_password)
3536

36-
token, session = bmc.get_session(new_password)
37+
try:
38+
token, session = bmc.get_session(new_password)
39+
except RedfishRequestError as e:
40+
logger.debug("Password test for %s failed. %s", ip_address, e)
41+
token, session = None, None
3742
if token and session:
3843
logger.info("Production BMC credentials are working on this BMC.")
3944
bmc.close_session(session=session, token=token)
@@ -45,7 +50,11 @@ def set_bmc_password(
4550
)
4651

4752
for test_password in filter(None, [old_password, *FACTORY_PASSWORDS]):
48-
token, session = bmc.get_session(test_password)
53+
try:
54+
token, session = bmc.get_session(test_password)
55+
except RedfishRequestError as e:
56+
logger.debug("Password test for %s failed. %s", ip_address, e)
57+
token, session = None, None
4958
if token and session:
5059
break
5160
# Go Slow, or else the BMC will lock us out for a

0 commit comments

Comments
 (0)