Add a function to change password in astute.yaml
This change creates a separate context manager for setting FUEL_ACCESS/password in astute.yaml. Implements: blueprint upgrade-fuel-admin-node Change-Id: I2699711c760650f9f5c69e6fb61c13b6190e61b4
This commit is contained in:
parent
52905fee5b
commit
b608842c24
@ -10,12 +10,8 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import shutil
|
||||
import tempfile
|
||||
import yaml
|
||||
|
||||
from octane.handlers.backup_restore import base
|
||||
from octane.util import helpers
|
||||
from octane.util import auth
|
||||
from octane.util import puppet
|
||||
|
||||
|
||||
@ -30,15 +26,5 @@ class PuppetApplyHost(base.Base):
|
||||
pass
|
||||
|
||||
def restore(self):
|
||||
_, tmp_file_name = tempfile.mkstemp(
|
||||
dir="/etc/fuel",
|
||||
prefix=".astute.yaml.octane")
|
||||
shutil.copy("/etc/fuel/astute.yaml", tmp_file_name)
|
||||
try:
|
||||
data = helpers.get_astute_dict()
|
||||
data["FUEL_ACCESS"]["password"] = self.context.password
|
||||
with open("/etc/fuel/astute.yaml", "w") as current:
|
||||
yaml.safe_dump(data, current, default_flow_style=False)
|
||||
with auth.set_astute_password(self.context):
|
||||
puppet.apply_host()
|
||||
finally:
|
||||
shutil.move(tmp_file_name, "/etc/fuel/astute.yaml")
|
||||
|
@ -561,47 +561,18 @@ def test_post_restore_nailgun(mocker, mock_open, dump, calls, data_for_update):
|
||||
])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exc_on_apply", [True, False])
|
||||
def test_post_restore_puppet_apply_host(mocker, mock_open, exc_on_apply):
|
||||
|
||||
class TestException(Exception):
|
||||
pass
|
||||
|
||||
mkstemp_mock = mocker.patch(
|
||||
"tempfile.mkstemp",
|
||||
return_value=(1, "/etc/fuel/.astute.yaml.bac"))
|
||||
mock_copy = mocker.patch("shutil.copy")
|
||||
mock_move = mocker.patch("shutil.move")
|
||||
yaml_load = mocker.patch(
|
||||
"yaml.load", return_value={"FUEL_ACCESS": {"password": "dump_pswd"}})
|
||||
yaml_dump = mocker.patch("yaml.safe_dump")
|
||||
def test_post_restore_puppet_apply_host(mocker):
|
||||
context = backup_restore.NailgunCredentialsContext(
|
||||
user="admin", password="user_pswd")
|
||||
archivator = puppet.PuppetApplyHost(None, context)
|
||||
if exc_on_apply:
|
||||
mock_apply = mocker.patch(
|
||||
"octane.util.puppet.apply_host",
|
||||
side_effect=TestException("test exception"))
|
||||
pytest.raises(TestException, archivator.restore)
|
||||
else:
|
||||
mock_set_astute_password = mocker.patch(
|
||||
"octane.util.auth.set_astute_password")
|
||||
mock_apply = mocker.patch("octane.util.puppet.apply_host")
|
||||
|
||||
archivator = puppet.PuppetApplyHost(None, context)
|
||||
archivator.restore()
|
||||
|
||||
assert mock_apply.called
|
||||
assert mock_open.call_args_list == [
|
||||
mock.call("/etc/fuel/astute.yaml", "r"),
|
||||
mock.call("/etc/fuel/astute.yaml", "w"),
|
||||
]
|
||||
yaml_load.assert_called_once_with(mock_open.return_value)
|
||||
yaml_dump.asswer_called_once_with(
|
||||
{'FUEL_ACCESS': {'password': 'user_pswd'}},
|
||||
mock_open.return_value,
|
||||
default_flow_style=False)
|
||||
mock_copy.assert_called_once_with("/etc/fuel/astute.yaml",
|
||||
"/etc/fuel/.astute.yaml.bac")
|
||||
mock_move.assert_called_once_with("/etc/fuel/.astute.yaml.bac",
|
||||
"/etc/fuel/astute.yaml")
|
||||
mkstemp_mock.assert_called_once_with(
|
||||
dir="/etc/fuel", prefix=".astute.yaml.octane")
|
||||
mock_set_astute_password.assert_called_once_with(context)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("nodes", [
|
||||
|
57
octane/tests/test_util_auth.py
Normal file
57
octane/tests/test_util_auth.py
Normal file
@ -0,0 +1,57 @@
|
||||
# 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
|
||||
#
|
||||
# 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.
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from octane.handlers import backup_restore
|
||||
from octane.util import auth
|
||||
|
||||
|
||||
class TestException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exc_on_apply", [True, False])
|
||||
def test_set_astute_password(mocker, mock_open, exc_on_apply):
|
||||
mkstemp_mock = mocker.patch(
|
||||
"tempfile.mkstemp",
|
||||
return_value=(1, "/etc/fuel/.astute.yaml.bac"))
|
||||
mock_copy = mocker.patch("shutil.copy")
|
||||
mock_move = mocker.patch("shutil.move")
|
||||
yaml_load = mocker.patch(
|
||||
"yaml.load", return_value={"FUEL_ACCESS": {"password": "dump_pswd"}})
|
||||
yaml_dump = mocker.patch("yaml.safe_dump")
|
||||
context = backup_restore.NailgunCredentialsContext(
|
||||
user="admin", password="user_pswd")
|
||||
if exc_on_apply:
|
||||
with pytest.raises(TestException):
|
||||
with auth.set_astute_password(context):
|
||||
raise TestException("text exception")
|
||||
else:
|
||||
with auth.set_astute_password(context):
|
||||
pass
|
||||
assert mock_open.call_args_list == [
|
||||
mock.call("/etc/fuel/astute.yaml", "r"),
|
||||
mock.call("/etc/fuel/astute.yaml", "w"),
|
||||
]
|
||||
yaml_load.assert_called_once_with(mock_open.return_value)
|
||||
yaml_dump.asswer_called_once_with(
|
||||
{'FUEL_ACCESS': {'password': 'user_pswd'}},
|
||||
mock_open.return_value,
|
||||
default_flow_style=False)
|
||||
mock_copy.assert_called_once_with("/etc/fuel/astute.yaml",
|
||||
"/etc/fuel/.astute.yaml.bac")
|
||||
mock_move.assert_called_once_with("/etc/fuel/.astute.yaml.bac",
|
||||
"/etc/fuel/astute.yaml")
|
||||
mkstemp_mock.assert_called_once_with(
|
||||
dir="/etc/fuel", prefix=".astute.yaml.octane")
|
35
octane/util/auth.py
Normal file
35
octane/util/auth.py
Normal file
@ -0,0 +1,35 @@
|
||||
# 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
|
||||
#
|
||||
# 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.
|
||||
|
||||
import shutil
|
||||
import tempfile
|
||||
import yaml
|
||||
|
||||
import contextlib
|
||||
|
||||
from octane.util import helpers
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def set_astute_password(auth_context):
|
||||
_, tmp_file_name = tempfile.mkstemp(
|
||||
dir="/etc/fuel",
|
||||
prefix=".astute.yaml.octane")
|
||||
shutil.copy("/etc/fuel/astute.yaml", tmp_file_name)
|
||||
try:
|
||||
data = helpers.get_astute_dict()
|
||||
data["FUEL_ACCESS"]["password"] = auth_context.password
|
||||
with open("/etc/fuel/astute.yaml", "w") as current:
|
||||
yaml.safe_dump(data, current, default_flow_style=False)
|
||||
yield
|
||||
finally:
|
||||
shutil.move(tmp_file_name, "/etc/fuel/astute.yaml")
|
Loading…
Reference in New Issue
Block a user