Merge "Fix plugin loading tests"

This commit is contained in:
Jenkins 2015-08-25 21:59:46 +00:00 committed by Gerrit Code Review
commit 34368ade9b
8 changed files with 58 additions and 30 deletions

View File

@ -62,7 +62,7 @@ class Token(BaseV3Loader):
@property @property
def plugin_class(self): def plugin_class(self):
return identity.Token return identity.V3Token
def get_options(self): def get_options(self):
options = super(Token, self).get_options() options = super(Token, self).get_options()

View File

@ -59,6 +59,7 @@ def get_plugin_loader(name):
""" """
try: try:
mgr = stevedore.DriverManager(namespace=PLUGIN_NAMESPACE, mgr = stevedore.DriverManager(namespace=PLUGIN_NAMESPACE,
invoke_on_load=True,
name=name) name=name)
except RuntimeError: except RuntimeError:
raise exceptions.NoMatchingPlugin(name) raise exceptions.NoMatchingPlugin(name)

View File

@ -48,7 +48,7 @@ def register_argparse_arguments(parser, argv, default=None):
if not options.os_auth_plugin: if not options.os_auth_plugin:
return None return None
if isinstance(options.os_auth_plugin, type): if isinstance(options.os_auth_plugin, base.BaseLoader):
msg = 'Default Authentication options' msg = 'Default Authentication options'
plugin = options.os_auth_plugin plugin = options.os_auth_plugin
else: else:

View File

@ -71,6 +71,18 @@ class Opt(object):
deprecated_opts=deprecated_opts, deprecated_opts=deprecated_opts,
metavar=self.metavar) metavar=self.metavar)
def __eq__(self, other):
return (type(self) == type(other) and
self.name == other.name and
self.type == other.type and
self.help == other.help and
self.secret == other.secret and
self.required == other.required and
self.dest == other.dest and
self.deprecated == other.deprecated and
self.default == other.default and
self.metavar == other.metavar)
@property @property
def _all_opts(self): def _all_opts(self):
return itertools.chain([self], self.deprecated) return itertools.chain([self], self.deprecated)
@ -83,7 +95,7 @@ class Opt(object):
def argparse_default(self): def argparse_default(self):
# select the first ENV that is not false-y or return None # select the first ENV that is not false-y or return None
for o in self._all_opts: for o in self._all_opts:
v = os.environ.get('OS_%s' % self.name.replace('-', '_').upper()) v = os.environ.get('OS_%s' % o.name.replace('-', '_').upper())
if v: if v:
return v return v

View File

@ -20,6 +20,7 @@ from keystoneauth1 import access
from keystoneauth1 import exceptions from keystoneauth1 import exceptions
from keystoneauth1 import fixture from keystoneauth1 import fixture
from keystoneauth1 import loading from keystoneauth1 import loading
from keystoneauth1.loading import base
from keystoneauth1 import plugin from keystoneauth1 import plugin
from keystoneauth1 import session from keystoneauth1 import session
from keystoneauth1.tests.unit import utils from keystoneauth1.tests.unit import utils
@ -42,8 +43,12 @@ class MockPlugin(plugin.BaseAuthPlugin):
class BoolType(object): class BoolType(object):
def __eq__(self, other):
# hack around oslo.config type comparison
return type(self) == type(other)
def __call__(self, value): def __call__(self, value):
return value.lower() in ('1', 'true', 't', 'yes', 'y') return str(value).lower() in ('1', 'true', 't', 'yes', 'y')
class MockLoader(loading.BaseLoader): class MockLoader(loading.BaseLoader):
@ -76,8 +81,8 @@ class MockManager(object):
def mock_plugin(f): def mock_plugin(f):
@functools.wraps(f) @functools.wraps(f)
def inner(*args, **kwargs): def inner(*args, **kwargs):
with mock.patch.object(loading, 'get_plugin_loader') as m: with mock.patch.object(base, 'get_plugin_loader') as m:
m.return_value = MockPlugin m.return_value = MockLoader()
args = list(args) + [m] args = list(args) + [m]
return f(*args, **kwargs) return f(*args, **kwargs)

View File

