def _notify_hooks(hooks: dict, exit_status: bool, io: IO) -> str: mapping = {True: 'on_each_up', False: 'on_each_down'} out = "" if exit_status in mapping and mapping[exit_status] in hooks: commands = hooks[mapping[exit_status]] if type(commands).__name__ != 'list': raise RunnerException.from_expected_list_of_hooks( mapping[exit_status]) for command in commands: io.debug('Triggering hook command "{}"'.format(command)) try: out += subprocess.check_output( command, shell=True, timeout=1800).decode('utf-8').strip() except subprocess.CalledProcessError as e: io.error( 'Cannot execute hook command "{cmd}". Error: {err}'. format(cmd=command, err=str(e.output) + str(e.stderr))) except subprocess.TimeoutExpired: io.error( 'Cannot execute hook command "{cmd}. Timed out while executing command"' .format(cmd=command)) except Exception: io.error( 'Cannot execute hook command "{cmd}. Unknown error"'. format(cmd=command)) return out
def test_io_output_processing_changes_output(self): """ Tests adding "[stdout]" and "[stderr]" prefixes to the output """ mocked_output = [] io = IO() io.set_log_level('info') io._stderr = io._stdout = lambda txt: mocked_output.append(txt) # add a processor that will append origin - "stdout" or "stderr" io.add_output_processor( lambda txt, origin: '[{}]: {}'.format(origin, txt)) io.info('Hello from stdout') io.error('Hello from stderr') mocked_output_as_str = " ".join(mocked_output) self.assertIn('[stdout]: \x1b', mocked_output_as_str) self.assertIn('[stderr]: \x1b', mocked_output_as_str)
def main(): io = IO() try: args = WaitForOutputApp.parse_args() io.set_log_level(args['log_level']) WaitForOutputApp(container=args['container'], command=args['command'], pattern=args['pattern'], timeout=int(args['timeout']), io=io) \ .main() except ResultSignal as signal: io.info(signal.message) if signal.exit_code == 0 else io.error( signal.message) sys.exit(signal.exit_code)