
The patch brings a couple of new concepts into cloudinit. We have a general miniframework for defining plugins and discovering them, with an actual implementation which uses the *pkgutil* builtin module. Built atop of this framework, we have a new data source loader, found in cloudinit.bases.DataSourceLoader. The loader operates on three concepts: - the *module iterator*, which is used to list modules from a specific location (in our case, from cloudinit.sources.__path__). The data source loader takes care to use only the modules that exports a given API. - the data source discovery API assumes that each data source module exports a function called `data_sources`, which should return a tuple of data source classes that the said module exports. The loader filters the modules that provides this API. - the data source loader uses a new concept called *search strategy* for discovering a potential data source. The search strategies are classes whose purpose is to select one or more data sources from a data source stream (any iterable). There are multiple ways to implement a strategy, the search can be either serial or parallel, there's no additional requirement as long as they return an iterable. Also, the strategies can be stacked together, for instance, having two strategies, one for selecting only the network data sources and another for selecting the available data sources from a list of potential data sources. This patch also adds a new API that uses the DataSourceLoader with a given module iterator and strategies for selecting one data source that cloudinit can use. Change-Id: I30f312191ce40e45445ed9e3fc8a3d4651903280
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
# Copyright 2015 Canonical Ltd.
|
|
# This file is part of cloud-init. See LICENCE file for license information.
|
|
#
|
|
# vi: ts=4 expandtab
|
|
|
|
import contextlib
|
|
import os
|
|
import shutil
|
|
import tempfile
|
|
|
|
from cloudinit import plugin_finder
|
|
from cloudinit.tests import TestCase
|
|
from cloudinit.tests import util
|
|
|
|
|
|
class TestPkgutilModuleIterator(TestCase):
|
|
|
|
@staticmethod
|
|
@contextlib.contextmanager
|
|
def _create_tmpdir():
|
|
tmpdir = tempfile.mkdtemp()
|
|
try:
|
|
yield tmpdir
|
|
finally:
|
|
shutil.rmtree(tmpdir)
|
|
|
|
@contextlib.contextmanager
|
|
def _create_package(self):
|
|
with self._create_tmpdir() as tmpdir:
|
|
path = os.path.join(tmpdir, 'good.py')
|
|
with open(path, 'w') as stream:
|
|
stream.write('name = 42')
|
|
|
|
# Make sure this fails.
|
|
bad = os.path.join(tmpdir, 'bad.py')
|
|
with open(bad, 'w') as stream:
|
|
stream.write('import missingmodule')
|
|
|
|
yield tmpdir
|
|
|
|
def test_pkgutil_module_iterator(self):
|
|
logging_format = ("Could not import the module 'bad' "
|
|
"using the search path %r")
|
|
|
|
with util.LogSnatcher('cloudinit.plugin_finder') as snatcher:
|
|
with self._create_package() as tmpdir:
|
|
expected_logging = logging_format % tmpdir
|
|
iterator = plugin_finder.PkgutilModuleIterator([tmpdir])
|
|
modules = list(iterator.list_modules())
|
|
|
|
self.assertEqual(len(modules), 1)
|
|
module = modules[0]
|
|
self.assertEqual(module.name, 42)
|
|
self.assertEqual(len(snatcher.output), 1)
|
|
self.assertEqual(snatcher.output[0], expected_logging)
|