@ -16,12 +16,12 @@ import uuid
import fixtures import fixtures
import mock import mock
from keystoneauth1 import base
from keystoneauth1 import loading from keystoneauth1 import loading
from keystoneauth1 import plugin
from keystoneauth1.tests.unit.auth import utils from keystoneauth1.tests.unit.auth import utils
class TesterPlugin(base.BaseAuthPlugin): class TesterPlugin(plugin.BaseAuthPlugin):
def get_token(self, *args, **kwargs): def get_token(self, *args, **kwargs):
return None return None
@ -71,7 +71,7 @@ class CliTests(utils.TestCase):
name = uuid.uuid4().hex name = uuid.uuid4().hex
argv = ['--os-auth-plugin', name] argv = ['--os-auth-plugin', name]
ret = loading.register_argparse_arguments(self.p, argv) ret = loading.register_argparse_arguments(self.p, argv)
self.assertIs(utils.MockPlugin, ret) self.assertIsInstance(ret, utils.MockLoader)
for n in ('--os-a-int', '--os-a-bool', '--os-a-float'): for n in ('--os-a-int', '--os-a-bool', '--os-a-float'):
self.assertIn(n, self.p.format_usage()) self.assertIn(n, self.p.format_usage())
@ -87,7 +87,7 @@ class CliTests(utils.TestCase):
'--os-a-bool', str(self.a_bool)] '--os-a-bool', str(self.a_bool)]
klass = loading.register_argparse_arguments(self.p, argv) klass = loading.register_argparse_arguments(self.p, argv)
self.assertIs(utils.MockPlugin, klass) self.assertIsInstance(klass, utils.MockLoader)
opts = self.p.parse_args(argv) opts = self.p.parse_args(argv)
self.assertEqual(name, opts.os_auth_plugin) self.assertEqual(name, opts.os_auth_plugin)
@ -107,7 +107,7 @@ class CliTests(utils.TestCase):
'--os-a-float', str(self.a_float)] '--os-a-float', str(self.a_float)]
klass = loading.register_argparse_arguments(self.p, argv) klass = loading.register_argparse_arguments(self.p, argv)
self.assertIs(utils.MockPlugin, klass) self.assertIsInstance(klass, utils.MockLoader)
opts = self.p.parse_args(argv) opts = self.p.parse_args(argv)
self.assertEqual(name, opts.os_auth_plugin) self.assertEqual(name, opts.os_auth_plugin)
@ -121,7 +121,7 @@ class CliTests(utils.TestCase):
def test_with_default_string_value(self, m): def test_with_default_string_value(self, m):
name = uuid.uuid4().hex name = uuid.uuid4().hex
klass = loading.register_argparse_arguments(self.p, [], default=name) klass = loading.register_argparse_arguments(self.p, [], default=name)
self.assertIs(utils.MockPlugin, klass) self.assertIsInstance(klass, utils.MockLoader)
m.assert_called_once_with(name) m.assert_called_once_with(name)
@utils.mock_plugin @utils.mock_plugin
@ -132,29 +132,29 @@ class CliTests(utils.TestCase):
klass = loading.register_argparse_arguments(self.p, klass = loading.register_argparse_arguments(self.p,
argv, argv,
default=default) default=default)
self.assertIs(utils.MockPlugin, klass) self.assertIsInstance(klass, utils.MockLoader)
m.assert_called_once_with(name) m.assert_called_once_with(name)
@utils.mock_plugin @utils.mock_plugin
def test_with_default_type_value(self, m): def test_with_default_type_value(self, m):
klass = loading.register_argparse_arguments(self.p, klass = loading.register_argparse_arguments(self.p,
[], [],
default=utils.MockPlugin) default=utils.MockLoader())
self.assertIs(utils.MockPlugin, klass) self.assertIsInstance(klass, utils.MockLoader)
self.assertEqual(0, m.call_count) self.assertEqual(0, m.call_count)
@utils.mock_plugin @utils.mock_plugin
def test_overrides_default_type_value(self, m): def test_overrides_default_type_value(self, m):
# using this test plugin would fail if called because there # using this test plugin would fail if called because there
# is no get_options() function # is no get_options() function
class TestPlugin(object): class TestLoader(object):
pass pass
name = uuid.uuid4().hex name = uuid.uuid4().hex
argv = ['--os-auth-plugin', name] argv = ['--os-auth-plugin', name]
klass = loading.register_argparse_arguments(self.p, klass = loading.register_argparse_arguments(self.p,
argv, argv,
default=TestPlugin) default=TestLoader)
self.assertIs(utils.MockPlugin, klass) self.assertIsInstance(klass, utils.MockLoader)
m.assert_called_once_with(name) m.assert_called_once_with(name)
@utils.mock_plugin @utils.mock_plugin

View File

