Merge "Consume new RoleConfig data in config download"

This commit is contained in:
Jenkins 2017-09-06 11:01:39 +00:00 committed by Gerrit Code Review
commit 4f25a69d17
4 changed files with 108 additions and 94 deletions

View File

@ -26,8 +26,11 @@ FAKE_STACK = {
},
'stack_name': 'overcloud',
'stack_status': "CREATE_COMPLETE",
'outputs': [{
'output_key': 'RoleData',
'outputs': [
{'output_key': 'RoleConfig',
'output_value': {
'foo_config': 'foo'}},
{'output_key': 'RoleData',
'output_value': {
'FakeCompute': {
'config_settings': {'nova::compute::libvirt::services::'
@ -70,11 +73,7 @@ FAKE_STACK = {
'upgrade_batch_tasks': [],
'upgrade_tasks': [{'name': 'Stop fake service',
'service': 'name=fake state=stopped',
'tags': 'step1'}]
}
}
}]
}
'tags': 'step1'}]}}}]}
def create_to_dict_mock(**kwargs):

View File

@ -11,9 +11,9 @@
# under the License.
import mock
import stat
from mock import call
from mock import patch
from osc_lib.tests import utils
from tripleoclient.tests.v1.overcloud_config import fakes
@ -30,8 +30,13 @@ class TestOvercloudConfig(utils.TestCommand):
self.app.client_manager.orchestration = mock.Mock()
self.workflow = self.app.client_manager.workflow_engine
@patch.object(overcloud_config.DownloadConfig, '_mkdir')
@patch.object(overcloud_config.DownloadConfig, '_open_file')
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_overcloud_config_generate_config(self, mock_tmpdir):
def test_overcloud_config_generate_config(self,
mock_tmpdir,
mock_open,
mock_mkdir):
arglist = ['--name', 'overcloud', '--config-dir', '/tmp']
verifylist = [
('name', 'overcloud'),
@ -44,34 +49,34 @@ class TestOvercloudConfig(utils.TestCommand):
'service_names',
'upgrade_batch_tasks', 'upgrade_tasks']
fake_role = [role for role in
fakes.FAKE_STACK['outputs'][0]['output_value']]
fakes.FAKE_STACK['outputs'][1]['output_value']]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
clients = self.app.client_manager
orchestration_client = clients.orchestration
orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
mock_tmpdir.return_value = "/tmp/tht"
with mock.patch('os.open') as open, \
mock.patch('os.fdopen'), mock.patch('os.mkdir') as mkdir:
self.cmd.take_action(parsed_args)
expected_mkdir_calls = [call('/tmp/tht/%s' % r,
stat.S_IRWXU) for r in fake_role]
mkdir.assert_has_calls(expected_mkdir_calls, any_order=True)
expected_mkdir_calls = [call('/tmp/tht/%s' % r) for r in fake_role]
mock_mkdir.assert_has_calls(expected_mkdir_calls, any_order=True)
expected_calls = []
for config in config_type_list:
for role in fake_role:
if config == 'step_config':
expected_calls += [call('/tmp/tht/%s/%s.pp' %
(role, config), 65,
stat.S_IRUSR | stat.S_IWUSR)]
(role, config))]
else:
expected_calls += [call('/tmp/tht/%s/%s.yaml' %
(role, config), 65,
stat.S_IRUSR | stat.S_IWUSR)]
open.assert_has_calls(expected_calls, any_order=True)
(role, config))]
mock_open.assert_has_calls(expected_calls, any_order=True)
@patch.object(overcloud_config.DownloadConfig, '_mkdir')
@patch.object(overcloud_config.DownloadConfig, '_open_file')
@mock.patch('tempfile.mkdtemp', autospec=True)
def test_overcloud_config_one_config_type(self, mock_tmpdir):
def test_overcloud_config_one_config_type(self,
mock_tmpdir,
mock_open,
mock_mkdir):
arglist = ['--name', 'overcloud', '--config-dir', '/tmp',
'--config-type', ['config_settings']]
@ -82,25 +87,20 @@ class TestOvercloudConfig(utils.TestCommand):
]
expected_config_type = 'config_settings'
fake_role = [role for role in
fakes.FAKE_STACK['outputs'][0]['output_value']]
fakes.FAKE_STACK['outputs'][1]['output_value']]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
clients = self.app.client_manager
orchestration_client = clients.orchestration
orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
mock_tmpdir.return_value = "/tmp/tht"
with mock.patch('os.open') as open, \
mock.patch('os.fdopen'), mock.patch('os.mkdir') as mkdir:
self.cmd.take_action(parsed_args)
expected_mkdir_calls = [call('/tmp/tht/%s' % r,
stat.S_IRWXU) for r in fake_role]
mkdir.assert_has_calls(expected_mkdir_calls, any_order=True)
expected_calls = [call('/tmp/tht/%s/%s.yaml' %
(r, expected_config_type), 65,
stat.S_IRUSR | stat.S_IWUSR)
expected_mkdir_calls = [call('/tmp/tht/%s' % r) for r in fake_role]
expected_calls = [call('/tmp/tht/%s/%s.yaml'
% (r, expected_config_type))
for r in fake_role]
open.assert_has_calls(expected_calls, any_order=True)
mock_mkdir.assert_has_calls(expected_mkdir_calls, any_order=True)
mock_open.assert_has_calls(expected_calls, any_order=True)
@mock.patch('os.mkdir')
@mock.patch('six.moves.builtins.open')
@ -140,7 +140,7 @@ class TestOvercloudConfig(utils.TestCommand):
orchestration_client.stacks.get.return_value = fakes.create_tht_stack()
mock_tmpdir.return_value = "/tmp/tht"
fake_role = [role for role in
fakes.FAKE_STACK['outputs'][0]['output_value']]
fakes.FAKE_STACK['outputs'][1]['output_value']]
fake_tasks = {'FakeController': [{'name': 'Stop fake service',
'service': 'name=fake '
'state=stopped',
@ -165,7 +165,7 @@ class TestOvercloudConfig(utils.TestCommand):
with mock.patch('os.fdopen'), \
mock.patch('os.mkdir'), mock.patch('os.open') as open:
playbook_tasks = self.cmd._write_playbook_get_tasks(
fakes.FAKE_STACK['outputs'][0]['output_value'][role]
fakes.FAKE_STACK['outputs'][1]['output_value'][role]
['upgrade_tasks'], role, filepath)
self.assertEqual(fake_tasks[role], playbook_tasks)
open.assert_called()

View File

@ -332,6 +332,15 @@ def get_role_data(stack):
return role_data
def get_role_config(stack):
role_data = {}
for output in stack.to_dict().get('outputs', {}):
if output['output_key'] == 'RoleConfig':
for role in output['output_value']:
role_data[role] = output['output_value'][role]
return role_data
def get_endpoint(key, stack):
endpoint_map = get_endpoint_map(stack)
if endpoint_map:

View File

@ -54,6 +54,12 @@ class DownloadConfig(command.Command):
)
return parser
@staticmethod
def _open_file(path):
return os.fdopen(os.open(path,
os.O_WRONLY | os.O_CREAT, 0o600),
'w')
def _step_tags_to_when(self, sorted_tasks):
for task in sorted_tasks:
tag = task.get('tags', '')
@ -77,8 +83,7 @@ class DownloadConfig(command.Command):
playbook.append({'name': '%s playbook' % role,
'hosts': role,
'tasks': sorted_tasks})
with os.fdopen(os.open(filepath, os.O_WRONLY | os.O_CREAT, 0o600),
'w') as conf_file:
with self._open_file(filepath) as conf_file:
yaml.safe_dump(playbook, conf_file, default_flow_style=False)
return sorted_tasks
@ -111,9 +116,7 @@ class DownloadConfig(command.Command):
for config in parsed_args.config_type or role.keys():
if config == 'step_config':
filepath = os.path.join(role_path, 'step_config.pp')
with os.fdopen(os.open(filepath,
os.O_WRONLY | os.O_CREAT, 0o600),
'w') as step_config:
with self._open_file(filepath) as step_config:
step_config.write('\n'.join(step for step in
role[config]
if step is not None))
@ -131,11 +134,14 @@ class DownloadConfig(command.Command):
str(e))
raise KeyError(message)
filepath = os.path.join(role_path, '%s.yaml' % config)
with os.fdopen(os.open(filepath,
os.O_WRONLY | os.O_CREAT, 0o600),
'w') as conf_file:
with self._open_file(filepath) as conf_file:
yaml.safe_dump(data,
conf_file,
default_flow_style=False)
role_config = utils.get_role_config(stack)
for config_name, config in six.iteritems(role_config):
conf_path = os.path.join(tmp_path, config_name + ".yaml")
with self._open_file(conf_path) as conf_file:
conf_file.write(config)
print("The TripleO configuration has been successfully generated "
"into: {0}".format(tmp_path))