/
test_setup_entrypoints.py
254 lines (225 loc) · 11.4 KB
/
test_setup_entrypoints.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
import importlib
import inspect
import os
import pkg_resources
import warnings
import twisted
from twisted.trial import unittest
from twisted.trial.unittest import SkipTest
from zope.interface.verify import verifyClass
from buildbot.interfaces import IBuildStep
from buildbot.interfaces import IChangeSource
from buildbot.interfaces import IScheduler
from buildbot.interfaces import IWorker
from buildbot.plugins.db import get_plugins
def get_python_module_contents(package_name):
spec = importlib.util.find_spec(package_name)
if spec is None or spec.origin is None:
return set()
pathname = os.path.dirname(spec.origin)
result = set()
with os.scandir(pathname) as dir_entries:
for dir_entry in dir_entries:
filename = dir_entry.name
if filename.startswith('__'):
continue
next_package_name = '.'.join((package_name, filename.partition('.')[0]))
if dir_entry.is_file() and filename.endswith('.py'):
result.add(next_package_name)
if dir_entry.is_dir():
result.add(next_package_name)
result |= get_python_module_contents(next_package_name)
return result
# NOTE: when running this test locally, make sure to reinstall master after every change to pick up
# new entry points.
class TestSetupPyEntryPoints(unittest.TestCase):
def test_changes(self):
known_not_exported = {
'buildbot.changes.gerritchangesource.GerritChangeSourceBase',
'buildbot.changes.base.ReconfigurablePollingChangeSource',
'buildbot.changes.base.PollingChangeSource',
'buildbot.changes.base.ChangeSource',
}
self.verify_plugins_registered('changes', 'buildbot.changes', IChangeSource,
known_not_exported)
def test_schedulers(self):
known_not_exported = {
'buildbot.schedulers.basic.BaseBasicScheduler',
'buildbot.schedulers.timed.Timed',
'buildbot.schedulers.trysched.TryBase',
'buildbot.schedulers.base.BaseScheduler',
'buildbot.schedulers.timed.NightlyBase',
'buildbot.schedulers.basic.Scheduler',
}
self.verify_plugins_registered('schedulers', 'buildbot.schedulers', IScheduler,
known_not_exported)
def test_steps(self):
known_not_exported = {
'buildbot.steps.download_secret_to_worker.RemoveWorkerFileSecret',
'buildbot.steps.source.base.Source',
'buildbot.steps.download_secret_to_worker.DownloadSecretsToWorker',
'buildbot.steps.shell.SetProperty',
'buildbot.steps.worker.WorkerBuildStep',
'buildbot.steps.vstudio.VisualStudio',
}
self.verify_plugins_registered('steps', 'buildbot.steps', IBuildStep, known_not_exported)
def test_util(self):
# work around Twisted bug 9384.
if pkg_resources.parse_version(twisted.__version__) < pkg_resources.parse_version("18.9.0"):
raise SkipTest('manhole.py can not be imported on old twisted and new python')
known_not_exported = {
'buildbot.util._notifier.Notifier',
'buildbot.util.backoff.ExponentialBackoffEngineAsync',
'buildbot.util.backoff.ExponentialBackoffEngineSync',
'buildbot.util.backoff.BackoffTimeoutExceededError',
'buildbot.util.backoff.ExponentialBackoffEngine',
'buildbot.util.bbcollections.KeyedSets',
'buildbot.util.codebase.AbsoluteSourceStampsMixin',
'buildbot.util.config.ConfiguredMixin',
'buildbot.util.croniter.croniter',
'buildbot.util.debounce.Debouncer',
'buildbot.util.deferwaiter.DeferWaiter',
'buildbot.util.deferwaiter.RepeatedActionHandler',
'buildbot.util.git.GitMixin',
'buildbot.util.git.GitStepMixin',
'buildbot.util.giturlparse.GitUrl',
'buildbot.util.httpclientservice.HTTPClientService',
'buildbot.util.httpclientservice.TreqResponseWrapper',
'buildbot.util.httpclientservice.TxRequestsResponseWrapper',
'buildbot.util.kubeclientservice.KubeClientService',
'buildbot.util.kubeclientservice.KubeConfigLoaderBase',
'buildbot.util.kubeclientservice.KubeError',
'buildbot.util.latent.CompatibleLatentWorkerMixin',
'buildbot.util.lineboundaries.LineBoundaryFinder',
'buildbot.util.lru.AsyncLRUCache',
'buildbot.util.lru.LRUCache',
'buildbot.util.maildir.MaildirService',
'buildbot.util.maildir.NoSuchMaildir',
'buildbot.util.netstrings.NetstringParser',
'buildbot.util.netstrings.NullAddress',
'buildbot.util.netstrings.NullTransport',
'buildbot.util.pathmatch.Matcher',
'buildbot.util.poll.Poller',
'buildbot.util.private_tempdir.PrivateTemporaryDirectory',
'buildbot.util.protocol.LineBuffer',
'buildbot.util.protocol.LineProcessProtocol',
'buildbot.util.pullrequest.PullRequestMixin',
'buildbot.util.queue.ConnectableThreadQueue',
'buildbot.util.queue.UndoableQueue',
'buildbot.util.raml.RamlLoader',
'buildbot.util.raml.RamlSpec',
'buildbot.util.runprocess.RunProcessPP',
'buildbot.util.runprocess.RunProcess',
'buildbot.util.sautils.InsertFromSelect',
'buildbot.util.service.AsyncMultiService',
'buildbot.util.service.AsyncService',
'buildbot.util.service.BuildbotService',
'buildbot.util.service.BuildbotServiceManager',
'buildbot.util.service.ClusteredBuildbotService',
'buildbot.util.service.MasterService',
'buildbot.util.service.ReconfigurableServiceMixin',
'buildbot.util.service.SharedService',
'buildbot.util.state.StateMixin',
'buildbot.util.subscription.Subscription',
'buildbot.util.subscription.SubscriptionPoint',
'buildbot.util.test_result_submitter.TestResultSubmitter',
}
self.verify_plugins_registered('util', 'buildbot.util', None, known_not_exported)
def test_reporters(self):
known_not_exported = {
'buildbot.reporters.base.ReporterBase',
'buildbot.reporters.generators.utils.BuildStatusGeneratorMixin',
'buildbot.reporters.gerrit.DEFAULT_REVIEW',
'buildbot.reporters.gerrit.DEFAULT_SUMMARY',
'buildbot.reporters.irc.IRCChannel',
'buildbot.reporters.irc.IRCContact',
'buildbot.reporters.irc.IrcStatusBot',
'buildbot.reporters.irc.IrcStatusFactory',
'buildbot.reporters.irc.UsageError',
'buildbot.reporters.mail.Domain',
'buildbot.reporters.message.MessageFormatterBase',
'buildbot.reporters.message.MessageFormatterBaseJinja',
'buildbot.reporters.telegram.TelegramChannel',
'buildbot.reporters.telegram.TelegramContact',
'buildbot.reporters.telegram.TelegramPollingBot',
'buildbot.reporters.telegram.TelegramStatusBot',
'buildbot.reporters.telegram.TelegramWebhookBot',
'buildbot.reporters.words.Channel',
'buildbot.reporters.words.Contact',
'buildbot.reporters.words.ForceOptions',
'buildbot.reporters.words.StatusBot',
'buildbot.reporters.words.ThrottledClientFactory',
'buildbot.reporters.words.UsageError',
'buildbot.reporters.words.WebhookResource',
}
self.verify_plugins_registered('reporters', 'buildbot.reporters', None, known_not_exported)
def test_secrets(self):
known_not_exported = {
'buildbot.secrets.manager.SecretManager',
'buildbot.secrets.providers.base.SecretProviderBase',
'buildbot.secrets.secret.SecretDetails',
'buildbot.secrets.providers.vault_hvac.VaultAuthenticator',
}
self.verify_plugins_registered('secrets', 'buildbot.secrets', None, known_not_exported)
def test_webhooks(self):
# in the case of webhooks the entry points list modules, not classes, so
# verify_plugins_registered won't work. For now let's ignore this edge case
get_plugins('webhooks', None, load_now=True)
def test_workers(self):
known_not_exported = {
'buildbot.worker.upcloud.UpcloudLatentWorker',
'buildbot.worker.base.AbstractWorker',
'buildbot.worker.latent.AbstractLatentWorker',
'buildbot.worker.latent.LocalLatentWorker',
'buildbot.worker.marathon.MarathonLatentWorker',
'buildbot.worker.docker.DockerBaseWorker',
}
self.verify_plugins_registered('worker', 'buildbot.worker', IWorker, known_not_exported)
def verify_plugins_registered(self, plugin_type, module_name, interface,
known_not_exported=None):
# This will verify whether we can load plugins, i.e. whether the entry points are valid.
plugins = get_plugins(plugin_type, interface, load_now=True)
# Now verify that are no unregistered plugins left.
existing_classes = self.get_existing_classes(module_name, interface)
exported_classes = {f'{plugins._get_entry(name)._entry.module_name}.{name}'
for name in plugins.names}
if known_not_exported is None:
known_not_exported = set()
not_exported_classes = existing_classes - exported_classes - known_not_exported
self.assertEqual(not_exported_classes, set())
self.assertEqual(known_not_exported - existing_classes, set())
def class_provides_iface(self, interface, klass):
try:
verifyClass(interface, klass)
return True
except Exception:
return False
def get_existing_classes(self, module_name, interface):
existing_modules = get_python_module_contents(module_name)
existing_classes = set()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
for existing_module in existing_modules:
module = importlib.import_module(existing_module)
for name, obj in inspect.getmembers(module):
if name.startswith('_'):
continue
if inspect.isclass(obj) and obj.__module__ == existing_module:
if interface is not None and not self.class_provides_iface(interface, obj):
continue
existing_classes.add(f'{existing_module}.{name}')
return existing_classes