test_hook.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. from stevedore import hook
  13. from stevedore.tests import utils
  14. class TestHook(utils.TestCase):
  15. def test_hook(self):
  16. em = hook.HookManager(
  17. 'stevedore.test.extension',
  18. 't1',
  19. invoke_on_load=True,
  20. invoke_args=('a',),
  21. invoke_kwds={'b': 'B'},
  22. )
  23. self.assertEqual(len(em.extensions), 1)
  24. self.assertEqual(em.names(), ['t1'])
  25. def test_get_by_name(self):
  26. em = hook.HookManager(
  27. 'stevedore.test.extension',
  28. 't1',
  29. invoke_on_load=True,
  30. invoke_args=('a',),
  31. invoke_kwds={'b': 'B'},
  32. )
  33. e_list = em['t1']
  34. self.assertEqual(len(e_list), 1)
  35. e = e_list[0]
  36. self.assertEqual(e.name, 't1')
  37. def test_get_by_name_missing(self):
  38. em = hook.HookManager(
  39. 'stevedore.test.extension',
  40. 't1',
  41. invoke_on_load=True,
  42. invoke_args=('a',),
  43. invoke_kwds={'b': 'B'},
  44. )
  45. try:
  46. em['t2']
  47. except KeyError:
  48. pass
  49. else:
  50. assert False, 'Failed to raise KeyError'