Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions python/understack-workflows/tests/test_bmc_credentials.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from unittest.mock import MagicMock

import pytest

from understack_workflows.bmc import RedfishRequestError
from understack_workflows.bmc import AuthException
from understack_workflows.bmc_credentials import set_bmc_password


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


@pytest.fixture
def mock_close(mocker):
mock = mocker.patch("understack_workflows.bmc_credentials.Bmc.close_session")
def mock_sleep(mocker):
mock = mocker.patch("understack_workflows.bmc_credentials.sleep", return_value=None)
return mock


@pytest.fixture
def mock_getsession_failed(mocker):
mock = mocker.patch("understack_workflows.bmc_credentials.Bmc.get_session")
mock.side_effect = [
(None, None),
(None, None),
(None, None),
(None, None),
(None, None),
] # patching 5 requests for session attempts.
return mock


@pytest.fixture
def mock_fail_auth(mocker):
mock_response = MagicMock()
mock_response.status_code = 402
mock_response.json.return_value = {"message": "Failure"}
mock = mocker.patch("requests.request", return_value=mock_response)
def mock_close(mocker):
mock = mocker.patch("understack_workflows.bmc_credentials.Bmc.close_session")
mock.return_value = None
return mock


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


def test_set_bmc_password_failed(mock_fail_auth):
with pytest.raises(RedfishRequestError):
def test_set_bmc_password_failed(mock_getsession_failed, mock_sleep):
with pytest.raises(AuthException):
set_bmc_password("1.2.3.4", "qwertyuiop")
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from understack_workflows.bmc import AuthException
from understack_workflows.bmc import Bmc
from understack_workflows.bmc import RedfishRequestError
from understack_workflows.helpers import setup_logger

FACTORY_B64 = b"Y2FsdmluLGNhbHZpbmNhbHZpbixjYWx2aW4xLGNhbHZpbmNhbHZpbjE="
Expand Down Expand Up @@ -33,7 +34,11 @@ def set_bmc_password(
"""
bmc = Bmc(ip_address=ip_address, username=username, password=new_password)

token, session = bmc.get_session(new_password)
try:
token, session = bmc.get_session(new_password)
except RedfishRequestError as e:
logger.debug("Password test for %s failed. %s", ip_address, e)
token, session = None, None
if token and session:
logger.info("Production BMC credentials are working on this BMC.")
bmc.close_session(session=session, token=token)
Expand All @@ -45,7 +50,11 @@ def set_bmc_password(
)

for test_password in filter(None, [old_password, *FACTORY_PASSWORDS]):
token, session = bmc.get_session(test_password)
try:
token, session = bmc.get_session(test_password)
except RedfishRequestError as e:
logger.debug("Password test for %s failed. %s", ip_address, e)
token, session = None, None
if token and session:
break
# Go Slow, or else the BMC will lock us out for a
Expand Down
Loading