401. Plugin infrastructure
[mediagoblin.git] / mediagoblin / tests / test_pluginapi.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import sys
18 from configobj import ConfigObj
19 from mediagoblin import mg_globals
20 from mediagoblin.init.plugins import setup_plugins
21 from mediagoblin.tools import pluginapi
22 from nose.tools import eq_
23
24
25 def with_cleanup(*modules_to_delete):
26 def _with_cleanup(fun):
27 """Wrapper that saves and restores mg_globals"""
28 def _with_cleanup_inner(*args, **kwargs):
29 old_app_config = mg_globals.app_config
30 old_global_config = mg_globals.global_config
31 # Need to delete icky modules before and after so as to make
32 # sure things work correctly.
33 for module in modules_to_delete:
34 try:
35 del sys.modules[module]
36 except KeyError:
37 pass
38 # The plugin cache gets populated as a side-effect of
39 # importing, so it's best to clear it before and after a test.
40 pcache = pluginapi.PluginCache()
41 pcache.clear()
42 try:
43 return fun(*args, **kwargs)
44 finally:
45 mg_globals.app_config = old_app_config
46 mg_globals.global_config = old_global_config
47 # Need to delete icky modules before and after so as to make
48 # sure things work correctly.
49 for module in modules_to_delete:
50 try:
51 del sys.modules[module]
52 except KeyError:
53 pass
54 pcache.clear()
55
56 _with_cleanup_inner.__name__ = fun.__name__
57 return _with_cleanup_inner
58 return _with_cleanup
59
60
61 def build_config(sections):
62 """Builds a ConfigObj object with specified data
63
64 :arg sections: list of ``(section_name, section_data,
65 subsection_list)`` tuples where section_data is a dict and
66 subsection_list is a list of ``(section_name, section_data,
67 subsection_list)``, ...
68
69 For example:
70
71 >>> build_config([
72 ... ('mediagoblin', {'key1': 'val1'}, []),
73 ... ('section2', {}, [
74 ... ('subsection1', {}, [])
75 ... ])
76 ... ])
77 """
78 cfg = ConfigObj()
79 cfg.filename = 'foo'
80 def _iter_section(cfg, section_list):
81 for section_name, data, subsection_list in section_list:
82 cfg[section_name] = data
83 _iter_section(cfg[section_name], subsection_list)
84
85 _iter_section(cfg, sections)
86 return cfg
87
88
89 @with_cleanup()
90 def test_no_plugins():
91 """Run setup_plugins with no plugins in config"""
92 cfg = build_config([('mediagoblin', {}, [])])
93 mg_globals.app_config = cfg['mediagoblin']
94 mg_globals.global_config = cfg
95
96 pcache = pluginapi.PluginCache()
97 setup_plugins()
98
99 # Make sure we didn't load anything.
100 eq_(len(pcache.plugin_classes), 0)
101 eq_(len(pcache.plugin_objects), 0)
102
103
104 @with_cleanup('mediagoblin.plugins.sampleplugin',
105 'mediagoblin.plugins.sampleplugin.main')
106 def test_one_plugin():
107 """Run setup_plugins with a single working plugin"""
108 cfg = build_config([
109 ('mediagoblin', {}, []),
110 ('plugins', {}, [
111 ('mediagoblin.plugins.sampleplugin', {}, [])
112 ])
113 ])
114
115 mg_globals.app_config = cfg['mediagoblin']
116 mg_globals.global_config = cfg
117
118 pcache = pluginapi.PluginCache()
119 setup_plugins()
120
121 # Make sure we only found one plugin class
122 eq_(len(pcache.plugin_classes), 1)
123 # Make sure the class is the one we think it is.
124 eq_(pcache.plugin_classes[0].__name__, 'SamplePlugin')
125
126 # Make sure there was one plugin created
127 eq_(len(pcache.plugin_objects), 1)
128 # Make sure we called setup_plugin on SamplePlugin
129 eq_(pcache.plugin_objects[0]._setup_plugin_called, 1)
130
131
132 @with_cleanup('mediagoblin.plugins.sampleplugin',
133 'mediagoblin.plugins.sampleplugin.main')
134 def test_same_plugin_twice():
135 """Run setup_plugins with a single working plugin twice"""
136 cfg = build_config([
137 ('mediagoblin', {}, []),
138 ('plugins', {}, [
139 ('mediagoblin.plugins.sampleplugin', {}, []),
140 ('mediagoblin.plugins.sampleplugin', {}, []),
141 ])
142 ])
143
144 mg_globals.app_config = cfg['mediagoblin']
145 mg_globals.global_config = cfg
146
147 pcache = pluginapi.PluginCache()
148 setup_plugins()
149
150 # Make sure we only found one plugin class
151 eq_(len(pcache.plugin_classes), 1)
152 # Make sure the class is the one we think it is.
153 eq_(pcache.plugin_classes[0].__name__, 'SamplePlugin')
154
155 # Make sure there was one plugin created
156 eq_(len(pcache.plugin_objects), 1)
157 # Make sure we called setup_plugin on SamplePlugin
158 eq_(pcache.plugin_objects[0]._setup_plugin_called, 1)