Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[determine-reboot-cause] Skip invoking platform code for unit tests for hardware_reboot_cause #6325

Merged
merged 2 commits into from
Dec 31, 2020

Conversation

vaibhavhd
Copy link
Contributor

@vaibhavhd vaibhavhd commented Dec 30, 2020

Fix unit tests in determine-reboot-cause to skip calling platform module libraries in unit test scope.

- Why I did it
MELLANOX build is failing for the recent PRs. The errors are due to platform library being invoked in a unit test for determine-reboot-cause script.

[2020-12-30T11:03:13.516Z] tests/determine-reboot-cause_test.py:102: 
[2020-12-30T11:03:13.516Z] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
[2020-12-30T11:03:13.516Z] scripts/determine-reboot-cause:107: in find_hardware_reboot_cause
[2020-12-30T11:03:13.516Z]     platform  = sonic_platform.platform.Platform()
[2020-12-30T11:03:13.516Z] /usr/local/lib/python3.7/dist-packages/sonic_platform/platform.py:21: in __init__
[2020-12-30T11:03:13.516Z]     self._chassis.initialize_components()
[2020-12-30T11:03:13.516Z] /usr/local/lib/python3.7/dist-packages/sonic_platform/chassis.py:167: in initialize_components
[2020-12-30T11:03:13.516Z]     self._component_list.extend(ComponentCPLD.get_component_list())
[2020-12-30T11:03:13.516Z] /usr/local/lib/python3.7/dist-packages/sonic_platform/component.py:745: in get_component_list
[2020-12-30T11:03:13.516Z]     cpld_number = cls._read_generic_file(cls.CPLD_NUMBER_FILE, cls.CPLD_NUMBER_MAX_LENGTH)
[2020-12-30T11:03:13.516Z] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

[2020-12-30T11:03:13.516Z]         try:
[2020-12-30T11:03:13.516Z]             with io.open(filename, 'r') as fileobj:
[2020-12-30T11:03:13.516Z]                 result = fileobj.read(len)
[2020-12-30T11:03:13.516Z]         except IOError as e:
[2020-12-30T11:03:13.516Z]             if not ignore_errors:
[2020-12-30T11:03:13.516Z] >               raise RuntimeError("Failed to read file {} due to {}".format(filename, repr(e)))
[2020-12-30T11:03:13.516Z] E               RuntimeError: Failed to read file /var/run/hw-management/config/cpld_num due to FileNotFoundError(2, 'No such file or directory')

- How I did it
Modified unit tests to use mock utility to skip calling sonic_platform libraries.

- How to verify it
Executed unit tests and Mellanox PR build expected to pass now.

- Which release branch to backport (provide reason below if selected)

  • 201811
  • 201911
  • 202006

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@@ -111,6 +111,9 @@ def find_hardware_reboot_cause():
except ImportError:
sonic_logger.log_warning("sonic_platform package not installed. Unable to detect hardware reboot causes.")
hardware_reboot_cause_major, hardware_reboot_cause_minor = REBOOT_CAUSE_NON_HARDWARE, "N/A"
except:
Copy link
Collaborator

Choose a reason for hiding this comment

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

for unit test failure, better to patch chassis.get_reboot_cause()

@@ -111,6 +111,9 @@ def find_hardware_reboot_cause():
except ImportError:
Copy link
Collaborator

Choose a reason for hiding this comment

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

ImportError [](start = 11, length = 11)

I think you use ImportError to determine test environment. This is fragile.
You'd better manipulate libraries in the test code, and not change scripts under test.
There are samples in https://github.com/Azure/sonic-snmpagent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that ImportError handling is not the best way to determine test env. In this change, based on the discussion offline, we have decided to use mock function to avoid hitting this code at all.

@vaibhavhd vaibhavhd changed the title Handle unknown exceptions encountered in platform lib [determine-reboot-cause] Skip invoking platform code for unit tests for hardware_reboot_cause Dec 30, 2020
@vaibhavhd vaibhavhd merged commit 40f69f0 into sonic-net:master Dec 31, 2020
@vaibhavhd vaibhavhd deleted the fix-mlnx-build-err branch December 31, 2020 01:51
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants