Unify the importing to avoid exception not caught issue#1864
Unify the importing to avoid exception not caught issue#1864lguohan merged 3 commits intosonic-net:masterfrom wangxin:fix-imports
Conversation
|
This pull request introduces 15 alerts and fixes 3 when merging a7863d4c33bec6ec70607dd1dc54638855f2f498 into 52b5b15 - view on LGTM.com new alerts:
fixed alerts:
|
|
This pull request introduces 6 alerts and fixes 3 when merging c4dd424a1a251bfa5952653c84b2b307dd0e7c70 into 77a6d1c - view on LGTM.com new alerts:
fixed alerts:
|
|
Retest this please |
|
@wangxin can we instead of removing the current dir from sys path, catch the exception with its full name? |
The sonic-mgmt/tests folder was added to sys.path so that the scripts and libs can use format `from common.xx import xx` to import from the `common` folder. By this way, objects may be identified as 'tests.common.xx' or 'common.xx' under different context. This caused some side effects like RunAnsibleModuleFail exception cannot be caught. The raised exception is identified as tests.common.errors.RunAnsibleModuleFail. But the script tries to catch common.errors.RunAnsibleModuleFail and fail to catch the exception. This PR removes the code for adding sonic-mgmt/tests/ to sys.path and updated the related importing to format like `from tests.common.xx import xx`. When we use pytest to run scripts under `sonic-mgmt/tests` folder, its parent folder `sonic-mgmt` is automatically added to sys.path. That's why we can use the new format to do importing. Signed-off-by: Xin Wang <xiwang5@microsoft.com>
|
@tahmed-dev Yes, we can specify full name to catch the exceptions. But it would be better to enforce importing from the tests package to avoid other potential similar issues that are kind of difficult to troubleshoot. |
|
This pull request introduces 3 alerts and fixes 3 when merging 225bc7a into 53222ad - view on LGTM.com new alerts:
fixed alerts:
|
|
This pull request introduces 3 alerts and fixes 3 when merging 4a13e38 into 53222ad - view on LGTM.com new alerts:
fixed alerts:
|
After PR #1864 is merged, `from common.xx import yy' is no longer supported. Probably this PR was created/tested before PR#1864 was merged. This commit is to fix the `from common.xx import yy' issue. Signed-off-by: Xin Wang <xiwang5@microsoft.com>
Description of PR
Summary:
Fixes # (issue)
The sonic-mgmt/tests folder was added to sys.path so that the scripts
and libs can use format
from common.xx import xxto import fromthe
commonfolder. By this way, objects may be identified as 'tests.common.xx'or 'common.xx' under different context. This caused some side effects
like RunAnsibleModuleFail exception cannot be caught. The raised
exception is identified as tests.common.errors.RunAnsibleModuleFail.
But the script tries to catch common.errors.RunAnsibleModuleFail and
fail to catch the exception.
This PR removes the code for adding sonic-mgmt/tests/ to sys.path and
updated the related importing to format like
from tests.common.xx import xx.When we use pytest to run scripts under
sonic-mgmt/testsfolder, its parentfolder
sonic-mgmtis automatically added to sys.path. That's why we canuse the new format to do importing.
Type of change
Approach
What is the motivation for this PR?
Since the
sonic/testsfolder is made as a package, adding subfolder of this package to sys.path would cause ambiguity issue of object identification. For example, in tests/common/platform/ssh_utils.py, the RunAnsibleModuleFail exception cannot be caught:The reason is that the raised exception has identification tests.common.errors.RunAnsibleModuleFail. However, the script here expects common.errors.RunAnsibleModuleFail here.
To avoid this issue, we should always explicitly use absolute import statement like 'from tests.common.xx import xx', or use relative import like 'from .common.xx import xx'. To prevent this issue, the code for adding tests/common folder to sys.path is also removed from tests/conftest.py.
How did you do it?
tests/commonto sys.path.How did you verify/test it?
Tested collect all the tests. No importing issue. Tested the scripts depend on ssh_util, the exception can be caught as expected.
Any platform specific information?
Supported testbed topology if it's a new test case?
Documentation