File: test_plugin.py

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (94 lines) | stat: -rw-r--r-- 3,153 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import sys

from botocore import hooks

from awscli import plugin
from awscli.testutils import mock, unittest


class FakeModule:
    def __init__(self):
        self.called = False
        self.context = None
        self.events_seen = []

    def awscli_initialize(self, context):
        self.called = True
        self.context = context
        self.context.register(
            'before_operation',
            (lambda **kwargs: self.events_seen.append(kwargs)),
        )


class TestPlugins(unittest.TestCase):
    def setUp(self):
        self.fake_module = FakeModule()
        sys.modules['__fake_plugin__'] = self.fake_module
        self.plugin_mapping = {
            'cli_legacy_plugin_path': 'fake-path',
            'fake_plugin': '__fake_plugin__',
        }

    def tearDown(self):
        del sys.modules['__fake_plugin__']

    def test_plugin_register(self):
        emitter = plugin.load_plugins(self.plugin_mapping)
        self.assertTrue(self.fake_module.called)
        self.assertTrue(isinstance(emitter, hooks.HierarchicalEmitter))
        self.assertTrue(
            isinstance(self.fake_module.context, hooks.HierarchicalEmitter)
        )

    def test_event_hooks_can_be_passed_in(self):
        hooks = plugin.HierarchicalEmitter()
        emitter = plugin.load_plugins(self.plugin_mapping, event_hooks=hooks)
        emitter.emit('before_operation')
        self.assertEqual(len(self.fake_module.events_seen), 1)

    def test_plugins_not_registered_if_cli_legacy_plugin_path_not_set(self):
        del self.plugin_mapping['cli_legacy_plugin_path']
        plugin.load_plugins(self.plugin_mapping)
        self.assertFalse(self.fake_module.called)


class TestPluginCanBePackage(unittest.TestCase):
    def setUp(self):
        self.fake_module = FakeModule()
        self.fake_package = mock.Mock()
        sys.modules['__fake_plugin__'] = self.fake_package
        sys.modules['__fake_plugin__.__fake__'] = self.fake_package
        sys.modules['__fake_plugin__.__fake__.bar'] = self.fake_module
        self.plugin_mapping = {
            'cli_legacy_plugin_path': 'fake-path',
            'fake_plugin': '__fake_plugin__.__fake__.bar',
        }

    def tearDown(self):
        del sys.modules['__fake_plugin__.__fake__']

    def test_plugin_register(self):
        plugin.load_plugins(
            {
                'cli_legacy_plugin_path': 'fake-path',
                'fake_plugin': '__fake_plugin__.__fake__.bar',
            }
        )
        self.assertTrue(self.fake_module.called)


if __name__ == '__main__':
    unittest.main()