From 7503553a5c6db219cf5d44e5b2921a0aff1a5dcf Mon Sep 17 00:00:00 2001 From: Satellite QE <115476073+Satellite-QE@users.noreply.github.com> Date: Thu, 6 Jun 2024 02:53:30 -0400 Subject: [PATCH] [6.14.z] Poll ncat tunnel pid (#15332) Poll ncat tunnel pid (#15056) when checking the pid right after ncat startup it could happen that no pid was found despite ncat being started. Polling solves this problem (cherry picked from commit e918739b1e2d3614d3c2d9919a9728f21115e1c9) Co-authored-by: dosas --- robottelo/host_helpers/satellite_mixins.py | 52 +++++++++++++++------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/robottelo/host_helpers/satellite_mixins.py b/robottelo/host_helpers/satellite_mixins.py index 60083dc0b7c..73d1280c6c0 100644 --- a/robottelo/host_helpers/satellite_mixins.py +++ b/robottelo/host_helpers/satellite_mixins.py @@ -6,6 +6,7 @@ import re import requests +from wait_for import TimedOutError, wait_for from robottelo.cli.proxy import CapsuleTunnelError from robottelo.config import settings @@ -279,26 +280,45 @@ def default_url_on_new_port(self, oldport, newport): :rtype: str """ - pre_ncat_procs = self.execute('pgrep ncat').stdout.splitlines() - with self.session.shell() as channel: - # if ncat isn't backgrounded, it prevents the channel from closing - command = f'ncat -kl -p {newport} -c "ncat {self.hostname} {oldport}" &' - logger.debug(f'Creating tunnel: {command}') - channel.send(command) + + def check_ncat_startup(pre_ncat_procs): post_ncat_procs = self.execute('pgrep ncat').stdout.splitlines() ncat_pid = set(post_ncat_procs).difference(set(pre_ncat_procs)) - if not len(ncat_pid): - err = channel.get_exit_signal() - logger.debug(f'Tunnel failed: {err}') - # Something failed, so raise an exception. - raise CapsuleTunnelError(f'Starting ncat failed: {err}') + if len(ncat_pid): + return ncat_pid + + return None + + def start_ncat(): + pre_ncat_procs = self.execute('pgrep ncat').stdout.splitlines() + with self.session.shell() as channel: + # if ncat isn't backgrounded, it prevents the channel from closing + command = f'ncat -kl -p {newport} -c "ncat {self.hostname} {oldport}" &' + logger.debug(f'Creating tunnel: {command}') + channel.send(command) + + try: + return wait_for( + check_ncat_startup, + func_args=[pre_ncat_procs], + fail_condition=None, + timeout=5, + delay=0.5, + )[0] + except TimedOutError as e: + err = channel.get_exit_signal() + logger.debug(f'Tunnel failed: {err}') + # Something failed, so raise an exception. + raise CapsuleTunnelError(f'Starting ncat failed: {err}') from e + + ncat_pid = start_ncat() + try: forward_url = f'https://{self.hostname}:{newport}' logger.debug(f'Yielding capsule forward port url: {forward_url}') - try: - yield forward_url - finally: - logger.debug(f'Killing ncat pid: {ncat_pid}') - self.execute(f'kill {ncat_pid.pop()}') + yield forward_url + finally: + logger.debug(f'Killing ncat pid: {ncat_pid}') + self.execute(f'kill {ncat_pid.pop()}') def validate_pulp_filepath( self,