def setUp(self): super(TestWalkerHandleHandler, self).setUp() tmpdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tmpdir) self.data = { "handlercount": 0, "frequency": "", "handlerdir": tmpdir, "handlers": helpers.ContentHandlers(), "data": None } self.expected_module_name = "part-handler-%03d" % ( self.data["handlercount"], ) expected_file_name = "%s.py" % self.expected_module_name self.expected_file_fullname = os.path.join(self.data["handlerdir"], expected_file_name) self.module_fake = FakeModule() self.ctype = None self.filename = None self.payload = "dummy payload" # Mock the write_file() function. We'll assert that it got called as # expected in each of the individual tests. resources = ExitStack() self.addCleanup(resources.close) self.write_file_mock = resources.enter_context( mock.patch('cloudinit.util.write_file'))
def setUp(self): MockerTestCase.setUp(self) self.data = { "handlercount": 0, "frequency": "", "handlerdir": self.makeDir(), "handlers": helpers.ContentHandlers(), "data": None } self.expected_module_name = "part-handler-%03d" % ( self.data["handlercount"], ) expected_file_name = "%s.py" % self.expected_module_name expected_file_fullname = os.path.join(self.data["handlerdir"], expected_file_name) self.module_fake = FakeModule() self.ctype = None self.filename = None self.payload = "dummy payload" # Mock the write_file function write_file_mock = self.mocker.replace(util.write_file, passthrough=False) write_file_mock(expected_file_fullname, self.payload, 0600)
def _do_handlers( self, data_msg, c_handlers_list, frequency, excluded=None ): """ Generalized handlers suitable for use with either vendordata or userdata """ if excluded is None: excluded = [] cdir = self.paths.get_cpath("handlers") idir = self._get_ipath("handlers") # Add the path to the plugins dir to the top of our list for importing # new handlers. # # Note(harlowja): instance dir should be read before cloud-dir for d in [cdir, idir]: if d and d not in sys.path: sys.path.insert(0, d) def register_handlers_in_dir(path): # Attempts to register any handler modules under the given path. if not path or not os.path.isdir(path): return potential_handlers = util.find_modules(path) for (fname, mod_name) in potential_handlers.items(): try: mod_locs, looked_locs = importer.find_module( mod_name, [""], ["list_types", "handle_part"] ) if not mod_locs: LOG.warning( "Could not find a valid user-data handler" " named %s in file %s (searched %s)", mod_name, fname, looked_locs, ) continue mod = importer.import_module(mod_locs[0]) mod = handlers.fixup_handler(mod) types = c_handlers.register(mod) if types: LOG.debug( "Added custom handler for %s [%s] from %s", types, mod, fname, ) except Exception: util.logexc( LOG, "Failed to register handler from %s", fname ) # This keeps track of all the active handlers c_handlers = helpers.ContentHandlers() # Add any handlers in the cloud-dir register_handlers_in_dir(cdir) # Register any other handlers that come from the default set. This # is done after the cloud-dir handlers so that the cdir modules can # take over the default user-data handler content-types. for mod in c_handlers_list: types = c_handlers.register(mod, overwrite=False) if types: LOG.debug("Added default handler for %s from %s", types, mod) # Form our cloud interface data = self.cloudify() def init_handlers(): # Init the handlers first for (_ctype, mod) in c_handlers.items(): if mod in c_handlers.initialized: # Avoid initiating the same module twice (if said module # is registered to more than one content-type). continue handlers.call_begin(mod, data, frequency) c_handlers.initialized.append(mod) def walk_handlers(excluded): # Walk the user data part_data = { "handlers": c_handlers, # Any new handlers that are encountered get writen here "handlerdir": idir, "data": data, # The default frequency if handlers don't have one "frequency": frequency, # This will be used when new handlers are found # to help write their contents to files with numbered # names... "handlercount": 0, "excluded": excluded, } handlers.walk(data_msg, handlers.walker_callback, data=part_data) def finalize_handlers(): # Give callbacks opportunity to finalize for (_ctype, mod) in c_handlers.items(): if mod not in c_handlers.initialized: # Said module was never inited in the first place, so lets # not attempt to finalize those that never got called. continue c_handlers.initialized.remove(mod) try: handlers.call_end(mod, data, frequency) except Exception: util.logexc(LOG, "Failed to finalize handler: %s", mod) try: init_handlers() walk_handlers(excluded) finally: finalize_handlers()