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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
import os
import pytest
from pluginbase import PluginBase
from buildstream._elementfactory import _ElementFactory
from buildstream._sourcefactory import _SourceFactory
from buildstream import PluginError
DATA_DIR = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'basics',
)
# Simple fixture to create a PluginBase object that
# we use for loading plugins.
@pytest.fixture()
def plugin_fixture():
return {
'base' : PluginBase(package='buildstream.plugins')
}
##############################################################
# Basics: test the fixture, test we can create the factories #
##############################################################
def test_fixture(plugin_fixture):
assert(isinstance(plugin_fixture['base'], PluginBase))
def test_source_factory(plugin_fixture):
factory = _SourceFactory(plugin_fixture['base'])
assert(isinstance(factory, _SourceFactory))
def test_element_factory(plugin_fixture):
factory = _ElementFactory(plugin_fixture['base'])
assert(isinstance(factory, _ElementFactory))
##############################################################
# Check that we can load custom sources & elements #
##############################################################
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'customsource'))
def test_custom_source(plugin_fixture, datafiles):
factory = _SourceFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
assert(isinstance(factory, _SourceFactory))
foo_type = factory.lookup('foo')
assert(foo_type.__name__ == 'FooSource')
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'customelement'))
def test_custom_element(plugin_fixture, datafiles):
factory = _ElementFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
assert(isinstance(factory, _ElementFactory))
foo_type = factory.lookup('foo')
assert(foo_type.__name__ == 'FooElement')
##############################################################
# Check plugin loading failure modes #
##############################################################
def test_missing_source(plugin_fixture):
factory = _SourceFactory(plugin_fixture['base'])
assert(isinstance(factory, _SourceFactory))
# Test fails if PluginError is not raised
with pytest.raises(PluginError) as exc:
foo_type = factory.lookup('foo')
def test_missing_element(plugin_fixture):
factory = _ElementFactory(plugin_fixture['base'])
assert(isinstance(factory, _ElementFactory))
# Test fails if PluginError is not raised
with pytest.raises(PluginError) as exc:
foo_type = factory.lookup('foo')
# Load one factory with 2 plugin directories both containing a foo plugin
@pytest.mark.datafiles(DATA_DIR)
def test_conflict_source(plugin_fixture, datafiles):
plugins1 = os.path.join(datafiles.dirname, datafiles.basename, 'customsource')
plugins2 = os.path.join(datafiles.dirname, datafiles.basename, 'anothersource')
with pytest.raises(PluginError) as exc:
factory = _SourceFactory(plugin_fixture['base'], [ plugins1, plugins2 ])
# Load one factory with 2 plugin directories both containing a foo plugin
@pytest.mark.datafiles(DATA_DIR)
def test_conflict_element(plugin_fixture, datafiles):
plugins1 = os.path.join(datafiles.dirname, datafiles.basename, 'customelement')
plugins2 = os.path.join(datafiles.dirname, datafiles.basename, 'anotherelement')
with pytest.raises(PluginError) as exc:
factory = _ElementFactory(plugin_fixture['base'], [ plugins1, plugins2 ])
# Load a factory with a plugin that returns a value instead of a Source subclass
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'notatype'))
def test_source_notatype(plugin_fixture, datafiles):
with pytest.raises(PluginError) as exc:
factory = _SourceFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
# Load a factory with a plugin that returns a value instead of a Element subclass
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'notatype'))
def test_element_notatype(plugin_fixture, datafiles):
with pytest.raises(PluginError) as exc:
factory = _ElementFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
# Load a factory with a plugin that returns a type which is not a Source subclass
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'wrongtype'))
def test_source_wrongtype(plugin_fixture, datafiles):
with pytest.raises(PluginError) as exc:
factory = _SourceFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
# Load a factory with a plugin that returns a type which is not a Element subclass
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'wrongtype'))
def test_element_wrongtype(plugin_fixture, datafiles):
with pytest.raises(PluginError) as exc:
factory = _ElementFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
# Load a factory with a plugin which fails to provide a setup() function
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'nosetup'))
def test_source_missing_setup(plugin_fixture, datafiles):
with pytest.raises(PluginError) as exc:
factory = _SourceFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
# Load a factory with a plugin which fails to provide a setup() function
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'nosetup'))
def test_element_missing_setup(plugin_fixture, datafiles):
with pytest.raises(PluginError) as exc:
factory = _ElementFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
# Load a factory with a plugin which provides a setup symbol that is not a function
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'badsetup'))
def test_source_bad_setup(plugin_fixture, datafiles):
with pytest.raises(PluginError) as exc:
factory = _SourceFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
# Load a factory with a plugin which provides a setup symbol that is not a function
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'badsetup'))
def test_element_bad_setup(plugin_fixture, datafiles):
with pytest.raises(PluginError) as exc:
factory = _ElementFactory(plugin_fixture['base'],
[ os.path.join(datafiles.dirname, datafiles.basename) ])
##############################################################
# Check we can load different contexts of plugin #
##############################################################
# Load two factories, both of which define a different 'foo' plugin
@pytest.mark.datafiles(DATA_DIR)
def test_source_multicontext(plugin_fixture, datafiles):
plugins1 = os.path.join(datafiles.dirname, datafiles.basename, 'customsource')
plugins2 = os.path.join(datafiles.dirname, datafiles.basename, 'anothersource')
factory1 = _SourceFactory(plugin_fixture['base'], [ plugins1 ])
factory2 = _SourceFactory(plugin_fixture['base'], [ plugins2 ])
assert(isinstance(factory1, _SourceFactory))
assert(isinstance(factory2, _SourceFactory))
foo_type1 = factory1.lookup('foo')
foo_type2 = factory2.lookup('foo')
assert(foo_type1.__name__ == 'FooSource')
assert(foo_type2.__name__ == 'AnotherFooSource')
# Load two factories, both of which define a different 'foo' plugin
@pytest.mark.datafiles(DATA_DIR)
def test_element_multicontext(plugin_fixture, datafiles):
plugins1 = os.path.join(datafiles.dirname, datafiles.basename, 'customelement')
plugins2 = os.path.join(datafiles.dirname, datafiles.basename, 'anotherelement')
factory1 = _ElementFactory(plugin_fixture['base'], [ plugins1 ])
factory2 = _ElementFactory(plugin_fixture['base'], [ plugins2 ])
assert(isinstance(factory1, _ElementFactory))
assert(isinstance(factory2, _ElementFactory))
foo_type1 = factory1.lookup('foo')
foo_type2 = factory2.lookup('foo')
assert(foo_type1.__name__ == 'FooElement')
assert(foo_type2.__name__ == 'AnotherFooElement')
|