Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/test_plugin.py
1566 views
1
# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import sys
14
from awscli.testutils import mock, unittest
15
16
from awscli import plugin
17
from botocore import hooks
18
19
20
class FakeModule(object):
21
def __init__(self):
22
self.called = False
23
self.context = None
24
self.events_seen = []
25
26
def awscli_initialize(self, context):
27
self.called = True
28
self.context = context
29
self.context.register(
30
'before_operation',
31
(lambda **kwargs: self.events_seen.append(kwargs)))
32
33
34
class TestPlugins(unittest.TestCase):
35
36
def setUp(self):
37
self.fake_module = FakeModule()
38
sys.modules['__fake_plugin__'] = self.fake_module
39
40
def tearDown(self):
41
del sys.modules['__fake_plugin__']
42
43
def test_plugin_register(self):
44
emitter = plugin.load_plugins({'fake_plugin': '__fake_plugin__'})
45
self.assertTrue(self.fake_module.called)
46
self.assertTrue(isinstance(emitter, hooks.HierarchicalEmitter))
47
self.assertTrue(isinstance(self.fake_module.context,
48
hooks.HierarchicalEmitter))
49
50
def test_event_hooks_can_be_passed_in(self):
51
hooks = plugin.HierarchicalEmitter()
52
emitter = plugin.load_plugins({'fake_plugin': '__fake_plugin__'},
53
event_hooks=hooks)
54
emitter.emit('before_operation')
55
self.assertEqual(len(self.fake_module.events_seen), 1)
56
57
58
class TestPluginCanBePackage(unittest.TestCase):
59
def setUp(self):
60
self.fake_module = FakeModule()
61
self.fake_package = mock.Mock()
62
sys.modules['__fake_plugin__'] = self.fake_package
63
sys.modules['__fake_plugin__.__fake__'] = self.fake_package
64
sys.modules['__fake_plugin__.__fake__.bar'] = self.fake_module
65
66
def tearDown(self):
67
del sys.modules['__fake_plugin__.__fake__']
68
69
def test_plugin_register(self):
70
plugin.load_plugins(
71
{'fake_plugin': '__fake_plugin__.__fake__.bar'})
72
self.assertTrue(self.fake_module.called)
73
74
75
if __name__ == '__main__':
76
unittest.main()
77
78