def tear_down_sources_task_error_processing_test(self): """Test error processing task to tear down installation sources.""" tear_down_task1 = create_autospec(Task) tear_down_task2 = create_autospec(Task) tear_down_task3 = create_autospec(Task) tear_down_task1.run.side_effect = SourceTearDownError("task1 error") tear_down_task3.run.side_effect = SourceTearDownError("task3 error") source1 = create_autospec(PayloadSourceBase) source2 = create_autospec(PayloadSourceBase) source1.tear_down_with_tasks.return_value = [tear_down_task1, tear_down_task2] source2.tear_down_with_tasks.return_value = [tear_down_task3] task = TearDownSourcesTask([source1, source2]) with self.assertLogs(level="ERROR") as cm: with self.assertRaises(SourceTearDownError): task.run() self.assertTrue(any(map(lambda x: "task1 error" in x, cm.output))) self.assertTrue(any(map(lambda x: "task3 error" in x, cm.output))) # all the tasks should be tear down even when exception raised tear_down_task1.run.assert_called_once() tear_down_task2.run.assert_called_once() tear_down_task3.run.assert_called_once()
def run(self): """Collect and call tear down tasks for all the sources.""" if not self._sources: raise SourceSetupError("No sources specified for tear down!") errors = [] for source in self._sources: tasks = source.tear_down_with_tasks() log.debug("Collected %s tasks from %s source", [task.name for task in tasks], source.type) for task in tasks: log.debug("Running task %s", task.name) try: task.run() except SourceTearDownError as e: message = "Task '{}' from source '{}' has failed, reason: {}".format( task.name, source.type, str(e)) errors.append(message) log.error("%s\n%s", message, traceback.format_exc()) if errors: raise SourceTearDownError("Sources tear down have failed", errors)
def _check_mount(self): """Check if the source is unmounted.""" if os.path.ismount(self._target_mount): raise SourceTearDownError( "The mount point {} is still in use.".format( self._target_mount))