Skip to content

fix(cloudstack): avoid UnboundLocalError for primary_nic on NoDHCPLeaseError - #7031

Open
sundeep8967 wants to merge 1 commit into
canonical:mainfrom
sundeep8967:fix/cloudstack-unbound-primary-nic
Open

fix(cloudstack): avoid UnboundLocalError for primary_nic on NoDHCPLeaseError#7031
sundeep8967 wants to merge 1 commit into
canonical:mainfrom
sundeep8967:fix/cloudstack-unbound-primary-nic

Conversation

@sundeep8967

Copy link
Copy Markdown

Problem

In DataSourceCloudStack._get_data, when self.perform_dhcp_setup is False, primary_nic is never assigned. If get_vr_address(self.distro) subsequently raises a NoDHCPLeaseError, the exception handler attempts to log:

LOG.warning("Unable to obtain a DHCP lease on %s", primary_nic)

which crashes with:

UnboundLocalError: cannot access local variable 'primary_nic' where it is not associated with a value

Solution

  • Initialized primary_nic = None before the DHCP setup branch in DataSourceCloudStack._get_data.
  • Added unit test test_get_data_no_dhcp_lease_error_without_dhcp_setup under tests/unittests/sources/test_cloudstack.py to verify that NoDHCPLeaseError with perform_dhcp_setup=False logs cleanly and returns False without raising an UnboundLocalError.

Fixes #6838

Testing

  • Verified test failure prior to the fix (UnboundLocalError reproduced).
  • Verified that all 29 tests in tests/unittests/sources/test_cloudstack.py pass with 100% success after the fix.

When perform_dhcp_setup is False, primary_nic was unassigned when
get_vr_address raised NoDHCPLeaseError, causing an unhandled
UnboundLocalError when logging. Initialize primary_nic to None.

Fixes canonicalGH-6838
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In DataSourceCloudStack the primary_nic is not Always Assigned a Value but it may Always be Read

1 participant