test_named.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 unittest import mock
  13. from stevedore import named
  14. from stevedore.tests import utils
  15. class TestNamed(utils.TestCase):
  16. def test_named(self):
  17. em = named.NamedExtensionManager(
  18. 'stevedore.test.extension',
  19. names=['t1'],
  20. invoke_on_load=True,
  21. invoke_args=('a',),
  22. invoke_kwds={'b': 'B'},
  23. )
  24. actual = em.names()
  25. self.assertEqual(actual, ['t1'])
  26. def test_enabled_before_load(self):
  27. # Set up the constructor for the FauxExtension to cause an
  28. # AssertionError so the test fails if the class is instantiated,
  29. # which should only happen if it is loaded before the name of the
  30. # extension is compared against the names that should be loaded by
  31. # the manager.
  32. init_name = 'stevedore.tests.test_extension.FauxExtension.__init__'
  33. with mock.patch(init_name) as m:
  34. m.side_effect = AssertionError
  35. em = named.NamedExtensionManager(
  36. 'stevedore.test.extension',
  37. # Look for an extension that does not exist so the
  38. # __init__ we mocked should never be invoked.
  39. names=['no-such-extension'],
  40. invoke_on_load=True,
  41. invoke_args=('a',),
  42. invoke_kwds={'b': 'B'},
  43. )
  44. actual = em.names()
  45. self.assertEqual(actual, [])
  46. def test_extensions_listed_in_name_order(self):
  47. # Since we don't know the "natural" order of the extensions, run
  48. # the test both ways: if the sorting is broken, one of them will
  49. # fail
  50. em = named.NamedExtensionManager(
  51. 'stevedore.test.extension',
  52. names=['t1', 't2'],
  53. name_order=True
  54. )
  55. actual = em.names()
  56. self.assertEqual(actual, ['t1', 't2'])
  57. em = named.NamedExtensionManager(
  58. 'stevedore.test.extension',
  59. names=['t2', 't1'],
  60. name_order=True
  61. )
  62. actual = em.names()
  63. self.assertEqual(actual, ['t2', 't1'])
  64. def test_load_fail_ignored_when_sorted(self):
  65. em = named.NamedExtensionManager(
  66. 'stevedore.test.extension',
  67. names=['e1', 't2', 'e2', 't1'],
  68. name_order=True,
  69. invoke_on_load=True,
  70. invoke_args=('a',),
  71. invoke_kwds={'b': 'B'},
  72. )
  73. actual = em.names()
  74. self.assertEqual(['t2', 't1'], actual)
  75. em = named.NamedExtensionManager(
  76. 'stevedore.test.extension',
  77. names=['e1', 't1'],
  78. name_order=False,
  79. invoke_on_load=True,
  80. invoke_args=('a',),
  81. invoke_kwds={'b': 'B'},
  82. )
  83. actual = em.names()
  84. self.assertEqual(['t1'], actual)