@ -23,6 +23,10 @@ from keystoneauth1.loading._plugins.identity import v3
from keystoneauth1.tests.unit.auth import utils from keystoneauth1.tests.unit.auth import utils
def to_oslo_opts(opts):
return [o._to_oslo_opt() for o in opts]
class ConfTests(utils.TestCase): class ConfTests(utils.TestCase):
def setUp(self): def setUp(self):
@ -45,8 +49,9 @@ class ConfTests(utils.TestCase):
self.conf_fixture.config(auth_section=section, group=self.GROUP) self.conf_fixture.config(auth_section=section, group=self.GROUP)
loading.register_conf_options(self.conf_fixture.conf, group=self.GROUP) loading.register_conf_options(self.conf_fixture.conf, group=self.GROUP)
self.conf_fixture.register_opts(v2.Password.get_options(), self.conf_fixture.register_opts(
group=section) to_oslo_opts(v2.Password().get_options()),
group=section)
self.conf_fixture.config(auth_plugin=self.V2PASS, self.conf_fixture.config(auth_plugin=self.V2PASS,
username=username, username=username,
@ -72,7 +77,7 @@ class ConfTests(utils.TestCase):
self.conf_fixture.config(auth_section=section, group=self.GROUP) self.conf_fixture.config(auth_section=section, group=self.GROUP)
loading.register_conf_options(self.conf_fixture.conf, group=self.GROUP) loading.register_conf_options(self.conf_fixture.conf, group=self.GROUP)
self.conf_fixture.register_opts(v3.Token().get_options(), self.conf_fixture.register_opts(to_oslo_opts(v3.Token().get_options()),
group=section) group=section)
self.conf_fixture.config(auth_plugin=self.V3TOKEN, self.conf_fixture.config(auth_plugin=self.V3TOKEN,
@ -107,11 +112,12 @@ class ConfTests(utils.TestCase):
@mock.patch('stevedore.DriverManager') @mock.patch('stevedore.DriverManager')
def test_other_params(self, m): def test_other_params(self, m):
m.return_value = utils.MockManager(utils.MockPlugin) m.return_value = utils.MockManager(utils.MockLoader())
driver_name = uuid.uuid4().hex driver_name = uuid.uuid4().hex
self.conf_fixture.register_opts(utils.MockPlugin.get_options(), self.conf_fixture.register_opts(
group=self.GROUP) to_oslo_opts(utils.MockLoader().get_options()),
group=self.GROUP)
self.conf_fixture.config(auth_plugin=driver_name, self.conf_fixture.config(auth_plugin=driver_name,
group=self.GROUP, group=self.GROUP,
**self.TEST_VALS) **self.TEST_VALS)
@ -120,12 +126,15 @@ class ConfTests(utils.TestCase):
self.assertTestVals(a) self.assertTestVals(a)
m.assert_called_once_with(namespace=loading.PLUGIN_NAMESPACE, m.assert_called_once_with(namespace=loading.PLUGIN_NAMESPACE,
name=driver_name) name=driver_name,
invoke_on_load=True)
@utils.mock_plugin @utils.mock_plugin
def test_same_section(self, m): def test_same_section(self, m):
self.conf_fixture.register_opts(utils.MockPlugin.get_options(), self.conf_fixture.register_opts(
group=self.GROUP) to_oslo_opts(utils.MockLoader().get_options()),
group=self.GROUP)
loading.register_conf_options(self.conf_fixture.conf, group=self.GROUP) loading.register_conf_options(self.conf_fixture.conf, group=self.GROUP)
self.conf_fixture.config(auth_plugin=uuid.uuid4().hex, self.conf_fixture.config(auth_plugin=uuid.uuid4().hex,
group=self.GROUP, group=self.GROUP,
@ -141,8 +150,9 @@ class ConfTests(utils.TestCase):
self.conf_fixture.config(auth_section=section, group=self.GROUP) self.conf_fixture.config(auth_section=section, group=self.GROUP)
loading.register_conf_options(self.conf_fixture.conf, group=self.GROUP) loading.register_conf_options(self.conf_fixture.conf, group=self.GROUP)
self.conf_fixture.register_opts(utils.MockPlugin.get_options(), self.conf_fixture.register_opts(to_oslo_opts(
group=section) utils.MockLoader().get_options()),
group=section)
self.conf_fixture.config(group=section, self.conf_fixture.config(group=section,
auth_plugin=uuid.uuid4().hex, auth_plugin=uuid.uuid4().hex,
**self.TEST_VALS) **self.TEST_VALS)
@ -168,6 +178,6 @@ class ConfTests(utils.TestCase):
def test_get_named(self): def test_get_named(self):
loaded_opts = loading.get_plugin_options('v2password') loaded_opts = loading.get_plugin_options('v2password')
plugin_opts = v2.Password.get_options() plugin_opts = v2.Password().get_options()
self.assertEqual(plugin_opts, loaded_opts) self.assertEqual(plugin_opts, loaded_opts)