Skip to content

utils_sriov: get a suitable pf#4352

Open
dzhengfy wants to merge 1 commit intoavocado-framework:masterfrom
dzhengfy:support_get_suitable_pf
Open

utils_sriov: get a suitable pf#4352
dzhengfy wants to merge 1 commit intoavocado-framework:masterfrom
dzhengfy:support_get_suitable_pf

Conversation

@dzhengfy
Copy link
Copy Markdown
Contributor

@dzhengfy dzhengfy commented Mar 30, 2026

This is to add a new function to get a suitable PF.

  • If more than one Up PF exists, return one of them.
  • If single Up PF exists and at least one Down PF exists, return this Down PF.
  • If single Up PF exists and no Down PF, return this Up PF.

Signed-off-by: Dan Zheng dzheng@redhat.com

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the get_suitable_pf_pci function in virttest/utils_sriov.py to select a Physical Function (PF) PCI address based on its operational status. The review feedback identifies a potential IndexError if no PFs are found and suggests refactoring the logic for better readability and explicit error handling when no suitable PF is available.

Comment thread virttest/utils_sriov.py Outdated
Comment on lines +133 to +146
if len(pf_up_list) < 2:
if len(pf_down_list) >= 1:
LOG.debug("Only one PF is in up status, so a PF in "
"down status will be returned:%s", pf_down_list[0])
return pf_down_list[0]
else:
LOG.debug("Only one PF is in up status and no PF in "
"down status, so the PF in up status will "
"be returned:%s", pf_up_list[0])
return pf_up_list[0]
else:
LOG.debug("At least two PFs are in up status, so a PF in up status "
"will be returned:%s", pf_up_list[0])
return pf_up_list[0]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The current logic has a potential IndexError if no 'up' or 'down' PFs are found. Additionally, the nested conditional statements can be simplified for better readability and maintainability. The suggested change refactors the logic to be more linear and explicitly handles the case where no suitable PF is found by raising a TestError. This aligns with the requirement to fail explicitly when a required feature or dependency is unavailable, preventing silent failures or misleading results.

    if len(pf_up_list) >= 2:
        LOG.debug("At least two PFs are in up status, so a PF in up status "
                  "will be returned:%s", pf_up_list[0])
        return pf_up_list[0]
    if len(pf_up_list) == 1 and pf_down_list:
        LOG.debug("Only one PF is in up status, so a PF in "
                  "down status will be returned:%s", pf_down_list[0])
        return pf_down_list[0]
    if pf_up_list:
        LOG.debug("Only one PF is in up status and no PF in "
                  "down status, so the PF in up status will "
                  "be returned:%s", pf_up_list[0])
        return pf_up_list[0]
    if pf_down_list:
        LOG.debug("No PFs are in up status, so a PF in "
                  "down status will be returned:%s", pf_down_list[0])
        return pf_down_list[0]

    raise exceptions.TestError("No suitable PF found (up or down).")
References
  1. When a test's intent is to use a specific feature, it should fail explicitly if that feature or its dependencies are unavailable to avoid masking configuration issues.

@dzhengfy dzhengfy force-pushed the support_get_suitable_pf branch from 28e69fb to 797a404 Compare March 30, 2026 13:48
@dzhengfy
Copy link
Copy Markdown
Contributor Author

Test result:
autotest/tp-libvirt#6828 (comment)

@dzhengfy dzhengfy force-pushed the support_get_suitable_pf branch from 797a404 to f76b9b5 Compare March 30, 2026 14:05
This is to add a new function to get a suitable PF.
If more than one Up PF exists, return one of them.
If single Up PF exists and at least one Down PF exists, return this
Down PF.
If single Up PF exists and no Down PF, return this Up PF.

Signed-off-by: Dan Zheng <dzheng@redhat.com>
@dzhengfy dzhengfy force-pushed the support_get_suitable_pf branch from f76b9b5 to 5e20d89 Compare March 30, 2026 14:08
@dzhengfy dzhengfy marked this pull request as ready for review March 30, 2026 14:09
Copy link
Copy Markdown
Contributor

@Yingshun Yingshun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, please fix the pre-commit failures

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.

2 participants