From 0c45afdbda5a181bdfb43add60287212a645b145 Mon Sep 17 00:00:00 2001 From: MariusBaldovin Date: Mon, 11 Nov 2024 15:32:38 +0000 Subject: [PATCH 1/4] added error handling if GRPC server can't start due to port 5001 is blocked by UFW firewall --- .../test/conn/python/src/connection_module.py | 235 ++++++++++-------- 1 file changed, 127 insertions(+), 108 deletions(-) diff --git a/modules/test/conn/python/src/connection_module.py b/modules/test/conn/python/src/connection_module.py index d47d25ba4..f682619e8 100644 --- a/modules/test/conn/python/src/connection_module.py +++ b/modules/test/conn/python/src/connection_module.py @@ -397,66 +397,75 @@ def _connection_dhcp_disconnect(self): result = None description = '' dev_iface = os.getenv('DEV_IFACE') - iface_status = self.host_client.check_interface_status(dev_iface) - if iface_status.code == 200: - LOGGER.info('Successfully resolved iface status') - if iface_status.status: - lease = self._dhcp_util.get_cur_lease(mac_address=self._device_mac, - timeout=self._lease_wait_time_sec) - if lease is not None: - LOGGER.info('Current device lease resolved') - if self._dhcp_util.is_lease_active(lease): - # Disable the device interface - iface_down = self.host_client.set_iface_down(dev_iface) - if iface_down: - LOGGER.info('Device interface set to down state') + try: + iface_status = self.host_client.check_interface_status(dev_iface) + if iface_status.code == 200: + LOGGER.info('Successfully resolved iface status') + if iface_status.status: + lease = self._dhcp_util.get_cur_lease(mac_address=self._device_mac, + timeout=self._lease_wait_time_sec) + if lease is not None: + LOGGER.info('Current device lease resolved') + if self._dhcp_util.is_lease_active(lease): - # Wait for the lease to expire - self._dhcp_util.wait_for_lease_expire(lease, - self._lease_wait_time_sec) + # Disable the device interface + iface_down = self.host_client.set_iface_down(dev_iface) + if iface_down: + LOGGER.info('Device interface set to down state') - # Wait an additonal 10 seconds to better test a true disconnect - # state - LOGGER.info('Waiting 10 seconds before bringing iface back up') - time.sleep(10) - - # Enable the device interface - iface_up = self.host_client.set_iface_up(dev_iface) - if iface_up: - LOGGER.info('Device interface set to up state') - - # Confirm device receives a new lease - if self._dhcp_util.get_cur_lease( - mac_address=self._device_mac, - timeout=self._lease_wait_time_sec): - if self._dhcp_util.is_lease_active(lease): - result = True - description = ( - 'Device received a DHCP lease after disconnect') + # Wait for the lease to expire + self._dhcp_util.wait_for_lease_expire(lease, + self._lease_wait_time_sec) + + # Wait an additonal 10 seconds to better test a true disconnect + # state + LOGGER.info('Waiting 10 seconds before bringing iface back up') + time.sleep(10) + + # Enable the device interface + iface_up = self.host_client.set_iface_up(dev_iface) + if iface_up: + LOGGER.info('Device interface set to up state') + + # Confirm device receives a new lease + if self._dhcp_util.get_cur_lease( + mac_address=self._device_mac, + timeout=self._lease_wait_time_sec): + if self._dhcp_util.is_lease_active(lease): + result = True + description = ( + 'Device received a DHCP lease after disconnect') + else: + result = False + description = ( + 'Could not confirm DHCP lease active after disconnect') else: result = False description = ( - 'Could not confirm DHCP lease active after disconnect') - else: - result = False - description = ( 'Device did not recieve a DHCP lease after disconnect') + else: + result = 'Error' + description = 'Failed to set device interface to up state' else: result = 'Error' - description = 'Failed to set device interface to up state' - else: - result = 'Error' - description = 'Failed to set device interface to down state' + description = 'Failed to set device interface to down state' + else: + result = 'Error' + description = 'No active lease available for device' else: result = 'Error' - description = 'No active lease available for device' + description = 'Device interface is down' else: result = 'Error' - description = 'Device interface is down' - else: + description = 'Device interface could not be resolved' + + except Exception: + LOGGER.error('Unable to connect to RPC server') result = 'Error' - description = 'Device interface could not be resolved' + description = ( + 'Check if UFW firewall is enabled and blocking the port 5001' + ) return result, description def _connection_dhcp_disconnect_ip_change(self): @@ -466,86 +475,96 @@ def _connection_dhcp_disconnect_ip_change(self): reserved_lease = None dev_iface = os.getenv('DEV_IFACE') if self._dhcp_util.setup_single_dhcp_server(): - iface_status = self.host_client.check_interface_status(dev_iface) - if iface_status.code == 200: - LOGGER.info('Successfully resolved iface status') - if iface_status.status: - lease = self._dhcp_util.get_cur_lease( - mac_address=self._device_mac, timeout=self._lease_wait_time_sec) - if lease is not None: - LOGGER.info('Current device lease resolved') - if self._dhcp_util.is_lease_active(lease): + try: + iface_status = self.host_client.check_interface_status(dev_iface) + if iface_status.code == 200: + LOGGER.info('Successfully resolved iface status') + if iface_status.status: + lease = self._dhcp_util.get_cur_lease( + mac_address=self._device_mac, timeout=self._lease_wait_time_sec) + if lease is not None: + LOGGER.info('Current device lease resolved') + if self._dhcp_util.is_lease_active(lease): - # Add a reserved lease with a different IP - ip_address = '10.10.10.30' - reserved_lease = self._dhcp_util.add_reserved_lease( - lease['hostname'], self._device_mac, ip_address) + # Add a reserved lease with a different IP + ip_address = '10.10.10.30' + reserved_lease = self._dhcp_util.add_reserved_lease( + lease['hostname'], self._device_mac, ip_address) - # Disable the device interface - iface_down = self.host_client.set_iface_down(dev_iface) - if iface_down: - LOGGER.info('Device interface set to down state') + # Disable the device interface + iface_down = self.host_client.set_iface_down(dev_iface) + if iface_down: + LOGGER.info('Device interface set to down state') - # Wait for the lease to expire - self._dhcp_util.wait_for_lease_expire(lease, - self._lease_wait_time_sec) + # Wait for the lease to expire + self._dhcp_util.wait_for_lease_expire(lease, + self._lease_wait_time_sec) - if reserved_lease: - # Wait an additonal 10 seconds to better test a true - # disconnect state - LOGGER.info( - 'Waiting 10 seconds before bringing iface back up') - time.sleep(10) - - # Enable the device interface - iface_up = self.host_client.set_iface_up(dev_iface) - if iface_up: - LOGGER.info('Device interface set to up state') - # Confirm device receives a new lease - reserved_lease_accepted = False - LOGGER.info('Checking device accepted new ip') - for _ in range(5): - LOGGER.info('Pinging device at IP: ' + ip_address) - if self._ping(ip_address): - LOGGER.debug('Ping success') - LOGGER.debug( - 'Reserved lease confirmed active in device') - reserved_lease_accepted = True - break + if reserved_lease: + # Wait an additonal 10 seconds to better test a true + # disconnect state + LOGGER.info( + 'Waiting 10 seconds before bringing iface back up') + time.sleep(10) + + # Enable the device interface + iface_up = self.host_client.set_iface_up(dev_iface) + if iface_up: + LOGGER.info('Device interface set to up state') + # Confirm device receives a new lease + reserved_lease_accepted = False + LOGGER.info('Checking device accepted new ip') + for _ in range(5): + LOGGER.info('Pinging device at IP: ' + ip_address) + if self._ping(ip_address): + LOGGER.debug('Ping success') + LOGGER.debug( + 'Reserved lease confirmed active in device') + reserved_lease_accepted = True + break + else: + LOGGER.info('Device did not respond to ping') + time.sleep(5) # Wait 5 seconds before trying again + + if reserved_lease_accepted: + result = True + description = ('Device received expected IP address ' + 'after disconnect') else: - LOGGER.info('Device did not respond to ping') - time.sleep(5) # Wait 5 seconds before trying again - - if reserved_lease_accepted: - result = True - description = ('Device received expected IP address ' - 'after disconnect') - else: - result = False - description = ( + result = False + description = ( 'Could not confirm DHCP lease active after disconnect' - ) + ) + else: + result = 'Error' + description = 'Failed to set device interface to up state' else: result = 'Error' - description = 'Failed to set device interface to up state' + description = ( + 'Failed to set reserved address in DHCP server' + ) else: result = 'Error' - description = 'Failed to set reserved address in DHCP server' - else: - result = 'Error' - description = 'Failed to set device interface to down state' + description = 'Failed to set device interface to down state' + else: + result = 'Error' + description = 'No active lease available for device' else: result = 'Error' - description = 'No active lease available for device' + description = 'Device interface is down' else: result = 'Error' - description = 'Device interface is down' - else: + description = 'Device interface could not be resolved' + except Exception: + LOGGER.error('Unable to connect to RPC server') result = 'Error' - description = 'Device interface could not be resolved' + description = ( + 'Check if UFW firewall is enabled and blocking the port 5001' + ) else: result = 'Error' description = 'Failed to configure network for test' + if reserved_lease: self._dhcp_util.delete_reserved_lease(self._device_mac) From 7117bfba9111d55a6bdf5a79cf80b752ee1559fa Mon Sep 17 00:00:00 2001 From: MariusBaldovin Date: Mon, 11 Nov 2024 16:16:07 +0000 Subject: [PATCH 2/4] fixed typo error --- modules/test/conn/python/src/connection_module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/test/conn/python/src/connection_module.py b/modules/test/conn/python/src/connection_module.py index f682619e8..ffe466e67 100644 --- a/modules/test/conn/python/src/connection_module.py +++ b/modules/test/conn/python/src/connection_module.py @@ -461,7 +461,7 @@ def _connection_dhcp_disconnect(self): description = 'Device interface could not be resolved' except Exception: - LOGGER.error('Unable to connect to RPC server') + LOGGER.error('Unable to connect to gRPC server') result = 'Error' description = ( 'Check if UFW firewall is enabled and blocking the port 5001' @@ -556,7 +556,7 @@ def _connection_dhcp_disconnect_ip_change(self): result = 'Error' description = 'Device interface could not be resolved' except Exception: - LOGGER.error('Unable to connect to RPC server') + LOGGER.error('Unable to connect to gRPC server') result = 'Error' description = ( 'Check if UFW firewall is enabled and blocking the port 5001' From 653c2730031d4d73f15ab2319220376aa10be99f Mon Sep 17 00:00:00 2001 From: MariusBaldovin Date: Mon, 11 Nov 2024 16:56:25 +0000 Subject: [PATCH 3/4] update --- modules/test/conn/python/src/connection_module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/test/conn/python/src/connection_module.py b/modules/test/conn/python/src/connection_module.py index ffe466e67..a2b59e5b2 100644 --- a/modules/test/conn/python/src/connection_module.py +++ b/modules/test/conn/python/src/connection_module.py @@ -464,7 +464,7 @@ def _connection_dhcp_disconnect(self): LOGGER.error('Unable to connect to gRPC server') result = 'Error' description = ( - 'Check if UFW firewall is enabled and blocking the port 5001' + 'Unable to connect to gRPC server. Check the firewall' ) return result, description @@ -559,7 +559,7 @@ def _connection_dhcp_disconnect_ip_change(self): LOGGER.error('Unable to connect to gRPC server') result = 'Error' description = ( - 'Check if UFW firewall is enabled and blocking the port 5001' + 'Unable to connect to gRPC server. Check the firewall' ) else: result = 'Error' From f65c32b1b26095b790b9abeff917133332c0881a Mon Sep 17 00:00:00 2001 From: MariusBaldovin Date: Wed, 13 Nov 2024 13:16:51 +0000 Subject: [PATCH 4/4] removed firewall info --- modules/test/conn/python/src/connection_module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/test/conn/python/src/connection_module.py b/modules/test/conn/python/src/connection_module.py index a2b59e5b2..538dd2509 100644 --- a/modules/test/conn/python/src/connection_module.py +++ b/modules/test/conn/python/src/connection_module.py @@ -464,7 +464,7 @@ def _connection_dhcp_disconnect(self): LOGGER.error('Unable to connect to gRPC server') result = 'Error' description = ( - 'Unable to connect to gRPC server. Check the firewall' + 'Unable to connect to gRPC server' ) return result, description @@ -559,7 +559,7 @@ def _connection_dhcp_disconnect_ip_change(self): LOGGER.error('Unable to connect to gRPC server') result = 'Error' description = ( - 'Unable to connect to gRPC server. Check the firewall' + 'Unable to connect to gRPC server' ) else: result = 'Error'