Fix magnum-template-manage

After patch: https://review.openstack.org/#/c/374906/
magnum-template-manage didn't worked because load_entry_point
method moved to Driver class.
With this patch, magnum-template-manage can be used to list
all available drivers in magnum.

The patch also renames magnum-template-manage cli to
magnum-driver-manage.
Drivers can now be listed using:-
magnum-driver-manage list-drivers
magnum-driver-manage list-drivers -d -p

DocImpact

Change-Id: I17ba94b0e2000486b5fcbf792991ad98183bd26c
Partially-Implements: blueprint bay-drivers
Closes-Bug: #1632630
This commit is contained in:
yatin 2016-10-13 10:42:36 +05:30
parent ba11900215
commit af59ea68b8
4 changed files with 97 additions and 138 deletions

View File

@ -11,7 +11,7 @@
# License for the specific language governing permissions and limitations
# under the License.
"""Starter script for magnum-template-manage."""
"""Starter script for magnum-driver-manage."""
import sys
from cliff import app
@ -19,22 +19,18 @@ from cliff import commandmanager
from cliff import lister
import magnum.conf
from magnum.drivers.common import template_def as tdef
from magnum.drivers.common import driver
from magnum import version
CONF = magnum.conf.CONF
def is_enabled(name):
return name in CONF.cluster.enabled_definitions
class TemplateList(lister.Lister):
class DriverList(lister.Lister):
"""List templates"""
def _print_rows(self, parsed_args, rows):
fields = ['name', 'enabled']
field_labels = ['Name', 'Enabled']
fields = ['name']
field_labels = ['Name']
if parsed_args.details:
fields.extend(['server_type', 'os', 'coe'])
@ -46,7 +42,7 @@ class TemplateList(lister.Lister):
for row in rows]
def get_parser(self, prog_name):
parser = super(TemplateList, self).get_parser(prog_name)
parser = super(DriverList, self).get_parser(prog_name)
parser.add_argument('-d', '--details',
action='store_true',
dest='details',
@ -57,40 +53,30 @@ class TemplateList(lister.Lister):
dest='paths',
help='display the path to each template file')
group = parser.add_mutually_exclusive_group()
group.add_argument('--enabled', action='store_true', dest='enabled',
help="display only enabled templates")
group.add_argument('--disabled', action='store_true', dest='disabled',
help="display only disabled templates")
return parser
def take_action(self, parsed_args):
rows = []
for entry_point, cls in tdef.TemplateDefinition.load_entry_points():
for entry_point, cls in driver.Driver.load_entry_points():
name = entry_point.name
if ((is_enabled(name) and not parsed_args.disabled) or
(not is_enabled(name) and not parsed_args.enabled)):
definition = cls()
template = dict(name=name, enabled=is_enabled(name),
path=definition.template_path)
if parsed_args.details:
for cluster_type in definition.provides:
row = dict()
row.update(template)
row.update(cluster_type)
rows.append(row)
else:
rows.append(template)
definition = cls().get_template_definition()
template = dict(name=name, path=definition.template_path)
if parsed_args.details:
for cluster_type in cls().provides:
row = dict()
row.update(template)
row.update(cluster_type)
rows.append(row)
else:
rows.append(template)
return self._print_rows(parsed_args, rows)
class TemplateCommandManager(commandmanager.CommandManager):
class DriverCommandManager(commandmanager.CommandManager):
COMMANDS = {
"list-templates": TemplateList,
"list-drivers": DriverList,
}
def load_commands(self, namespace):
@ -98,12 +84,12 @@ class TemplateCommandManager(commandmanager.CommandManager):
self.add_command(name, command_class)
class TemplateManager(app.App):
class DriverManager(app.App):
def __init__(self):
super(TemplateManager, self).__init__(
description='Magnum Template Manager',
super(DriverManager, self).__init__(
description='Magnum Driver Manager',
version=version.version_info,
command_manager=TemplateCommandManager(None),
command_manager=DriverCommandManager(None),
deferred_help=True)
@ -113,4 +99,4 @@ def main(args=None):
CONF([],
project='magnum',
version=version.version_info.release_string())
return TemplateManager().run(args)
return DriverManager().run(args)

View File

@ -0,0 +1,72 @@
# Copyright 2016 - Fujitsu, Ltd.
#
# 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
from magnum.cmd import driver_manage
from magnum.tests import base
class TestMagnumDriverManage(base.TestCase):
# Fake entrypoints method
@staticmethod
def _fake_entry(num_of_entries):
while num_of_entries:
fake_entry = mock.MagicMock()
fake_entry.name = 'magnum_' + 'test_' + \
'foo_' + 'bar'*num_of_entries
fake_cls = mock.MagicMock()
fake_definition = fake_cls()
fake_definition.provides = [{'coe': 'foo', 'os': 'bar',
'server_type': 'test'}]
fake_definition.get_template_definition.return_value = \
mock.MagicMock(template_path='fake_path')
yield fake_entry, fake_cls
num_of_entries -= 1
@mock.patch.object(driver_manage.DriverManager, 'run')
@mock.patch('sys.argv', ['foo', 'bar'])
def test_none_arg(self, mock_run):
args = None
driver_manage.main(args)
mock_run.assert_called_once_with(['bar'])
# NOTE(hieulq): we fake the entrypoints then we need to mock the cliff
# produce_output in order to assert with fake value
@mock.patch('magnum.cmd.driver_manage.DriverList.produce_output')
@mock.patch('magnum.drivers.common.driver.Driver')
def test_correct_arg_with_details_and_path(self, mock_driver,
mock_produce):
args = ['list-drivers', '-d', '-p']
mock_driver.load_entry_points.return_value = self._fake_entry(1)
driver_manage.main(args)
mock_driver.load_entry_points.assert_called_once_with()
mock_produce.assert_called_once_with(mock.ANY, mock.ANY,
[('magnum_test_foo_bar',
'test',
'bar', 'foo', 'fake_path')])
# NOTE(hieulq): we fake the entrypoints then we need to mock the cliff
# produce_output in order to assert with fake value
@mock.patch('magnum.cmd.driver_manage.DriverList.produce_output')
@mock.patch('magnum.drivers.common.driver.Driver')
def test_correct_arg_without_details_and_path(self, mock_driver,
mock_produce):
args = ['list-drivers']
mock_driver.load_entry_points.return_value = self._fake_entry(1)
driver_manage.main(args)
mock_driver.load_entry_points.assert_called_once_with()
mock_produce.assert_called_once_with(mock.ANY, mock.ANY,
[('magnum_test_foo_bar',)])

View File

@ -1,99 +0,0 @@
# Copyright 2016 - Fujitsu, Ltd.
#
# 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
from magnum.cmd import template_manage
from magnum.tests import base
class TestMagnumTemplateManage(base.TestCase):
# Fake entrypoints method
@staticmethod
def _fake_entry(num_of_entries):
while num_of_entries:
fake_entry = mock.MagicMock()
fake_entry.name = 'magnum_' + 'test_' + \
'foo_' + 'bar'*num_of_entries
fake_cls = mock.MagicMock()
fake_definition = fake_cls()
fake_definition.provides = [{'coe': 'foo', 'os': 'bar',
'server_type': 'test'}]
fake_definition.template_path = 'fake_path'
yield fake_entry, fake_cls
num_of_entries -= 1
@mock.patch.object(template_manage.TemplateManager, 'run')
@mock.patch('sys.argv', ['foo', 'bar'])
def test_none_arg(self, mock_run):
args = None
template_manage.main(args)
mock_run.assert_called_once_with(['bar'])
# NOTE(hieulq): we fake the entrypoints then we need to mock the cliff
# produce_output in order to assert with fake value
@mock.patch('magnum.cmd.template_manage.TemplateList.produce_output')
@mock.patch('magnum.drivers.common.template_def.TemplateDefinition')
def test_correct_arg_with_details_and_path(self, mock_tdef, mock_produce):
args = ['list-templates', '-d', '-p']
mock_tdef.load_entry_points.return_value = self._fake_entry(1)
template_manage.main(args)
mock_tdef.load_entry_points.assert_called_once_with()
mock_produce.assert_called_once_with(mock.ANY, mock.ANY,
[('magnum_test_foo_bar',
False, 'test',
'bar', 'foo', 'fake_path')])
# NOTE(hieulq): we fake the entrypoints then we need to mock the cliff
# produce_output in order to assert with fake value
@mock.patch('magnum.cmd.template_manage.TemplateList.produce_output')
@mock.patch('magnum.drivers.common.template_def.TemplateDefinition')
def test_correct_arg_without_details_and_path(self, mock_tdef,
mock_produce):
args = ['list-templates']
mock_tdef.load_entry_points.return_value = self._fake_entry(1)
template_manage.main(args)
mock_tdef.load_entry_points.assert_called_once_with()
mock_produce.assert_called_once_with(mock.ANY, mock.ANY,
[('magnum_test_foo_bar', False)])
# NOTE(hieulq): we fake the entrypoints then we need to mock the cliff
# produce_output in order to assert with fake value
@mock.patch('magnum.cmd.template_manage.TemplateList.produce_output')
@mock.patch('magnum.drivers.common.template_def.TemplateDefinition')
def test_correct_arg_with_enabled_template(self, mock_tdef, mock_produce):
args = ['list-templates', '--enabled']
self.config(enabled_definitions={'magnum_test_foo_bar'},
group='cluster')
mock_tdef.load_entry_points.return_value = self._fake_entry(2)
template_manage.main(args)
mock_tdef.load_entry_points.assert_called_once_with()
mock_produce.assert_called_once_with(mock.ANY, mock.ANY,
[('magnum_test_foo_bar', True)])
# NOTE(hieulq): we fake the entrypoints then we need to mock the cliff
# produce_output in order to assert with fake value
@mock.patch('magnum.cmd.template_manage.TemplateList.produce_output')
@mock.patch('magnum.drivers.common.template_def.TemplateDefinition')
def test_correct_arg_with_disabled_template(self, mock_tdef, mock_produce):
args = ['list-templates', '--disable']
self.config(enabled_definitions={'magnum_test_foo_bar'},
group='cluster')
mock_tdef.load_entry_points.return_value = self._fake_entry(2)
template_manage.main(args)
mock_tdef.load_entry_points.assert_called_once_with()
mock_produce.assert_called_once_with(mock.ANY, mock.ANY,
[('magnum_test_foo_barbar',
False)])

View File

@ -49,7 +49,7 @@ console_scripts =
magnum-api = magnum.cmd.api:main
magnum-conductor = magnum.cmd.conductor:main
magnum-db-manage = magnum.cmd.db_manage:main
magnum-template-manage = magnum.cmd.template_manage:main
magnum-driver-manage = magnum.cmd.driver_manage:main
oslo.config.opts =
magnum = magnum.opts:list_opts