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
13from stevedore import hook
14from stevedore.tests import utils
15
16
17class TestHook(utils.TestCase):
18    def test_hook(self):
19        em = hook.HookManager(
20            'stevedore.test.extension',
21            't1',
22            invoke_on_load=True,
23            invoke_args=('a',),
24            invoke_kwds={'b': 'B'},
25        )
26        self.assertEqual(len(em.extensions), 1)
27        self.assertEqual(em.names(), ['t1'])
28
29    def test_get_by_name(self):
30        em = hook.HookManager(
31            'stevedore.test.extension',
32            't1',
33            invoke_on_load=True,
34            invoke_args=('a',),
35            invoke_kwds={'b': 'B'},
36        )
37        e_list = em['t1']
38        self.assertEqual(len(e_list), 1)
39        e = e_list[0]
40        self.assertEqual(e.name, 't1')
41
42    def test_get_by_name_missing(self):
43        em = hook.HookManager(
44            'stevedore.test.extension',
45            't1',
46            invoke_on_load=True,
47            invoke_args=('a',),
48            invoke_kwds={'b': 'B'},
49        )
50        try:
51            em['t2']
52        except KeyError:
53            pass
54        else:
55            assert False, 'Failed to raise KeyError'
56